-
-
Save ggamel/4737225 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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