Skip to content

Instantly share code, notes, and snippets.

@appleton
Created December 18, 2012 09:53
Show Gist options
  • Save appleton/4326739 to your computer and use it in GitHub Desktop.
Save appleton/4326739 to your computer and use it in GitHub Desktop.
A Rakefile to help managing a Jekyll site. Fill out your own ssh config in the CONFIG hash. Note that publish requires that you have ssh keys set up with your server, this doesn't work with ssh passwords (because I didn't need it).
require 'pty'
CONFIG = {
:remote => {
:host => 'my.ssh-host.com',
:user => 'my-ssh-username',
:path => 'path-to/site/root'
},
:posts => 'local/posts/directory'
}
def build
# Generate Site
puts `jekyll`
# Copy .htaccess across
FileUtils.cp './.htaccess', './_site/.htaccess'
end
def publish
puts 'Publishing site to sever...'
cmd = "rsync -av ./_site/ -e ssh #{CONFIG[:remote][:user]}@#{CONFIG[:remote][:host]}:#{CONFIG[:remote][:path]}"
puts cmd
# Shell out to rsync
begin
PTY.spawn(cmd) do |stdin, stdout, pid|
begin
stdin.each { |line| print line }
rescue Errno::EIO
puts 'Errno:EIO error'
end
end
rescue PTY::ChildExited
puts 'The child process exited!'
end
end
desc 'Generate site with Jekyll'
task :build do
build
end
desc 'Generate site with Jekyll and then push results to the server'
task :publish do
build
publish
end
# Usage: rake post title="A Title" [date="2012-02-09"] [category="portfolio"]
desc "Begin a new post"
task :post do
category = ENV['category'] || ''
dir = CONFIG[:posts]
title = ENV["title"] || "new-post"
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
abort("rake aborted: '#{dir}' directory not found.") unless FileTest.directory?(dir)
begin
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d')
rescue Exception => e
puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!"
exit -1
end
filename = File.join(dir, "#{date}-#{slug}.md")
abort("rake aborted: file #{filename} already exists") if File.exist?(filename)
puts "Creating new post: #{filename}..."
# Create new post file
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: #{title.gsub(/-/,' ')}"
post.puts "category: #{category}"
post.puts "---"
end
puts 'Done!'
end
# Available tasks
rake build
Generate site with Jekyll and then clean up unneeded files ready to deploy
rake post
Begin a new post
rake publish
Generate site with Jekyll and then rsync results to the server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment