forked from swagger-api/swagger-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cakefile
132 lines (114 loc) · 4.69 KB
/
Cakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
fs = require 'fs'
path = require 'path'
{exec} = require 'child_process'
less = require 'less'
sourceFiles = [
'SwaggerUi'
'view/HeaderView'
'view/MainView'
'view/ResourceView'
'view/OperationView'
'view/StatusCodeView'
'view/ParameterView'
'view/SignatureView'
'view/ContentTypeView'
'view/ResponseContentTypeView'
'view/ParameterContentTypeView'
]
task 'clean', 'Removes distribution', ->
console.log 'Clearing dist...'
exec 'rm -rf dist'
task 'dist', 'Build a distribution', ->
console.log "Build distribution in ./dist"
fs.mkdirSync('dist') if not path.existsSync('dist')
fs.mkdirSync('dist/lib') if not path.existsSync('dist/lib')
appContents = new Array remaining = sourceFiles.length
for file, index in sourceFiles then do (file, index) ->
console.log " : Reading src/main/coffeescript/#{file}.coffee"
fs.readFile "src/main/coffeescript/#{file}.coffee", 'utf8', (err, fileContents) ->
throw err if err
appContents[index] = fileContents
precompileTemplates() if --remaining is 0
precompileTemplates= ->
console.log ' : Precompiling templates...'
templateFiles = fs.readdirSync('src/main/template')
templateContents = new Array remaining = templateFiles.length
for file, index in templateFiles then do (file, index) ->
console.log " : Compiling src/main/template/#{file}"
exec "handlebars src/main/template/#{file} -f dist/_#{file}.js", (err, stdout, stderr) ->
throw err if err
fs.readFile 'dist/_' + file + '.js', 'utf8', (err, fileContents) ->
throw err if err
templateContents[index] = fileContents
fs.unlink 'dist/_' + file + '.js'
if --remaining is 0
templateContents.push '\n\n'
fs.writeFile 'dist/_swagger-ui-templates.js', templateContents.join('\n\n'), 'utf8', (err) ->
throw err if err
build()
build = ->
console.log ' : Collecting Coffeescript source...'
appContents.push '\n\n'
fs.writeFile 'dist/_swagger-ui.coffee', appContents.join('\n\n'), 'utf8', (err) ->
throw err if err
console.log ' : Compiling...'
exec 'coffee --compile dist/_swagger-ui.coffee', (err, stdout, stderr) ->
throw err if err
fs.unlink 'dist/_swagger-ui.coffee'
console.log ' : Combining with javascript...'
exec 'cat src/main/javascript/doc.js dist/_swagger-ui-templates.js dist/_swagger-ui.js > dist/swagger-ui.js', (err, stdout, stderr) ->
throw err if err
fs.unlink 'dist/_swagger-ui.js'
fs.unlink 'dist/_swagger-ui-templates.js'
console.log ' : Minifying all...'
exec 'java -jar "./bin/yuicompressor-2.4.7.jar" --type js -o ' + 'dist/swagger-ui.min.js ' + 'dist/swagger-ui.js', (err, stdout, stderr) ->
throw err if err
lessc()
lessc = ->
# Someone who knows CoffeeScript should make this more Coffee-licious
console.log ' : Compiling LESS...'
less.render fs.readFileSync("src/main/less/screen.less", 'utf8'), (err, css) ->
console.log err
fs.writeFileSync("src/main/html/css/screen.css", css)
pack()
pack = ->
console.log ' : Packaging...'
exec 'cp -r lib dist'
console.log ' : Copied swagger-ui libs'
exec 'cp -r node_modules/swagger-client/lib/swagger.js dist/lib'
console.log ' : Copied swagger dependencies'
exec 'cp -r src/main/html/* dist'
console.log ' : Copied html dependencies'
console.log ' !'
task 'spec', "Run the test suite", ->
exec "open spec.html", (err, stdout, stderr) ->
throw err if err
task 'watch', 'Watch source files for changes and autocompile', ->
# Function which watches all files in the passed directory
watchFiles = (dir) ->
files = fs.readdirSync(dir)
for file, index in files then do (file, index) ->
console.log " : " + dir + "/#{file}"
fs.watchFile dir + "/#{file}", (curr, prev) ->
if +curr.mtime isnt +prev.mtime
invoke 'dist'
notify "Watching source files for changes..."
# Watch specific source files
for file, index in sourceFiles then do (file, index) ->
console.log " : " + "src/main/coffeescript/#{file}.coffee"
fs.watchFile "src/main/coffeescript/#{file}.coffee", (curr, prev) ->
if +curr.mtime isnt +prev.mtime
invoke 'dist'
# watch all files in these folders
watchFiles("src/main/template")
watchFiles("src/main/javascript")
watchFiles("src/main/html")
watchFiles("src/main/less")
watchFiles("src/test")
notify = (message) ->
return unless message?
console.log message
# options =
# title: 'CoffeeScript'
# image: 'bin/CoffeeScript.png'
# try require('growl') message, options