forked from markmsmith-work/rally_analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCakefile
207 lines (182 loc) · 7.02 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
fs = require('fs')
path = require('path')
{print} = require('sys')
{spawn, exec} = require('child_process')
fs = require('fs')
path = require('path')
jsp = require("uglify-js").parser
pro = require("uglify-js").uglify
deployHTMLFromDirectory = (directory, uglify = true) ->
contents = fs.readdirSync(directory)
placeholders = {}
files = ("#{file}" for file in contents when (file.indexOf('.html') > 0))
deployDirectory = path.join(__dirname, 'deploy')
unless path.existsSync(deployDirectory)
fs.mkdirSync(deployDirectory, '755')
for fileName in files
fullPath = path.join(directory, fileName)
htmlFileString = fs.readFileSync(fullPath, 'utf-8')
rString = '<script +type="text/javascript" +src="([-_a-z0-9A-Z/\.]+)"( +deploy_src="([-_:a-z0-9A-Z/\.]+)" *>)'
r = new RegExp(rString)
rOutput = r.exec(htmlFileString)
while rOutput?
htmlFileString = htmlFileString.replace(rOutput[0], "<script type=\"text/javascript\" src=\"#{rOutput[3]}\">")
rOutput = r.exec(htmlFileString)
r2String = '<script +type="text/javascript" +src="([-_a-z0-9A-Z/\.]+)" *>'
r2 = new RegExp(r2String)
r2Output = r2.exec(htmlFileString)
while r2Output?
jsFileName = r2Output[1]
jsFullPath = path.join(directory, jsFileName)
if path.existsSync(jsFullPath)
jsFileString = fs.readFileSync(jsFullPath, 'utf-8')
if jsFileString?
if uglify
ast = jsp.parse(jsFileString)
ast = pro.ast_mangle(ast)
ast = pro.ast_squeeze(ast)
jsFileString = pro.gen_code(ast)
htmlFileString = htmlFileString.replace(r2Output[0], "\n<script type=\"text/javascript\">\n#{jsFileString}\n")
else
console.log("\nWARNING: Could not find local file #{jsFileName} referenced from #{fileName}. \n Your deploy file may still work if it's optional or if the file can be found relative to the web address.")
key = Math.floor(Math.random() * 9999999999)
placeholders[key] = r2Output[0]
htmlFileString = htmlFileString.replace(r2Output[0], "***PLACEHOLDER" + key + 'PLACEHOLDER***')
r2Output = r2.exec(htmlFileString)
# get rid of placeholder stuff
for key, value of placeholders
htmlFileString = htmlFileString.replace("***PLACEHOLDER" + key + 'PLACEHOLDER***', value)
basename = path.basename(fileName, '.html')
if uglify
outputFileFullPath = path.join(deployDirectory, basename + '-min.html')
else
outputFileFullPath = path.join(deployDirectory, fileName)
fs.writeFileSync(outputFileFullPath, htmlFileString)
task('deploy', 'Combine local .js with .html from ./src and ./examples; then output to ./deploy', () ->
invoke('compile')
deployHTMLFromDirectory(path.join(__dirname, 'src'), false)
deployHTMLFromDirectory(path.join(__dirname, 'examples'), false)
deployHTMLFromDirectory(path.join(__dirname, 'src'))
deployHTMLFromDirectory(path.join(__dirname, 'examples'))
)
run = (command, options, next) ->
if options? and options.length > 0
command += ' ' + options.join(' ')
exec(command, (error, stdout, stderr) ->
if stderr.length > 0
console.log("Stderr exec'ing command '#{command}'...\n" + stderr)
if error?
console.log('exec error: ' + error)
if next?
next(stdout)
else
if stdout.length > 0
console.log("Stdout exec'ing command '#{command}'...\n" + stdout)
)
task('compile', 'Compile CS to JS and place in ./lib. Good for development.', () ->
options = ['-c', '-o', 'lib', 'src']
run('coffee', options)
)
task('watch', 'Recompile CoffeeScript source files when modified and place in ./lib *\n' +
' * Actually, it is best to just run `jitter src lib` from the command line', () ->
options = ['src', 'lib']
run('jitter', options)
# jitter = spawn('jitter', options)
# jitter.stdout.on('data', (data) ->
# console.log(data)
# )
# jitter.stderr.on('data', (data) ->
# console.log(data)
# )
# jitter.stdout.on('exit', (code) ->
# if code != 0
# console.log('Failed: ' + code)
# )
)
task('docs', 'Generate docs with CoffeeDoc and place in ./docs', () ->
fs.readdir('src', (err, contents) ->
files = ("#{file}" for file in contents when (file.indexOf('.coffee') > 0))
# Make sure the file with the same name as the project (directory) is at the beginning
projectCoffeeFile = path.basename(__dirname) + '.coffee'
srcPlus = "#{projectCoffeeFile}"
position = files.indexOf(srcPlus)
if position > 0
files = [srcPlus].concat(files[0..position-1], files[position+1..files.length-1])
process.chdir(__dirname + '/src')
run('coffeedoc', ['-o', '../docs', '--readme', '-r', '../README.md'].concat(files))
process.chdir(__dirname)
run('coffeedoctest', ['--readme', '--requirepath', 'src', 'src'])
)
)
task('pub-docs', 'Push master to gh-pages on github', () ->
process.chdir(__dirname)
run('git push -f origin master:gh-pages')
)
# task('install', 'Install globally but from this source using npm', () ->
# process.chdir(__dirname)
# run('npm install -g .')
# )
# task('publish', 'Publish to npm', () ->
# process.chdir(__dirname)
# run('npm publish .')
# )
task('test', 'Run the test suite with nodeunit', () ->
console.log(__dirname)
{reporters} = require 'nodeunit'
process.chdir __dirname
reporters.default.run ['test']
)
option('-n', '--name [NAME]', 'name of project to create')
task('create', 'Creates a new directory with the recommended structure', (options) ->
name = options.name
console.log(name)
fs.mkdir(name)
fs.mkdir(name + '/src')
fs.mkdir(name + '/test')
fs.mkdir(name + '/node_modules')
fs.readdir(name, (err, files) ->
if 'package.json' in files
console.log('package.json already exists')
else
run('system_profiler SPSoftwareDataType', [], (stdout) ->
matcher = /User Name: ([\w\s]+) \(/
groups = matcher.exec(stdout)
packageJSON = """{
"name": "#{name}",
"description": "",
"version": "0.1.0",
"author": "#{groups[1]}",
"dependencies": {},
"devDependencies": {}
}"""
fs.writeFileSync(name + '/package.json', packageJSON)
)
if 'Cakefile' in files
console.log('Cakefile already exists')
else
fs.linkSync('Cakefile', name + '/Cakefile')
if '.gitignore' in files
console.log('.gitignore already exists')
else
gitignore = """
coffeedoctest_temp
.DS_Store
"""
fs.writeFileSync(name + '/.gitignore', gitignore)
if 'index.html' in files
console.log('index.html already exists')
else
indexHtml = """
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Refresh" content="0; url=http:docs/index.html" />
</head>
<body>
<p>Please follow <a href="http:docs/index.html">this link</a>.</p>
</body>
</html>
"""
fs.writeFileSync(name + '/index.html', indexHtml)
)
)