Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 220 lines (181 sloc) 7.177 kb
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
1 // Load the basic Geddy toolkit
2 require('../lib/geddy')
3
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
4 // Dependencies
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
5 var fs = require('fs')
6 , path = require('path')
c87da75 More Node 0.8 support.
mde authored
7 , existsSync = typeof fs.existsSync == 'function' ?
8 fs.existsSync : path.existsSync
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
9 , inflection = require('../deps/inflection')
10 , utils = require('../lib/utils')
3b29a7e @larzconwell Fixed bug where apps couldn't be generated and resources couldn't be cre...
larzconwell authored
11 , adapter = require('../lib/template/adapters').Adapter
12 , templato = new (require('../deps/templato'));
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
13
14 namespace('gen', function () {
15
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
16 var _writeTemplate = function (name, filename, dirname, options) {
17 options = options || {};
18
19 var names = _getInflections(name)
20 , text = fs.readFileSync(path.join(__dirname, filename + '.ejs'), 'utf8').toString()
cf999ea @larzconwell Deleted bare controller template
larzconwell authored
21 , bare = options.bare || false // Default to full controller
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
22 , templ
23 , filePath;
24
25 // Render with the right model name
26 templ = new adapter({ data: {ext: '.ejs'}, text: text, templato: templato });
cf999ea @larzconwell Deleted bare controller template
larzconwell authored
27 templ.process({names: names, bare: bare});
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
28
29 // Write file
30 filePath = path.join('app', dirname, names.filename[options.inflection] + '.js');
31 fs.writeFileSync(filePath, templ.markup, 'utf8');
32
33 console.log('[ADDED] ' + filePath);
34 };
35
36 var _getInflections = function (nameParam) {
37 var name = utils.string.snakeize(nameParam)
38 , namePlural = inflection.pluralize(name);
39
40 return {
41 filename: {
42 singular: name
43 , plural: namePlural
44 },
45 constructor: {
46 singular: utils.string.camelize(name, true)
47 , plural: utils.string.camelize(namePlural, true)
48 },
49 property: {
50 singular: utils.string.camelize(name)
51 , plural: utils.string.camelize(namePlural)
52 }
53 };
54 };
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
55
56 // Creates a new Geddy app scaffold
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
57 task('app', [], function(name, engine) {
58 if(!name) throw new Error('No app-name specified.');
59 if(!engine) engine = 'ejs';
60
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
61 var mkdirs = [
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
62 ''
63 , 'config'
64 , 'app/models'
65 , 'app/controllers'
66 , 'lib'
67 , 'log'
68 , 'node_modules'
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
69 ]
70 , cps = [
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
71 ['views/' + engine, 'app/views']
72 , ['public', '']
73 , ['router.js', 'config']
74 , ['init.js', 'config']
75 , ['environment.js', 'config']
76 , ['development.js', 'config']
77 , ['production.js', 'config']
78 , ['main.js', 'app/controllers']
79 , ['application.js', 'app/controllers']
80 , ['favicon.ico', 'public']
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
81 ];
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
82
83 mkdirs.forEach(function(dir) {
84 jake.mkdirP(path.join(name, dir));
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
85 });
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
86 cps.forEach(function(cp) {
87 jake.cpR(path.join(__dirname, 'base', cp[0]), path.join(name, cp[1]));
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
88 });
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
89
90 console.log('Created app ' + name + '.');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
91 });
92
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
93 // Creates a resource(Model, Controller, Views and resource route)
94 task('resource', function(name, engine) {
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
95 jake.Task['gen:model'].invoke(name);
96 jake.Task['gen:controller'].invoke(name);
97 jake.Task['gen:route'].invoke(name);
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
98 jake.Task['gen:views'].invoke(name, { engine: engine });
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
99 });
100
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
101 task('model', [], function(name) {
102 _writeTemplate(name, 'resource_model', 'models', { inflection: 'singular' });
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
103 });
104
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
105 task('controller', [], function(name) {
cf999ea @larzconwell Deleted bare controller template
larzconwell authored
106 _writeTemplate(name, 'resource_controller', 'controllers', { inflection: 'plural', bare: false });
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
107 });
108
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
109 task('route', [], function(name, options) {
110 options = options || {};
111
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
112 var names = _getInflections(name)
113 , routeType = options.bare ? 'Bare' : 'Resource'
114 , filePath = path.normalize('config/router.js')
115 , text = fs.readFileSync(filePath, 'utf8').toString()
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
116 , newRoute
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
117 , routeArr;
118
119 if (options.bare) {
120 newRoute = 'router.match(\'/' + names.filename.plural +
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
121 '\').to({controller: \'' + names.constructor.plural +
122 '\', action: \'index\'});';
123 } else newRoute = 'router.resource(\'' + names.filename.plural + '\');'
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
124
125 // Don't add the same route over and over
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
126 if(text.indexOf(newRoute) == -1) {
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
127 // Add the new resource route just above the export
128 routerArr = text.split('exports.router');
129 routerArr[0] += newRoute + '\n';
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
130
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
131 text = routerArr.join('exports.router');
132 fs.writeFileSync(filePath, text, 'utf8');
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
133
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
134 console.log(routeType + ' ' + names.filename.plural +
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
135 ' route added to ' + filePath);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
136 }
137 else {
138 console.log('(' + routeType + ' ' + names.filename.plural +
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
139 ' route already defined in ' + filePath + ')');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
140 }
141 });
142
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
143 task('views', [], function(name, options) {
144 options = options || {};
145
146 // If `engine` option isn't set then default to EJS
147 if(!options.engine) options.engine = 'ejs';
148
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
149 var names = _getInflections(name)
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
150 , engine = options.engine
151 , appViewDir = path.join('app', 'views', names.filename.plural)
152 , templateViewDir = path.join(__dirname, 'views', engine)
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
153 , cmds = []
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
154 , ext = '.html'
155 , appLayoutPath
156 , actions
157 , addActionView;
158
159 // Set extension based on engine option
160 if(engine === 'ejs') ext += '.ejs';
161 if(engine === 'jade') ext += '.jade';
162 if(engine === 'handlebars') ext += '.hbs';
163 if(engine === 'mustache') ext += '.ms';
164
165 // Set application layout path
166 appLayoutPath = path.join('app', 'views', 'layouts', 'application');
167 // Copy template view to application path
168 addActionView = function(action) {
169 jake.cpR(path.join(templateViewDir, action + ext), appViewDir);
170 };
171
172 jake.mkdirP(appViewDir);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
173 jake.mkdirP('app/views/layouts');
174 addActionView('index');
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
175
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
176 // Add views for the other CRUD actions when doing a full-on resource
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
177 if(!options.bare) {
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
178 ['add', 'edit', 'show'].forEach(function (action) {
179 addActionView(action);
180 });
181 }
182
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
183 // Create default layout if one doesn't exist
184 // Hack: There should be a better way to detect if a application layout exists
185 if(!existsSync(appLayoutPath + '.html.ejs') && !existsSync(appLayoutPath + '.html.jade') &&
186 !existsSync(appLayoutPath + '.html.hbs') && !existsSync(appLayoutPath + '.html.ms')) {
187 // Copy template layout file to apps application layout file
188 jake.cpR(path.join(templateViewDir, 'layout' + ext), appLayoutPath + ext);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
189 }
190
191 console.log('Created view templates.');
192 });
193
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
194 task('bareController', [], function(name, engine) {
cf999ea @larzconwell Deleted bare controller template
larzconwell authored
195 _writeTemplate(name, 'resource_controller', 'controllers', { inflection: 'plural', bare: true });
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
196
197 jake.Task['gen:route'].invoke(name, { bare: true });
198 jake.Task['gen:views'].invoke(name, { bare: true, engine: engine });
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
199 });
200
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
201 // Generate a new application secret in environment.js
202 task('secret', [], function(name) {
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
203 var filename = path.join(process.cwd(), 'config/environment.js')
204 , conf = fs.readFileSync(filename).toString()
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
205 , secret = utils.string.uuid(128)
206 , confArr;
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
207
208 // Remove any old secret
209 conf = conf.replace(/\nconfig.secret.+;\n/, '');
210
211 confArr = conf.split('module.exports = config;');
212 conf = confArr[0] + "config.secret = '" + secret + "';\n\n" +
213 'module.exports = config;' + confArr[1];
214 fs.writeFileSync(filename, conf);
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
215
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
216 console.log('app-secret added to environment.js config.');
217 });
218
219 });
Something went wrong with that request. Please try again.