gruntfile.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. var matchdep = require('matchdep'),
  9. tmp = require('temporary'),
  10. path = require('path');
  11. module.exports = function(grunt) {
  12. 'use strict';
  13. matchdep.filterDev('grunt-*').forEach(grunt.loadNpmTasks);
  14. var tmpDir = new tmp.Dir();
  15. grunt.initConfig({
  16. /* project configuration */
  17. pkg: grunt.file.readJSON('package.json'),
  18. prj: {
  19. src: 'src',
  20. build: 'build',
  21. release: 'release',
  22. desktopApp: 'desktop_app',
  23. dist: 'dist',
  24. mockserver: 'server',
  25. test: 'test',
  26. fixtures: 'fixtures',
  27. vendor: 'bower_components',
  28. jsPattern: '**/*.js',
  29. files: {
  30. src: ['<%= prj.js.src %>/<%= prj.jsPattern %>'],
  31. srcTest: ['<%= prj.test %>/<%= prj.jsPattern %>']
  32. },
  33. dependencies: [
  34. '<%= prj.vendor %>/lodash/lodash.min.js',
  35. '<%= prj.vendor %>/angular/angular.min.js',
  36. '<%= prj.vendor %>/angular-bootstrap/ui-bootstrap-tpls.min.js',
  37. '<%= prj.vendor %>/angular-ui-router/release/angular-ui-router.min.js',
  38. '<%= prj.vendor %>/angular-animate/angular-animate.min.js',
  39. '<%= prj.vendor %>/angular-aria/angular-aria.min.js',
  40. '<%= prj.vendor %>/angular-gettext/dist/angular-gettext.min.js',
  41. '<%= prj.vendor %>/angular-material/angular-material.min.js',
  42. '<%= prj.vendor %>/angular-promise-extras/angular-promise-extras.js',
  43. '<%= prj.vendor %>/ng-file-upload/ng-file-upload.min.js',
  44. '<%= prj.vendor %>/ng-file-upload/ng-file-upload-shim.min.js">'
  45. ],
  46. cssDependencies: [
  47. '<%= prj.vendor %>/angular-material/angular-material.css'
  48. ],
  49. devDependencies: [
  50. '<%= prj.vendor %>/angular-mocks/angular-mocks.js'
  51. ],
  52. css: {
  53. src: '<%= prj.src %>/css',
  54. build: '<%= prj.build %>/css',
  55. release: '<%= prj.release %>/css'
  56. },
  57. sass: {
  58. src: '<%= prj.src %>/scss',
  59. build: '<%= prj.css.build %>'
  60. },
  61. js: {
  62. src: '<%= prj.src %>',
  63. build: '<%= prj.build %>/scripts',
  64. release: '<%= prj.release %>'
  65. },
  66. assets: {
  67. src: '<%= prj.src %>/assets',
  68. build: '<%= prj.build %>/assets',
  69. release: '<%= prj.release %>/assets'
  70. }
  71. },
  72. /* build tasks */
  73. clean: {
  74. build: ['<%= prj.build %>'],
  75. release: ['<%= prj.release %>'],
  76. dist: ['<%= prj.dist %>'],
  77. desktopApp: ['<%= prj.desktopApp %>']
  78. },
  79. html2js: {
  80. partials: {
  81. options: {
  82. module: 'partials'
  83. },
  84. src: ['<%= prj.src %>/**/*.html'],
  85. dest: '<%= prj.js.build %>/partials.js'
  86. }
  87. },
  88. sass: {
  89. options: {
  90. sourceMap: true
  91. },
  92. dist: {
  93. files: {
  94. '<%= prj.sass.build %>/style.css' : '<%= prj.sass.src %>/style.scss'
  95. }
  96. },
  97. scss: {
  98. src: ['<%= prj.sass.src %>/style.scss'],
  99. dest: '<%= prj.sass.build %>/style.css'
  100. }
  101. },
  102. /* release tasks */
  103. cssmin: {
  104. styles: {
  105. src: [
  106. '<%= sass.scss.dest %>',
  107. '<%= prj.css.build %>/**/*.css'
  108. ],
  109. dest: '<%= prj.css.release %>/<%= pkg.name %>-<%= pkg.version %>.css'
  110. }
  111. },
  112. concat: {
  113. srcScripts: {
  114. src: ['<%= prj.js.build %>/**/*.js'],
  115. dest: '<%= prj.js.release %>/<%= pkg.name %>-<%= pkg.version %>.js'
  116. },
  117. scripts: {
  118. src: [
  119. '<%= prj.dependencies %>',
  120. '<%= concat.srcScripts.dest %>'
  121. ],
  122. dest: '<%= concat.srcScripts.dest %>'
  123. },
  124. styles: {
  125. src: [
  126. '<%= prj.cssDependencies %>',
  127. '<%= cssmin.styles.dest %>'
  128. ],
  129. dest: '<%= cssmin.styles.dest %>'
  130. }
  131. },
  132. ngAnnotate: {
  133. options: {
  134. singleQuotes: true
  135. },
  136. sources: {
  137. src: ['<%= concat.srcScripts.dest %>'],
  138. dest: '<%= concat.srcScripts.dest %>'
  139. }
  140. },
  141. uglify: {
  142. options: {
  143. mangle: true
  144. },
  145. sources: {
  146. src: ['<%= concat.srcScripts.dest %>'],
  147. dest: '<%= concat.srcScripts.dest %>'
  148. }
  149. },
  150. compress: {
  151. release: {
  152. options: {
  153. archive: '<%= prj.dist %>/<%= pkg.name %>-<%= pkg.version %>.zip'
  154. },
  155. expand: true,
  156. cwd: '<%= prj.desktopApp %>/',
  157. src: ['**']
  158. }
  159. },
  160. /**
  161. * Task for gettext and extracting all data into modules
  162. */
  163. nggettext_extract: {
  164. pot: {
  165. files: {
  166. 'po/template.pot': [
  167. '<%= prj.src %>/**/*.html',
  168. '<%= prj.files.src %>'
  169. ]
  170. }
  171. }
  172. },
  173. nggettext_compile: {
  174. all: {
  175. options: {
  176. format: "json"
  177. },
  178. files: [{
  179. expand: true,
  180. dot: true,
  181. cwd: "po",
  182. dest: "<%= prj.desktopApp %>/langs",
  183. src: ["*.po"],
  184. ext: ".json"
  185. }]
  186. }
  187. },
  188. /* build/release tasks */
  189. index: {
  190. build: {
  191. dir: '<%= prj.build %>',
  192. src: [
  193. '<%= prj.cssDependencies %>',
  194. '<%= cssmin.styles.src %>',
  195. '<%= prj.dependencies %>',
  196. '<%= concat.srcScripts.src %>'
  197. ]
  198. },
  199. release: {
  200. dir: '<%= prj.release %>',
  201. src: [
  202. '<%= concat.scripts.dest %>',
  203. '<%= concat.styles.dest %>'
  204. ]
  205. }
  206. },
  207. copy: {
  208. build_css: {
  209. expand: true,
  210. cwd: '<%= prj.css.src %>',
  211. src: ['**/*.css'],
  212. dest: '<%= prj.css.build %>/'
  213. },
  214. build_cssDependencies: {
  215. expand: true,
  216. src: ['<%= prj.cssDependencies %>'],
  217. dest: '<%= prj.build %>/'
  218. },
  219. build_scripts: {
  220. expand: true,
  221. cwd: '<%= prj.js.src %>',
  222. src: ['<%= prj.jsPattern %>'],
  223. dest: '<%= prj.js.build %>/'
  224. },
  225. build_dependencies: {
  226. expand: true,
  227. src: ['<%= prj.dependencies %>'],
  228. dest: '<%= prj.build %>/'
  229. },
  230. build_assets: {
  231. expand: true,
  232. cwd: '<%= prj.assets.src %>/',
  233. src: ['**'],
  234. dest: '<%= prj.assets.build %>/'
  235. },
  236. release_assets: {
  237. expand: true,
  238. cwd: '<%= prj.assets.build %>/',
  239. src: ['**'],
  240. dest: '<%= prj.assets.release %>'
  241. },
  242. nwjs_package: {
  243. expand: true,
  244. cwd: '<%= prj.src %>/',
  245. src: ['package.json'],
  246. dest: '<%= prj.build %>'
  247. },
  248. nwjs_release_package: {
  249. expand: true,
  250. cwd: '<%= prj.src %>/',
  251. src: ['package.json'],
  252. dest: '<%= prj.release %>'
  253. },
  254. nggettext_langs: {
  255. expand: true,
  256. cwd: '<%= prj.desktopApp %>/langs',
  257. src: ['**'],
  258. dest: '<%= prj.mockserver %>/responses/langs'
  259. }
  260. },
  261. karma: {
  262. options: {
  263. frameworks: ['jasmine'],
  264. browsers: ['PhantomJS'],
  265. plugins: [
  266. 'karma-jasmine',
  267. 'karma-phantomjs-launcher',
  268. 'karma-spec-reporter',
  269. 'karma-junit-reporter',
  270. 'karma-coverage'
  271. ],
  272. preprocessors: {
  273. '<%= prj.files.src %>': 'coverage'
  274. },
  275. singleRun: true
  276. },
  277. build: {
  278. reporters: [
  279. 'spec'
  280. ],
  281. files: {
  282. src: [
  283. '<%= prj.dependencies %>',
  284. '<%= prj.devDependencies %>',
  285. '<%= prj.files.src %>',
  286. '<%= prj.files.srcTest %>'
  287. ]
  288. },
  289. coverageReporter: {
  290. type: 'text-summary',
  291. dir: tmpDir.path
  292. }
  293. },
  294. release: {
  295. reporters: [
  296. 'junit',
  297. 'coverage'
  298. ],
  299. files: '<%= karma.build.files %>',
  300. junitReporter: {
  301. outputFile: '<%= prj.dist %>/test-results.xml'
  302. },
  303. coverageReporter: {
  304. type: 'lcov',
  305. dir: '<%= prj.dist %>/coverage/'
  306. }
  307. }
  308. },
  309. nodewebkit: {
  310. build: {
  311. options: {
  312. build_dir: '<%= prj.desktopApp %>',
  313. mac: true,
  314. win: true,
  315. linux32: true,
  316. linux64: true,
  317. version: '0.12.2',
  318. mac_icns: '<%= prj.assets.src %>/img/app_icon.icns'
  319. },
  320. src: ['<%= prj.build %>/**/*']
  321. },
  322. release: {
  323. options: {
  324. build_dir: '<%= prj.desktopApp %>',
  325. mac: true,
  326. win: true,
  327. linux32: true,
  328. linux64: true,
  329. version: '0.12.2',
  330. mac_icns: '<%= prj.assets.src %>/img/app_icon.icns'
  331. },
  332. src: ['<%= prj.release %>/**/*']
  333. }
  334. },
  335. /* dev tasks */
  336. freddie: {
  337. build: {
  338. options: {
  339. root: '<%= prj.build %>',
  340. port: 3000,
  341. proxy: {
  342. // '/sdm-ws-rest': 'http://10.0.16.54:8090/sdm-ws-rest' // antonio
  343. // '/sdm-ws-rest': 'http://10.0.16.91:8090/sdm-ws-rest' // montse
  344. // '/sdm-ws-rest': 'http://10.0.16.245:8090/sdm-ws-rest' // syed
  345. '/sdm-ws-rest': 'http://localhost:8090/sdm-ws-rest'
  346. }
  347. }
  348. }
  349. },
  350. express: {
  351. options: {
  352. // serverreload: true,
  353. port: 8090
  354. },
  355. dev: {
  356. options: {
  357. server: path.resolve('./server/mockserver')
  358. }
  359. }
  360. },
  361. watch: {
  362. options: {
  363. livereload: true
  364. },
  365. scripts: {
  366. files: ['<%= prj.files.src %>'],
  367. tasks: ['karma:build', 'copy:build_scripts']
  368. },
  369. assets: {
  370. files: ['<%= prj.assets.src %>/**'],
  371. tasks: ['copy:build_assets']
  372. },
  373. partials: {
  374. files: ['<%= html2js.partials.src %>'],
  375. tasks: ['html2js']
  376. },
  377. css: {
  378. files: ['<%= prj.css.src %>/**/*.css'],
  379. tasks: ['copy:build_css']
  380. },
  381. sass: {
  382. files: ['<%= prj.sass.src %>/**/*.scss'],
  383. tasks: ['sass']
  384. },
  385. tests: {
  386. files: ['<%= prj.files.srcTest %>'],
  387. tasks: ['karma:build']
  388. }
  389. }
  390. });
  391. /* main tasks */
  392. grunt.registerTask('compile', [
  393. 'html2js',
  394. 'sass',
  395. 'copy:build_css',
  396. 'copy:build_scripts',
  397. 'copy:build_dependencies',
  398. 'copy:build_cssDependencies',
  399. 'copy:build_assets',
  400. 'nggettext_compile',
  401. 'copy:nggettext_langs'
  402. ]);
  403. grunt.registerTask('build', [
  404. 'clean',
  405. // 'jshint',
  406. // 'karma:build',
  407. 'compile',
  408. 'index:build'
  409. ]);
  410. grunt.registerTask('dev', [
  411. 'openport:watch.options.livereload:35729',
  412. 'build',
  413. 'freddie',
  414. 'express',
  415. 'watch'
  416. ]);
  417. grunt.registerTask('release', [
  418. 'clean',
  419. // 'jshint',
  420. 'karma:release',
  421. 'compile',
  422. 'cssmin',
  423. 'copy:release_assets',
  424. 'concat:srcScripts',
  425. 'ngAnnotate',
  426. 'uglify',
  427. 'concat:scripts',
  428. 'concat:styles',
  429. 'index:release',
  430. 'version',
  431. 'copy:nwjs_release_package',
  432. 'nodewebkit:release',
  433. 'compress:release'
  434. ]);
  435. grunt.registerTask('app', [
  436. 'build',
  437. 'copy:nwjs_package',
  438. 'nodewebkit:build'
  439. ]);
  440. grunt.registerTask('default', ['release']);
  441. /* custom task definitions */
  442. (function() {
  443. var ext = function(ext) {
  444. var extRE = new RegExp('\.' + ext + '$');
  445. return RegExp.prototype.test.bind(extRE);
  446. };
  447. var task = function() {
  448. var dir = this.data.dir,
  449. files = this.filesSrc;
  450. var relativePath = function(file) {
  451. return file.replace(dir + '/', '');
  452. };
  453. var scripts = files.filter(ext('js')).map(relativePath),
  454. styles = files.filter(ext('css')).map(relativePath),
  455. dev = grunt.task.current.target === 'build';
  456. var src = grunt.config('prj.src') + '/index.html',
  457. dest = dir + '/index.html';
  458. grunt.file.copy(src, dest, {
  459. process: function(contents) {
  460. return grunt.template.process(contents, {
  461. data: {
  462. scripts: scripts,
  463. styles: styles,
  464. dev: dev,
  465. livereloadPort: grunt.config.get("watch.options.livereload")
  466. }
  467. });
  468. }
  469. });
  470. };
  471. grunt.registerMultiTask('index', 'Process index template', task);
  472. })();
  473. (function() {
  474. var task = function() {
  475. var src = grunt.config('prj.src') + '/version.txt',
  476. dest = grunt.config('prj.release') + '/version.txt';
  477. grunt.file.copy(src, dest, {
  478. process: function(contents) {
  479. return grunt.template.process(contents, {
  480. data: {
  481. pkg: grunt.config('pkg'),
  482. env: process.env
  483. }
  484. });
  485. }
  486. });
  487. };
  488. grunt.registerTask('version', 'Process version template', task);
  489. })();
  490. // var parentgrunt = require('./node_modules/lib-parent-grunt/tasks/lib-parent-grunt.js');
  491. // parentgrunt.config(grunt, '<%= prj.desktopApp %>'+'/', 'project');
  492. // parentgrunt.task(grunt);
  493. };