Gruntfile.coffee 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. smoke_tests:
  96. expand: true,
  97. flatten: false,
  98. cwd: 'test/smoke/coffee',
  99. src: ['**/*.coffee'],
  100. dest: 'test/smoke/js/',
  101. ext: '.js'
  102. unit_tests:
  103. expand: true,
  104. flatten: false,
  105. cwd: 'test/UnitTests/coffee',
  106. src: ['**/*.coffee'],
  107. dest: 'test/UnitTests/js/',
  108. ext: '.js'
  109. acceptance_tests:
  110. expand: true,
  111. flatten: false,
  112. cwd: 'test/acceptance/coffee',
  113. src: ['**/*.coffee'],
  114. dest: 'test/acceptance/js/',
  115. ext: '.js'
  116. less:
  117. app:
  118. files:
  119. "public/stylesheets/style.css": "public/stylesheets/style.less"
  120. postcss:
  121. options:
  122. map: true,
  123. processors: [
  124. require('autoprefixer')({browsers: [ 'last 2 versions', 'ie >= 10' ]})
  125. ]
  126. dist:
  127. src: 'public/stylesheets/style.css'
  128. env:
  129. run:
  130. add:
  131. NODE_TLS_REJECT_UNAUTHORIZED:0
  132. requirejs:
  133. compile:
  134. options:
  135. optimize:"uglify2"
  136. uglify2:
  137. mangle: false
  138. appDir: "public/js"
  139. baseUrl: "./"
  140. dir: "public/minjs"
  141. inlineText: false
  142. preserveLicenseComments: false
  143. paths:
  144. "moment": "libs/#{PackageVersions.lib('moment')}"
  145. "mathjax": "/js/libs/mathjax/MathJax.js?config=TeX-AMS_HTML"
  146. "pdfjs-dist/build/pdf": "libs/#{PackageVersions.lib('pdfjs')}/pdf"
  147. "ace": "#{PackageVersions.lib('ace')}"
  148. shim:
  149. "pdfjs-dist/build/pdf":
  150. deps: ["libs/#{PackageVersions.lib('pdfjs')}/compatibility"]
  151. skipDirOptimize: true
  152. modules: [
  153. {
  154. name: "main",
  155. exclude: ["libs"]
  156. }, {
  157. name: "ide",
  158. exclude: ["libs", "pdfjs-dist/build/pdf"]
  159. }, {
  160. name: "libs"
  161. },{
  162. name: "ace/mode-latex"
  163. },{
  164. name: "ace/worker-latex"
  165. }
  166. ]
  167. clean:
  168. app: ["app/js"]
  169. unit_tests: ["test/UnitTests/js"]
  170. acceptance_tests: ["test/acceptance/js"]
  171. mochaTest:
  172. unit:
  173. src: ["test/UnitTests/js/#{grunt.option('feature') or '**'}/*.js"]
  174. options:
  175. reporter: grunt.option('reporter') or 'spec'
  176. grep: grunt.option("grep")
  177. smoke:
  178. src: ['test/smoke/js/**/*.js']
  179. options:
  180. reporter: grunt.option('reporter') or 'spec'
  181. grep: grunt.option("grep")
  182. acceptance:
  183. src: ["test/acceptance/js/#{grunt.option('feature') or '**'}/*.js"]
  184. options:
  185. timeout: 40000
  186. reporter: grunt.option('reporter') or 'spec'
  187. grep: grunt.option("grep")
  188. "git-rev-parse":
  189. version:
  190. options:
  191. prop: 'commit'
  192. file_append:
  193. default_options: files: [ {
  194. append: '\n//ide.js is complete - used for automated testing'
  195. input: 'public/minjs/ide.js'
  196. output: 'public/minjs/ide.js'
  197. }]
  198. sed:
  199. version:
  200. path: "app/views/sentry.pug"
  201. pattern: '@@COMMIT@@',
  202. replacement: '<%= commit %>',
  203. release:
  204. path: "app/views/sentry.pug"
  205. pattern: "@@RELEASE@@"
  206. replacement: process.env.BUILD_NUMBER || "(unknown build)"
  207. availabletasks:
  208. tasks:
  209. options:
  210. filter: 'exclude',
  211. tasks: [
  212. 'coffee'
  213. 'less'
  214. 'clean'
  215. 'mochaTest'
  216. 'availabletasks'
  217. 'wrap_sharejs'
  218. 'requirejs'
  219. 'execute'
  220. 'bunyan'
  221. ]
  222. groups:
  223. "Compile tasks": [
  224. "compile:server"
  225. "compile:client"
  226. "compile:tests"
  227. "compile"
  228. "compile:unit_tests"
  229. "compile:smoke_tests"
  230. "compile:css"
  231. "compile:minify"
  232. "install"
  233. ]
  234. "Test tasks": [
  235. "test:unit"
  236. "test:acceptance"
  237. ]
  238. "Run tasks": [
  239. "run"
  240. "default"
  241. ]
  242. "Misc": [
  243. "help"
  244. ]
  245. moduleCompileServerTasks = []
  246. moduleCompileUnitTestTasks = []
  247. moduleUnitTestTasks = []
  248. moduleCompileClientTasks = []
  249. moduleIdeClientSideIncludes = []
  250. moduleMainClientSideIncludes = []
  251. if fs.existsSync "./modules"
  252. for module in fs.readdirSync "./modules"
  253. if fs.existsSync "./modules/#{module}/index.coffee"
  254. config.coffee["module_#{module}_server"] = {
  255. expand: true,
  256. flatten: false,
  257. cwd: "modules/#{module}/app/coffee",
  258. src: ['**/*.coffee'],
  259. dest: "modules/#{module}/app/js",
  260. ext: '.js'
  261. }
  262. config.coffee["module_#{module}_index"] = {
  263. src: "modules/#{module}/index.coffee",
  264. dest: "modules/#{module}/index.js"
  265. }
  266. moduleCompileServerTasks.push "coffee:module_#{module}_server"
  267. moduleCompileServerTasks.push "coffee:module_#{module}_index"
  268. config.coffee["module_#{module}_unit_tests"] = {
  269. expand: true,
  270. flatten: false,
  271. cwd: "modules/#{module}/test/unit/coffee",
  272. src: ['**/*.coffee'],
  273. dest: "modules/#{module}/test/unit/js",
  274. ext: '.js'
  275. }
  276. config.mochaTest["module_#{module}_unit"] = {
  277. src: ["modules/#{module}/test/unit/js/*.js"]
  278. options:
  279. reporter: grunt.option('reporter') or 'spec'
  280. grep: grunt.option("grep")
  281. }
  282. moduleCompileUnitTestTasks.push "coffee:module_#{module}_unit_tests"
  283. moduleUnitTestTasks.push "mochaTest:module_#{module}_unit"
  284. if fs.existsSync "./modules/#{module}/public/coffee/ide/index.coffee"
  285. config.coffee["module_#{module}_client_ide"] = {
  286. expand: true,
  287. flatten: false,
  288. cwd: "modules/#{module}/public/coffee/ide",
  289. src: ['**/*.coffee'],
  290. dest: "public/js/ide/#{module}",
  291. ext: '.js'
  292. }
  293. moduleCompileClientTasks.push "coffee:module_#{module}_client_ide"
  294. moduleIdeClientSideIncludes.push "ide/#{module}/index"
  295. if fs.existsSync "./modules/#{module}/public/coffee/main/index.coffee"
  296. config.coffee["module_#{module}_client_main"] = {
  297. expand: true,
  298. flatten: false,
  299. cwd: "modules/#{module}/public/coffee/main",
  300. src: ['**/*.coffee'],
  301. dest: "public/js/main/#{module}",
  302. ext: '.js'
  303. }
  304. moduleCompileClientTasks.push "coffee:module_#{module}_client_main"
  305. moduleMainClientSideIncludes.push "main/#{module}/index"
  306. grunt.initConfig config
  307. grunt.registerTask 'wrap_sharejs', 'Wrap the compiled ShareJS code for AMD module loading', () ->
  308. content = fs.readFileSync "public/js/libs/sharejs.js"
  309. fs.writeFileSync "public/js/libs/sharejs.js", """
  310. define(["ace/ace"], function() {
  311. #{content}
  312. return window.sharejs;
  313. });
  314. """
  315. grunt.registerTask 'help', 'Display this help list', 'availabletasks'
  316. grunt.registerTask 'compile:modules:server', 'Compile all the modules', moduleCompileServerTasks
  317. grunt.registerTask 'compile:modules:unit_tests', 'Compile all the modules unit tests', moduleCompileUnitTestTasks
  318. grunt.registerTask 'compile:modules:client', 'Compile all the module client side code', moduleCompileClientTasks
  319. grunt.registerTask 'compile:modules:inject_clientside_includes', () ->
  320. content = fs.readFileSync("public/js/ide.js").toString()
  321. content = content.replace(/, "__IDE_CLIENTSIDE_INCLUDES__"/g, moduleIdeClientSideIncludes.map((i) -> ", \"#{i}\"").join(""))
  322. fs.writeFileSync "public/js/ide.js", content
  323. content = fs.readFileSync("public/js/main.js").toString()
  324. content = content.replace(/, "__MAIN_CLIENTSIDE_INCLUDES__"/g, moduleMainClientSideIncludes.map((i) -> ", \"#{i}\"").join(""))
  325. fs.writeFileSync "public/js/main.js", content
  326. grunt.registerTask 'compile:server', 'Compile the server side coffee script', ['clean:app', 'coffee:app', 'coffee:app_dir', 'compile:modules:server']
  327. grunt.registerTask 'compile:client', 'Compile the client side coffee script', ['coffee:client', 'coffee:sharejs', 'wrap_sharejs', "compile:modules:client", 'compile:modules:inject_clientside_includes']
  328. grunt.registerTask 'compile:css', 'Compile the less files to css', ['less', 'postcss:dist']
  329. grunt.registerTask 'compile:minify', 'Concat and minify the client side js', ['requirejs', "file_append", "exec:cssmin",]
  330. grunt.registerTask 'compile:unit_tests', 'Compile the unit tests', ['clean:unit_tests', 'coffee:unit_tests']
  331. grunt.registerTask 'compile:acceptance_tests', 'Compile the acceptance tests', ['clean:acceptance_tests', 'coffee:acceptance_tests']
  332. grunt.registerTask 'compile:smoke_tests', 'Compile the smoke tests', ['coffee:smoke_tests']
  333. grunt.registerTask 'compile:tests', 'Compile all the tests', ['compile:smoke_tests', 'compile:unit_tests']
  334. grunt.registerTask 'compile', 'Compiles everything need to run web-sharelatex', ['compile:server', 'compile:client', 'compile:css']
  335. grunt.registerTask 'quickcompile:coffee', 'Compiles only changed coffee files',['newer:coffee']
  336. grunt.registerTask 'install', "Compile everything when installing as an npm module", ['compile']
  337. 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)
  338. grunt.registerTask 'test:acceptance', 'Run the acceptance tests (use --grep=<regex> or --feature=<feature> for individual tests)', ['compile:acceptance_tests', 'mochaTest:acceptance']
  339. grunt.registerTask 'test:smoke', 'Run the smoke tests', ['compile:smoke_tests', 'mochaTest:smoke']
  340. grunt.registerTask 'test:modules:unit', 'Run the unit tests for the modules', ['compile:modules:server', 'compile:modules:unit_tests'].concat(moduleUnitTestTasks)
  341. grunt.registerTask 'run:watch', "Compile and run the web-sharelatex server", ['compile', 'env:run', 'parallel']
  342. grunt.registerTask 'run', "Compile and run the web-sharelatex server", ['compile', 'env:run', 'exec']
  343. grunt.registerTask 'default', 'run'
  344. grunt.registerTask 'version', "Write the version number into sentry.pug", ['git-rev-parse', 'sed']