Skip to content

Instantly share code, notes, and snippets.

@ctkjose
Last active January 18, 2016 17:53
Show Gist options
  • Save ctkjose/cd7a33d380b7515178e0 to your computer and use it in GitHub Desktop.
Save ctkjose/cd7a33d380b7515178e0 to your computer and use it in GitHub Desktop.
//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();

Binder Build Command for KomodoEdit

This script is a build utility for Komodo using the Binder extension available at KOBINDER.

To install create a new user script in Komodo's toolbox.

It uses Matthias Mullie php minify script.

Using this tool

Create a file config.build.js in the root folder of your project.

Add a build object with your build actions like this:

build = {
	"actions" : [
		{"action": "copy", "source": "engine/js/rea.ui.widget.src.core.js", "dest": "engine/js/rea.ui.widgets.min.js"},
		{"action" : "append","dest" : "engine/js/rea.ui.widgets.min.js",'source' : "engine/js/",'name_match' : "rea\.ui\.widget\.src\.ui\_[A-Za-z\_\.0-9]+\.js"},
		{"action" : "minify","file" : "engine/js/rea.ui.widgets.min.js"}
	],
}

Available Actions

copy

Creates a copy of a file in another location.

PARAM DESC
source A relative path to a file or a folder.
dest A relative path to a file or a folder.
name_match Optional, indicates a regex to match files when source is a folder.

If source is a folder and dest is a file, all files in the folder will be appended to dest.

append

Appends the contents of a file into another.

PARAM DESC
source A relative path to a file or a folder.
dest A relative path to a file or a folder.
name_match Optional, indicates a regex to match files when source is a folder.

NOTE: If dest is a file, then the file will no be emptied, you may want to use the action clear to delete the file's content.

join

Appends the contents of a folder into a file.

PARAM DESC
source A relative path to a file or a folder.
file A relative path to the file to be created.
name_match Indicates a regex to match files when source is a folder.

NOTE: If file exists its contents will be cleared.

minify

Minify a file.

PARAM DESC
file A relative path to the file to be minified.

clear

Deletes the contents of a file.

PARAM DESC
file A relative path to the file to be cleared.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment