-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
100 lines (81 loc) · 3.11 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require 'jekyll'
require 'tmpdir'
require 'stringex'
require 'pathname'
source_dir = "." # source file directory
deploy_dir = "_site" # deploy directory (for Github pages deployment)
stash_dir = "_stash" # directory to stash posts for speedy generation
posts_dir = "_posts" # directory for blog files
new_post_ext = "md" # default new page file extension when using the new_page task
# Usage:
# bundle exec rake site:<task>
namespace :site do
desc "Preview site"
task :serve do
system "bundle exec jekyll serve --limit 5 --incremental"
end
desc "Generate site"
task :generate do
system "JEKYLL_ENV=production bundle exec jekyll build"
# Below worked for Jekyll 3.x
#ENV['JEKYLL_ENV'] = 'production'
#Jekyll::Site.new(Jekyll.configuration({
# "source" => ".",
# "destination" => "_site",
# "config" => "_config.yml"
#})).process
end
desc "Publish blog to gh-pages"
task :publish => [:generate] do
# Compile the Jekyll site using the config.
# Get the origin to which we are going to push the site.
origin = `git config --get remote.origin.url`
# Make a temporary directory for the build before production release.
# This will be torn down once the task is complete.
Dir.mktmpdir do |tmp|
prevdir = Dir.pwd
# Copy accross our compiled _site directory.
cp_r "_site/.", tmp
# Switch in to the tmp dir.
Dir.chdir tmp
# Prepare all the content in the repo for deployment.
system "git init" # Init the repo.
system "git add . && git commit -m 'Site updated at #{Time.now.utc}'" # Add and commit all the files.
# Add the origin remote for the parent repo to the tmp folder.
system "git remote add origin #{origin}"
# Push the files to the gh-pages branch, forcing an overwrite.
system "git push origin master:refs/heads/gh-pages --force"
# Restore where we were, to avoid getcwd error otherwise
Dir.chdir prevdir
end
# Done.
end
desc "Begin a new post in #{source_dir}/#{posts_dir}"
task :post, :title do |t, args|
title = ENV["title"] || get_stdin("Enter a title for your post: ")
mkdir_p "#{source_dir}/#{posts_dir}"
date = (ENV["date"] ? Time.parse(ENV["date"]) : Time.now)
dateYmd = date.strftime('%Y-%m-%d')
filename = "#{dateYmd}-#{title.to_url}.#{new_post_ext}"
pathname = "#{source_dir}/#{posts_dir}/#{filename}"
if File.exist?(pathname)
abort("rake aborted!") if ask("#{pathname} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new post: #{Pathname.new(pathname).cleanpath}"
open(pathname, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/&/,'&')}\""
post.puts "date: #{dateYmd} #{Time.now.strftime('%H:%M:%S %z')}"
post.puts "comments: true"
post.puts "permalink: /#{date.strftime('%Y/%m')}/#{title.to_url}.html"
post.puts "tags: []"
post.puts "thumbnail: "
post.puts "---"
end
end
end
def get_stdin(message)
print message
STDIN.gets.chomp
end