Skip to content

Instantly share code, notes, and snippets.

@tooky
Created June 28, 2013 15:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tooky/5885618 to your computer and use it in GitHub Desktop.
Save tooky/5885618 to your computer and use it in GitHub Desktop.
A simple way to manage a Jekyll static site that uses plugins
require 'jekyll'
module Heavies
GITHUB_REPOSITORY="heavies/beta"
SOURCE_BRANCH="source"
PUBLISHED_BRANCH="gh-pages"
PUBLISHED_DIR=".published"
ROOT_DIR=File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
module Publish
def self.dir
File.join(ROOT_DIR, PUBLISHED_DIR)
end
def self.source_branch
SOURCE_BRANCH
end
def create_published_dir
system "git clone -b #{PUBLISHED_BRANCH} git@github.com:#{GITHUB_REPOSITORY}.git #{Publish.dir}"
end
def clean_published_dir
Dir.chdir(Publish.dir) do
system "git add -A"
system "git reset --hard"
system "git checkout #{PUBLISHED_BRANCH}"
system "git pull --ff-only -ff origin #{PUBLISHED_BRANCH}"
end
end
def remove_published_dir
FileUtils.rm_rf(Publish.dir)
end
def generate_site
# Uses the standard configuration settings in _config.yml
# Overides the destination
Jekyll::Site.new(Jekyll.configuration({
"destination" => Publish.dir
})).process
end
def ready?
regex = /\A## #{Publish.source_branch}\Z/m
!regex.match(read_current_status).nil?
end
def publish_site
sha1 = publishing_sha1
Dir.chdir(Publish.dir) do
system "git add -A"
message = "Site updated at #{Time.now.utc}\n\n"
message += "Updated to: #{sha1}\n"
message += "CHANGES: #{compare_url(sha1)}"
system "git commit -m #{message.shellescape}"
system "git push origin #{PUBLISHED_BRANCH}"
end
end
def compare_url(sha1=publishing_sha1)
compare_url = "https://github.com/#{GITHUB_REPOSITORY}/compare/#{comparison(sha1)}"
end
def comparison(sha1=publishing_sha1)
"#{last_sha1}...#{sha1}"
end
private
def read_current_status
Dir.chdir(ROOT_DIR) do
system 'git fetch'
return `git status --porcelain -b`.strip
end
end
def publishing_sha1
Dir.chdir(ROOT_DIR) do
return `git rev-parse HEAD`.strip
end
end
def last_sha1
Dir.chdir(Publish.dir) do
body = `git log --pretty=format:%b -1`
return body[/^Updated to: (.+)$/,1]
end
end
end
end
require 'heavies_publish'
require 'launchy'
desc 'Publish the site'
task :publish => 'publish:publish'
namespace :publish do
include Heavies::Publish
directory Heavies::Publish.dir
file Heavies::Publish.dir do
create_published_dir
end
desc 'Get the latest published version'
task :clean => Heavies::Publish.dir do
clean_published_dir
end
desc 'Remove the published directory'
task :clobber do
remove_published_dir
end
task :generate => :clean do
generate_site
end
desc 'Compare to the currently published site'
task :compare => :clean do
system "git log #{comparison}"
end
namespace :compare do
require 'launchy'
desc 'View github compare view between source HEAD and last published version'
task :github do
Launchy.open(compare_url)
end
end
task :ready do
unless ready?
puts "You may only publish the '#{Heavies::Publish.source_branch}' branch, and it must be fully up-to-date and pushed."
exit(1)
end
end
task :publish => [:ready, :generate] do
publish_site
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment