Gruntfile.coffee 13 KB

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