Skip to content

Instantly share code, notes, and snippets.

@osdrv
Created May 15, 2012 09:18
Show Gist options
  • Save osdrv/2700313 to your computer and use it in GitHub Desktop.
Save osdrv/2700313 to your computer and use it in GitHub Desktop.
Cakefile
fs = require 'fs'
{exec} = require 'child_process'
util = require 'util'
growl = require 'growl'
appFiles = [
]
exclude = /chanto/
task 'coffeeFiles', 'how much coffee you got?!', ->
traverseFileSystem = (currentPath) ->
files = fs.readdirSync currentPath
for file in files
do (file) ->
currentFile = currentPath + '/' + file
stats = fs.statSync(currentFile)
if stats.isFile() and currentFile.indexOf('.coffee') > 1 and !exclude.test(currentFile) and appFiles.join('=').indexOf("#{currentFile}=") < 0
appFiles.push currentFile
else if stats.isDirectory()
traverseFileSystem currentFile
traverseFileSystem './'
util.log "#{appFiles.length} coffee files found."
return appFiles
task 'watch', 'Watch prod source files and build changes', ->
invoke 'build'
util.log "Watching for changes in src"
for file in appFiles then do (file) ->
fs.watchFile file, (curr, prev) ->
if +curr.mtime isnt +prev.mtime
util.log "Saw change in #{file}"
grrrr 'Whoa. Saw a change. Building. Hold plz.'
invoke 'build'
task 'build', 'Build single application file from source files', ->
invoke 'coffeeFiles'
appContents = new Array remaining = appFiles.length
for file, index in appFiles then do (file, index) ->
fs.readFile file, 'utf8', (err, fileContents) ->
throw err if err
appContents[index] = fileContents
process() if --remaining is 0
process = ->
fs.writeFile './app.coffee', appContents.join('\n\n'), 'utf8', (err) ->
throw err if err
exec 'coffee --compile ./app.coffee', (err, stdout, stderr) ->
if err
util.log 'Error compiling coffee file.'
util.log err
grrrr 'Uh, your coffee is bad.'
else
fs.unlink './app.coffee', (err) ->
if err
util.log 'Couldn\'t delete the app.coffee file/'
util.log 'Done building coffee file.'
grrrr 'Okay, coffee is ready.'
task "chanto:g", "Chanto classes generator", ( options ) ->
params = process.argv.slice 3
camelize = ( arg ) ->
arg.replace( /(?:^|[-_])(\w)/g, ( i, c ) ->
return "" if !c
c.toUpperCase()
)
class_templates = {
data_action: '%%action_class%% = ( options ) ->\n
this._init( options )\n
null
\n
%%action_class%%.prototype = $.extend( {}, ActionWithData.prototype, {\n
_init: ( options ) ->
Action.prototype._init.call( this, options )\n
this.data = new EvegatorData()\n
bang: () ->\n
self = this\n
renderer = new %%renderer_class%%({\n
loadData: self._loadData\n
})\n
renderer.render()\n
})\n'
action: '%%action_class%% = ( options ) ->\n
this._init( options )\n
null\n
\n
%%action_class%%.prototype = $.extend( {}, Action.prototype, {\n
_init: ( options ) ->\n
Action.prototype._init.call( this, options )\n
this.data = new EvegatorData()\n
\n
bang: () ->\n
self = this\n
renderer = new %%renderer_class%%()\n
renderer.render()\n
})\n
\n
window.%%action_class%% = %%action_class%%'
renderer: '%%renderer_class%% = ( options ) ->\n
this._init( options )\n
null\n
\n
%%renderer_class%%.prototype = $.extend( {}, Renderer.prototype, {\n
_init: ( options ) ->\n
Renderer.prototype._init.call( this, options )\n
this.page = $("#%%class_name%%-page")\n
\n
render: () ->\n
# renderer code here\n
})\n
\n
window.%%renderer_class%% = %%renderer_class%%'
data_renderer: '%%renderer_class%% = ( options ) ->\n
this._init( options )\n
null\n
\n
%%renderer_class%%.prototype = $.extend( {}, Renderer.prototype, {\n
_init: ( options ) ->\n
Renderer.prototype._init.call( this, options )\n
this.page = $("#%%class_name%%-page")\n
# data filters set\n
this.filters = []\n
\n
render: () ->\n
this.loadData( "%%class_name%%", filters, ( data ) ->\n
# proceed data here\n
)\n
})\n
\n
window.%%renderer_class%% = %%renderer_class%%'
}
_buildClassFiles = ( type, class_name ) ->
util.log "preparing to build #{type} for class #{class_name}"
action_class = camelize( "#{class_name}_action" )
renderer_class = camelize( "#{class_name}_renderer" )
throw "Unknown class type given: #{type}" if !class_templates[ type ]
content = class_templates[ type ].replace( /%%action_class%%/g, action_class ).replace( /%%renderer_class%%/g, renderer_class ).replace( /%%class_name%%/g, class_name )
util.log( "./app/#{type}s/#{class_name}_#{type}.coffee" )
fs.writeFileSync "./app/#{type}s/#{class_name}_#{type}.coffee", content, 'utf8'
util.log "done"
actions = []
switch params.length
when 0
util.log "Nothing to do, skipping"
when 1
actions = [ "action", "renderer" ]
class_name = params[0]
else
use_load_data_templates = false
for k, a in params
if k == "-d" || k == "--data"
params.splice( a, 1 )
use_load_data_templates = true
break
if use_load_data_templates
actions = [ "data_action", "data_renderer" ]
else
util.log "Hmm, to much arguments given"
for a in actions
_buildClassFiles( a, class_name )
process.exit 0
grrrr = (message = '') ->
options =
title: 'CoffeeScript'
growl message, options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment