This repository has been archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
192 lines (164 loc) · 4.59 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Setup Bundler
require 'rubygems'
require 'bundler/setup'
# Rsync configuration
ssh_user = "[email protected]"
ssh_port = "22"
document_root = "~/website.com/"
rsync_delete = true
rsync_args = "" # Any extra arguments to pass to rsync
# GitHub pages configuration
github_user = "username"
github_project = "" # Leave this variable empty if you're using user or organization pages
deploy_branch = "master" # For project pages, replace it with "gh-pages"
# Misc
source_dir = "."
public_dir = "./_site"
deploy_dir = "./_deploy" # Deployment directory for GitHub Pages
posts_dir = "./_posts"
post_ext = "markdown"
#
# Overview
#
desc "List tasks"
task :list do
puts "Tasks: #{(Rake::Task.tasks - [Rake::Task[:list]]).join(', ')}"
puts "(type rake -T for more detail)\n\n"
end
#
# Posts
#
desc "Create a post"
task :create_post, :title do |t, args|
if args.title
title = args.title
else
title = get_stdin("Enter a title for your post: ")
end
mkdir_p "#{source_dir}/#{posts_dir}"
filename = "#{source_dir}/#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{slugify(title)}.#{post_ext}"
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/&/,'&')}\""
post.puts "description:"
post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}"
post.puts "comments: true"
post.puts "categories:"
post.puts "cover_image:"
post.puts "tags:"
post.puts "---"
end
end
#
# Build and preview site
#
desc "Cleanup cache files"
task :clean do
system "rm -rf #{public_dir}"
end
desc "Build jekyll site"
task :build do
puts "Building site with Jekyll..."
system "jekyll build"
end
desc "Preview jekyll site"
task :preview do
puts "Build and preview site..."
system "jekyll serve --watch"
end
#
# Deploy
#
namespace :deploy do
desc "Deploy jekyll site via rsync"
task :rsync => :build do
exclude = ""
if File.exists?('./rsync-exclude')
exclude = "--exclude-from '#{File.expand_path('./rsync-exclude')}'"
end
puts "Deploying website via Rsync..."
ok_failed system("rsync -avze 'ssh -p #{ssh_port}' #{exclude} #{rsync_args} #{"--delete" unless rsync_delete == false} #{public_dir}/ #{ssh_user}:#{document_root}")
end
desc "Deploy GitHub pages"
task :github, [:message] => [:build] do |t, args|
if args.message
message = args.message
else
message = "Site updated at #{Time.now.utc}"
end
puts "## Deploying branch to Github Pages "
puts "## Pulling any updates from Github Pages "
cd "#{deploy_dir}" do
system "git checkout #{deploy_branch}"
system "git pull origin #{deploy_branch}"
end
puts "\n## Copying #{public_dir} to #{deploy_dir}"
cp_r "#{public_dir}/.", deploy_dir
cd "#{deploy_dir}" do
system "git add -A"
puts "\n## Committing: #{message}"
system "git commit -m \"#{message}\""
puts "\n## Pushing generated #{deploy_dir} website"
system "git push origin #{deploy_branch}"
puts "\n## Github Pages deploy complete"
end
end
end
desc "Setup deploy directory and branch for Github Pages deployment"
task :setup_github_pages do
rm_rf deploy_dir
mkdir deploy_dir
cd "#{deploy_dir}" do
system "git init"
system "echo 'Hello, GitHub Pages.' > index.html"
system "git add ."
system "git commit -m \"Initial commit\""
system "git branch -m #{deploy_branch}"
system "git remote add origin #{repo_url github_user, github_project}"
end
puts "\n---\n## Now you can deploy to #{repo_url github_user, github_project} with `rake deploy:github` ##"
end
#
# Helper methods
#
def ok_failed(condition)
if (condition)
puts "OK"
else
puts "FAILED"
end
end
def get_stdin(message)
print message
STDIN.gets.chomp
end
def ask(message, valid_options)
if valid_options
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
else
answer = get_stdin(message)
end
answer
end
def slugify(str)
str.downcase.gsub(/[^a-z0-9]+/, '-').gsub(/^-|-$/, '');
end
def site_url(user)
if File.exists?('source/CNAME')
"http://#{IO.read('source/CNAME').strip}"
else
"http://#{user}.github.io"
end
end
def repo_url(user, project)
if project == ''
"https://github.com/#{user}/#{user}.github.io.git"
else
"https://github.com/#{user}/#{project}.git"
end
end