-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathrakefile
253 lines (212 loc) · 6.55 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
require 'aws-sdk'
require 'cgi'
def s3
$s3 ||= AWS::S3.new(access_key_id: ENV["S3_ACCESS_KEY_ID"] , secret_access_key: ENV["S3_ACCESS_KEY"], region: 'eu-west-1')
end
def bucket
$bucket ||= s3.buckets['prod-sky-web-toolkit']
end
def latest_version_number
$latest_version_number ||= File.open('app/_config.yml').read.match(/version:(.+)/)[1].strip
end
def version_already_exists?
bucket.objects.any? { |obj| obj.key == latest_version_number }
end
def is_release_version?
latest_version_number.match /^[0-9.]*$/
end
def version_is_release_candidate?
latest_version_number.include? 'rc-'
end
def is_feature_branch?
branchName.include? 'feature'
end
def is_feature_version?
latest_version_number.include? 'feature-'
end
def branchName
`git branch | sed -n '/\* /s///p'`
end
def static_to_upload
puts "Working out static files to upload. Working directory: #{Dir.getwd}"
Dir.chdir("_site") do
Dir.glob("static/**/*.*")
end
end
def fonts_to_upload
puts "Working out font files to upload. Working directory: #{Dir.getwd}"
Dir.chdir("_site/dist") do
Dir.glob("fonts/*.*")
end
end
def assets_to_upload
puts "Working out dist files to upload. Working directory: #{Dir.getwd}"
["stylesheets/toolkit.css", "scripts/toolkit.js", "scripts/toolkit.min.js",
"images/icon.png"]
end
def templates_to_upload
puts "Working out _site files to upload. Working directory: #{Dir.getwd}"
Dir.glob("_site/**/*.*")
end
def content_type(file)
case File.extname(file)
when '.svg'
content_type = 'image/svg+xml'
when '.eot'
content_type = 'application/vnd.ms-fontobject'
when '.ttf'
content_type = 'font/ttf'
when '.woff'
content_type = 'application/x-font-woff'
when '.map'
content_type = 'text/javascript'
when '.js'
content_type = 'text/javascript'
when '.css'
content_type = 'text/css'
when '.png'
content_type = 'image/png'
when '.html'
content_type = 'text/html'
else
content_type = 'image/jpeg'
end
content_type
end
def configGit
`git config --global user.email "[email protected]"`
`git config --global user.name "Rake file"`
end
def emptyGitCache
system "git rm --cached app/fonts/min -r"
system "git rm --cached _site -r"
system "git rm --cached dist -r"
end
def tagBuild
version = "#{latest_version_number}"
puts "*** Tagging Version #{version} ***"
configGit
system "git tag -a v#{version} -m \"Rake deploy: auto tag on #{getDate}\""
system "git push origin master v#{version}"
end
def pushToAmazonS3
puts "*** Pushing to the AmazonS3 ***"
system "grunt"
version = "#{latest_version_number}"
doc_resources = []
asset_files = assets_to_upload
puts 'Uploading dist files'
puts "Working directory: #{Dir.getwd}"
Dir.chdir("_site/dist") do
puts "Working directory: #{Dir.getwd}"
asset_files.each do |file|
puts "Uploading dist file: #{file}"
doc_resources << bucket.objects["#{version}/#{file}"].write(File.open(file).read, cache_control: 'public, max-age=2592000', content_type: content_type(file), acl: :public_read )
end
end
site_files = templates_to_upload
puts 'Uploading _site files'
puts "Working directory: #{Dir.getwd}"
site_files.each do |file|
puts "Uploading _site file: #{file}"
doc_resources << bucket.objects["#{version}/#{file}"].write(File.open(file).read, cache_control: 'public, max-age=2592000', content_type: content_type(file), acl: :public_read )
end
static_files = static_to_upload
puts 'Uploading static files'
puts "Working directory: #{Dir.getwd}"
Dir.chdir("_site") do
puts "Working directory: #{Dir.getwd}"
static_files.each do |file|
puts "Uploading static file: #{file}"
doc_resources << bucket.objects["#{file}"].write(File.open(file).read, cache_control: 'public, max-age=2592000', content_type: content_type(file), acl: :public_read )
end
end
font_files = fonts_to_upload
puts 'Uploading font files'
puts "Working directory: #{Dir.getwd}"
Dir.chdir("_site/dist") do
puts "Working directory: #{Dir.getwd}"
font_files.each do |file|
puts "Uploading font file: #{file}"
doc_resources << bucket.objects["#{version}/#{file}"].write(File.open(file).read, cache_control: 'public, max-age=2592000', content_type: content_type(file), acl: :public_read )
end
end
puts "Uploaded. See http://web-toolkit.global.sky.com/#{version}/_site/index.html"
end
def updateGHPages
puts "*** Updating GH Pages and Pushing to github.io ***"
`cp -r _site /tmp`
configGit
`git clean -fdx`
`git stash`
origBranchName = branchName
`git checkout gh-pages`
`git pull origin gh-pages`
`rm -rf ./*`
`mv /tmp/_site/* .`
`rmdir /tmp/_site`
`touch .nojekyll`
`git add --all`
`git commit -m "#{latest_version_number} version"`
`git push origin gh-pages`
puts "Committed and pushed to GH Pages"
`git checkout #{origBranchName}`
`git stash pop`
puts "Uploaded. See http://skyglobal.github.io/web-toolkit"
end
def updateBranch branch
puts "*** push branch: #{branch} ***"
system "git push origin HEAD:" + branch
end
def getDate
`date +'%d-%m-%Y'`
end
desc 'Deploys a new version to the CDN and skyglobal.github.io/web-toolkit'
task :deploy do
if is_feature_version?
puts "*** version is a feature '#{latest_version_number}'. Please commit in a 'feature-xxx' branch. ***"
fail
end
if version_is_release_candidate?
puts "*** version is a Release Candidate '#{latest_version_number}'. Please commit in a 'rc-xxx' branch. ***"
fail
end
if !is_release_version?
puts "*** version is not a release version '#{latest_version_number}' ***"
fail
end
if version_already_exists?
puts "Version #{latest_version_number} exists so exiting."
# tip: this "next" command can be commented out to force a push to s3 and gh-pages
next
end
pushToAmazonS3
updateGHPages
tagBuild
end
desc 'Deploys feature to the CDN'
task :deploy_feature do
if !is_feature_version?
puts "*** version is not a feature '#{latest_version_number}' ***"
fail
end
updateBranch branchName
if version_already_exists?
puts "Version #{latest_version_number} exists so exiting."
next
end
pushToAmazonS3
end
desc 'Deploys release candidate to the CDN'
task :deploy_rc do
if !version_is_release_candidate?
puts "*** version is not a Release Candidate '#{latest_version_number}' ***"
fail
end
updateBranch branchName
if version_already_exists?
puts "Version #{latest_version_number} exists so exiting."
next
end
pushToAmazonS3
end