forked from dbtek/bootswatch-dist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
266 lines (252 loc) · 9.91 KB
/
Gruntfile.js
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
'use strict';
module.exports = function (grunt) {
grunt.initConfig({
// Metadata
pkg: grunt.file.readJSON('package.json'),
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
'* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
' Licensed <%= props.license %> */\n',
// Task configuration
gruntfile: {
src: 'gruntfile.js'
},
// shared config between update tasks
update: {
destination: 'update/',
version: ''
},
curl: {
// Bootstrap files
'update/bootstrap/js/bootstrap.js': 'http://netdna.bootstrapcdn.com/bootstrap/latest/js/bootstrap.js',
'update/bootstrap/js/bootstrap.min.js': 'http://netdna.bootstrapcdn.com/bootstrap/latest/js/bootstrap.min.js',
'update/bootstrap/fonts/glyphicons-halflings-regular.eot': 'http://netdna.bootstrapcdn.com/bootstrap/latest/fonts/glyphicons-halflings-regular.eot',
'update/bootstrap/fonts/glyphicons-halflings-regular.woff': 'http://netdna.bootstrapcdn.com/bootstrap/latest/fonts/glyphicons-halflings-regular.woff',
'update/bootstrap/fonts/glyphicons-halflings-regular.ttf': 'http://netdna.bootstrapcdn.com/bootstrap/latest/fonts/glyphicons-halflings-regular.ttf',
'update/bootstrap/fonts/glyphicons-halflings-regular.svg': 'http://netdna.bootstrapcdn.com/bootstrap/latest/fonts/glyphicons-halflings-regular.svg'
},
http: {
bootswatch: {
options: {
url: 'http://api.bootswatch.com/3/',
callback: function(error, response, data) {
data = JSON.parse(data);
if(error)
grunt.log.error('Something went wrong. Can\'t reach Bootswatch API');
else {
var latestVersion = data.version;
grunt.config.set('update.version', latestVersion);
grunt.config.set('update.themes', data.themes);
grunt.log.writeln('We have ',grunt.config.get('pkg.bootswatch.version') + ', latest version: ' + latestVersion);
if(grunt.config.get('pkg.bootswatch.version') < latestVersion) {
grunt.log.writeln('New version is available. Updating to: ' + latestVersion);
// fetch themes
data.themes.forEach(function(theme) {
// create dynamic http tasks and run them
grunt.config.set('http.fetch' + theme.name + '.options.url', theme.css);
grunt.config.set('http.fetch' + theme.name + '.dest', 'update/'+ theme.name.toLowerCase() + '/bootstrap.css');
grunt.task.run('http:fetch' + theme.name);
grunt.config.set('http.fetch' + theme.name + 'Min.options.url', theme.cssMin);
grunt.config.set('http.fetch' + theme.name + 'Min.dest', 'update/'+ theme.name.toLowerCase() + '/bootstrap.min.css');
grunt.task.run('http:fetch' + theme.name + 'Min');
});
grunt.task.run('fetchBootstrapFiles');
grunt.task.run('release');
grunt.task.run('updatePackageJson');
}
else {
grunt.task.run('checkThemes');
grunt.task.run('releaseNewThemes');
grunt.task.run('updatePackageJson');
}
}
}
}
}
},
clean: {
dist: {
src: ['dist']
}
},
shell: {
setUser: {
options: {
stdout: false
},
command: 'git config --global user.name "Travis Build" && git config --global user.email "$GH_TOKEN"'
},
cloneProject: {
options: {
stdout: false
},
command: 'git clone https://$GH_TOKEN@' + grunt.file.readJSON('package.json').repository.url.split('://')[1] + ' dist'
},
createBranch: {
options: {
execOptions: {
cwd: 'dist'
}
},
command: function (theme) {
return 'git checkout --orphan ' + theme + ' && rm -rf * .gitignore .travis.yml';
}
},
switchBranch: {
options: {
failOnError: true,
callback: function(err, stdout, stderr, cb) {
if(err) {
grunt.task.run('shell:createBranch:' + this.args[0]);
}
cb();
},
execOptions: {
cwd: 'dist'
}
},
command: function (theme) {
return 'git checkout ' + theme;
}
},
copyUpdate: {
options: {
},
command: function (theme) {
return 'mkdir -p dist/css && ' +
'cp -f update/' + theme + '/*.css dist/css/ && ' +
'cp -Rf update/bootstrap/* dist';
}
},
commitChanges: {
options: {
failOnError: false,
execOptions: {
cwd: 'dist'
}
},
command: 'git add . --all && ' +
'git commit -m "Auto update - Build #$TRAVIS_BUILD_NUMBER [ci skip]"'
},
tagVersion: {
options: {
failOnError: false,
execOptions: {
cwd: 'dist'
}
},
command: function(theme, version) {
return 'git tag ' + version + '-' + theme + ' -m " ' + version + ' update" -f'
}
},
pushChanges: {
options: {
stdout: false,
stderr: false,
execOptions: {
cwd: 'dist'
}
},
command: function(theme) {
return 'git push origin ' + theme + ' --tags -f';
}
},
commitPackageJson: {
options: {
failOnError: false,
stdout: false,
stderr: false
},
command: 'git add package.json && ' +
'git commit -m "Auto update - Build #$TRAVIS_BUILD_NUMBER [ci skip]" && ' +
'git push origin master'
}
}
});
grunt.loadNpmTasks('grunt-curl');
grunt.loadNpmTasks('grunt-http');
grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.registerTask('updatePackageJson', 'Updates version of package.json file ', function() {
var content = grunt.file.readJSON('package.json');
content.bootswatch.version = grunt.config.get('update.version');
grunt.file.write('package.json', JSON.stringify(content, undefined, 2));
grunt.task.run('shell:commitPackageJson');
});
grunt.registerTask('updateBowerJson', 'Updates version of bower.json for themes ', function(theme) {
// read source json
var content = grunt.file.readJSON('bower.json');
content.version = grunt.config.get('update.version') + '-' + theme;
grunt.file.write('dist/bower.json', JSON.stringify(content, undefined, 2));
});
grunt.registerTask('fetchBootstrapFiles', [
'curl'
]);
grunt.registerTask('release', '', function() {
grunt.task.run('shell:setUser');
grunt.task.run('clean:dist');
grunt.task.run('shell:cloneProject');
grunt.config.get('update.themes').forEach(function(theme) {
grunt.task.run('shell:switchBranch:'+theme.name.toLowerCase());
grunt.task.run('shell:copyUpdate:'+theme.name.toLowerCase());
grunt.task.run('updateBowerJson:'+theme.name.toLowerCase());
grunt.task.run('shell:commitChanges');
grunt.task.run('shell:tagVersion:' + theme.name.toLowerCase() + ':' + grunt.config.get('update.version'));
grunt.task.run('shell:pushChanges:' + theme.name.toLowerCase());
});
});
grunt.registerTask('releaseNewThemes', function() {
grunt.log.writeln;('themes to release',grunt.config.get('update.newThemes').length);
if(grunt.config.get('update.newThemes').length > 0) {
grunt.config.set('update.themes', grunt.config.get('update.newThemes'));
grunt.task.run('fetchBootstrapFiles');
grunt.task.run('release');
}
else
grunt.log.write('All themes up to date. Version: ' + grunt.config.get('pkg.bootswatch.version'));
})
grunt.registerTask('checkThemes', '', function() {
grunt.config.set('update.newThemes', []);
grunt.config.get('update.themes').forEach(function(theme) {
var url = grunt.config.get('pkg.repository.rawUrl') + '/' + theme.name.toLowerCase() + '/bower.json';
grunt.config.set('http.check' + theme.name + '.options.url', url);
grunt.config.set('http.check' + theme.name + '.options.ignoreErrors', true);
grunt.config.set('http.check' + theme.name + '.options.callback', function(error, response, data) {
var release = false;
data = JSON.parse(data);
if(response.statusCode == '404') {
release = true;
grunt.log.writeln('New theme found: ' + theme.name);
}
else if(data.version < grunt.config.get('update.version')) {
release = true;
grunt.log.writeln('Theme out of date: ' + theme.name);
}
else {
grunt.log.writeln(theme.name + ' up to date. (' + data.version + ')');
}
if(release) {
// new theme release
grunt.config.set('http.fetch' + theme.name + '.options.url', theme.css);
grunt.config.set('http.fetch' + theme.name + '.dest', 'update/' + theme.name.toLowerCase() + '/bootstrap.css');
grunt.task.run('http:fetch' + theme.name);
grunt.config.set('http.fetch' + theme.name + 'Min.options.url', theme.cssMin);
grunt.config.set('http.fetch' + theme.name + 'Min.dest', 'update/' + theme.name.toLowerCase() + '/bootstrap.min.css');
grunt.task.run('http:fetch' + theme.name + 'Min');
var newThemes = grunt.config.get('update.newThemes');
newThemes.push(theme);
grunt.config.set('update.newThemes', newThemes);
}
});
grunt.task.run('http:check' + theme.name);
});
});
grunt.registerTask('checkUpdate', [
'http:bootswatch'
]);
// Default task
grunt.registerTask('default', [
'checkUpdate'
]);
};