Gruntfile.coffee 13 KB

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