Gruntfile.coffee 9.3 KB

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