-
Notifications
You must be signed in to change notification settings - Fork 24
/
Cakefile
141 lines (123 loc) · 5.38 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
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
uglifyJS = require("uglify-js")
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
console.log(fileName)
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 = uglifyJS.minify(jsFullPath).code
htmlFileString = htmlFileString.replace(r2Output[0], "\n<script type=\"text/javascript\">\n#{jsFileString}\n")
# 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('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('../node_modules/.bin/coffeedoc', ['-o', '../docs'].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('upstream', 'Update local repository from upstream. \n' +
' Define upstream with `git remote add upstream git://github.com/RallySoftware/rally_analytics.git`', () ->
process.chdir(__dirname)
run('git', ['fetch', 'upstream'], () ->
run('git merge upstream/master')
)
)
# 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']
)