Skip to content

Instantly share code, notes, and snippets.

@jocafa
Created August 4, 2011 20:59
Show Gist options
  • Select an option

  • Save jocafa/1126266 to your computer and use it in GitHub Desktop.

Select an option

Save jocafa/1126266 to your computer and use it in GitHub Desktop.
sys = require 'sys'
fs = require 'fs'
util = require 'util'
childProc = require 'child_process'
coffee = require 'coffee-script'
stylus = require 'stylus'
uglify = require 'uglify-js'
_ = require 'underscore'
_.mixin(require('underscore.string'))
timestamps = {}
glowProc = null
# ================================================================[ Tasks ]====
task 'coffee', 'Compile all the coffeescript files in ./src', ->
console.log 'Building ./public/js/Glow.js'
src = "global = (exports ? this)\n\n" + fs.readFileSync(__dirname + '/src/client/GlowClient.coffee')
src += fs.readFileSync(__dirname + '/src/client/Module.coffee')
src += recursivelyCat './src/client/widgets', 'coffee'
src += recursivelyCat './src/client/modules', 'coffee'
compiled = coffee.compile src
fs.writeFileSync './public/js/Glow.js', compiled
task 'stylus', 'Compile all of the stylus files in ./src', ->
console.log 'Building ./public/css/Glow.css'
src = recursivelyCat './src/client', 'styl'
stylus.render src, (err, css) ->
if err
console.log err
else
fs.writeFileSync './public/css/Glow.css', css
task 'build', 'Build all the static files', ->
invoke 'coffee'
invoke 'stylus'
task 'minify', 'Minify all js and css', ->
task 'package', 'Build all platform packages', ->
task 'watch', 'Watch me code and recompile stuff as needed. Also starts the server.', ->
invoke 'build'
detectChanges('./src')
invoke 'run'
watchfn = ->
if detectChanges('./src') is true
invoke 'build'
#invoke 'run'
setTimeout arguments.callee, 1000
watchfn()
task 'run', 'Start up GlowServer', ->
if glowProc
console.log 'killing GlowServer'
glowProc.kill()
console.log 'starting GlowServer'
glowProc = childProc.exec 'coffee ./src/server/GlowServer.coffee'
glowProc.stdout.on 'data', (data) ->
sys.print data
glowProc.stderr.on 'data', (data) ->
sys.print data
glowProc.stderr.on 'exit', (code) ->
sys.print "GlowServer exited: #{code}"
task 'test', 'Run all of the unit tests', ->
childProc.exec 'coffee test/foo.coffee'
###
task 'iOS', 'Build package for iOS' ->
task 'Android', 'Build package for Android' ->
task 'WebOS', 'Build package for WebOS' ->
###
# ==============================================================[ Helpers ]====
recursivelyCat = (path, ext) ->
files = []
dirs = []
flat = ""
for thing in fs.readdirSync(path).sort()
stats = fs.statSync("#{path}/#{thing}")
if stats.isFile()
if _(thing).endsWith(".#{ext}")
files.push thing
else if stats.isDirectory()
dirs.push thing
for file in files
if _(file).endsWith '.styl'
filecomment = '@css{/* ' + _.lpad "[ #{path}/#{file} ]==== */}", 77, '='
else
filecomment = '/* ' + _.lpad "[ #{path}/#{file} ]==== */", 77, '='
flat += "\n\n#{filecomment}\n\n" + fs.readFileSync("#{path}/#{file}", 'utf8')
for dir in dirs
flat += arguments.callee("#{path}/#{dir}", ext)
return flat
detectChanges = (path) ->
changesFound = false
for thing in fs.readdirSync(path).sort()
fullthing = "#{path}/#{thing}"
stats = fs.statSync(fullthing)
if stats.isFile()
if (timestamps[fullthing] + '') != (stats.mtime + '')
changesFound = true
timestamps[fullthing] = stats.mtime
else if stats.isDirectory()
changesFound = arguments.callee(fullthing) || changesFound
return changesFound
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment