Gruntfile.coffee 11 KB

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