Gruntfile.coffee 12 KB

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