-
Notifications
You must be signed in to change notification settings - Fork 6
/
Rakefile
165 lines (145 loc) · 3.82 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
# encoding: utf-8
require 'yaml'
##
# Load Nanoc3 tasks for additional validators
#
# require 'nanoc3/tasks'
##
# Configurable Constants
#
BASE_URL = 'http://myapp.herokuapp.com'
##
# Heroku-based Deployment
#
# Requires a Heroku Application (a heroku remote) that runs on Celadon Cedar
#
desc 'Deploy the website to Heroku using Git.'
task :deploy do
prepare!
compile!
Rake::Task["optimize:all"].invoke
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
##
# Re-compile by removing the output directory and re-compiling
#
def compile!
puts "Compiling website.."
puts %x[rm -rf output]
puts %x[nanoc compile]
end
##
# Prepares the deployment environment
#
def prepare!
%x[git checkout master]
unless %x[git status] =~ /nothing to commit \(working directory clean\)/
puts "Please commit your changes on the master branch before deploying!"
exit 1
end
puts "Creating and moving in to \"deployment\" branch.."
puts %x[git checkout -b deployment]
puts "Removing \"output\" directory from .gitignore.."
gitignore = File.read(".gitignore")
File.open(".gitignore", "w") do |file|
file.write(gitignore.gsub("output", ""))
end
change_base_url_to(BASE_URL)
end
##
# Moves back to the "master" branch and removes the "deployment" branch
#
def revert!
%x[git checkout master]
%x[git branch -D deployment]
end
##
# Deploys the application via git to Heroku
#
def deploy!
puts "Adding and committing compiled output for deployment.."
puts %x[git add .]
puts %x[git commit -a -m "temporary commit for deployment"]
puts 'Deploying to Heroku..'
puts %x[git push heroku HEAD:master --force]
end