source: Dev/trunk/Gruntfile.js @ 520

Last change on this file since 520 was 520, checked in by hendrikvanantwerpen, 11 years ago

Updated README and made syncs more resilient.

File size: 10.6 KB
Line 
1/** Gruntfile - control build and deploy tasks
2 */
3
4var _ = require('underscore');
5
6module.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                        create_target: true
182                    },
183                    json: true
184                }
185            },
186            'db-push-local-to-cloud': {
187                options: {
188                    url: "<%= localDbURL %>/_replicate",
189                    method: 'POST',
190                    body: {
191                        source: "<%= dbName %>",
192                        target: "<%= herokuConfig.CLOUDANT_URL %>/<%= dbName %>",
193                        create_target: true
194                    },
195                    json: true
196                }
197            }
198        },
199        jshint: {
200            'lint-sources': { options: { jshintrc: srcDir+".jshintrc" },
201                              files: { src: [ srcDir+'client/qed-client/**/*.js', srcDir+'server/**/*.js' ].concat(exclude(dest(coffeeMap))) } },
202            'lint-generated': { options: { jshintrc: srcDir+".jshintrc-generated" },
203                                files: { src: dest(coffeeMap) } }
204        },
205        jsonlint: {
206            'lint': { files: { src: [ srcDir+'client/qed-client/**/*.json', srcDir+'server/**/*.json' ] } }
207        },
208        less: {
209            options: { strictImports: false,
210                       dumpLineNumbers: "all" },
211            compile: { files: lessMap }
212        },
213        'path-check': {
214            'heroku': {
215                src: ['heroku']
216            },
217            'foreman': {
218                src: ['foreman']
219            },
220            'svn': {
221                src: ['svn']
222            },
223            'git': {
224                src: ['git']
225            }
226        },
227        requirejs: {
228            basePath: srcDir+'client/',
229            packages: [
230                { name: "dojo", location: "dojo" },
231                { name: "dijit", location: "dijit" },
232                { name: "dojox", location: "dojox" },
233                { name: "qed-client", location: "qed-client" }
234            ]
235        },
236        'svn-ignore': {
237            compile: {
238                files: { src: dest(coffeeMap).concat(dest(lessMap)) }
239            }
240        },
241        'svn-ignore-clean': {
242            clean: {
243                files: [{
244                    expand: true,
245                    cwd: srcDir,
246                    src: ['**', '!node_modules/**', '!client/dojo/**', '!client/dijit/**', '!client/dojox/**', '!client/util/**'],
247                    filter: 'isDirectory'
248                }]
249            }
250        },
251        tv4: {
252            lint: {
253                options: {
254                    root: grunt.file.readJSON('json-schema-draft-04.json'),
255                    multi: true
256                },
257                src: [srcDir+'server/config/couchdb-schema.json']
258            }
259        },
260        usebanner: {
261            'generated-css': { options: { position: 'top',
262                                          banner: '/* This CSS file is generated. All edits will be lost on recompile. */'},
263                               files: { src: dest(lessMap) }},
264            'generated-js': { options: { position: 'top',
265                                         banner: '/* This JS file is generated, All edits will be lost on recompile. */'},
266                               files: { src: dest(coffeeMap) }}
267        }
268    });
269
270    // LOAD TASKS
271
272    grunt.loadNpmTasks('grunt-amd-check');
273    grunt.loadNpmTasks('grunt-banner');
274    grunt.loadNpmTasks('grunt-coffeelint');
275    grunt.loadNpmTasks('grunt-contrib-clean');
276    grunt.loadNpmTasks('grunt-contrib-coffee');
277    grunt.loadNpmTasks('grunt-contrib-copy');
278    grunt.loadNpmTasks('grunt-contrib-jshint');
279    grunt.loadNpmTasks('grunt-contrib-less');
280    grunt.loadNpmTasks('grunt-dojo');
281    grunt.loadNpmTasks('grunt-git-deploy');
282    grunt.loadNpmTasks('grunt-htmlhint');
283    grunt.loadNpmTasks('grunt-http');
284    grunt.loadNpmTasks('grunt-jsonlint');
285    grunt.loadNpmTasks('grunt-path-check');
286    grunt.loadNpmTasks('grunt-svninfo');
287    grunt.loadNpmTasks('grunt-tv4');
288    grunt.loadTasks('./grunt-tasks');
289
290    // UTIL FUNCTIONS
291
292    function dest(files) {
293        return _.chain(files)
294            .map(function(item){ return item.dest; })
295            .flatten()
296            .value();
297    }
298
299    function exclude(dest) {
300        return _.map(dest, function(dest){ return '!'+dest; });
301    }
302   
303};
Note: See TracBrowser for help on using the repository browser.