#Updated 2023.03.16
#Updated by Chandra
require 'English'
#require 'html-proofer'
require 'jekyll'
require 's3_website'
# Extend string to allow for bold text.
class String
def bold
"\033[1m#{self}\033[0m"
end
end
# Usage: bundle exec rake deploy. Clean, build, optimize, push to S3, and notify. - removed :clean (placed before build) to see if image optimization would be faster.
task :deploy => [ :git_master, :git_fetch, :build, :push, :notify ] do
end
task :clean do
puts 'Cleaning up _site...'.bold
Jekyll::Commands::Clean.process({})
end
desc "build the site"
task :build do
sh "bundle exec jekyll build"
end
# Usage: rake optimize
task default: %w[imageOptimize]
task :optimize do
puts 'Compressing images...'.bold
sh "image_optim -r ./_site/assets/images/"
end
# lib/tasks/assets.rake
#require 'image_optim'
#require 'image_optim/runner'
#namespace :assets do
# desc 'Runs image_optim on the assets.'
# task :optimize do
# image_optim = ImageOptim::Runner.new(recursive: true)
# image_optim.run!(Dir['_site/assets/images/**/*.{svg,jpg,png,jpeg,gif}'])
# end
#end
task :purgecss do
    puts 'use purge css...'.bold
    sh "purgecss --css _site/assets/css/main.css --content _site/**/*.html --output _site/assets/css/"
    end

task :push do
puts 'Push site to Amazon S3...'.bold
sh "bundle exec s3_website push --force"
end
# Usage: rake notify
task :notify => ["notify:pingomatic", "notify:google", "notify:bing", "notify:pingpubsubhubbub"]
desc "Notify various services that the site has been updated"
namespace :notify do
text = File.read("_config.yml", :encoding => 'utf-8')
matchdata = text.match(/^url: "([^"]*)"/)
deploy_notify = matchdata[1]
desc "Notify Ping-O-Matic"
task :pingomatic do
begin
require 'xmlrpc/client'
puts "* Notifying Ping-O-Matic that the site has updated"
XMLRPC::Client.new('rpc.pingomatic.com', '/').call('weblogUpdates.extendedPing', '#{deploy_notify}' , '#{deploy_notify}', '#{deploy_notify}/feed.xml')
rescue LoadError
puts "! Could not ping ping-o-matic, because XMLRPC::Client could not be found."
end
end
desc "Notify Google of updated sitemap"
task :google do
begin
require 'net/http'
require 'uri'
puts "* Notifying Google that the site has updated"
Net::HTTP.get("www.google.com", "/webmasters/tools/ping?sitemap=" + URI.encode_www_form_component("#{deploy_notify}/sitemap.xml"))
puts "www.google.com/webmasters/tools/ping?sitemap=#{deploy_notify}/sitemap.xml"
rescue LoadError
puts "! Could not ping Google about our sitemap, because Net::HTTP or URI could not be found."
end
end
desc "Notify Bing of updated sitemap"
task :bing do
begin
require 'net/http'
require 'uri'
puts '* Notifying Bing that the site has updated'
Net::HTTP.get("www.bing.com", "/webmaster/ping.aspx?siteMap=" + URI.encode_www_form_component("#{deploy_notify}/sitemap.xml"))
puts "www.bing.com/webmaster/ping.aspx?siteMap=#{deploy_notify}/sitemap.xml"
rescue LoadError
puts "! Could not ping Bing about our sitemap, because Net::HTTP or URI could not be found."
end
end
desc 'Notify pubsubhubbub server.'
task :pingpubsubhubbub do
cfg = YAML.load_file("_config.yml")
deploy_notify = URI.extract(deploy_notify, ['http', 'https'])
data = 'hub.mode=publish&hub.url=' + "#{deploy_notify[0]}" + CGI::escape("/feed.xml")
headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
puts '* Pinging pubsubhubbub server'
request = Net::HTTP.new('pubsubhubbub.appspot.com', 80)
response = request.post('http://pubsubhubbub.appspot.com/publish', data, headers)
if response.code == "200" || response.code == "204"
puts "Successfully submitted sitemap to pubsubhubbub"
else
puts "pubsubhubbub response: #{response.body}"
fail
end
end
end
# Usage: rake html_proofer
desc 'html-proofer checks the site for broken links, missing tags, etc.'
task :html_proofer do
sh "bundle exec jekyll build"
options = { :assume_extension => true, :check_favicon => true, :check_opengraph => true, :allow_hash_href => true}
HTMLProofer.check_directory(
'_site/', options).run
end
# Usage: rake amp
desc 'Test website AMP validation'
task :amp do
puts 'Running AMP Validator...'.bold
system('say "running a.m.p. validator"')
amp_dir = '_site/amp'
system("find #{amp_dir} -iname *.html | xargs -L1 amphtml-validator")
if $CHILD_STATUS.exitstatus.zero?
puts 'AMP Validator finished successfully.'
system('say "a.m.p validator finished successfuly"')
else
puts 'AMP Validator FAILED.'
system('say "a m p validator found errors"')
exit($CHILD_STATUS.exitstatus)
end
end
# Usage: rake git_master
desc 'Pull any new commits from the repository'
task :git_master do
puts 'Switching To Github master branch...'.bold
sh 'git checkout master'
end
# Usage: rake git_fetch
desc 'Pull any new commits from the repository'
task :git_fetch do
puts 'Pulling in new commits...'.bold
sh 'git fetch'
end