-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
398 lines (349 loc) · 14.9 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
require "rubygems"
require "bundler/setup"
require "stringex"
require "fileutils"
## -- Deploy config -- ##
deploy_default = "s3"
## -- Misc Configs -- ##
public_dir = "public" # compiled site directory
source_dir = "source" # source file directory
blog_index_dir = 'source' # directory for your blog's index page (if you put your index in source/blog/index.html, set this to 'source/blog')
stash_dir = "_stash" # directory to stash posts for speedy generation
posts_dir = "_posts" # directory for blog files
themes_dir = ".themes" # directory for blog files
new_post_ext = "markdown" # default new post file extension when using the new_post task
new_page_ext = "markdown" # default new page file extension when using the new_page task
server_port = "4000" # port for preview server eg. localhost:4000
asset_version = Time.new.strftime("%y%m%d%H%M") # For asset versioning
desc "Initial setup for Octopress: copies the default theme into the path of Jekyll's generator. Rake install defaults to rake install[classic] to install a different theme run rake install[some_theme_name]"
task :install, :theme do |_, args|
if File.directory?(source_dir) || File.directory?("sass")
abort("rake aborted!") if ask("A theme is already installed, proceeding will overwrite existing files. Are you sure?", %w(y n)) == 'n'
end
# copy theme into working Jekyll directories
theme = args.theme || 'classic'
puts "## Copying "+theme+" theme into ./#{source_dir} and ./sass"
mkdir_p source_dir
cp_r "#{themes_dir}/#{theme}/source/.", source_dir
mkdir_p "sass"
cp_r "#{themes_dir}/#{theme}/sass/.", "sass"
mkdir_p "#{source_dir}/#{posts_dir}"
mkdir_p public_dir
end
#######################
# Working with Jekyll #
#######################
desc "Generate jekyll site for development"
task :generate => [:sass, :update_asset_versions, :jekyll, :combine, :minify]
desc "Generate jekyll site for production"
task :generate_optimized => [:sass, :update_asset_versions, :jekyll, :combine, :minify, :gzip]
desc "Process Sass"
task :sass do
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
puts "## Generating CSS with Compass"
system "compass compile --sass-dir #{source_dir}/_sass --css-dir #{source_dir}/stylesheets --images-dir #{source_dir}/images"
end
desc "Run Jekyll"
task :jekyll do
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
puts "## Generating Site with Jekyll"
system "jekyll build"
end
desc "Combine CSS"
task :combine_css do
puts "## Combining CSS"
styles_dir = "#{source_dir}/stylesheets"
system "cat #{styles_dir}/bootstrap/bootstrap.css #{styles_dir}/bootstrap/responsive.css #{styles_dir}/syntax/syntax.css #{styles_dir}/custom.css > #{styles_dir}/all.css"
end
desc "Combine JS"
task :combine_js do
puts "## Combining JS"
scripts_dir = "#{source_dir}/javascripts"
system "cat #{scripts_dir}/custom.js > #{scripts_dir}/all.js"
end
desc "Combine CSS/JS"
task :combine => [:combine_css, :combine_js]
desc "Minify CSS"
task :minify_css do
puts "## Minifying CSS"
input = "#{source_dir}/stylesheets/all.css"
output = "#{source_dir}/stylesheets/all.#{asset_version}.css"
system "cleancss -e -o #{output} #{input}"
Dir.glob("#{source_dir}/stylesheets/all.*").each do |f|
FileUtils.cp(f, "#{public_dir}/stylesheets")
end
end
desc "Minify JS"
task :minify_js do
puts "## Minifying JS"
input = "#{source_dir}/javascripts/all.js"
output = "#{source_dir}/javascripts/all.#{asset_version}.js"
source_map_option = "--source-map \"root='https://www.eriwen.com',filename='#{source_dir}/javascripts/all.#{asset_version}.js.map'\""
system "uglifyjs #{input} -o #{output} -m -c #{source_map_option}"
Dir.glob("#{source_dir}/javascripts/all.*").each do |f|
FileUtils.cp(f, "#{public_dir}/javascripts")
end
end
desc "Minify CSS/JS"
task :minify => [:minify_css, :minify_js]
desc "Optimize Images"
task :optimize_images do
puts "## Optimizing Images"
Dir.glob("#{source_dir}/images/*.{jp,pn}g").each do |f|
webp_file = "#{f[0..-4]}webp"
puts "Checking #{f} -> #{webp_file}"
if test(?f, f) and not File.exists?(webp_file)
ok_failed system("cwebp -q 100 #{f} -o #{webp_file}")
end
end
end
desc "GZip HTML"
task :gzip_html do
puts "## GZipping HTML"
system 'find public/ -type f -name \*.html -exec gzip -9 {} \;'
# Batch rename .html.gz to .html
Dir['**/*.html.gz'].each do |f|
test(?f, f) and File.rename(f, f.gsub(/\.html\.gz/, '.html'))
end
end
desc "GZip CSS"
task :gzip_css do
puts "## GZipping CSS"
styles_dir = "#{public_dir}/stylesheets"
system "gzip -9 #{styles_dir}/all.#{asset_version}.css"
system "mv #{styles_dir}/all.#{asset_version}.css{.gz,}"
end
desc "GZip JS"
task :gzip_js do
puts "## GZipping JS"
scripts_dir = "#{public_dir}/javascripts"
system "gzip -9 #{scripts_dir}/all.#{asset_version}.js"
system "mv #{scripts_dir}/all.#{asset_version}.js{.gz,}"
system "gzip -9 #{scripts_dir}/stacktrace.min.js"
system "mv #{scripts_dir}/stacktrace.min.js{.gz,}"
end
desc "GZip All"
task :gzip => [:gzip_html, :gzip_css, :gzip_js]
desc "Update head include for static assets"
task :update_asset_versions do
puts "## Updating asset versions"
# Replace instances of all.js and all.1234.js with all.{version}.js
content = ''
File.open("#{source_dir}/_includes/head.html", 'r') do |file|
content = file.read.gsub(/all(\.\d+)?\./, "all.#{asset_version}.")
end
File.open("#{source_dir}/_includes/head.html", 'w') do |file|
file.write(content)
end
content = ''
File.open("#{source_dir}/_includes/after_footer.html", 'r') do |file|
content = file.read.gsub(/all(\.\d+)?\./, "all.#{asset_version}.")
end
File.open("#{source_dir}/_includes/after_footer.html", 'w') do |file|
file.write(content)
end
end
desc "Watch the site and regenerate when it changes"
task :watch do
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
puts "Starting to watch source with Jekyll and Compass."
system "compass compile --css-dir #{source_dir}/stylesheets" unless File.exist?("#{source_dir}/stylesheets/screen.css")
jekyll_pid = Process.spawn({"OCTOPRESS_ENV" =>"preview"}, "jekyll --auto")
compass_pid = Process.spawn("compass watch")
trap("INT") do
[jekyll_pid, compass_pid].each { |pid| Process.kill(9, pid) rescue Errno::ESRCH }
exit 0
end
[jekyll_pid, compass_pid].each { |pid| Process.wait(pid) }
end
desc "preview the site in a web browser"
task :preview do
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
puts "Starting to watch source with Jekyll and Compass. Starting Rack on port #{server_port}"
system "compass compile --css-dir #{source_dir}/stylesheets" unless File.exist?("#{source_dir}/stylesheets/screen.css")
# Replace instances of all.{version}.js with all.js
content = ''
File.open("#{source_dir}/_includes/head.html", 'r') do |file|
content = file.read.gsub(/all(\.\d+)?\./, "all.")
end
File.open("#{source_dir}/_includes/head.html", 'w') do |file|
file.write(content)
end
jekyll_pid = Process.spawn({"OCTOPRESS_ENV" =>"preview"}, "jekyll --auto")
compass_pid = Process.spawn("compass watch")
rackup_pid = Process.spawn("rackup --port #{server_port}")
trap("INT") do
[jekyll_pid, compass_pid, rackup_pid].each { |pid| Process.kill(9, pid) rescue Errno::ESRCH }
exit 0
end
[jekyll_pid, compass_pid, rackup_pid].each { |pid| Process.wait(pid) }
end
# usage rake new_post[my-new-post] or rake new_post['my new post'] or rake new_post (defaults to "new-post")
desc "Begin a new post in #{source_dir}/#{posts_dir}"
task :new_post, :title do |_, args|
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
mkdir_p "#{source_dir}/#{posts_dir}"
args.with_defaults(:title => 'new-post')
title = args.title
filename = "#{source_dir}/#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.#{new_post_ext}"
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", %w(y n)) == 'n'
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "comments: true"
post.puts "title: \"#{title.gsub(/&/,'&')}\""
post.puts "slug: #{title.downcase.gsub(/ /, '-')}"
post.puts "published: false"
post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
post.puts "category: "
post.puts "---"
end
end
# usage rake new_page[my-new-page] or rake new_page[my-new-page.html] or rake new_page (defaults to "new-page.markdown")
desc "Create a new page in #{source_dir}/(filename)/index.#{new_page_ext}"
task :new_page, :filename do |t, args|
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
args.with_defaults(:filename => 'new-page')
page_dir = [source_dir]
if args.filename.downcase =~ /(^.+\/)?(.+)/
filename, dot, extension = $2.rpartition('.').reject(&:empty?) # Get filename and extension
title = filename
page_dir.concat($1.downcase.sub(/^\//, '').split('/')) unless $1.nil? # Add path to page_dir Array
if extension.nil?
page_dir << filename
filename = "index"
end
extension ||= new_page_ext
page_dir = page_dir.map! { |d| d = d.to_url }.join('/') # Sanitize path
filename = filename.downcase.to_url
mkdir_p page_dir
file = "#{page_dir}/#{filename}.#{extension}"
if File.exist?(file)
abort("rake aborted!") if ask("#{file} already exists. Do you want to overwrite?", %w(y n)) == 'n'
end
puts "Creating new page: #{file}"
open(file, 'w') do |page|
page.puts "---"
page.puts "layout: page"
page.puts "title: \"#{title}\""
page.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
page.puts "comments: true"
page.puts "sharing: true"
page.puts "footer: true"
page.puts "---"
end
else
puts "Syntax error: #{args.filename} contains unsupported characters"
end
end
# usage rake isolate[my-post]
desc "Move all other posts than the one currently being worked on to a temporary stash location (stash) so regenerating the site happens much quicker."
task :isolate, :filename do |_, args|
stash_dir = "#{source_dir}/#{stash_dir}"
FileUtils.mkdir(stash_dir) unless File.exist?(stash_dir)
Dir.glob("#{source_dir}/#{posts_dir}/*.*") do |post|
FileUtils.mv post, stash_dir unless post.include?(args.filename)
end
end
desc "Move all stashed posts back into the posts directory, ready for site generation."
task :integrate do
FileUtils.mv Dir.glob("#{source_dir}/#{stash_dir}/*.*"), "#{source_dir}/#{posts_dir}/"
end
desc "Clean out caches: .pygments-cache, .gist-cache, .sass-cache"
task :clean do
rm_rf %w(.pygments-cache/** .gist-cache/** .sass-cache/** source/stylesheets/screen.css)
end
desc "Move sass to sass.old, install sass theme updates, replace sass/custom with sass.old/custom"
task :update_style, :theme do |_, args|
theme = args.theme || 'classic'
if File.directory?("sass.old")
puts "removed existing sass.old directory"
rm_r "sass.old", :secure=>true
end
mv "sass", "sass.old"
puts "## Moved styles into sass.old/"
cp_r "#{themes_dir}/"+theme+"/sass/", "sass"
cp_r "sass.old/custom/.", "sass/custom"
puts "## Updated Sass ##"
end
desc "Move source to source.old, install source theme updates, replace source/_includes/navigation.html with source.old's navigation"
task :update_source, :theme do |_, args|
theme = args.theme || 'classic'
if File.directory?("#{source_dir}.old")
puts "## Removed existing #{source_dir}.old directory"
rm_r "#{source_dir}.old", :secure=>true
end
mkdir "#{source_dir}.old"
cp_r "#{source_dir}/.", "#{source_dir}.old"
puts "## Copied #{source_dir} into #{source_dir}.old/"
cp_r "#{themes_dir}/"+theme+"/source/.", source_dir, :remove_destination=>true
cp_r "#{source_dir}.old/_includes/custom/.", "#{source_dir}/_includes/custom/", :remove_destination=>true
cp "#{source_dir}.old/favicon.png", source_dir
mv "#{source_dir}/index.html", "#{blog_index_dir}", :force=>true if blog_index_dir != source_dir
cp "#{source_dir}.old/index.html", source_dir if blog_index_dir != source_dir && File.exists?("#{source_dir}.old/index.html")
puts "## Updated #{source_dir} ##"
end
##############
# Deploying #
##############
desc "Default deploy task"
task :deploy do
# Check if preview posts exist, which should not be published
if File.exists?(".preview-mode")
puts "## Found posts in preview mode, regenerating files ..."
File.delete(".preview-mode")
Rake::Task[:generate].execute
end
Rake::Task[:copydot].invoke(source_dir, public_dir)
Rake::Task["#{deploy_default}"].execute
end
desc "Deploy website via s3cmd"
task :s3 do
puts "## Deploying website"
bucket = 'www.eriwen.com'
s3cmd_path = 'aws s3'
# sync gzipped html files
ok_failed system("#{s3cmd_path} sync --acl public-read public/ s3://#{bucket}/ --content-type 'text/html; charset=utf-8' --content-encoding 'gzip' --exclude '*.*' --include '*.html'")
# sync non gzipped, non js/css/image files
ok_failed system("#{s3cmd_path} sync --acl public-read public/ s3://#{bucket}/ --exclude 'images/' --exclude '*.css' --exclude '*.js' --exclude '*.html'")
# sync gzipped css and js
ok_failed system("#{s3cmd_path} sync --acl public-read public/stylesheets/ s3://#{bucket}/stylesheets/ --content-encoding 'gzip' --cache-control 'public, max-age=31600000'")
ok_failed system("#{s3cmd_path} sync --acl public-read public/javascripts/ s3://#{bucket}/javascripts/ --content-encoding 'gzip' --cache-control 'public, max-age=31600000'")
# sync all images
ok_failed system("#{s3cmd_path} sync --acl public-read public/images/ s3://#{bucket}/images/ --cache-control 'public, max-age=31600000'")
end
desc "Generate website and deploy"
task :gen_deploy => [:clean, :integrate, :generate, :deploy]
desc "copy dot files for deployment"
task :copydot, :source, :dest do |_, args|
FileList["#{args.source}/**/.*"].exclude("**/.", "**/..", "**/.DS_Store", "**/._*").each do |file|
cp_r file, file.gsub(/#{args.source}/, "#{args.dest}") unless File.directory?(file)
end
end
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
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