forked from xdd0sx/nanoc-heroku
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
190 lines (168 loc) · 4.58 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
# coding: UTF-8
require 'yaml'
##
# Load Nanoc3 tasks for additional validators
#
# require 'nanoc3/tasks'
##
# Configurable Constants
#
BASE_URL = 'https://myrepo.github.com'
# Switch PAGES_BRANCH to master for a Pages repo (e.g. username.github.com) and make NANOC_BRANCH something else.
NANOC_BRANCH = 'gh-pages-nanoc'
PAGES_BRANCH = 'gh-pages'
# A list of files to include in the deployment that are not in the output/ directory.
ADDITIONAL_FILES = [
# 'CNAME'
]
##
# Github Pages-based Deployment
#
desc 'Deploy the website to Github Pages using Git. Set commit="some message" to set the commit message for the pages branch.'
task :deploy do
ENV['commit'] ||= 'Updated content.'
prepare!
compile!
Rake::Task["optimize:all"].invoke
commit!
deploy!
revert!
end
##
# A couple of rake tasks that'll optimize JPG, PNG, JavaScripts and Stylesheet files
#
namespace :optimize do
##
# Gem Requirement:
# YUI-Compressor - Bundled in Gemfile
#
desc 'Compress all stylesheet files'
task :stylesheets do
require 'yui/compressor'
compressor = YUI::CssCompressor.new
Dir['output/**/*.css'].each do |stylesheet|
puts "Compressing Stylesheet: #{stylesheet}"
css = File.read(stylesheet)
File.open(stylesheet, 'w') do |file|
file.write(compressor.compress(css))
end
end
end
##
# Gem Requirement:
# YUI-Compressor - Bundled in Gemfile
#
desc 'Compress all javascript files'
task :javascripts do
require 'yui/compressor'
compressor = YUI::JavaScriptCompressor.new(:munge => true)
Dir['output/**/*.js'].each do |javascript|
puts "Compressing JavaScript: #{javascript}"
js = File.read(javascript)
File.open(javascript, 'w') do |file|
file.write(compressor.compress(js))
end
end
end
##
# Package Requirement:
# jpegoptim
# Install OSX:
# brew install jpegoptim
# Install Ubuntu:
# [apt-get | aptitude] install jpegoptim
#
desc 'Optimize JPG images in output/images directory using jpegoptim'
task :jpg do
puts `find output/images -name '*.jpg' -exec jpegoptim {} \\;`
end
##
# Package Requirement:
# optipng
# Install OSX:
# brew install optipng
# Install Ubuntu:
# [apt-get | aptitude] install optipng
#
desc 'Optimize PNG images in output/images directory using optipng'
task :png do
puts `find output/images -name '*.png' -exec optipng {} \\;`
end
##
# Convenient task for performing all optimization techniques at once
#
desc 'Optimize all JPG, PNG, Stylesheet and JavaScript files in the output directory'
task :all => [:jpg, :png, :javascripts, :stylesheets]
end
##
# Use this method to change the base url in the configuration file
# so you can automate that instead of manually changing it everytime
# when you want to deploy the website
#
def change_base_url_to(url)
puts "Changing Base URL to #{url}.."
config = YAML.load_file('./config.yaml')
config['base_url'] = url
File.open('./config.yaml', 'w') do |file|
file.write(config.to_yaml)
end
end
##
# Prepares the deployment environment
#
def prepare!
unless %x[git status] =~ /nothing to commit/
puts "Please commit or stash your changes before deploying!"
exit 1
end
end
##
# Re-compile by removing the output directory and re-compiling
#
def compile!
change_base_url_to(BASE_URL)
puts "Compiling website.."
puts %x[rm -rf output]
puts %x[nanoc compile]
puts "Reverting code changes..."
puts %x[git reset --hard]
end
##
# Commits compiled output.
#
def commit!
puts "Creating and moving to \"#{PAGES_BRANCH}\" branch.."
# Create gh-pages branch if necessary
unless system("git checkout #{PAGES_BRANCH}")
%x[git checkout --orphan #{PAGES_BRANCH}]
%x[git ls-files].split("\n").each do |tracked_file|
%x[git rm -f #{tracked_file}] unless tracked_file.start_with?('output/') || ADDITIONAL_FILES.include?(tracked_file)
end
end
# Add all output and commit it
puts "Adding and committing compiled output for deployment.."
ADDITIONAL_FILES.each do |file|
puts %x[git checkout #{NANOC_BRANCH} #{file}]
puts %x[git add -f #{file}]
end
Dir['output/*'].each do |output_file|
destination = output_file.sub(/\Aoutput\//, '')
puts %x[rm -rf "#{destination}"]
puts %x[mv "#{output_file}" "#{destination}"]
puts %x[git add -f "#{destination}"]
end
puts %x[git commit -m "#{ENV['commit'].gsub('"', '\"')}"]
end
##
# Deploys the site via git
#
def deploy!
puts 'Pushing to Github..'
puts %x[git push origin HEAD:#{PAGES_BRANCH} --force]
end
##
# Moves back to the nanoc branch
#
def revert!
%x[git checkout #{NANOC_BRANCH}]
end