|
//A Basic Build Tool |
|
|
|
function run(){ |
|
var p = binder.ko.projectGetPath(); |
|
if ( p.length <= 0) return; |
|
|
|
var pd = binder.ko.pathJoin(p, "config.build.js"); |
|
//alert(pd); |
|
var ok = binder.ko.pathExists(pd); |
|
if(!ok){ |
|
alert("Error: Build config file not found at [" + p + "]."); |
|
return; |
|
} |
|
|
|
|
|
var url = binder.ko.pathGetURI( pd ); |
|
var $this = {'css' : null }; |
|
binder.ko.loadJS(url, $this); |
|
|
|
builder.build(build); |
|
} |
|
|
|
var builder = { |
|
scene : {}, |
|
path : "", |
|
build : function(o){ |
|
this.scene = o; |
|
this.path = binder.ko.projectGetPath(); |
|
if ( this.path.length <= 0) return; |
|
|
|
this.runActions(); |
|
|
|
binder.ko.notifyInfo("BUILD DONE", "BUILDER"); |
|
}, |
|
runActions: function(){ |
|
if(!this.scene || !this.scene.hasOwnProperty("actions") || !Array.isArray(this.scene.actions)) return; |
|
for(var i=0; i<this.scene.actions.length;i++){ |
|
var a = this.scene.actions[i]; |
|
var cmd = "cmd_" + a.action; |
|
|
|
if(!this.hasOwnProperty(cmd)) continue; |
|
this[cmd](a); |
|
|
|
} |
|
}, |
|
log: function(s){ |
|
binder.ko.runoutputWrite(s); |
|
}, |
|
findFiles: function(dir, name_match){ |
|
var p = binder.ko.pathJoin(this.path, dir); |
|
var files = binder.ko.pathListFiles(p); |
|
var out = []; |
|
|
|
var r = '.*'; |
|
if(name_match &&(name_match.length > 0)){ |
|
r = new RegExp(name_match); |
|
} |
|
for(var file of files){ |
|
var f = {"name": file, "path": binder.ko.pathJoin(p,file)}; |
|
if(r.exec(file)){ |
|
out.push(f); |
|
} |
|
} |
|
|
|
return out; |
|
|
|
}, |
|
cmd_append: function(e){ |
|
var files = []; |
|
if(e.hasOwnProperty("name_match")){ |
|
var files = this.findFiles(e.source, e.name_match); |
|
}else{ |
|
var f = binder.ko.pathJoin(this.path, e.source); |
|
if( binder.ko.pathIsDir(f) ){ |
|
var files = this.findFiles(e.source, ""); |
|
}else{ |
|
files.push({"name":e.source,"path": f}); |
|
} |
|
} |
|
|
|
var fout =binder.ko.pathJoin(this.path, e.dest); |
|
var slog = "BUILD::APPEND::" + fout + "\n"; |
|
|
|
if( binder.ko.pathIsDir(fout) ){ |
|
slog+= "Expecting a file for output!\n"; |
|
this.log(slog); |
|
return; |
|
} |
|
|
|
for(var i=0; i<files.length;i++){ |
|
slog+= "APPEND::" + files[i].name + "::" + fout +"\n"; |
|
var cmd = "cat " + files[i].path + " >> " + fout; |
|
var s = binder.ko.shellExec(cmd, true); |
|
slog+= s; |
|
} |
|
|
|
|
|
this.log(slog); |
|
}, |
|
cmd_copy: function(e){ |
|
var files = []; |
|
if(e.hasOwnProperty("name_match")){ |
|
files = this.findFiles(e.source, e.name_match); |
|
}else{ |
|
var f = binder.ko.pathJoin(this.path, e.source); |
|
if( binder.ko.pathIsDir(f) ){ |
|
files = this.findFiles(e.source, ""); |
|
}else{ |
|
files.push({"name":e.source,"path": f}); |
|
} |
|
} |
|
|
|
var fout =binder.ko.pathJoin(this.path, e.dest); |
|
var slog = "BUILD::COPY::" + fout + "\n"; |
|
|
|
if( binder.ko.pathIsDir(fout) ){ |
|
for(var i=0; i<files.length;i++){ |
|
slog+= "CP::" + files[i].name + "::" + fout +"\n"; |
|
var cmd = "cp " + files[i].path + " " + fout; |
|
var s = binder.ko.shellExec(cmd, true); |
|
slog+= s; |
|
} |
|
}else{ |
|
for(var i=0; i<files.length;i++){ |
|
slog+= "JOIN::" + files[i].name + "::" + fout + "\n"; |
|
var cmd = "cat " + files[i].path; |
|
cmd += (i==0) ? " > " : " >> "; |
|
cmd += fout; |
|
|
|
var s = binder.ko.shellExec(cmd, true); |
|
} |
|
} |
|
|
|
this.log(slog); |
|
}, |
|
cmd_minify: function(e){ |
|
var fout =binder.ko.pathJoin(this.path, e.file); |
|
|
|
var cmd = binder.ko.pathNormalize("~/bin/minify.php"); |
|
cmd= "php -e " + cmd + " " + fout + " " + fout; |
|
|
|
var s = binder.ko.shellExec(cmd, true); |
|
//alert(cmd); |
|
|
|
this.log("BUILD::MINIFY::" + fout + "\n"); |
|
}, |
|
cmd_clear: function(e){ |
|
var files = this.findFiles(e.source, e.name_match); |
|
var fout =binder.ko.pathJoin(this.path, e.file); |
|
|
|
var slog = "BUILD::CLEAR::" + fout + "\n"; |
|
binder.ko.shellExec("cp /dev/null " + fout); |
|
this.log(slog); |
|
}, |
|
cmd_join: function(e){ |
|
var files = this.findFiles(e.source, e.name_match); |
|
var fout =binder.ko.pathJoin(this.path, e.file); |
|
|
|
var slog = "BUILD::JOIN::" + fout + "\n"; |
|
binder.ko.shellExec("cp /dev/null " + fout); |
|
for(var i=0; i<files.length;i++){ |
|
slog+= "JOIN::" + e.file + "::" + files[i].name + "\n"; |
|
var cmd = "cat " + files[i].path + " >> " + fout; |
|
var s = binder.ko.shellExec(cmd, true); |
|
} |
|
|
|
this.log(slog); |
|
}, |
|
} |
|
|
|
run(); |