Created
July 2, 2014 11:37
-
-
Save Evgenus/c0bf68aa475d9007a83a to your computer and use it in GitHub Desktop.
Cakefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require("fs") | |
path = require("path") | |
coffee = require("coffee-script") | |
uglify = require("uglify-js") | |
mkdirpSync = require('mkdirp').sync | |
sass = require('node-sass') | |
walker = require('fs-walk-glob-rules') | |
{exec} = require('child_process') | |
minify = (source) -> | |
ast = uglify.parse(source) | |
ast.figure_out_scope() | |
compressor = uglify.Compressor | |
sequences : true | |
properties : true | |
dead_code : true | |
drop_debugger : true | |
unsafe : false | |
conditionals : true | |
comparisons : true | |
evaluate : true | |
booleans : true | |
loops : true | |
unused : true | |
hoist_funs : true | |
hoist_vars : false | |
if_return : true | |
join_vars : true | |
cascade : true | |
side_effects : true | |
warnings : false | |
negate_iife : false | |
global_defs : {} | |
compressed = ast.transform(compressor) | |
minified = compressed.print_to_string() | |
return minified | |
minify_more = (source) -> | |
ast = uglify.parse(source) | |
ast.figure_out_scope() | |
ast.compute_char_frequency() | |
ast.mangle_names() | |
compressor = uglify.Compressor | |
warnings : false | |
negate_iife : false | |
global_defs: {} | |
compressed = ast.transform(compressor) | |
minified = compressed.print_to_string | |
bracketize : true | |
comments : /License/ | |
return minified | |
write_file = (relative, data) -> | |
outpath = path.join(".", relative) | |
mkdirpSync(path.dirname(outpath)) | |
fs.writeFileSync(outpath, data) | |
transform = (source, destination, func, excludes) -> | |
rules = {} | |
rules[source] = destination | |
walked = walker.transformSync | |
root: "." | |
rules: rules | |
excludes: [ | |
"./node_modules/**", | |
"./.git/**" | |
] | |
for data in walked | |
content = fs.readFileSync(data.path, encoding: "utf8") | |
result = func(data.relative, data.result, content, data.match) | |
if destination? and result? | |
write_file(data.result, result) | |
task "compile-builder", "compile builder coffee source into javascript", -> | |
console.log("Compiling builder") | |
transform "./src/builder/(**/*).coffee", "./lib/$1.js", (input, output, data) -> | |
console.log("Compiling %s --> %s", input, output) | |
return coffee.compile(data, bare: true) | |
task "compile-loader", "compile loader coffee source into javascript", -> | |
console.log("Compiling loader") | |
transform "./src/loader/(**/*).coffee", "./lib/assets/$1.js", (input, output, data) -> | |
console.log("Compiling %s --> Minifying --> %s", input, output) | |
result = coffee.compile(data, bare: true) | |
return minify(result) | |
parts = [ # order of this files is important | |
"./src/bootstrap/ui.coffee", | |
"./src/bootstrap/app.coffee", | |
] | |
out = [] | |
for part in parts | |
transform part, null, (input, _, data) -> | |
console.log("Compiling %s -->", input) | |
result = coffee.compile(data, bare: true) | |
out.push(result) | |
return | |
bootstrap_path = "./lib/assets/bootstrap.js" | |
bootstrap_data = out.join("") | |
console.log(" --> Combining %s", bootstrap_path) | |
write_file(bootstrap_path, bootstrap_data) | |
task "populate-assets", "prepare assets to be used by builder", -> | |
transform "./src/cryptojs/encoder.coffee", null, (input, _, data) -> | |
encoder = coffee.compile(data, bare: true) | |
console.log("Compiling %s -->", input) | |
transform "./bower_components/cryptojslib/rollups/(md5|sha1|sha224|sha256|sha3|sha384|sha512|ripemd160).js", "./lib/assets/hash/$1.js", (input, output, data, match) -> | |
console.log(" Combining %s --> %s", input, output) | |
hash_name = match[1] | |
return minify_more(""" | |
(function() { | |
#{data}; | |
var encoder = #{encoder} | |
return (function(data) { | |
var hash = CryptoJS.algo["#{hash_name.toUpperCase()}"].create(); | |
hash.update(encoder(data)); | |
return hash.finalize().toString(CryptoJS.enc.Hex); | |
}); | |
})();""") | |
transform "./src/cryptojs/encoder.coffee", null, (input, _, data) -> | |
encoder = coffee.compile(data, bare: true) | |
console.log("Compiling %s -->", input) | |
transform "./src/cryptojs/decoder.coffee", null, (input, _, data) -> | |
decoder = coffee.compile(data, bare: true) | |
console.log("Compiling %s -->", input) | |
transform "./bower_components/cryptojslib/components/core.js", "./lib/assets/encoding/identity.js", (input, output, data) -> | |
console.log(" Combining --> %s", "./lib/assets/cypher/identity.js") | |
return minify_more(""" | |
(function() { | |
#{data}; | |
var encoder = #{encoder} | |
var decoder = #{decoder} | |
return (function(data, password) { | |
return decoder(encoder(data)); | |
}); | |
})();""") | |
#28 ISSUE. Here you could build additional decoders for compression or encryption | |
transform "./bower_components/localforage/dist/(localforage).min.js", "./lib/assets/$1.js", (input, output, data) -> | |
console.log("Copying %s --> %s", input, output) | |
return data | |
transform "./src/builder/index.tmpl", "lib/assets/index.tmpl", (input, output, data) -> | |
console.log("Copying %s --> %s", input, output) | |
return data | |
transform "./src/builder/appcache.tmpl", "lib/assets/appcache.tmpl", (input, output, data) -> | |
console.log("Copying %s --> %s", input, output) | |
return data | |
transform "./src/bootstrap/style.scss", "lib/assets/bootstrap.css", (input, output, data) -> | |
console.log("Compiling %s --> %s", input, output) | |
sass.renderFile | |
file: path.join('.', input) | |
outputStyle: 'compressed' | |
outFile: output | |
success: (x) -> console.log("Success", x) | |
error: console.log.bind(console) | |
return | |
task "test", "run unittests", -> | |
cmd = ["npm", "run", "test:unit:short"].join(" ") | |
console.log(cmd) | |
exec cmd, (err, stdout, stderr) -> | |
console.log stdout + stderr | |
task "build", "compile all coffeescript files to javascript", -> | |
invoke 'compile-builder' | |
invoke 'compile-loader' | |
invoke 'populate-assets' | |
task "sbuild", "build routine for sublime", -> | |
invoke 'build' | |
invoke 'test' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var coffee, exec, fs, minify, minify_more, mkdirpSync, path, sass, transform, uglify, walker, write_file; | |
fs = require("fs"); | |
path = require("path"); | |
coffee = require("coffee-script"); | |
uglify = require("uglify-js"); | |
mkdirpSync = require('mkdirp').sync; | |
sass = require('node-sass'); | |
walker = require('fs-walk-glob-rules'); | |
exec = require('child_process').exec; | |
minify = function(source) { | |
var ast, compressed, compressor, minified; | |
ast = uglify.parse(source); | |
ast.figure_out_scope(); | |
compressor = uglify.Compressor({ | |
sequences: true, | |
properties: true, | |
dead_code: true, | |
drop_debugger: true, | |
unsafe: false, | |
conditionals: true, | |
comparisons: true, | |
evaluate: true, | |
booleans: true, | |
loops: true, | |
unused: true, | |
hoist_funs: true, | |
hoist_vars: false, | |
if_return: true, | |
join_vars: true, | |
cascade: true, | |
side_effects: true, | |
warnings: false, | |
negate_iife: false, | |
global_defs: {} | |
}); | |
compressed = ast.transform(compressor); | |
minified = compressed.print_to_string(); | |
return minified; | |
}; | |
minify_more = function(source) { | |
var ast, compressed, compressor, minified; | |
ast = uglify.parse(source); | |
ast.figure_out_scope(); | |
ast.compute_char_frequency(); | |
ast.mangle_names(); | |
compressor = uglify.Compressor({ | |
warnings: false, | |
negate_iife: false, | |
global_defs: {} | |
}); | |
compressed = ast.transform(compressor); | |
minified = compressed.print_to_string({ | |
bracketize: true, | |
comments: /License/ | |
}); | |
return minified; | |
}; | |
write_file = function(relative, data) { | |
var outpath; | |
outpath = path.join(".", relative); | |
mkdirpSync(path.dirname(outpath)); | |
return fs.writeFileSync(outpath, data); | |
}; | |
transform = function(source, destination, func, excludes) { | |
var content, data, result, rules, walked, _i, _len, _results; | |
rules = {}; | |
rules[source] = destination; | |
walked = walker.transformSync({ | |
root: ".", | |
rules: rules, | |
excludes: ["./node_modules/**", "./.git/**"] | |
}); | |
_results = []; | |
for (_i = 0, _len = walked.length; _i < _len; _i++) { | |
data = walked[_i]; | |
content = fs.readFileSync(data.path, { | |
encoding: "utf8" | |
}); | |
result = func(data.relative, data.result, content, data.match); | |
if ((destination != null) && (result != null)) { | |
_results.push(write_file(data.result, result)); | |
} else { | |
_results.push(void 0); | |
} | |
} | |
return _results; | |
}; | |
task("compile-builder", "compile builder coffee source into javascript", function() { | |
console.log("Compiling builder"); | |
return transform("./src/builder/(**/*).coffee", "./lib/$1.js", function(input, output, data) { | |
console.log("Compiling %s --> %s", input, output); | |
return coffee.compile(data, { | |
bare: true | |
}); | |
}); | |
}); | |
task("compile-loader", "compile loader coffee source into javascript", function() { | |
var bootstrap_data, bootstrap_path, out, part, parts, _i, _len; | |
console.log("Compiling loader"); | |
transform("./src/loader/(**/*).coffee", "./lib/assets/$1.js", function(input, output, data) { | |
var result; | |
console.log("Compiling %s --> Minifying --> %s", input, output); | |
result = coffee.compile(data, { | |
bare: true | |
}); | |
return minify(result); | |
}); | |
parts = ["./src/bootstrap/ui.coffee", "./src/bootstrap/app.coffee"]; | |
out = []; | |
for (_i = 0, _len = parts.length; _i < _len; _i++) { | |
part = parts[_i]; | |
transform(part, null, function(input, _, data) { | |
var result; | |
console.log("Compiling %s -->", input); | |
result = coffee.compile(data, { | |
bare: true | |
}); | |
out.push(result); | |
}); | |
} | |
bootstrap_path = "./lib/assets/bootstrap.js"; | |
bootstrap_data = out.join(""); | |
console.log(" --> Combining %s", bootstrap_path); | |
return write_file(bootstrap_path, bootstrap_data); | |
}); | |
task("populate-assets", "prepare assets to be used by builder", function() { | |
transform("./src/cryptojs/encoder.coffee", null, function(input, _, data) { | |
var encoder; | |
encoder = coffee.compile(data, { | |
bare: true | |
}); | |
console.log("Compiling %s -->", input); | |
return transform("./bower_components/cryptojslib/rollups/(md5|sha1|sha224|sha256|sha3|sha384|sha512|ripemd160).js", "./lib/assets/hash/$1.js", function(input, output, data, match) { | |
var hash_name; | |
console.log(" Combining %s --> %s", input, output); | |
hash_name = match[1]; | |
return minify_more("(function() {\n " + data + ";\n var encoder = " + encoder + "\n return (function(data) {\n var hash = CryptoJS.algo[\"" + (hash_name.toUpperCase()) + "\"].create();\n hash.update(encoder(data));\n return hash.finalize().toString(CryptoJS.enc.Hex);\n });\n})();"); | |
}); | |
}); | |
transform("./src/cryptojs/encoder.coffee", null, function(input, _, data) { | |
var encoder; | |
encoder = coffee.compile(data, { | |
bare: true | |
}); | |
console.log("Compiling %s -->", input); | |
return transform("./src/cryptojs/decoder.coffee", null, function(input, _, data) { | |
var decoder; | |
decoder = coffee.compile(data, { | |
bare: true | |
}); | |
console.log("Compiling %s -->", input); | |
return transform("./bower_components/cryptojslib/components/core.js", "./lib/assets/encoding/identity.js", function(input, output, data) { | |
console.log(" Combining --> %s", "./lib/assets/cypher/identity.js"); | |
return minify_more("(function() {\n " + data + ";\n var encoder = " + encoder + "\n var decoder = " + decoder + "\n return (function(data, password) {\n return decoder(encoder(data));\n });\n})();"); | |
}); | |
}); | |
}); | |
transform("./bower_components/localforage/dist/(localforage).min.js", "./lib/assets/$1.js", function(input, output, data) { | |
console.log("Copying %s --> %s", input, output); | |
return data; | |
}); | |
transform("./src/builder/index.tmpl", "lib/assets/index.tmpl", function(input, output, data) { | |
console.log("Copying %s --> %s", input, output); | |
return data; | |
}); | |
transform("./src/builder/appcache.tmpl", "lib/assets/appcache.tmpl", function(input, output, data) { | |
console.log("Copying %s --> %s", input, output); | |
return data; | |
}); | |
return transform("./src/bootstrap/style.scss", "lib/assets/bootstrap.css", function(input, output, data) { | |
console.log("Compiling %s --> %s", input, output); | |
sass.renderFile({ | |
file: path.join('.', input), | |
outputStyle: 'compressed', | |
outFile: output, | |
success: function(x) { | |
return console.log("Success", x); | |
}, | |
error: console.log.bind(console) | |
}); | |
}); | |
}); | |
task("test", "run unittests", function() { | |
var cmd; | |
cmd = ["npm", "run", "test:unit:short"].join(" "); | |
console.log(cmd); | |
return exec(cmd, function(err, stdout, stderr) { | |
return console.log(stdout + stderr); | |
}); | |
}); | |
task("build", "compile all coffeescript files to javascript", function() { | |
invoke('compile-builder'); | |
invoke('compile-loader'); | |
return invoke('populate-assets'); | |
}); | |
task("sbuild", "build routine for sublime", function() { | |
invoke('build'); | |
return invoke('test'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment