Gruntfile.coffee 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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/pdf"]
  91. }, {
  92. name: "libs"
  93. },{
  94. name: "ace/mode-latex"
  95. }
  96. ]
  97. clean:
  98. app: ["app/js"]
  99. unit_tests: ["test/UnitTests/js"]
  100. mochaTest:
  101. unit:
  102. src: ["test/UnitTests/js/#{grunt.option('feature') or '**'}/*.js"]
  103. options:
  104. reporter: grunt.option('reporter') or 'spec'
  105. grep: grunt.option("grep")
  106. smoke:
  107. src: ['test/smoke/js/**/*.js']
  108. options:
  109. reporter: grunt.option('reporter') or 'spec'
  110. grep: grunt.option("grep")
  111. availabletasks:
  112. tasks:
  113. options:
  114. filter: 'exclude',
  115. tasks: [
  116. 'coffee'
  117. 'less'
  118. 'clean'
  119. 'mochaTest'
  120. 'availabletasks'
  121. 'wrap_sharejs'
  122. 'requirejs'
  123. 'execute'
  124. 'bunyan'
  125. ]
  126. groups:
  127. "Compile tasks": [
  128. "compile:server"
  129. "compile:client"
  130. "compile:tests"
  131. "compile"
  132. "compile:unit_tests"
  133. "compile:smoke_tests"
  134. "compile:css"
  135. "compile:minify"
  136. "install"
  137. ]
  138. "Test tasks": [
  139. "test:unit"
  140. ]
  141. "Run tasks": [
  142. "run"
  143. "default"
  144. ]
  145. "Misc": [
  146. "help"
  147. ]
  148. moduleCompileServerTasks = []
  149. moduleCompileUnitTestTasks = []
  150. moduleUnitTestTasks = []
  151. moduleCompileClientTasks = []
  152. moduleIdeClientSideIncludes = []
  153. moduleMainClientSideIncludes = []
  154. if fs.existsSync "./modules"
  155. for module in fs.readdirSync "./modules"
  156. if fs.existsSync "./modules/#{module}/index.coffee"
  157. config.coffee["module_#{module}_server"] = {
  158. expand: true,
  159. flatten: false,
  160. cwd: "modules/#{module}/app/coffee",
  161. src: ['**/*.coffee'],
  162. dest: "modules/#{module}/app/js",
  163. ext: '.js'
  164. }
  165. config.coffee["module_#{module}_index"] = {
  166. src: "modules/#{module}/index.coffee",
  167. dest: "modules/#{module}/index.js"
  168. }
  169. moduleCompileServerTasks.push "coffee:module_#{module}_server"
  170. moduleCompileServerTasks.push "coffee:module_#{module}_index"
  171. config.coffee["module_#{module}_unit_tests"] = {
  172. expand: true,
  173. flatten: false,
  174. cwd: "modules/#{module}/test/unit/coffee",
  175. src: ['**/*.coffee'],
  176. dest: "modules/#{module}/test/unit/js",
  177. ext: '.js'
  178. }
  179. config.mochaTest["module_#{module}_unit"] = {
  180. src: ["modules/#{module}/test/unit/js/*.js"]
  181. options:
  182. reporter: grunt.option('reporter') or 'spec'
  183. grep: grunt.option("grep")
  184. }
  185. moduleCompileUnitTestTasks.push "coffee:module_#{module}_unit_tests"
  186. moduleUnitTestTasks.push "mochaTest:module_#{module}_unit"
  187. if fs.existsSync "./modules/#{module}/public/coffee/ide/index.coffee"
  188. config.coffee["module_#{module}_client_ide"] = {
  189. expand: true,
  190. flatten: false,
  191. cwd: "modules/#{module}/public/coffee/ide",
  192. src: ['**/*.coffee'],
  193. dest: "public/js/ide/#{module}",
  194. ext: '.js'
  195. }
  196. moduleCompileClientTasks.push "coffee:module_#{module}_client_ide"
  197. moduleIdeClientSideIncludes.push "ide/#{module}/index"
  198. if fs.existsSync "./modules/#{module}/public/coffee/main/index.coffee"
  199. config.coffee["module_#{module}_client_main"] = {
  200. expand: true,
  201. flatten: false,
  202. cwd: "modules/#{module}/public/coffee/main",
  203. src: ['**/*.coffee'],
  204. dest: "public/js/main/#{module}",
  205. ext: '.js'
  206. }
  207. moduleCompileClientTasks.push "coffee:module_#{module}_client_main"
  208. moduleMainClientSideIncludes.push "main/#{module}/index"
  209. grunt.initConfig config
  210. grunt.registerTask 'wrap_sharejs', 'Wrap the compiled ShareJS code for AMD module loading', () ->
  211. content = fs.readFileSync "public/js/libs/sharejs.js"
  212. fs.writeFileSync "public/js/libs/sharejs.js", """
  213. define(["ace/ace"], function() {
  214. #{content}
  215. return window.sharejs;
  216. });
  217. """
  218. grunt.registerTask 'help', 'Display this help list', 'availabletasks'
  219. grunt.registerTask 'compile:modules:server', 'Compile all the modules', moduleCompileServerTasks
  220. grunt.registerTask 'compile:modules:unit_tests', 'Compile all the modules unit tests', moduleCompileUnitTestTasks
  221. grunt.registerTask 'compile:modules:client', 'Compile all the module client side code', moduleCompileClientTasks
  222. grunt.registerTask 'compile:modules:inject_clientside_includes', () ->
  223. content = fs.readFileSync("public/js/ide.js").toString()
  224. content = content.replace(/, "__IDE_CLIENTSIDE_INCLUDES__"/g, moduleIdeClientSideIncludes.map((i) -> ", \"#{i}\"").join(""))
  225. fs.writeFileSync "public/js/ide.js", content
  226. content = fs.readFileSync("public/js/main.js").toString()
  227. content = content.replace(/, "__MAIN_CLIENTSIDE_INCLUDES__"/g, moduleMainClientSideIncludes.map((i) -> ", \"#{i}\"").join(""))
  228. fs.writeFileSync "public/js/main.js", content
  229. grunt.registerTask 'compile:server', 'Compile the server side coffee script', ['clean:app', 'coffee:app', 'coffee:app_dir', 'compile:modules:server']
  230. grunt.registerTask 'compile:client', 'Compile the client side coffee script', ['coffee:client', 'coffee:sharejs', 'wrap_sharejs', "compile:modules:client", 'compile:modules:inject_clientside_includes']
  231. grunt.registerTask 'compile:css', 'Compile the less files to css', ['less']
  232. grunt.registerTask 'compile:minify', 'Concat and minify the client side js', ['requirejs']
  233. grunt.registerTask 'compile:unit_tests', 'Compile the unit tests', ['clean:unit_tests', 'coffee:unit_tests']
  234. grunt.registerTask 'compile:smoke_tests', 'Compile the smoke tests', ['coffee:smoke_tests']
  235. grunt.registerTask 'compile:tests', 'Compile all the tests', ['compile:smoke_tests', 'compile:unit_tests']
  236. grunt.registerTask 'compile', 'Compiles everything need to run web-sharelatex', ['compile:server', 'compile:client', 'compile:css']
  237. grunt.registerTask 'install', "Compile everything when installing as an npm module", ['compile']
  238. grunt.registerTask 'test:unit', 'Run the unit tests (use --grep=<regex> or --feature=<feature> for individual tests)', ['compile:server', 'compile:unit_tests', 'mochaTest:unit']
  239. grunt.registerTask 'test:smoke', 'Run the smoke tests', ['compile:smoke_tests', 'mochaTest:smoke']
  240. grunt.registerTask 'test:modules:unit', 'Run the unit tests for the modules', ['compile:modules:server', 'compile:modules:unit_tests'].concat(moduleUnitTestTasks)
  241. grunt.registerTask 'run', "Compile and run the web-sharelatex server", ['compile', 'bunyan', 'execute']
  242. grunt.registerTask 'default', 'run'