Last active
April 11, 2016 11:34
-
-
Save tlattimore/94f2f3895e911772eecb5daa3e908b4d to your computer and use it in GitHub Desktop.
Personal Blog Deployment
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
# | |
# == Configuration relevant to deployment | |
post: | |
template: _post.txt | |
extension: md | |
editor: 'mvim -v' | |
git: | |
branch: master | |
transfer: | |
command: rsync | |
settings: -avp --delete | |
source: _site/ | |
destination: "username@url.site/www/dir" |
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
# | |
# == Dependencies | |
# | |
require 'rake' | |
require 'yaml' | |
require 'fileutils' | |
require 'rbconfig' | |
# | |
# == Configuration | |
# | |
# Set "rake watch" as default task | |
task :default => :watch | |
#Tag date YYYY-MM-DD HH:MM:SS +/-TTTT | |
TAG_DATE = Time.now.strftime("%Y-%m-%d-%H-%M") | |
# Load the configuration file | |
CONFIG = YAML.load_file("_config.yml") | |
# Get and parse the date | |
DATE = Time.now.strftime("%Y-%m-%d") | |
# Current time | |
CURRENT_TIME= Time.now.strftime("%Y-%m-%d %H:%M:%S") | |
# | |
# == Helper functions | |
# | |
# Execute a system command | |
def execute(command) | |
system "#{command}" | |
end | |
# Chech the title | |
def check_title(title) | |
if title.nil? or title.empty? | |
raise "Please add a title to your file." | |
end | |
end | |
# Transform the filename and date to a slug | |
def transform_to_slug(title, extension) | |
characters = /("|'|!|\?|:|\s\z)/ | |
whitespace = /\s/ | |
"#{title.gsub(characters,"").gsub(whitespace,"-").downcase}.#{extension}" | |
end | |
# Save the file with the title in the YAML front matter | |
def write_file(content, title, directory, filename) | |
parsed_content = "#{content.sub("title:", "title: \"#{title}\"")}#{content.sub("date:", "date: \"#{CURRENT_TIME}\"")}" | |
content = content.gsub("title:", "title: \"#{title}\"") | |
parsed_content = content.gsub("date:", "date: \"#{CURRENT_TIME}\"") | |
#parsed_content = parsed_content + "#{content.sub("date:", "date: \"#{CURRENT_TIME}\"")}" | |
File.write("#{directory}/#{filename}", parsed_content) | |
puts "#{filename} was created in '#{directory}'." | |
end | |
# Create the file with the slug and open the default editor | |
def create_file(directory, filename, content, title, editor) | |
if File.exists?("#{directory}/#{filename}") | |
raise "The file already exists." | |
else | |
write_file(content, title, directory, filename) | |
if editor && !editor.nil? | |
sleep 1 | |
execute("#{editor} #{directory}/#{filename}") | |
end | |
end | |
end | |
# Allow passing argv to tasks without throwing | |
# undefined task errors. | |
def escape_argv(argv) | |
# This defines an empty task at runtime for | |
# each argv item. | |
argv.each { |a| task a.to_sym do ; end } | |
end | |
# | |
# == Tasks | |
# | |
# rake post "My Post Title" | |
desc "Create a post in _posts" | |
task :post do | |
escape_argv(ARGV) | |
title = ARGV[1] | |
if title.nil? or title.empty? | |
raise "Please add a title to your file." | |
end | |
template = CONFIG["post"]["template"] | |
extension = CONFIG["post"]["extension"] | |
editor = CONFIG["editor"] | |
filename = transform_to_slug("#{DATE}-#{title}", extension) | |
content = File.read(template) | |
create_file('_posts', filename, content, title, editor) | |
end | |
# rake build | |
desc "Build the site" | |
task :build do | |
execute("bundle exec jekyll build") | |
end | |
# rake deploy "Commit message" | |
desc "Deploy the site to a remote git repo" | |
task :deploy do | |
escape_argv(ARGV) | |
message = ARGV[1] | |
branch = CONFIG["git"]["branch"] | |
command = CONFIG["transfer"]["command"] | |
source = CONFIG["transfer"]["source"] | |
destination = CONFIG["transfer"]["destination"] | |
settings = CONFIG["transfer"]["settings"] | |
if message.nil? or message.empty? | |
message = CURRENT_TIME | |
end | |
if branch.nil? or branch.empty? | |
raise "Please add a branch." | |
else | |
execute("bundle exec jekyll build") | |
execute("git add .") | |
execute("git tag #{TAG_DATE}") | |
execute("git commit -m \"#{CURRENT_TIME}\"") | |
execute("git push --follow-tags origin #{branch} && git push --tags") | |
end | |
if command.nil? or command.empty? | |
raise "Please choose a file transfer command." | |
else | |
Rake::Task[:build].invoke | |
execute("rsync #{settings} #{source} #{destination}") | |
puts "Your site was transfered." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment