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