1 | /** Gruntfile - control build and deploy tasks |
---|
2 | */ |
---|
3 | |
---|
4 | var _ = require('underscore'); |
---|
5 | |
---|
6 | module.exports = function(grunt) { |
---|
7 | |
---|
8 | // TASKS |
---|
9 | |
---|
10 | // default task |
---|
11 | grunt.registerTask('default', ['compile']); |
---|
12 | // development |
---|
13 | grunt.registerTask('compile', |
---|
14 | "Lint all sources and compile Coffee and LESS sources.", |
---|
15 | ['path-check:svn' |
---|
16 | ,'svn-ignore:compile' |
---|
17 | ,'jshint:lint-sources' |
---|
18 | ,'htmlhint:lint' |
---|
19 | ,'coffeelint:lint' |
---|
20 | ,'jsonlint:lint' |
---|
21 | //,'tv4:lint' |
---|
22 | ,'less:compile' |
---|
23 | ,'usebanner:generated-css' |
---|
24 | ,'coffee:compile' |
---|
25 | ,'usebanner:generated-js' |
---|
26 | ,'jshint:lint-generated' |
---|
27 | //,'amd-check' // too smart about plugins, r.js can't find them |
---|
28 | ]); |
---|
29 | grunt.registerTask('run', |
---|
30 | "Compile and start the server locally with foreman.", |
---|
31 | ['path-check:foreman' |
---|
32 | ,'compile' |
---|
33 | ,'foreman:dev']); |
---|
34 | grunt.registerTask('build', |
---|
35 | "Compile and make a Dojo build of the client (compress sources).", |
---|
36 | ['compile' |
---|
37 | ,'clean:build' |
---|
38 | ,'copy:build' |
---|
39 | ,'dojo:build' |
---|
40 | ]); |
---|
41 | grunt.registerTask('run-build', |
---|
42 | "Make a build and start server from build locally with foreman.", |
---|
43 | ['path-check:foreman' |
---|
44 | ,'build' |
---|
45 | ,'foreman:build']); |
---|
46 | grunt.registerTask('deploy', |
---|
47 | "Make a build and deploy it to Heroku.", |
---|
48 | ['path-check:svn' |
---|
49 | ,'path-check:git' |
---|
50 | ,'svninfo' |
---|
51 | ,'build' |
---|
52 | ,'git_deploy:deploy']); |
---|
53 | // database management |
---|
54 | grunt.registerTask('db-backup-cloud-to-dated-local', |
---|
55 | "Backup the Heroku database to a timestamped local database.", |
---|
56 | ['path-check:heroku' |
---|
57 | ,'heroku-config' |
---|
58 | ,'http:db-backup-cloud-to-dated-local']); |
---|
59 | grunt.registerTask('db-pull-cloud-to-local', |
---|
60 | "Synchronize local database with the Heroku database (pull changes).", |
---|
61 | ['path-check:heroku' |
---|
62 | ,'heroku-config' |
---|
63 | ,'http:db-pull-cloud-to-local']); |
---|
64 | grunt.registerTask('db-push-local-to-cloud', |
---|
65 | "Synchronize the Heroku database with the local database (push changes).", |
---|
66 | ['path-check:heroku' |
---|
67 | ,'heroku-config' |
---|
68 | ,'http:db-push-local-to-cloud']); |
---|
69 | grunt.registerTask('#', |
---|
70 | "---\nTASKS BELOW ARE INTERNAL AND SHOULD NOT USUALLY BE CALLED FROM THE COMMAND-LINE\n---", |
---|
71 | []); |
---|
72 | |
---|
73 | // FILES AND FOLDERS |
---|
74 | |
---|
75 | var srcDir = 'src/'; |
---|
76 | var buildDir = 'build/'; |
---|
77 | var coffeeMap = grunt.file.expandMapping( |
---|
78 | [ 'client/qed-client/**/*.coffee', 'server/**/*.coffee' ], |
---|
79 | srcDir, |
---|
80 | { cwd: srcDir, ext: '.js' }); |
---|
81 | var lessMap = grunt.file.expandMapping( |
---|
82 | [ 'client/qed-client/css/qed.less' ], |
---|
83 | srcDir, |
---|
84 | { cwd: srcDir, ext: '.css' }); |
---|
85 | |
---|
86 | var mode = process.env.QED_ENV || 'dev'; |
---|
87 | var dbNames = { |
---|
88 | dev: 'qed-dev', |
---|
89 | production: 'qed' |
---|
90 | }; |
---|
91 | if ( !( mode in dbNames ) ) { |
---|
92 | throw new Error("Unknown mode "+mode+" specified."); |
---|
93 | } |
---|
94 | |
---|
95 | // TASK CONFIG |
---|
96 | |
---|
97 | grunt.initConfig({ |
---|
98 | localDbURL: "http://localhost:5984", |
---|
99 | dbName: dbNames[mode], |
---|
100 | timestamp: "<%= grunt.template.today(\"UTC:yyyymmdd't'HHMMss'z'\") %>", |
---|
101 | |
---|
102 | 'amd-check': { |
---|
103 | files: [ srcDir+'client/qed-client/**/*.js' ] |
---|
104 | }, |
---|
105 | clean: { |
---|
106 | build: { src: [buildDir] } |
---|
107 | }, |
---|
108 | coffee: { |
---|
109 | options: { bare: true }, |
---|
110 | compile: { files: coffeeMap } |
---|
111 | }, |
---|
112 | coffeelint: { |
---|
113 | lint: { options: require('./'+srcDir+'.coffeelint.json'), |
---|
114 | files: coffeeMap } |
---|
115 | }, |
---|
116 | copy: { |
---|
117 | build: { files: [{ expand: true, |
---|
118 | cwd: srcDir, |
---|
119 | src: ['**', '!client/*/**' ], |
---|
120 | dest: buildDir }]} |
---|
121 | }, |
---|
122 | dojo: { |
---|
123 | build: { options: { dojo: srcDir+'client/dojo/dojo.js', |
---|
124 | profile: srcDir+'client/client.profile.js', |
---|
125 | releaseDir: '../../'+buildDir }} |
---|
126 | }, |
---|
127 | foreman: { |
---|
128 | dev: { |
---|
129 | options: { |
---|
130 | cwd: srcDir |
---|
131 | } |
---|
132 | }, |
---|
133 | build: { |
---|
134 | options: { |
---|
135 | cwd: buildDir |
---|
136 | } |
---|
137 | } |
---|
138 | }, |
---|
139 | git_deploy: { |
---|
140 | deploy: { |
---|
141 | options: { |
---|
142 | url: 'git@heroku.com:quod-erat.git', |
---|
143 | branch: 'master', |
---|
144 | message: "Deployment of revision <%= svninfo.rev %> on <%= grunt.template.today() %>." |
---|
145 | }, |
---|
146 | src: buildDir |
---|
147 | } |
---|
148 | }, |
---|
149 | 'heroku-config': { |
---|
150 | options: { |
---|
151 | app: 'quod-erat', |
---|
152 | keys: ['CLOUDANT_URL'] |
---|
153 | } |
---|
154 | }, |
---|
155 | htmlhint: { |
---|
156 | options: { htmlhintrc: srcDir+".htmlhintrc" }, |
---|
157 | lint: { files: [{ expand: true, |
---|
158 | cwd: srcDir, |
---|
159 | src: ['client/*.html', 'client/qed-client/**/*.html'] }]} |
---|
160 | }, |
---|
161 | http: { |
---|
162 | 'db-backup-cloud-to-dated-local': { |
---|
163 | options: { |
---|
164 | url: "<%= localDbURL %>/_replicate", |
---|
165 | method: 'POST', |
---|
166 | body: { |
---|
167 | source: "<%= herokuConfig.CLOUDANT_URL %>/<%= dbName %>", |
---|
168 | target: "<%= dbName %>-<%= timestamp %>", |
---|
169 | create_target: true |
---|
170 | }, |
---|
171 | json: true |
---|
172 | } |
---|
173 | }, |
---|
174 | 'db-pull-cloud-to-local': { |
---|
175 | options: { |
---|
176 | url: "<%= localDbURL %>/_replicate", |
---|
177 | method: 'POST', |
---|
178 | body: { |
---|
179 | source: "<%= herokuConfig.CLOUDANT_URL %>/<%= dbName %>", |
---|
180 | target: "<%= dbName %>" |
---|
181 | }, |
---|
182 | json: true |
---|
183 | } |
---|
184 | }, |
---|
185 | 'db-push-local-to-cloud': { |
---|
186 | options: { |
---|
187 | url: "<%= localDbURL %>/_replicate", |
---|
188 | method: 'POST', |
---|
189 | body: { |
---|
190 | source: "<%= dbName %>", |
---|
191 | target: "<%= herokuConfig.CLOUDANT_URL %>/<%= dbName %>" |
---|
192 | }, |
---|
193 | json: true |
---|
194 | } |
---|
195 | } |
---|
196 | }, |
---|
197 | jshint: { |
---|
198 | 'lint-sources': { options: { jshintrc: srcDir+".jshintrc" }, |
---|
199 | files: { src: [ srcDir+'client/qed-client/**/*.js', srcDir+'server/**/*.js' ].concat(exclude(dest(coffeeMap))) } }, |
---|
200 | 'lint-generated': { options: { jshintrc: srcDir+".jshintrc-generated" }, |
---|
201 | files: { src: dest(coffeeMap) } } |
---|
202 | }, |
---|
203 | jsonlint: { |
---|
204 | 'lint': { files: { src: [ srcDir+'client/qed-client/**/*.json', srcDir+'server/**/*.json' ] } } |
---|
205 | }, |
---|
206 | less: { |
---|
207 | options: { strictImports: false, |
---|
208 | dumpLineNumbers: "all" }, |
---|
209 | compile: { files: lessMap } |
---|
210 | }, |
---|
211 | 'path-check': { |
---|
212 | 'heroku': { |
---|
213 | src: ['heroku'] |
---|
214 | }, |
---|
215 | 'foreman': { |
---|
216 | src: ['foreman'] |
---|
217 | }, |
---|
218 | 'svn': { |
---|
219 | src: ['svn'] |
---|
220 | }, |
---|
221 | 'git': { |
---|
222 | src: ['git'] |
---|
223 | } |
---|
224 | }, |
---|
225 | requirejs: { |
---|
226 | basePath: srcDir+'client/', |
---|
227 | packages: [ |
---|
228 | { name: "dojo", location: "dojo" }, |
---|
229 | { name: "dijit", location: "dijit" }, |
---|
230 | { name: "dojox", location: "dojox" }, |
---|
231 | { name: "qed-client", location: "qed-client" } |
---|
232 | ] |
---|
233 | }, |
---|
234 | 'svn-ignore': { |
---|
235 | compile: { |
---|
236 | files: { src: dest(coffeeMap).concat(dest(lessMap)) } |
---|
237 | } |
---|
238 | }, |
---|
239 | 'svn-ignore-clean': { |
---|
240 | clean: { |
---|
241 | files: [{ |
---|
242 | expand: true, |
---|
243 | cwd: srcDir, |
---|
244 | src: ['**', '!node_modules/**', '!client/dojo/**', '!client/dijit/**', '!client/dojox/**', '!client/util/**'], |
---|
245 | filter: 'isDirectory' |
---|
246 | }] |
---|
247 | } |
---|
248 | }, |
---|
249 | tv4: { |
---|
250 | lint: { |
---|
251 | options: { |
---|
252 | root: grunt.file.readJSON('json-schema-draft-04.json'), |
---|
253 | multi: true |
---|
254 | }, |
---|
255 | src: [srcDir+'server/config/couchdb-schema.json'] |
---|
256 | } |
---|
257 | }, |
---|
258 | usebanner: { |
---|
259 | 'generated-css': { options: { position: 'top', |
---|
260 | banner: '/* This CSS file is generated. All edits will be lost on recompile. */'}, |
---|
261 | files: { src: dest(lessMap) }}, |
---|
262 | 'generated-js': { options: { position: 'top', |
---|
263 | banner: '/* This JS file is generated, All edits will be lost on recompile. */'}, |
---|
264 | files: { src: dest(coffeeMap) }} |
---|
265 | } |
---|
266 | }); |
---|
267 | |
---|
268 | // LOAD TASKS |
---|
269 | |
---|
270 | grunt.loadNpmTasks('grunt-amd-check'); |
---|
271 | grunt.loadNpmTasks('grunt-banner'); |
---|
272 | grunt.loadNpmTasks('grunt-coffeelint'); |
---|
273 | grunt.loadNpmTasks('grunt-contrib-clean'); |
---|
274 | grunt.loadNpmTasks('grunt-contrib-coffee'); |
---|
275 | grunt.loadNpmTasks('grunt-contrib-copy'); |
---|
276 | grunt.loadNpmTasks('grunt-contrib-jshint'); |
---|
277 | grunt.loadNpmTasks('grunt-contrib-less'); |
---|
278 | grunt.loadNpmTasks('grunt-dojo'); |
---|
279 | grunt.loadNpmTasks('grunt-git-deploy'); |
---|
280 | grunt.loadNpmTasks('grunt-htmlhint'); |
---|
281 | grunt.loadNpmTasks('grunt-http'); |
---|
282 | grunt.loadNpmTasks('grunt-jsonlint'); |
---|
283 | grunt.loadNpmTasks('grunt-path-check'); |
---|
284 | grunt.loadNpmTasks('grunt-svninfo'); |
---|
285 | grunt.loadNpmTasks('grunt-tv4'); |
---|
286 | grunt.loadTasks('./grunt-tasks'); |
---|
287 | |
---|
288 | // UTIL FUNCTIONS |
---|
289 | |
---|
290 | function dest(files) { |
---|
291 | return _.chain(files) |
---|
292 | .map(function(item){ return item.dest; }) |
---|
293 | .flatten() |
---|
294 | .value(); |
---|
295 | } |
---|
296 | |
---|
297 | function exclude(dest) { |
---|
298 | return _.map(dest, function(dest){ return '!'+dest; }); |
---|
299 | } |
---|
300 | |
---|
301 | }; |
---|