-
Notifications
You must be signed in to change notification settings - Fork 15
/
Rakefile
258 lines (233 loc) · 8.64 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#----------------------------------------------------------------------
# Configuration
#----------------------------------------------------------------------
SOURCE = "."
CONFIG = {
'posts' => File.join(SOURCE, "_posts"),
'post_ext' => "md",
# Setup for GitHub Pages.
# If you deploy to a 'gh-pages' branch on GitHub,
# use automatic Project Page generator at first.
# See https://help.github.com/articles/creating-pages-with-the-automatic-generator
'type_github' => {
'deploy_dir' => "_deploy",
'deploy_remote' => "origin",
'deploy_branch' => "master",
# 'deploy_branch' => "gh-pages",
},
# Setup for Heroku.
# Buildpack: https://github.com/pearkes/heroku-buildpack-static
'type_heroku' => {
'deploy_dir' => "_heroku",
'deploy_remote' => "heroku",
'deploy_branch' => "master",
'buildpack' => "https://github.com/pearkes/heroku-buildpack-static",
}
}
task :default => :preview
#----------------------------------------------------------------------
# Preview a blog.
# @usage: rake preview
#----------------------------------------------------------------------
desc "preview on http://localhost:4000/"
task :preview do
# jekyll('--pygments --auto --server')
jekyll('--auto --server')
end
#----------------------------------------------------------------------
# Create a new post.
# @usage: rake post title="A Title" [date="2012-02-09"]
#----------------------------------------------------------------------
desc "Begin a new post in #{CONFIG['posts']}"
task :post, :title do |t, args|
abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts'])
title = ENV["title"] || args.with_defaults(:title => 'new-post') && args.title
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
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(CONFIG['posts'], "#{date}-#{slug}.#{CONFIG['post_ext']}")
if File.exist?(filename)
abort("rake aborted!") unless ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'y'
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
post.puts "title: \"#{title.gsub(/-/,' ').gsub(/&/,'&')}\""
post.puts "description: \"\""
post.puts "keywords: \"\""
post.puts "excerpt: \"\""
post.puts "thumbnail: \"\""
post.puts "category: [uncategorized]"
post.puts "tags: []"
post.puts "comments: true"
post.puts "published: true"
post.puts "---"
post.puts "\n<!--more-->"
end
end
#----------------------------------------------------------------------
# Create a new page.
# @usage: rake page name="about.html"
# You can also specify a sub-directory path.
# If you don't specify a file extention we create an index.html
# at the path specified
#----------------------------------------------------------------------
desc "Create a new page."
task :page do
name = ENV["name"] || "new-page.md"
filename = File.join(SOURCE, "#{name}")
filename = File.join(filename, "index.html") if File.extname(filename) == ""
title = File.basename(filename, File.extname(filename)).gsub(/[\W\_]/, " ").gsub(/\b\w/){$&.upcase}
if File.exist?(filename)
abort("rake aborted!") unless ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'y'
end
mkdir_p File.dirname(filename)
puts "Creating new page: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: page"
post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
post.puts "title: \"#{title}\""
post.puts "description: \"\""
post.puts "keywords: \"\""
post.puts "published: true"
post.puts "---"
end
end
#----------------------------------------------------------------------
# Clean a blog.
# @usage: rake clean
#----------------------------------------------------------------------
desc "Clean up generated site"
task :clean do
cleanup
end
#----------------------------------------------------------------------
# Build & Deploy for GitHub type
# @usage: rake build / rake deploy
#----------------------------------------------------------------------
desc "Build site with Jekyll"
task :build => :clean do
build_site(CONFIG['type_github'])
end
desc "Deploy to GitHub or Bitbucket"
multitask :deploy => :build do
deploy_site(CONFIG['type_github'])
end
#----------------------------------------------------------------------
# Setup git remote server.
# @usage: rake setup_remote [repository-name]
#----------------------------------------------------------------------
desc "Set up _deploy folder and git remote server"
task :setup_remote, :repo do |t, args|
config = CONFIG['type_github']
if File.exist?("#{config['deploy_dir']}/.git")
abort("rake aborted!") unless ask("remote server already exists. Do you want to overwrite?", ['y', 'n']) == 'y'
end
if args.repo
repo_url = args.repo
else
repo_url = get_stdin("Enter your repository uri for read/write: ")
end
abort("rake aborted!") unless ask("Create #{config['deploy_branch']} on " + repo_url + ".\nContinu?", ['y', 'n']) == 'y'
rm_rf "#{config['deploy_dir']}"
mkdir "#{config['deploy_dir']}"
cd "#{config['deploy_dir']}" do
system "git init"
system "echo 'My page is coming soon …' > index.html"
system "touch .nojekyll"
system "git add ."
system "git commit -m \"setup remote\""
system "git branch -m #{config['deploy_branch']}" unless "#{config['deploy_branch']}" == 'master'
system "git remote add #{config['deploy_remote']} #{repo_url}"
end
puts "\n---\n## Now you can deploy to remote server with `rake deploy` ##"
end
#----------------------------------------------------------------------
# Build & Deploy for Heroku type
# @usage: rake build_heroku / rake heroku
#----------------------------------------------------------------------
desc "Build site for Heroku"
task :build_heroku => :clean do
build_site(CONFIG['type_heroku'])
end
desc "Deploy app to Heroku"
multitask :heroku => :build_heroku do
deploy_site(CONFIG['type_heroku'])
end
#----------------------------------------------------------------------
# Setup heroku server.
# @usage: rake heroku [app-name]
#----------------------------------------------------------------------
desc "Set up _heroku folder and create app with buildpack"
task :setup_heroku, :app do |t, args|
config = CONFIG['type_heroku']
if File.exist?("#{config['deploy_dir']}/.git")
abort("rake aborted!") unless ask("remote server already exists. Do you want to overwrite?", ['y', 'n']) == 'y'
end
if args.app
app_name = args.app
else
app_name = get_stdin("Enter new app name (if blank, Heroku makes it fancy!): ")
end
abort("rake aborted!") unless ask("Create #{config['deploy_branch']} for " + app_name + ".\nContinu?", ['y', 'n']) == 'y'
rm_rf "#{config['deploy_dir']}"
mkdir "#{config['deploy_dir']}"
cd "#{config['deploy_dir']}" do
system "git init"
system "heroku create #{app_name} --stack cedar --buildpack #{config['buildpack']}"
end
puts "\n---\n## Now you can deploy to remote server with `rake deploy` ##"
end
#----------------------------------------------------------------------
# Build & Deploy common function
#----------------------------------------------------------------------
def cleanup
puts "## cleanup _site ..."
sh 'rm -rf _site'
end
def build_site(args)
puts "## building _site ..."
# jekyll('--pygments')
jekyll()
if !Dir.exist?("#{args['deploy_dir']}")
abort("you should setup remote server ... rake aborted!")
end
(Dir["#{args['deploy_dir']}/*"]).each { |f| rm_rf(f) }
system "cp -Rp _site/* #{args['deploy_dir']}"
puts "## copying _site to #{args['deploy_dir']}"
end
def deploy_site(args)
puts "## Deploying ..."
cd "#{args['deploy_dir']}" do
system "git add ."
system "git add -u"
puts "\n## Committing: Site updated at #{Time.now.utc}"
message = "Site updated at #{Time.now.utc}"
system "git commit -m '#{message}'"
puts "\n## Pushing generated #{args['deploy_dir']} to git server"
system "git push #{args['deploy_remote']} #{args['deploy_branch']} --force"
puts "\n## Deploy complete"
end
end
def jekyll(opts = '')
sh 'jekyll ' + opts
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 get_stdin(message)
print message
STDIN.gets.chomp
end