Skip to content

Instantly share code, notes, and snippets.

@mlafeldt
Last active April 20, 2018 16:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mlafeldt/bb4d6e73f181ea55923c to your computer and use it in GitHub Desktop.
Save mlafeldt/bb4d6e73f181ea55923c to your computer and use it in GitHub Desktop.
The Rake tasks I use to manage my blog
require 'stringex'
POSTS_DIR = '_posts'
BUILD_DIR = '_site'
DEPLOY_DIR = '_deploy'
DEPLOY_BRANCH = 'master'
def git(*args)
sh 'git', *args
end
def open_in_editor(filename)
sh "open", "-a", "/Applications/Byword.app", filename
end
desc "Build the site"
task :build do
sh "jekyll build"
end
desc "Start web server to preview site"
task :preview do
sh "jekyll serve --drafts --watch"
end
desc 'Create a new draft'
task :new_draft, :title do |t, args|
title = args[:title] || 'New Draft'
filename = File.join('_drafts', "#{title.to_url}.md")
puts "==> Creating new draft: #{filename}"
open(filename, 'w') do |f|
f << "---\n"
f << "layout: post\n"
f << "title: \"#{title.to_html(true)}\"\n"
f << "tags:\n"
f << "---\n"
f << "\n"
f << "Add awesome content here.\n"
end
open_in_editor(filename)
end
desc 'Create a new post'
task :new_post, :title do |t, args|
title = args[:title] || 'New Post'
timestamp = Time.now.strftime('%Y-%m-%d')
filename = File.join(POSTS_DIR, "#{timestamp}-#{title.to_url}.md")
puts "==> Creating new post: #{filename}"
open(filename, 'w') do |f|
f << "---\n"
f << "layout: post\n"
f << "title: \"#{title.to_html(true)}\"\n"
f << "tags:\n"
f << "---\n"
f << "\n"
f << "Add awesome post content here.\n"
end
open_in_editor(filename)
end
desc 'Create a new page'
task :new_page, :title do |t, args|
title = args[:title] || 'New Page'
filename = File.join(title.to_url, 'index.md')
puts "==> Creating new page: #{filename}"
mkdir_p title.to_url
open(filename, 'w') do |f|
f << "---\n"
f << "layout: page\n"
f << "title: \"#{title.to_html(true)}\"\n"
f << "---\n"
f << "\n"
f << "Add awesome page content here.\n"
end
open_in_editor(filename)
end
desc 'Deploy the site via Git'
task :deploy => :build do
repo_url = `git config jekyll.deployurl`.chomp
if repo_url.empty?
abort 'error: set git repo url via `git config jekyll.deployurl` first'
end
if File.directory?(DEPLOY_DIR)
rm_rf Dir["#{DEPLOY_DIR}/*"]
else
mkdir_p DEPLOY_DIR
end
if File.directory?(File.join(DEPLOY_DIR, '.git'))
puts "==> Updating Git repository in #{DEPLOY_DIR} ..."
cd DEPLOY_DIR do
git 'fetch', '--prune'
git 'reset', '--hard', "origin/#{DEPLOY_BRANCH}"
end
else
puts "==> Cloning Git repository into #{DEPLOY_DIR} ..."
git 'clone', '--branch', DEPLOY_BRANCH, repo_url, DEPLOY_DIR
end
puts "==> Copying files from #{BUILD_DIR} to #{DEPLOY_DIR} ..."
cp_r Dir["#{BUILD_DIR}/*"], DEPLOY_DIR
cd DEPLOY_DIR do
puts '==> Pushing changes to remote Git repository...'
git 'add', '--all'
git 'commit', '--message', "Site updated at #{Time.now}"
git 'push', 'origin', DEPLOY_BRANCH
end
puts '==> Site successfully deployed.'
end
task :default => :preview
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment