Gruntfile.coffee 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. fs = require "fs"
  2. PackageVersions = require "./app/coffee/infrastructure/PackageVersions"
  3. module.exports = (grunt) ->
  4. grunt.loadNpmTasks 'grunt-contrib-coffee'
  5. grunt.loadNpmTasks 'grunt-contrib-less'
  6. grunt.loadNpmTasks 'grunt-contrib-clean'
  7. grunt.loadNpmTasks 'grunt-mocha-test'
  8. grunt.loadNpmTasks 'grunt-available-tasks'
  9. grunt.loadNpmTasks 'grunt-contrib-requirejs'
  10. grunt.loadNpmTasks 'grunt-bunyan'
  11. grunt.loadNpmTasks 'grunt-sed'
  12. grunt.loadNpmTasks 'grunt-git-rev-parse'
  13. grunt.loadNpmTasks 'grunt-file-append'
  14. grunt.loadNpmTasks 'grunt-file-append'
  15. grunt.loadNpmTasks 'grunt-env'
  16. grunt.loadNpmTasks 'grunt-newer'
  17. grunt.loadNpmTasks 'grunt-contrib-watch'
  18. grunt.loadNpmTasks 'grunt-parallel'
  19. grunt.loadNpmTasks 'grunt-exec'
  20. # grunt.loadNpmTasks 'grunt-contrib-imagemin'
  21. # grunt.loadNpmTasks 'grunt-sprity'
  22. config =
  23. exec:
  24. run:
  25. command:"node app.js | ./node_modules/logger-sharelatex/node_modules/bunyan/bin/bunyan --color"
  26. cssmin:
  27. command:"node_modules/clean-css/bin/cleancss --s0 -o public/stylesheets/style.css public/stylesheets/style.css"
  28. watch:
  29. coffee:
  30. files: 'public/**/*.coffee'
  31. tasks: ['quickcompile:coffee']
  32. options: {}
  33. less:
  34. files: '**/*.less'
  35. tasks: ['compile:css']
  36. options: {}
  37. parallel:
  38. run:
  39. tasks:['exec', 'watch']
  40. options:
  41. grunt:true
  42. stream:true
  43. # imagemin:
  44. # dynamic:
  45. # files: [{
  46. # expand: true
  47. # cwd: 'public/img/'
  48. # src: ['**/*.{png,jpg,gif}']
  49. # dest: 'public/img/'
  50. # }]
  51. # options:
  52. # interlaced:false
  53. # optimizationLevel: 7
  54. # sprity:
  55. # sprite:
  56. # options:
  57. # cssPath:"/img/"
  58. # 'style': '../../public/stylesheets/app/sprites.less'
  59. # margin: 0
  60. # src: ['./public/img/flags/24/*.png']
  61. # dest: './public/img/sprite'
  62. coffee:
  63. app_dir:
  64. expand: true,
  65. flatten: false,
  66. cwd: 'app/coffee',
  67. src: ['**/*.coffee'],
  68. dest: 'app/js/',
  69. ext: '.js'
  70. app:
  71. src: 'app.coffee'
  72. dest: 'app.js'
  73. sharejs:
  74. options:
  75. join: true
  76. files:
  77. "public/js/libs/sharejs.js": [
  78. "public/coffee/ide/editor/sharejs/header.coffee"
  79. "public/coffee/ide/editor/sharejs/vendor/types/helpers.coffee"
  80. "public/coffee/ide/editor/sharejs/vendor/types/text.coffee"
  81. "public/coffee/ide/editor/sharejs/vendor/types/text-api.coffee"
  82. "public/coffee/ide/editor/sharejs/vendor/client/microevent.coffee"
  83. "public/coffee/ide/editor/sharejs/vendor/client/doc.coffee"
  84. "public/coffee/ide/editor/sharejs/vendor/client/ace.coffee"
  85. ]
  86. client:
  87. expand: true,
  88. flatten: false,
  89. cwd: 'public/coffee',
  90. src: ['**/*.coffee'],
  91. dest: 'public/js/',
  92. ext: '.js'
  93. smoke_tests:
  94. expand: true,
  95. flatten: false,
  96. cwd: 'test/smoke/coffee',
  97. src: ['**/*.coffee'],
  98. dest: 'test/smoke/js/',
  99. ext: '.js'
  100. unit_tests:
  101. expand: true,
  102. flatten: false,
  103. cwd: 'test/UnitTests/coffee',
  104. src: ['**/*.coffee'],
  105. dest: 'test/UnitTests/js/',
  106. ext: '.js'
  107. acceptance_tests:
  108. expand: true,
  109. flatten: false,
  110. cwd: 'test/acceptance/coffee',
  111. src: ['**/*.coffee'],
  112. dest: 'test/acceptance/js/',
  113. ext: '.js'
  114. less:
  115. app:
  116. files:
  117. "public/stylesheets/style.css": "public/stylesheets/style.less"
  118. env:
  119. run:
  120. add:
  121. NODE_TLS_REJECT_UNAUTHORIZED:0
  122. requirejs:
  123. compile:
  124. options:
  125. optimize:"uglify2"
  126. uglify2:
  127. mangle: false
  128. appDir: "public/js"
  129. baseUrl: "./"
  130. dir: "public/minjs"
  131. inlineText: false
  132. preserveLicenseComments: false
  133. paths:
  134. "moment": "libs/#{PackageVersions.lib('moment')}"
  135. "mathjax": "/js/libs/mathjax/MathJax.js?config=TeX-AMS_HTML"
  136. "pdfjs-dist/build/pdf": "libs/#{PackageVersions.lib('pdfjs')}/pdf"
  137. "ace": "#{PackageVersions.lib('ace')}"
  138. shim:
  139. "pdfjs-dist/build/pdf":
  140. deps: ["libs/#{PackageVersions.lib('pdfjs')}/compatibility"]
  141. skipDirOptimize: true
  142. modules: [
  143. {
  144. name: "main",
  145. exclude: ["libs"]
  146. }, {
  147. name: "ide",
  148. exclude: ["libs", "pdfjs-dist/build/pdf"]
  149. }, {
  150. name: "libs"
  151. },{
  152. name: "ace/mode-latex"
  153. },{
  154. name: "ace/worker-latex"
  155. }
  156. ]
  157. clean:
  158. app: ["app/js"]
  159. unit_tests: ["test/UnitTests/js"]
  160. acceptance_tests: ["test/acceptance/js"]
  161. mochaTest:
  162. unit:
  163. src: ["test/UnitTests/js/#{grunt.option('feature') or '**'}/*.js"]
  164. options:
  165. reporter: grunt.option('reporter') or 'spec'
  166. grep: grunt.option("grep")
  167. smoke:
  168. src: ['test/smoke/js/**/*.js']
  169. options:
  170. reporter: grunt.option('reporter') or 'spec'
  171. grep: grunt.option("grep")
  172. acceptance:
  173. src: ["test/acceptance/js/#{grunt.option('feature') or '**'}/*.js"]
  174. options:
  175. timeout: 40000
  176. reporter: grunt.option('reporter') or 'spec'
  177. grep: grunt.option("grep")
  178. "git-rev-parse":
  179. version:
  180. options:
  181. prop: 'commit'
  182. file_append:
  183. default_options: files: [ {
  184. append: '\n//ide.js is complete - used for automated testing'
  185. input: 'public/minjs/ide.js'
  186. output: 'public/minjs/ide.js'
  187. }]
  188. sed:
  189. version:
  190. path: "app/views/sentry.jade"
  191. pattern: '@@COMMIT@@',
  192. replacement: '<%= commit %>',
  193. release:
  194. path: "app/views/sentry.jade"
  195. pattern: "@@RELEASE@@"
  196. replacement: process.env.BUILD_NUMBER || "(unknown build)"
  197. availabletasks:
  198. tasks:
  199. options:
  200. filter: 'exclude',
  201. tasks: [
  202. 'coffee'
  203. 'less'
  204. 'clean'
  205. 'mochaTest'
  206. 'availabletasks'
  207. 'wrap_sharejs'
  208. 'requirejs'
  209. 'execute'
  210. 'bunyan'
  211. ]
  212. groups:
  213. "Compile tasks": [
  214. "compile:server"
  215. "compile:client"
  216. "compile:tests"
  217. "compile"
  218. "compile:unit_tests"
  219. "compile:smoke_tests"
  220. "compile:css"
  221. "compile:minify"
  222. "install"
  223. ]
  224. "Test tasks": [
  225. "test:unit"
  226. "test:acceptance"
  227. ]
  228. "Run tasks": [
  229. "run"
  230. "default"
  231. ]
  232. "Misc": [
  233. "help"
  234. ]
  235. moduleCompileServerTasks = []
  236. moduleCompileUnitTestTasks = []
  237. moduleUnitTestTasks = []
  238. moduleCompileClientTasks = []
  239. moduleIdeClientSideIncludes = []
  240. moduleMainClientSideIncludes = []
  241. if fs.existsSync "./modules"
  242. for module in fs.readdirSync "./modules"
  243. if fs.existsSync "./modules/#{module}/index.coffee"
  244. config.coffee["module_#{module}_server"] = {
  245. expand: true,
  246. flatten: false,
  247. cwd: "modules/#{module}/app/coffee",
  248. src: ['**/*.coffee'],
  249. dest: "modules/#{module}/app/js",
  250. ext: '.js'
  251. }
  252. config.coffee["module_#{module}_index"] = {
  253. src: "modules/#{module}/index.coffee",
  254. dest: "modules/#{module}/index.js"
  255. }
  256. moduleCompileServerTasks.push "coffee:module_#{module}_server"
  257. moduleCompileServerTasks.push "coffee:module_#{module}_index"
  258. config.coffee["module_#{module}_unit_tests"] = {
  259. expand: true,
  260. flatten: false,
  261. cwd: "modules/#{module}/test/unit/coffee",
  262. src: ['**/*.coffee'],
  263. dest: "modules/#{module}/test/unit/js",
  264. ext: '.js'
  265. }
  266. config.mochaTest["module_#{module}_unit"] = {
  267. src: ["modules/#{module}/test/unit/js/*.js"]
  268. options:
  269. reporter: grunt.option('reporter') or 'spec'
  270. grep: grunt.option("grep")
  271. }
  272. moduleCompileUnitTestTasks.push "coffee:module_#{module}_unit_tests"
  273. moduleUnitTestTasks.push "mochaTest:module_#{module}_unit"
  274. if fs.existsSync "./modules/#{module}/public/coffee/ide/index.coffee"
  275. config.coffee["module_#{module}_client_ide"] = {
  276. expand: true,
  277. flatten: false,
  278. cwd: "modules/#{module}/public/coffee/ide",
  279. src: ['**/*.coffee'],
  280. dest: "public/js/ide/#{module}",
  281. ext: '.js'
  282. }
  283. moduleCompileClientTasks.push "coffee:module_#{module}_client_ide"
  284. moduleIdeClientSideIncludes.push "ide/#{module}/index"
  285. if fs.existsSync "./modules/#{module}/public/coffee/main/index.coffee"
  286. config.coffee["module_#{module}_client_main"] = {
  287. expand: true,
  288. flatten: false,
  289. cwd: "modules/#{module}/public/coffee/main",
  290. src: ['**/*.coffee'],
  291. dest: "public/js/main/#{module}",
  292. ext: '.js'
  293. }
  294. moduleCompileClientTasks.push "coffee:module_#{module}_client_main"
  295. moduleMainClientSideIncludes.push "main/#{module}/index"
  296. grunt.initConfig config
  297. grunt.registerTask 'wrap_sharejs', 'Wrap the compiled ShareJS code for AMD module loading', () ->
  298. content = fs.readFileSync "public/js/libs/sharejs.js"
  299. fs.writeFileSync "public/js/libs/sharejs.js", """
  300. define(["ace/ace"], function() {
  301. #{content}
  302. return window.sharejs;
  303. });
  304. """
  305. grunt.registerTask 'help', 'Display this help list', 'availabletasks'
  306. grunt.registerTask 'compile:modules:server', 'Compile all the modules', moduleCompileServerTasks
  307. grunt.registerTask 'compile:modules:unit_tests', 'Compile all the modules unit tests', moduleCompileUnitTestTasks
  308. grunt.registerTask 'compile:modules:client', 'Compile all the module client side code', moduleCompileClientTasks
  309. grunt.registerTask 'compile:modules:inject_clientside_includes', () ->
  310. content = fs.readFileSync("public/js/ide.js").toString()
  311. content = content.replace(/, "__IDE_CLIENTSIDE_INCLUDES__"/g, moduleIdeClientSideIncludes.map((i) -> ", \"#{i}\"").join(""))
  312. fs.writeFileSync "public/js/ide.js", content
  313. content = fs.readFileSync("public/js/main.js").toString()
  314. content = content.replace(/, "__MAIN_CLIENTSIDE_INCLUDES__"/g, moduleMainClientSideIncludes.map((i) -> ", \"#{i}\"").join(""))
  315. fs.writeFileSync "public/js/main.js", content
  316. grunt.registerTask 'compile:server', 'Compile the server side coffee script', ['clean:app', 'coffee:app', 'coffee:app_dir', 'compile:modules:server']
  317. grunt.registerTask 'compile:client', 'Compile the client side coffee script', ['coffee:client', 'coffee:sharejs', 'wrap_sharejs', "compile:modules:client", 'compile:modules:inject_clientside_includes']
  318. grunt.registerTask 'compile:css', 'Compile the less files to css', ['less']
  319. grunt.registerTask 'compile:minify', 'Concat and minify the client side js', ['requirejs', "file_append", "exec:cssmin",]
  320. grunt.registerTask 'compile:unit_tests', 'Compile the unit tests', ['clean:unit_tests', 'coffee:unit_tests']
  321. grunt.registerTask 'compile:acceptance_tests', 'Compile the acceptance tests', ['clean:acceptance_tests', 'coffee:acceptance_tests']
  322. grunt.registerTask 'compile:smoke_tests', 'Compile the smoke tests', ['coffee:smoke_tests']
  323. grunt.registerTask 'compile:tests', 'Compile all the tests', ['compile:smoke_tests', 'compile:unit_tests']
  324. grunt.registerTask 'compile', 'Compiles everything need to run web-sharelatex', ['compile:server', 'compile:client', 'compile:css']
  325. grunt.registerTask 'quickcompile:coffee', 'Compiles only changed coffee files',['newer:coffee']
  326. grunt.registerTask 'install', "Compile everything when installing as an npm module", ['compile']
  327. grunt.registerTask 'test:unit', 'Run the unit tests (use --grep=<regex> or --feature=<feature> for individual tests)', ['compile:server', 'compile:modules:server', 'compile:unit_tests', 'compile:modules:unit_tests', 'mochaTest:unit'].concat(moduleUnitTestTasks)
  328. grunt.registerTask 'test:acceptance', 'Run the acceptance tests (use --grep=<regex> or --feature=<feature> for individual tests)', ['compile:acceptance_tests', 'mochaTest:acceptance']
  329. grunt.registerTask 'test:smoke', 'Run the smoke tests', ['compile:smoke_tests', 'mochaTest:smoke']
  330. grunt.registerTask 'test:modules:unit', 'Run the unit tests for the modules', ['compile:modules:server', 'compile:modules:unit_tests'].concat(moduleUnitTestTasks)
  331. grunt.registerTask 'run:watch', "Compile and run the web-sharelatex server", ['compile', 'env:run', 'parallel']
  332. grunt.registerTask 'run', "Compile and run the web-sharelatex server", ['compile', 'env:run', 'exec']
  333. grunt.registerTask 'default', 'run'
  334. grunt.registerTask 'version', "Write the version number into sentry.jade", ['git-rev-parse', 'sed']