Commit 195a5ca1 authored by Evaryont's avatar Evaryont

Split out code from Rakefile a bit more

parent 55189880
require 'bundler/setup'
@project_root = File.expand_path('..', __FILE__)
require 'tmpdir'
require_relative 'lib/helpers/rake.rb'
include EvaryontsHelpers::Rake
desc "Create a new blog post"
# Sorta black magic for this task. But makes it easy to write an article title!
......@@ -79,6 +82,14 @@ task :server do
sh "bundle exec middleman server"
end
namespace :server do
desc "Run a WEBrick HTTP server"
task :simple => :build do
cd File.join(ROOT_DIR, 'build')
sh "ruby -run -e httpd . -p 4567"
end
end
desc "Build and upload my website to everywhere"
# So the order here is important:
# - Ensure the repository is clean & ready
......@@ -122,23 +133,54 @@ end
desc 'Run the lighthouse tool against evaryont.me or environment variable $url'
task :lighthouse do
require 'tmpdir'
dir = Dir.mktmpdir('lighthouse')
port = 9299
url = ENV['url'] || 'https://evaryont.me'
# Launch headless chrome into the background
chrome_process = fork do
command = "/usr/lib/chromium/chromium --disable-translate --disable-extensions --disable-background-networking --safebrowsing-disable-auto-update --disable-sync --metrics-recording-only --disable-default-apps --no-first-run --remote-debugging-port=#{port} --user-data-dir=#{dir} --headless about:blank"
puts command
exec command
end
puts "Waiting for a few seconds to allow chrome to launch first before lighthouse"
sleep 3
launch_chrome
ENV['LIGHTHOUSE_CHROMIUM_PATH'] = "/usr/bin/chromium"
sh "lighthouse --quiet --view --output-path=./lighthouse-results.html --port=#{port} #{url}"
sh "lighthouse --quiet --view --output-path=./lighthouse-results.html --port=#{chromium_port} #{url}"
end
task :resume2 do
launch_chrome
require 'chrome_remote'
require 'base64'
chrome = ChromeRemote.client(host: 'localhost', port: chromium_port)
chrome.send_cmd "Network.enable"
chrome.send_cmd "Page.enable"
# Navigate to github.com and wait for the page to load
chrome.send_cmd "Page.navigate", url: "https://evaryont.me/resume.html"
chrome.wait_for "Page.loadEventFired"
# Take page screenshot
options = {
paperWidth: 8.27, # A4 height
paperHeight: 11.69, # A4 width
marginTop: 0.31, # 8mm in inches
marginLeft: 0.04, # 1mm in inches
marginRight: 0.04,
marginBottom: 0.04,
margin: {
top: "0px",
left: "0px",
right: "0px",
bottom: "0px",
},
displayHeaderFooter: true,
footerTemplate: '<p></p>',
headerTemplate: '<div style="float: right;">Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>',
}
response = chrome.send_cmd "Page.printToPDF", options
File.write "output.pdf", Base64.decode64(response["data"])
end
Process.kill("INT", chrome_process)
rm_rf dir
desc 'Run HTML Proofer over the output'
task :proofer => :build do
require 'html-proofer'
options = { assume_extension: true }
HTMLProofer.check_directory("./build", options).run
end
module EvaryontsHelpers
module Rake
ROOT_DIR = `git rev-parse --show-toplevel`.chomp
def chromium_port
ENV['CHROMIUM_PORT'].to_i > 0 ? ENV['CHROMIUM_PORT'].to_i : 9299
end
def chromium_dir
@chromium_dir ||= Dir.mktmpdir('chromium-user_data')
end
# Launch headless chrome into the background. Designed for Arch-based systems
def launch_chrome
chrome_process = fork do
command = "/usr/lib/chromium/chromium --disable-translate --disable-gpu --disable-extensions --disable-background-networking --safebrowsing-disable-auto-update --disable-sync --metrics-recording-only --disable-default-apps --no-first-run --remote-debugging-port=#{chromium_port} --user-data-dir=#{chromium_dir} --headless about:blank"
puts command
exec command
end
puts "Waiting for a few seconds to allow chrome to launch first before lighthouse"
sleep 3
at_exit {
Process.kill("INT", chrome_process)
rm_rf chromium_dir
}
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment