Gruntfile.coffee 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. fs = require "fs"
  2. spawn = require("child_process").spawn
  3. rimraf = require "rimraf"
  4. Path = require "path"
  5. SERVICES = [{
  6. name: "web"
  7. repo: "git@github.com:sharelatex/web-sharelatex.git"
  8. }, {
  9. name: "document-updater"
  10. repo: "git@github.com:sharelatex/document-updater-sharelatex.git"
  11. }, {
  12. name: "clsi"
  13. repo: "git@github.com:sharelatex/clsi-sharelatex.git"
  14. }]
  15. module.exports = (grunt) ->
  16. grunt.loadNpmTasks 'grunt-bunyan'
  17. grunt.loadNpmTasks 'grunt-execute'
  18. grunt.loadNpmTasks 'grunt-available-tasks'
  19. grunt.loadNpmTasks 'grunt-concurrent'
  20. execute = {}
  21. for service in SERVICES
  22. execute[service.name] =
  23. src: "#{service.name}/app.js"
  24. grunt.initConfig
  25. execute: execute
  26. concurrent:
  27. all:
  28. tasks: ("run:#{service.name}" for service in SERVICES)
  29. options:
  30. limit: SERVICES.length
  31. logConcurrentOutput: true
  32. availabletasks:
  33. tasks:
  34. options:
  35. filter: 'exclude',
  36. tasks: [
  37. 'concurrent'
  38. 'execute'
  39. 'bunyan'
  40. 'availabletasks'
  41. ]
  42. groups:
  43. "Run tasks": [
  44. "run"
  45. "run:all"
  46. "default"
  47. ].concat ("run:#{service.name}" for service in SERVICES)
  48. "Misc": [
  49. "help"
  50. ]
  51. "Install tasks": ("install:#{service.name}" for service in SERVICES).concat(["install:all", "install"])
  52. "Update tasks": ("update:#{service.name}" for service in SERVICES).concat(["update:all", "update"])
  53. "Config tasks": ["install:config"]
  54. for service in SERVICES
  55. do (service) ->
  56. grunt.registerTask "install:#{service.name}", "Download and set up the #{service.name} service", () ->
  57. done = @async()
  58. Helpers.installService(service.repo, service.name, done)
  59. grunt.registerTask "update:#{service.name}", "Checkout and update the #{service.name} service", () ->
  60. done = @async()
  61. Helpers.updateService(service.name, done)
  62. grunt.registerTask "run:#{service.name}", "Run the ShareLaTeX #{service.name} service", ["bunyan", "execute:#{service.name}"]
  63. grunt.registerTask 'install:all', "Download and set up all ShareLaTeX services", ("install:#{service.name}" for service in SERVICES)
  64. grunt.registerTask 'install', 'install:all'
  65. grunt.registerTask 'update:all', "Checkout and update all ShareLaTeX services", ("update:#{service.name}" for service in SERVICES)
  66. grunt.registerTask 'update', 'update:all'
  67. grunt.registerTask 'run', "Run all of the sharelatex processes", ['concurrent:all']
  68. grunt.registerTask 'run:all', 'run'
  69. grunt.registerTask 'install:config', "Install a custom config from a git repository (set SHARELATEX_CONFIG_REPO to the repository location)", () ->
  70. Helpers.installCustomConfig @async()
  71. grunt.registerTask 'help', 'Display this help list', 'availabletasks'
  72. grunt.registerTask 'default', 'run'
  73. Helpers =
  74. installService: (repo_src, dir, callback = (error) ->) ->
  75. Helpers.cloneGitRepo repo_src, dir, (error) ->
  76. return callback(error) if error?
  77. Helpers.installNpmModules dir, (error) ->
  78. return callback(error) if error?
  79. Helpers.runGruntInstall dir, (error) ->
  80. return callback(error) if error?
  81. callback()
  82. updateService: (dir, callback = (error) ->) ->
  83. Helpers.updateGitRepo dir, (error) ->
  84. return callback(error) if error?
  85. Helpers.installNpmModules dir, (error) ->
  86. return callback(error) if error?
  87. Helpers.runGruntInstall dir, (error) ->
  88. return callback(error) if error?
  89. callback()
  90. cloneGitRepo: (repo_src, dir, callback = (error) ->) ->
  91. if !fs.existsSync(dir)
  92. proc = spawn "git", ["clone", repo_src, dir], stdio: "inherit"
  93. proc.on "close", () ->
  94. callback()
  95. else
  96. console.log "#{dir} already installed, skipping."
  97. callback()
  98. updateGitRepo: (dir, callback = (error) ->) ->
  99. proc = spawn "git", ["checkout", "master"], cwd: dir, stdio: "inherit"
  100. proc.on "close", () ->
  101. proc = spawn "git", ["pull"], cwd: dir, stdio: "inherit"
  102. proc.on "close", () ->
  103. callback()
  104. installNpmModules: (dir, callback = (error) ->) ->
  105. proc = spawn "npm", ["install"], stdio: "inherit", cwd: dir
  106. proc.on "close", () ->
  107. callback()
  108. runGruntInstall: (dir, callback = (error) ->) ->
  109. proc = spawn "grunt", ["install"], stdio: "inherit", cwd: dir
  110. proc.on "close", () ->
  111. callback()
  112. installCustomConfig: (callback = (error) ->) ->
  113. if !process.env.SHARELATEX_CONFIG_REPO?
  114. return callback(new Error("Please set the SHARELATEX_CONFIG_REPO enviroment variable to point to a git repository."))
  115. rimraf "config-local", (error) ->
  116. Helpers.cloneGitRepo process.env.SHARELATEX_CONFIG_REPO, "config-local", (error) ->
  117. return callback(error) if error?
  118. for file in fs.readdirSync("config-local")
  119. unless file == ".git"
  120. fs.symlinkSync(Path.resolve("config-local/#{file}"), "config/#{file}")
  121. callback()