Skip to content

Instantly share code, notes, and snippets.

@yumitsu
Created March 8, 2012 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yumitsu/2000124 to your computer and use it in GitHub Desktop.
Save yumitsu/2000124 to your computer and use it in GitHub Desktop.
Rake::CLI in JavaScript
var Namespace, Namespaces, Task, namespace, task;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Namespaces = {
names: [],
descriptions: [],
tasks: [],
injectMethods: function(ins) {
var nspaces;
nspaces = this;
ins.fqn = function() {
return this._nameChain().join(':');
};
ins.prefix = function() {
var chain;
chain = this._nameChain();
chain.pop();
return chain.join(':');
};
ins._nameChain = function(nspace, chain) {
if (nspace == null) {
nspace = this;
}
if (chain == null) {
chain = [];
}
chain.unshift(nspace.name);
if (nspace.parent != null) {
chain = nspace._nameChain(nspace.parent, chain);
}
return chain;
};
return ins._addTask = function(task) {
if (nspaces.descriptions.length !== 0) {
task.description = nspaces.descriptions.shift();
}
return nspaces.tasks.push(task);
};
},
add: function(namespace) {
this.injectMethods(namespace);
return this.names.push({
name: namespace.name,
prefix: namespace.prefix()
});
},
remove: function(namespace) {
var name, newList, ns;
name = (function() {
switch (typeof namespace) {
case "object":
return namespace.name;
case "string":
return namespace;
}
})();
newList = (function() {
var _i, _len, _ref, _results;
_ref = this.names;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
ns = _ref[_i];
_results.push(ns.name !== name ? ns : void 0);
}
return _results;
}).call(this);
if (newList.length !== this.names.length) {
return this.names = newList;
}
}
};
Namespace = (function() {
function Namespace(name, parent) {
var _ref;
this.name = name;
this.parent = parent;
if ((_ref = this.parent) == null) {
this.parent = null;
}
this.registerNamespace();
}
Namespace.prototype.addTask = function(task) {
task._setPrefix(this.fqn());
return this._addTask(task);
};
Namespace.prototype.findTask = function(name) {};
Namespace.prototype.runTask = function(name) {};
Namespace.prototype.registerNamespace = function() {
return Namespaces.add(this);
};
return Namespace;
})();
Task = (function() {
function Task(name, callback) {
this.name = name;
this.description = null;
}
Task.prototype._setPrefix = function(prefix) {
var _ref;
this.prefix = prefix;
return (_ref = this.prefix) != null ? _ref : this.prefix = "";
};
Task.prototype.fqn = function() {
return "" + this.prefix + this.name;
};
return Task;
})();
namespace = function(name, cbk) {
this.ns = new Namespace(name, null);
namespace = __bind(function(name, cbk) {
this.ns = new Namespace(name, this.ns);
return cbk();
}, this);
return cbk();
};
task = function(name, callback) {
var desc, _task;
_task = new Task(name, callback);
this.ns.addTask(_task);
return desc = function(description) {
return Namespaces.descriptions.push(description);
};
};
/**
Test code:
namespace('test', function() {
namespace('xynta', function() {
namespace('dno', function() {
desc('Test task');
task('test', function() {
сonsole.log('Hello world!');
});
});
});
});
console.log(Namespaces);
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment