Skip to content

Instantly share code, notes, and snippets.

@mfenner
Created April 14, 2014 19:09
Show Gist options
  • Save mfenner/10675094 to your computer and use it in GitHub Desktop.
Save mfenner/10675094 to your computer and use it in GitHub Desktop.
Rakefile for Jekyll/Travis (work in progress)
#############################################################################
#
# Modified version of jekyllrb Rakefile
# https://github.com/jekyll/jekyll/blob/master/Rakefile
#
#############################################################################
require 'rake'
require 'jekyll'
require 'date'
require 'git'
# read Jekyll configuration
config = Jekyll::Configuration::DEFAULTS
if File.exist? '_config.yml'
config = config.merge(Jekyll::Configuration.new.read_config_file('_config.yml'))
end
@destination = config["destination"]
# read information from git repo
# source branch is the current branch
# destination branch is derived from source branch name
path = File.expand_path File.dirname(__FILE__)
repo = Git.open(path)
@source_branch = repo.branches.find { |b| b.name if b.current && b.remote.nil? }
@destination_branch = @source_branch == "master" ? "gh-pages" : "master"
@git_user = repo.config('user.name')
@git_email = repo.config('user.email')
@remote = repo.remote(:origin).url
@user_name = "mfenner"
@repo_name = "mfenner.github.io"
#############################################################################
#
# Helper functions
#
#############################################################################
# File activesupport/lib/active_support/inflector/transliterate.rb, line 80
def parameterize(string, sep = '-')
# replace accented chars with their ascii
# simplified from original to remove dependency
parameterized_string = string.dup.force_encoding('US-ASCII')
# Turn unwanted chars into the separator
# changed from original: allow A-Z
parameterized_string.gsub!(/[^a-zA-Z0-9\-_]+/, sep)
unless sep.nil? || sep.empty?
re_sep = Regexp.escape(sep)
# No more than one of the separator in a row.
parameterized_string.gsub!(/#{re_sep}{2,}/, sep)
# Remove leading/trailing separator.
parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/, '')
end
parameterized_string.downcase
end
def check_destination
unless Dir.exist? @destination
#@repo = Git.clone("https://#{ENV['GIT_NAME']}:#{ENV['GH_TOKEN']}@github.com/#{USERNAME}/#{REPO}.git", :path => DESTINATION)
sh "git clone #{@remote} #{@destination}"
end
end
#############################################################################
#
# Post and page tasks
#
#############################################################################
namespace :post do
desc "Create a new post"
task :create do
title = ENV["title"] || "new-post"
begin
slug = parameterize(title)
puts slug
rescue => e
puts "Error: invalid characters in title"
exit -1
end
begin
date = ENV['date'] ? Date.parse(ENV['date']) : Date.today
rescue => e
puts "Error: date format must be YYYY-MM-DD"
exit -1
end
filename = File.join("_posts", "#{date}-#{slug}.md")
if File.exist?(filename)
puts "Error: post already exists"
exit -1
end
header = { "layout" => "post", "title" => title }
content = header.to_yaml + "---\n"
if IO.write(filename, content)
puts "Post #{filename} created"
else
puts "Error: #{filename} could not be written"
end
end
end
namespace :page do
desc "Create a new page"
task :create do
title = ENV["title"] || "new-page"
begin
slug = parameterize(title)
puts slug
rescue => e
puts "Error: invalid characters in title"
exit -1
end
folder = ENV["folder"] || "."
filename = File.join(folder, "#{slug}.md")
if File.exist?(filename)
puts "Error: page already exists"
exit -1
end
header = { "layout" => "page", "title" => title }
content = header.to_yaml + "---\n"
if IO.write(filename, content)
puts "Page #{filename} created"
else
puts "Error: #{filename} could not be written"
end
end
end
#############################################################################
#
# Site tasks
#
#############################################################################
namespace :site do
desc "Generate the site"
task :build do
check_destination
sh "bundle exec jekyll build"
end
desc "Generate the site and serve locally"
task :serve do
check_destination
sh "bundle exec jekyll serve"
end
desc "Generate the site, serve locally and watch for changes"
task :watch do
sh "bundle exec jekyll serve --watch"
end
desc "Generate the site and push changes to remote origin"
task :deploy do
# Detect pull request
if ENV['TRAVIS_PULL_REQUEST'].to_s.to_i > 0
puts 'Pull request detected. Not proceeding with deploy.'
exit
end
# Configure git if this is run in Travis CI
if ENV["TRAVIS"]
sh "git config --global user.name '#{@git_user}'"
sh "git config --global user.email '#{@git_email}'"
sh "git config --global push.default simple"
end
# Make sure destination folder exists as git repo
check_destination
sh "git checkout #{@source_branch}"
Dir.chdir(@destination) { sh "git checkout #{@destination_branch}" }
# Generate the site
sh "bundle exec jekyll build"
# Commit and push to github
sha = `git log`.match(/[a-z0-9]{40}/)[0]
Dir.chdir(@destination) do
sh "git add --all ."
sh "git commit -m 'Updating to #{@user_name}/#{@repo_name}@#{sha}.'"
sh "git push origin #{@destination_branch}"
puts "Pushed updated branch #{@destination_branch} to GitHub Pages"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment