Gruntfile.coffee 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. fs = require "fs"
  2. module.exports = (grunt) ->
  3. grunt.loadNpmTasks 'grunt-contrib-coffee'
  4. grunt.loadNpmTasks 'grunt-contrib-less'
  5. grunt.loadNpmTasks 'grunt-contrib-clean'
  6. grunt.loadNpmTasks 'grunt-mocha-test'
  7. grunt.loadNpmTasks 'grunt-available-tasks'
  8. grunt.loadNpmTasks 'grunt-contrib-requirejs'
  9. grunt.loadNpmTasks 'grunt-execute'
  10. grunt.loadNpmTasks 'grunt-bunyan'
  11. config =
  12. execute:
  13. app:
  14. src: "app.js"
  15. coffee:
  16. app_dir:
  17. expand: true,
  18. flatten: false,
  19. cwd: 'app/coffee',
  20. src: ['**/*.coffee'],
  21. dest: 'app/js/',
  22. ext: '.js'
  23. app:
  24. src: 'app.coffee'
  25. dest: 'app.js'
  26. sharejs:
  27. options:
  28. join: true
  29. files:
  30. "public/js/libs/sharejs.js": [
  31. "public/coffee/ide/editor/sharejs/header.coffee"
  32. "public/coffee/ide/editor/sharejs/vendor/types/helpers.coffee"
  33. "public/coffee/ide/editor/sharejs/vendor/types/text.coffee"
  34. "public/coffee/ide/editor/sharejs/vendor/types/text-api.coffee"
  35. "public/coffee/ide/editor/sharejs/vendor/client/microevent.coffee"
  36. "public/coffee/ide/editor/sharejs/vendor/client/doc.coffee"
  37. "public/coffee/ide/editor/sharejs/vendor/client/ace.coffee"
  38. ]
  39. client:
  40. expand: true,
  41. flatten: false,
  42. cwd: 'public/coffee',
  43. src: ['**/*.coffee'],
  44. dest: 'public/js/',
  45. ext: '.js'
  46. smoke_tests:
  47. expand: true,
  48. flatten: false,
  49. cwd: 'test/smoke/coffee',
  50. src: ['**/*.coffee'],
  51. dest: 'test/smoke/js/',
  52. ext: '.js'
  53. unit_tests:
  54. expand: true,
  55. flatten: false,
  56. cwd: 'test/UnitTests/coffee',
  57. src: ['**/*.coffee'],
  58. dest: 'test/UnitTests/js/',
  59. ext: '.js'
  60. less:
  61. app:
  62. files:
  63. "public/stylesheets/style.css": "public/stylesheets/style.less"
  64. requirejs:
  65. compile:
  66. options:
  67. optimize:"uglify2"
  68. uglify2:
  69. mangle: false
  70. appDir: "public/js"
  71. baseUrl: "./"
  72. dir: "public/minjs"
  73. inlineText: false
  74. preserveLicenseComments: false
  75. paths:
  76. "moment": "libs/moment-2.7.0"
  77. "mathjax": "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"
  78. shim:
  79. "libs/pdfListView/PdfListView":
  80. deps: ["libs/pdf"]
  81. "libs/pdf":
  82. deps: ["libs/compatibility"]
  83. skipDirOptimize: true
  84. modules: [
  85. {
  86. name: "main",
  87. exclude: ["libs"]
  88. }, {
  89. name: "ide",
  90. exclude: ["libs", "libs/jquery-layout"]
  91. }, {
  92. name: "libs"
  93. }
  94. ]
  95. clean:
  96. app: ["app/js"]
  97. unit_tests: ["test/UnitTests/js"]
  98. mochaTest:
  99. unit:
  100. src: ["test/UnitTests/js/#{grunt.option('feature') or '**'}/*.js"]
  101. options:
  102. reporter: grunt.option('reporter') or 'spec'
  103. grep: grunt.option("grep")
  104. smoke:
  105. src: ['test/smoke/js/**/*.js']
  106. options:
  107. reporter: grunt.option('reporter') or 'spec'
  108. grep: grunt.option("grep")
  109. availabletasks:
  110. tasks:
  111. options:
  112. filter: 'exclude',
  113. tasks: [
  114. 'coffee'
  115. 'less'
  116. 'clean'
  117. 'mochaTest'
  118. 'availabletasks'
  119. 'wrap_sharejs'
  120. 'requirejs'
  121. 'execute'
  122. 'bunyan'
  123. ]
  124. groups:
  125. "Compile tasks": [
  126. "compile:server"
  127. "compile:client"
  128. "compile:tests"
  129. "compile"
  130. "compile:unit_tests"
  131. "compile:smoke_tests"
  132. "compile:css"
  133. "compile:minify"
  134. "install"
  135. ]
  136. "Test tasks": [
  137. "test:unit"
  138. ]
  139. "Run tasks": [
  140. "run"
  141. "default"
  142. ]
  143. "Misc": [
  144. "help"
  145. ]
  146. moduleCompileServerTasks = []
  147. moduleCompileUnitTestTasks = []
  148. moduleUnitTestTasks = []
  149. moduleCompileClientTasks = []
  150. moduleIdeClientSideIncludes = []
  151. moduleMainClientSideIncludes = []
  152. if fs.existsSync "./modules"
  153. for module in fs.readdirSync "./modules"
  154. if fs.existsSync "./modules/#{module}/index.coffee"
  155. config.coffee["module_#{module}_server"] = {
  156. expand: true,
  157. flatten: false,
  158. cwd: "modules/#{module}/app/coffee",
  159. src: ['**/*.coffee'],
  160. dest: "modules/#{module}/app/js",
  161. ext: '.js'
  162. }
  163. config.coffee["module_#{module}_index"] = {
  164. src: "modules/#{module}/index.coffee",
  165. dest: "modules/#{module}/index.js"
  166. }
  167. moduleCompileServerTasks.push "coffee:module_#{module}_server"
  168. moduleCompileServerTasks.push "coffee:module_#{module}_index"
  169. config.coffee["module_#{module}_unit_tests"] = {
  170. expand: true,
  171. flatten: false,
  172. cwd: "modules/#{module}/test/unit/coffee",
  173. src: ['**/*.coffee'],
  174. dest: "modules/#{module}/test/unit/js",
  175. ext: '.js'
  176. }
  177. config.mochaTest["module_#{module}_unit"] = {
  178. src: ["modules/#{module}/test/unit/js/*.js"]
  179. options:
  180. reporter: grunt.option('reporter') or 'spec'
  181. grep: grunt.option("grep")
  182. }
  183. moduleCompileUnitTestTasks.push "coffee:module_#{module}_unit_tests"
  184. moduleUnitTestTasks.push "mochaTest:module_#{module}_unit"
  185. if fs.existsSync "./modules/#{module}/public/coffee/ide/index.coffee"
  186. config.coffee["module_#{module}_client_ide"] = {
  187. expand: true,
  188. flatten: false,
  189. cwd: "modules/#{module}/public/coffee/ide",
  190. src: ['**/*.coffee'],
  191. dest: "public/js/ide/#{module}",
  192. ext: '.js'
  193. }
  194. moduleCompileClientTasks.push "coffee:module_#{module}_client_ide"
  195. moduleIdeClientSideIncludes.push "ide/#{module}/index"
  196. if fs.existsSync "./modules/#{module}/public/coffee/main/index.coffee"
  197. config.coffee["module_#{module}_client_main"] = {
  198. expand: true,
  199. flatten: false,
  200. cwd: "modules/#{module}/public/coffee/main",
  201. src: ['**/*.coffee'],
  202. dest: "public/js/main/#{module}",
  203. ext: '.js'
  204. }
  205. moduleCompileClientTasks.push "coffee:module_#{module}_client_main"
  206. moduleMainClientSideIncludes.push "main/#{module}/index"
  207. grunt.initConfig config
  208. grunt.registerTask 'wrap_sharejs', 'Wrap the compiled ShareJS code for AMD module loading', () ->
  209. content = fs.readFileSync "public/js/libs/sharejs.js"
  210. fs.writeFileSync "public/js/libs/sharejs.js", """
  211. define(["ace/ace"], function() {
  212. #{content}
  213. return window.sharejs;
  214. });
  215. """
  216. grunt.registerTask 'help', 'Display this help list', 'availabletasks'
  217. grunt.registerTask 'compile:modules:server', 'Compile all the modules', moduleCompileServerTasks
  218. grunt.registerTask 'compile:modules:unit_tests', 'Compile all the modules unit tests', moduleCompileUnitTestTasks
  219. grunt.registerTask 'compile:modules:client', 'Compile all the module client side code', moduleCompileClientTasks
  220. grunt.registerTask 'compile:modules:inject_clientside_includes', () ->
  221. content = fs.readFileSync("public/js/ide.js").toString()
  222. content = content.replace(/, "__IDE_CLIENTSIDE_INCLUDES__"/g, moduleIdeClientSideIncludes.map((i) -> ", \"#{i}\"").join(""))
  223. fs.writeFileSync "public/js/ide.js", content
  224. content = fs.readFileSync("public/js/main.js").toString()
  225. content = content.replace(/, "__MAIN_CLIENTSIDE_INCLUDES__"/g, moduleMainClientSideIncludes.map((i) -> ", \"#{i}\"").join(""))
  226. fs.writeFileSync "public/js/main.js", content
  227. grunt.registerTask 'compile:server', 'Compile the server side coffee script', ['clean:app', 'coffee:app', 'coffee:app_dir', 'compile:modules:server']
  228. grunt.registerTask 'compile:client', 'Compile the client side coffee script', ['coffee:client', 'coffee:sharejs', 'wrap_sharejs', "compile:modules:client", 'compile:modules:inject_clientside_includes']
  229. grunt.registerTask 'compile:css', 'Compile the less files to css', ['less']
  230. grunt.registerTask 'compile:minify', 'Concat and minify the client side js', ['requirejs']
  231. grunt.registerTask 'compile:unit_tests', 'Compile the unit tests', ['clean:unit_tests', 'coffee:unit_tests']
  232. grunt.registerTask 'compile:smoke_tests', 'Compile the smoke tests', ['coffee:smoke_tests']
  233. grunt.registerTask 'compile:tests', 'Compile all the tests', ['compile:smoke_tests', 'compile:unit_tests']
  234. grunt.registerTask 'compile', 'Compiles everything need to run web-sharelatex', ['compile:server', 'compile:client', 'compile:css']
  235. grunt.registerTask 'install', "Compile everything when installing as an npm module", ['compile']
  236. grunt.registerTask 'test:unit', 'Run the unit tests (use --grep=<regex> or --feature=<feature> for individual tests)', ['compile:server', 'compile:unit_tests', 'mochaTest:unit']
  237. grunt.registerTask 'test:smoke', 'Run the smoke tests', ['compile:smoke_tests', 'mochaTest:smoke']
  238. grunt.registerTask 'test:modules:unit', 'Run the unit tests for the modules', ['compile:modules:server', 'compile:modules:unit_tests'].concat(moduleUnitTestTasks)
  239. grunt.registerTask 'run', "Compile and run the web-sharelatex server", ['compile', 'bunyan', 'execute']
  240. grunt.registerTask 'default', 'run'