Skip to content

Instantly share code, notes, and snippets.

@j-mcnally
Created September 17, 2012 17:33
Show Gist options
  • Save j-mcnally/3738653 to your computer and use it in GitHub Desktop.
Save j-mcnally/3738653 to your computer and use it in GitHub Desktop.
How to start a tower server?
# https://github.com/cowboy/grunt/blob/master/docs/task_init.md
# https://github.com/kmiyashiro/grunt-mocha
module.exports = (grunt) ->
grunt.loadNpmTasks('grunt-less')
#grunt.loadNpmTasks('grunt-stylus')
grunt.loadNpmTasks('grunt-compass')
async = require('async')
mint = require('mint')
coffeecup = require('coffeecup')
fs = require('fs')
require('tower')
# @todo tmp
Tower.Application.instance().initialize()
#register restart task
grunt.registerTask 'restartNode', 'Restart tower app via supervisor', ->
exec = require('child_process').exec
exec("touch tmp/restart/restart.txt") #touch the restart file to kick over supervisor
grunt.registerMultiTask 'templates', 'Compile templates', ->
name = ""
taskDone = @async()
files = grunt.file.expand(['app/views/**/*.coffee'])
result = []
for file in files
continue unless file.match(/app\/views\/.+\.coffee$/)
continue unless file.match(/\.coffee$/)
# @todo tmp, dont need these for the client
continue if file.match('layouts/application') || file.match('shared')
result.push [file.replace(/\.coffee$/, ""), fs.readFileSync(file)]
template = "Tower.View.cache =\n"
iterator = (item, next) =>
name = item[0].replace(/app\/(?:client\/)?views\//, '')#.replace('/', '_')
# _table.coffee
fileName = name.split('/')
fileName = fileName[fileName.length - 1]
return next() if fileName.match(/^_/) # if it's a partial, don't include
try
string = coffeecup.render(item[1])
catch error
try
prefix = name.split('/')[0]
view = new Tower.View(collectionName: prefix)
opts = type: 'coffee', inline: true, template: item[1].toString(), prefixes: [prefix]
cb = (error, body) =>
string = body
view.render(opts, cb)
catch error
console.log item[0], error
return next() # so we can still have some templates
template += " '#{name}': Ember.Handlebars.compile('"
# make it render to HTML for ember
template += "#{string}')\n"
next()
async.forEachSeries result, iterator, (error) =>
template += '_.extend(Ember.TEMPLATES, Tower.View.cache)\n'
mint.coffee template, bare: true, (error, string) =>
if error
console.log error
return taskDone(error)
else
fs.writeFileSync "public/javascripts/templates.js", string
taskDone()
grunt.registerMultiTask 'copy', 'Copy files to destination folder and replace @VERSION with pkg.version', ->
replaceVersion = (source) ->
source.replace /@VERSION/g, grunt.config('pkg.version')
copyFile = (src, dest) ->
if /(js|css|json)$/.test(src)
grunt.file.copy src, dest,
process: replaceVersion
else
grunt.file.copy src, dest
files = grunt.file.expandFiles(@file.src)
target = @file.dest + '/'
strip = @data.strip
renameCount = 0
fileName = undefined
strip = new RegExp('^' + grunt.template.process(strip, grunt.config()).replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')) if typeof strip is 'string'
files.forEach (fileName) ->
targetFile = (if strip then fileName.replace(strip, '') else fileName)
copyFile fileName, target + targetFile
grunt.log.writeln 'Copied ' + files.length + ' files.'
for fileName of @data.renames
renameCount += 1
copyFile fileName, target + grunt.template.process(@data.renames[fileName], grunt.config())
grunt.log.writeln 'Renamed ' + renameCount + ' files.' if renameCount
# https://github.com/avalade/grunt-coffee
path = require("path")
# CoffeeScript
grunt.registerMultiTask "coffee", "Compile CoffeeScript files", ->
dest = @file.dest + '/'
options = @data.options
strip = @data.strip
extension = @data.extension
strip = new RegExp('^' + grunt.template.process(strip, grunt.config()).replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')) if typeof strip is 'string'
grunt.file.expandFiles(@file.src).forEach (filepath) ->
targetFile = dest + (if strip then filepath.replace(strip, '') else filepath)
grunt.helper "coffee", filepath, targetFile, grunt.utils._.clone(options), extension
if grunt.task.current.errorCount
false
else
true
grunt.registerHelper "coffee", (src, destPath, options, extension) ->
coffee = require("coffee-script")
js = ""
options = options or {}
extension = (if extension then extension else ".js")
dest = path.dirname(destPath) + '/' + path.basename(destPath, ".coffee") + extension
#console.log dest
options.bare = true if options.bare isnt false
try
js = coffee.compile(grunt.file.read(src), options)
grunt.file.write dest, js
return true
catch e
grunt.log.error "Error in " + src + ":\n" + e
return false
_ = grunt.utils._
file = grunt.file
# CoffeeScript files
files = file.expand([
'app/**/**/*.coffee'
'test/**/*.coffee'
'config/locales/**/*.coffee'
'config/application.coffee'
'config/routes.coffee'
'config/assets.coffee'
])
jsFiles = file.expand([
'vendor/javascripts/**/*.js'
])
config =
pkg: '<json:package.json>'
coffee:
all:
src: files
dest: 'public/javascripts'
options:
bare: false
less:
bootstrap:
src: 'vendor/stylesheets/bootstrap/bootstrap.less'
dest: 'public/stylesheets/vendor/stylesheets/bootstrap/bootstrap.css'
watch:
sass:
#files: ['app/client/stylesheets/application.styl']
#tasks: ['stylus']
files: ['app/client/stylesheets/application.css.scss']
tasks: ['grunt-sass']
copy:
js:
src: ['vendor/**/*.js']
dest: 'public/javascripts'
css:
src: ['vendor/**/*.css']
dest: 'public/stylesheets'
templates:
all: {}
compass:
compile:
dest: 'public/stylesheets/app/client/stylesheets'
src: 'app/client/stylesheets'
forcecompile: true
relativeassets: true
linecomments: true
for name in files
config.coffee[name] =
src: name
dest: 'public/javascripts'
options:
bare: true
config.watch[name] =
files: [name]
tasks: ["coffee:#{name}", "restartNode"] #restart when watch happens after recompiling coffee files
for name in jsFiles
config.copy[name] =
src: [name]
dest: 'public/javascripts'
config.watch[name] =
files: [name]
tasks: ["copy:#{name}", "restartNode"] #restart when watch happens after recompiling coffee files
grunt.initConfig(config)
#grunt.loadNpmTasks 'grunt-coffee'
grunt.registerTask 'default', 'copy:js copy:css coffee:all less compass templates restartNode' #restart when grunt happens after recompiling coffee files
grunt.registerTask 'start', 'default watch'
module.exports = require('./grunt.coffee')
grunt
grunt watch &
supervisor -w tmp/restart -e txt server.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment