Permalink
Browse files

Add support for user-defined generators.

  • Loading branch information...
paulmillr committed May 11, 2012
1 parent 90efff5 commit a1bdba2e4c2c7f45f72a6446005db0a66ffb266b
Showing with 72 additions and 5 deletions.
  1. +9 −0 CHANGELOG.md
  2. +63 −5 src/commands/scaffold.coffee
View
@@ -1,4 +1,13 @@
# Brunch 1.2.0 (unreleased)
+* Greatly improved `brunch generate`:
+ * User can now define his own generators in `config.generators`.
+ * Default generators are now:
+ * controllerTest, modelTest, viewTest
+ * template, style
+ * controller (generates controllerTest too)
+ * model (generates modelTest too)
+ * view (generates template, style & viewTest too)
+ * scaffold (generates controller, model, view and their generators)
* Added pushState support to the built-in webserver.
* Improved config API:
* Added `paths.ignored` param that redefines
@@ -13,6 +13,14 @@ flatten = (array) ->
categories =
backbone:
+ modelTest: 'javascripts'
+ model: 'javascripts'
+ template: 'templates'
+ style: 'stylesheets'
+ viewTest: 'javascripts'
+ view: 'javascripts'
+
+ chaplin:
controllerTest: 'javascripts'
controller: 'javascripts'
modelTest: 'javascripts'
@@ -22,8 +30,44 @@ categories =
viewTest: 'javascripts'
view: 'javascripts'
+frameworkChocies = ->
+ Object.keys(categories).join(', ')
+
+generatorChoices = (framework) ->
+ Object.keys(categories[framework] or {}).join(', ')
+
generators = (config, generator) ->
backbone:
+ modelTest: (name) ->
+ [sysPath.join(config.paths.test, 'models', "#{name}_test")]
+
+ model: (name) ->
+ [sysPath.join(config.paths.app, 'models', "#{name}")].concat(
+ generator('modelTest', name)
+ )
+
+ template: (name) ->
+ [sysPath.join(config.paths.app, 'views', 'templates', "#{name}")]
+
+ style: (name) ->
+ [sysPath.join(config.paths.app, 'views', 'styles', "#{name}")]
+
+ viewTest: (name) ->
+ [sysPath.join(config.paths.test, 'views', "#{name}_view_test")]
+
+ view: (name) ->
+ [sysPath.join(config.paths.app, 'views', "#{name}_view")].concat(
+ generator('viewTest', name),
+ generator('template', name),
+ generator('style', name)
+ )
+
+ scaffold: (name) ->
+ generator('controller', name).concat(
+ generator('model', name), generator('view', name)
+ )
+
+ chaplin:
# What generates what:
# controller: controller, controllerTest
# model: model, modelTest
@@ -71,28 +115,42 @@ generators = (config, generator) ->
getGenerator = (config, plugins) ->
framework = config.framework or 'backbone'
+ unless categories[framework]?
+ return logger.error "Framework #{framework} isn't supported. Use one of:
+#{frameworkChocies()}"
+
getExtension = (type) ->
category = categories[framework]?[type]
if category?
config.files[category]?.defaultExtension ? ''
else
+ logger.error "Generator #{type} isn't supported. Use one of:
+#{generatorChoices(framework)}."
''
generatorMap = null
getGeneratorMap = ->
generatorMap ?= generators config, generator
generator = (type, name) ->
+ configGenerator = config.generators?[type]
+ getData = (item) ->
+ if typeof item is 'function'
+ item name, type
+ else
+ item
+
extension = getExtension type
plugin = plugins.filter((plugin) -> plugin.extension is extension)[0]
dataGenerator = plugin?.generators?[framework]?[type]
- data = if dataGenerator?
- if typeof dataGenerator is 'function'
- dataGenerator name, type
- else
- dataGenerator
+
+ data = if configGenerator?
+ getData configGenerator
+ else if dataGenerator?
+ getData dataGenerator
else
''
+
getPaths = getGeneratorMap()[framework]?[type]
paths = (getPaths? name) or []
nonStrings = paths.filter (path) -> typeof path isnt 'string'

0 comments on commit a1bdba2

Please sign in to comment.