Skip to content

Instantly share code, notes, and snippets.

@yumitsu
Created March 8, 2012 10:15
Show Gist options
  • Save yumitsu/2000140 to your computer and use it in GitHub Desktop.
Save yumitsu/2000140 to your computer and use it in GitHub Desktop.
Rake::CLI in CoffeeScript
Namespaces =
names: []
descriptions: []
tasks: []
injectMethods: (ins) ->
nspaces = @
ins.fqn = ->
@_nameChain().join ':'
ins.prefix = ->
chain = @_nameChain()
chain.pop()
chain.join ':'
ins._nameChain = (nspace, chain) ->
nspace ?= @
chain ?= []
chain.unshift(nspace.name)
if nspace.parent?
chain = nspace._nameChain nspace.parent, chain
chain
ins._addTask = (task) ->
if nspaces.descriptions.length isnt 0 then task.description = nspaces.descriptions.shift()
nspaces.tasks.push task
add: (namespace) ->
@injectMethods namespace
@names.push
name: namespace.name
prefix: namespace.prefix()
remove: (namespace) ->
name = switch typeof(namespace)
when "object"
namespace.name
when "string"
namespace
newList = for ns in @names
ns if ns.name isnt name
@names = newList if newList.length isnt @names.length
class Namespace
constructor: (@name, @parent) ->
@parent ?= null
@registerNamespace()
addTask: (task) ->
task._setPrefix @fqn()
@_addTask task
findTask: (name) ->
runTask: (name) ->
registerNamespace: ->
Namespaces.add @
class Task
constructor: (@name, callback) ->
@description = null
_setPrefix: (@prefix) ->
@prefix ?= ""
fqn: () ->
"#{@prefix}#{@name}"
namespace = (name, cbk) ->
@ns = new Namespace name, null
namespace = (name, cbk) =>
this.ns = new Namespace name, @ns
cbk()
cbk()
task = (name, callback) ->
_task = new Task name, callback
@ns.addTask _task
desc = (description) ->
Namespaces.descriptions.push description
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment