You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

118 rivejä
4.2 KiB

  1. fs = require 'fs'
  2. path = require 'path'
  3. {exec} = require 'child_process'
  4. sourceFiles = [
  5. 'SwaggerUi'
  6. 'view/HeaderView'
  7. 'view/MainView'
  8. 'view/ResourceView'
  9. 'view/OperationView'
  10. 'view/StatusCodeView'
  11. 'view/ParameterView'
  12. 'view/SignatureView'
  13. 'view/ContentTypeView'
  14. 'view/ResponseContentTypeView'
  15. 'view/ParameterContentTypeView'
  16. ]
  17. task 'clean', 'Removes distribution', ->
  18. console.log 'Clearing dist...'
  19. exec 'rm -rf dist'
  20. task 'dist', 'Build a distribution', ->
  21. console.log "Build distribution in ./dist"
  22. fs.mkdirSync('dist') if not path.existsSync('dist')
  23. fs.mkdirSync('dist/lib') if not path.existsSync('dist/lib')
  24. appContents = new Array remaining = sourceFiles.length
  25. for file, index in sourceFiles then do (file, index) ->
  26. console.log " : Reading src/main/coffeescript/#{file}.coffee"
  27. fs.readFile "src/main/coffeescript/#{file}.coffee", 'utf8', (err, fileContents) ->
  28. throw err if err
  29. appContents[index] = fileContents
  30. precompileTemplates() if --remaining is 0
  31. precompileTemplates= ->
  32. console.log ' : Precompiling templates...'
  33. templateFiles = fs.readdirSync('src/main/template')
  34. templateContents = new Array remaining = templateFiles.length
  35. for file, index in templateFiles then do (file, index) ->
  36. console.log " : Compiling src/main/template/#{file}"
  37. exec "handlebars src/main/template/#{file} -f dist/_#{file}.js", (err, stdout, stderr) ->
  38. throw err if err
  39. fs.readFile 'dist/_' + file + '.js', 'utf8', (err, fileContents) ->
  40. throw err if err
  41. templateContents[index] = fileContents
  42. fs.unlink 'dist/_' + file + '.js'
  43. if --remaining is 0
  44. templateContents.push '\n\n'
  45. fs.writeFile 'dist/_swagger-ui-templates.js', templateContents.join('\n\n'), 'utf8', (err) ->
  46. throw err if err
  47. build()
  48. build = ->
  49. console.log ' : Collecting Coffeescript source...'
  50. appContents.push '\n\n'
  51. fs.writeFile 'dist/_swagger-ui.coffee', appContents.join('\n\n'), 'utf8', (err) ->
  52. throw err if err
  53. console.log ' : Compiling...'
  54. exec 'coffee --compile dist/_swagger-ui.coffee', (err, stdout, stderr) ->
  55. throw err if err
  56. fs.unlink 'dist/_swagger-ui.coffee'
  57. console.log ' : Combining with javascript...'
  58. exec 'cat src/main/javascript/doc.js dist/_swagger-ui-templates.js dist/_swagger-ui.js > dist/swagger-ui.js', (err, stdout, stderr) ->
  59. throw err if err
  60. fs.unlink 'dist/_swagger-ui.js'
  61. fs.unlink 'dist/_swagger-ui-templates.js'
  62. console.log ' : Minifying all...'
  63. exec 'java -jar "./bin/yuicompressor-2.4.7.jar" --type js -o ' + 'dist/swagger-ui.min.js ' + 'dist/swagger-ui.js', (err, stdout, stderr) ->
  64. throw err if err
  65. pack()
  66. pack = ->
  67. console.log ' : Packaging...'
  68. exec 'cp -r lib dist'
  69. exec 'cp -r node_modules/swagger-client/lib/swagger.js dist/lib'
  70. exec 'cp -r src/main/html/* dist'
  71. console.log ' !'
  72. task 'spec', "Run the test suite", ->
  73. exec "open spec.html", (err, stdout, stderr) ->
  74. throw err if err
  75. task 'watch', 'Watch source files for changes and autocompile', ->
  76. # Function which watches all files in the passed directory
  77. watchFiles = (dir) ->
  78. files = fs.readdirSync(dir)
  79. for file, index in files then do (file, index) ->
  80. console.log " : " + dir + "/#{file}"
  81. fs.watchFile dir + "/#{file}", (curr, prev) ->
  82. if +curr.mtime isnt +prev.mtime
  83. invoke 'dist'
  84. notify "Watching source files for changes..."
  85. # Watch specific source files
  86. for file, index in sourceFiles then do (file, index) ->
  87. console.log " : " + "src/main/coffeescript/#{file}.coffee"
  88. fs.watchFile "src/main/coffeescript/#{file}.coffee", (curr, prev) ->
  89. if +curr.mtime isnt +prev.mtime
  90. invoke 'dist'
  91. # watch all files in these folders
  92. watchFiles("src/main/template")
  93. watchFiles("src/main/javascript")
  94. watchFiles("src/main/html")
  95. watchFiles("src/test")
  96. notify = (message) ->
  97. return unless message?
  98. console.log message
  99. # options =
  100. # title: 'CoffeeScript'
  101. # image: 'bin/CoffeeScript.png'
  102. # try require('growl') message, options