forked from LinkedInAttic/inject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCakefile
158 lines (139 loc) · 4.92 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
# VERSION = "0.2.0"
PROJECT = "inject"
fs = require("fs")
exec = require("child_process").exec
path = require("path")
compiler = "java -jar ./build/gcc/compiler.jar --compilation_level SIMPLE_OPTIMIZATIONS"
# compilerInputFile = "--js ./artifacts/#{PROJECT}-#{VERSION}.js"
# compilerOutputFile = "--js_output_file ./artifacts/#{PROJECT}-#{VERSION}.min.js"
packages =
min:
name: "#{PROJECT}.min.js"
headers: [ "./src/copyright-lic-min.js" ]
files: [
"./src/inject.coffee"
]
copy: [ "./src/relay.html" ]
full:
uncompressed: true
name: "#{PROJECT}.js"
headers: [
"./src/copyright.js"
"./src/licenses.js"
]
files: [
"./src/inject.coffee"
]
copy: [ "./src/relay.html" ]
fileSources = {}
tmpdir = "./tmp"
artifacts = "./artifacts/dev"
task "build", "Build the Project", ->
readSrc = (file, cb) ->
if file.indexOf(".coffee") == -1
# read file into slot
console.log "Reading #{file}"
fs.readFile "#{file}", "utf8", (err, contents) ->
if err then return cb(err, null)
cb(null, contents)
compile = (file, cb) ->
exec "coffee --output #{tmpdir} --compile #{file}", (err, stdout, stderr) ->
if err then return cb(err, null)
jsName = path.basename(file.replace(/\.coffee$/, ".js"))
readSrc "#{tmpdir}/#{jsName}", (err, JScontents) ->
if err then return cb(err, null)
compress "#{tmpdir}/#{jsName}", (err, GCCcontents) ->
if err then return cb(err, null)
cb(null, [JScontents, GCCcontents])
compress = (file, cb) ->
if file.indexOf(tmpdir) isnt 0
err = new Error("Can only compress items in tempdir")
return cb(err, null)
exec "java -jar ./build/gcc/compiler.jar --compilation_level SIMPLE_OPTIMIZATIONS --js #{file} --js_output_file #{file}.gcc", (err, stdout, stderr) ->
if err then return cb(err, null)
gccName = path.basename("#{file}.gcc")
readSrc "#{tmpdir}/#{gccName}", (err, contents) ->
if err then return cb(err, null)
cb(null, contents)
copyArchive = {}
copy = (from, to, cb) ->
key = "#{from}::#{to}"
if copyArchive[key] then return cb(null, null)
copyArchive[key] = true
console.log "Copy #{from} => #{to}"
srcFile = fs.createReadStream("#{from}");
outFile = fs.createWriteStream("#{to}");
srcFile.once "open", () ->
require("util").pump srcFile, outFile, () ->
cb(null, null)
namespace = (file) ->
file = file.replace(/[^a-zA-Z0-9\.\_\-]/g, "_")
readInAllFiles = (allFiles, cb) ->
remaining = allFiles.length
innerCompile = (file) ->
# this is a coffeescript file
compile file, (err, contents) ->
if err then return cb(err, null)
fileSources[namespace(file)] = contents[1]
fileSources["!"+namespace(file)] = contents[0]
cb() if --remaining is 0
innerRead = (file) ->
# regular JS file
readSrc file, (err, contents) ->
if err then return cb(err, null)
fileSources[namespace(file)] = contents
cb() if --remaining is 0
for file in allFiles
if file.indexOf(".coffee") isnt -1
innerCompile(file)
if file.indexOf(".js") isnt -1
innerRead(file)
assemble = (pkg, cb) ->
contents = []
copies = pkg.copy.length
useUncompressed = pkg.uncompressed is true
for name in pkg.headers
content = if useUncompressed and fileSources["!"+namespace(name)] then fileSources["!"+namespace(name)] else fileSources[namespace(name)]
contents.push content
for name in pkg.files
content = if useUncompressed and fileSources["!"+namespace(name)] then fileSources["!"+namespace(name)] else fileSources[namespace(name)]
contents.push content
fs.writeFile "#{artifacts}/#{pkg.name}", contents.join("\n\n"), (err) ->
if err then return cb(err, null)
for item in pkg.copy
# fromItem = item.replace(/\{VERSION\}/, "")
fromItem = item
# .replace(/\{VERSION\}/, "-#{VERSION}")
toItem = item.replace(/^\.\/src\//, "")
copy fromItem, "#{artifacts}/#{toItem}", (err) ->
if err then return cb(err, null)
cb(null, null) if --copies is 0
cleanup = (cb) ->
exec "rm -rf #{tmpdir}", (err, stdout, stderr) ->
if err then return cb(err, null)
cb(null, null)
complete = () ->
console.log "Done!"
# main
files = []
fileCache = {}
pkgCount = 0
for pkg, contents of packages
pkgCount++
for file in contents.files
if !fileCache[file]
files.push(file)
fileCache[file] = true
for file in contents.headers
if !fileCache[file]
files.push(file)
fileCache[file] = true
readInAllFiles files, (err) ->
throw err if err
for pkg, contents of packages
assemble contents, (err) ->
throw err if err
if --pkgCount is 0
cleanup () ->
throw err if err
complete()