Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 493 lines (410 sloc) 16.394 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')
d92f157 Get generators working
mde authored
7 , utils = require('../node_modules/utilities')
3b29a7e @larzconwell Fixed bug where apps couldn't be generated and resources couldn't be cre...
larzconwell authored
8 , adapter = require('../lib/template/adapters').Adapter
9 , templato = new (require('../deps/templato'));
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
10
11 namespace('gen', function () {
12
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
13 var _writeTemplate = function (name, filename, dirname, options) {
14 options = options || {};
15
d92f157 Get generators working
mde authored
16 var names = utils.string.getInflections(name)
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
17 , text = fs.readFileSync(path.join(__dirname, filename + '.ejs'), 'utf8').toString()
cf999ea @larzconwell Deleted bare controller template
larzconwell authored
18 , bare = options.bare || false // Default to full controller
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
19 , templ
20 , filePath;
21
22 // Render with the right model name
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
23 templ = new adapter({data: {ext: '.ejs'}, text: text, templato: templato});
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
24 templ.process({names: names, bare: bare, properties: options.properties});
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
25
26 // Write file
27 filePath = path.join('app', dirname, names.filename[options.inflection] + '.js');
28 fs.writeFileSync(filePath, templ.markup, 'utf8');
29
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
30 console.log('[Added] ' + filePath);
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
31 };
32
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
33 var _formatModelProperties = function(properties) {
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
34 var obj = {'default': {name: 'id', type: 'string'}};
35 if(!properties) return obj;
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
36
37 var itemsArr = properties.split(' ')
38 , name
39 , type
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
40 , args
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
41 , i
42 , value;
43
26600c5 @larzconwell Just made a few loop optimizations for Array loops
larzconwell authored
44 i = -1;
45 while(++i < itemsArr.length) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
46 value = itemsArr[i];
47 name = value.replace(/:.*/g, '');
48 type = value.replace(/[a-zA-Z]*:?/, '');
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
49 args = type.replace(/[a-zA-Z]*:?/, '');
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
50
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
51 // Take off any args on the type
52 type = type.replace(/:.*/g, '');
53
54 // Defaults and alias's
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
55 if(!type) type = 'string';
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
56 if(type === 'integer') type = 'int';
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
57 if(type === 'bool') type = 'boolean';
58 if(args === 'def') args = 'default';
59 if(type === 'default' || type === 'def') {
60 // If the type is `default` assume they want `string`
61 // type and want this property to be default
62 type = 'string';
63 args = 'default';
64 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
65
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
66 // Set it as default if given the `default` argument
67 if(args === 'default') {
68 obj['default'] = {name: name, type: type};
69 // Add the original ID property
70 obj['id'] = obj['id'] || {name: 'id', type: 'string'};
71 } else {
72 // If ID property is given and it matches the default
73 // then rewrite the default with the new ID property
74 if(name === 'id' && obj['default'].name === 'id') {
75 obj['default'] = {name: name, type: type};
76 } else obj[name] = {name: name, type: type};
77 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
78 }
79
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
80 return obj;
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
81 };
82
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
83 // Creates a new Geddy app scaffold
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
84 task('app', [], function(name, engine) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
85 if(!name) throw new Error('No app name specified.');
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
86 if(!engine) engine = 'ejs';
87
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
88 var mkdirs = [
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
89 ''
90 , 'config'
91 , 'app/models'
92 , 'app/controllers'
93 , 'lib'
94 , 'log'
95 , 'node_modules'
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
96 ]
97 , cps = [
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
98 ['views/' + engine, 'app/views']
99 , ['public', '']
100 , ['router.js', 'config']
101 , ['init.js', 'config']
102 , ['environment.js', 'config']
103 , ['development.js', 'config']
104 , ['production.js', 'config']
105 , ['main.js', 'app/controllers']
106 , ['application.js', 'app/controllers']
107 , ['favicon.ico', 'public']
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
108 ];
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
109
110 mkdirs.forEach(function(dir) {
111 jake.mkdirP(path.join(name, dir));
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
112 });
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
113 cps.forEach(function(cp) {
114 jake.cpR(path.join(__dirname, 'base', cp[0]), path.join(name, cp[1]));
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
115 });
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
116
117 console.log('Created app ' + name + '.');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
118 });
119
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
120 // Creates a resource with a model, controller and a resource route
121 task('resource', function(name, modelProperties) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
122 if(!name) throw new Error('No resource name specified.');
d92f157 Get generators working
mde authored
123 var names = utils.string.getInflections(name);
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
124
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
125 jake.Task['gen:model'].invoke(name, modelProperties);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
126 jake.Task['gen:controller'].invoke(name);
127 jake.Task['gen:route'].invoke(name);
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
128
129 // Create views folder but not actions
130 jake.mkdirP(path.join('app', 'views', names.filename.plural));
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
131
132 console.log('[Added] ' + names.filename.plural + ' view directory');
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
133 });
134
135 // Creates a full scaffold with views, a model, controller and a resource route
136 task('scaffold', function(name, engine, modelProperties) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
137 if(!modelProperties && engine) {
138 modelProperties = engine;
139 engine = '';
140 }
141 if(!name) throw new Error('No scaffold name specified.');
142 if(!engine) engine = 'ejs';
143
d92f157 Get generators working
mde authored
144 var names = utils.string.getInflections(name)
034b2c1 @larzconwell Added a few tests for helpers and changed the scaffold generator so that...
larzconwell authored
145 , jsEnvironment = path.normalize('config/environment.js')
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
146 , coffeeEnvironment = path.normalize('config/environment.coffee')
147 , environmentPath
148 , environmentArr
149 , text
150 , splitText
151 , dbContent = '';
152
153 // Check if the environment file exists
d92f157 Get generators working
mde authored
154 utils.file.searchParentPath(jsEnvironment, function(err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
155 if(err) {
156 var jsErr = err;
157 // If jsEnvironment wasn't found, try finding coffee variant
d92f157 Get generators working
mde authored
158 utils.file.searchParentPath(coffeeEnvironment, function(err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
159 if(err) {
160 throw jsErr;
161 } else {
162 environmentPath = coffeeEnvironment;
163 }
164 });
165 } else {
166 environmentPath = jsEnvironment;
167 }
168 });
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
169
170 jake.Task['gen:modelScaffold'].invoke(name, modelProperties);
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
171 jake.Task['gen:controllerScaffold'].invoke(name, {properties: modelProperties});
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
172 jake.Task['gen:route'].invoke(name);
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
173 jake.Task['gen:viewsScaffold'].invoke(name, {engine: engine, properties: modelProperties});
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
174
175 // Add the following line to `config/environment.js/.coffee`
176 if(environmentPath) {
177 text = fs.readFileSync(environmentPath, 'utf8').toString();
178
179 // Create text to add to environment
180 if(environmentPath.match('.coffee')) {
181 dbContent = [
182 ' db:'
183 , ' mongo:'
034b2c1 @larzconwell Added a few tests for helpers and changed the scaffold generator so that...
larzconwell authored
184 , ' db: \'' + names.property.plural + '\''
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
185 ].join('\n');
186 splitText = 'config = ';
187 } else if(environmentPath.match('.js')) {
188 dbContent = [
189 ' db: {'
190 , ' mongo: {'
034b2c1 @larzconwell Added a few tests for helpers and changed the scaffold generator so that...
larzconwell authored
191 , ' db: \'' + names.property.plural + '\''
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
192 , ' }'
193 , ' }'
194 ].join('\n');
195 splitText = 'var config = {';
196 }
197
198 // Don't add the db config over and over
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
199 if(text.indexOf('db:') == -1) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
200 environmentArr = text.split(splitText);
201 environmentArr[0] += splitText + '\n' + dbContent + '\n';
202
203 text = environmentArr.join('');
204 fs.writeFileSync(environmentPath, text, 'utf8');
205
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
206 console.log('[Added] Database configuration to ' + environmentPath);
207 } else console.log('Database configuration already set in ' + environmentPath);
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
208 }
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
209 });
210
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
211 task('model', [], function(name, properties) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
212 if(!name) throw new Error('No model name specified.');
213
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
214 _writeTemplate(name, 'resource/model', 'models', {
215 inflection: 'singular'
216 , properties: _formatModelProperties(properties)
217 });
218 });
219
220 task('modelScaffold', [], function(name, properties) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
221 if(!name) throw new Error('No model name specified.');
222
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
223 _writeTemplate(name, 'scaffold/model', 'models', {
224 inflection: 'singular'
225 , properties: _formatModelProperties(properties)
226 });
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
227 });
228
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
229 task('controller', [], function(name) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
230 if(!name) throw new Error('No controller name specified.');
231
232 _writeTemplate(name, 'resource/controller', 'controllers', {inflection: 'plural', bare: false});
233 });
234
235 task('controllerScaffold', [], function(name, options) {
236 if(!name) throw new Error('No controller name specified.');
237 options = options || {};
238
239 _writeTemplate(name, 'scaffold/controller', 'controllers', {
240 inflection: 'plural'
241 , bare: false
242 , properties: _formatModelProperties(options.properties)
243 });
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
244 });
245
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
246 task('bareController', [], function(name, engine) {
247 if(!name) throw new Error('No controller name specified.');
248 if(!engine) engine = 'ejs';
249
250 _writeTemplate(name, 'resource/controller', 'controllers', {inflection: 'plural', bare: true});
251 jake.Task['gen:route'].invoke(name, {bare: true});
252 jake.Task['gen:views'].invoke(name, {bare: true, engine: engine});
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
253 });
254
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
255 task('route', [], function(name, options) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
256 if(!name) throw new Error('No route name specified.');
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
257 options = options || {};
258
d92f157 Get generators working
mde authored
259 var names = utils.string.getInflections(name)
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
260 , routeType = options.bare ? 'Bare' : 'Resource'
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
261 , jsRouter = path.normalize('config/router.js')
262 , coffeeRouter = path.normalize('config/router.coffee')
263 , routerPath
264 , routerArr
265 , text
266 , splitText
267 , newRoute;
268
269 // Check if the router file exists
d92f157 Get generators working
mde authored
270 utils.file.searchParentPath(jsRouter, function(err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
271 if(err) {
272 var jsErr = err;
273 // If jsEnvironment wasn't found, try finding coffee variant
d92f157 Get generators working
mde authored
274 utils.file.searchParentPath(coffeeRouter, function(err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
275 if(err) {
276 throw jsErr;
277 } else {
278 routerPath = coffeeRouter;
279 }
280 });
281 } else {
282 routerPath = jsRouter;
283 }
284 });
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
285
286 if(routerPath) {
287 text = fs.readFileSync(routerPath, 'utf8');
288
289 if(routerPath.match('.coffee')) {
290 if(options.bare) {
291 newRoute = 'router.match(\'/' + names.filename.plural +
292 '\').to controller: \'' + names.constructor.plural +
293 '\', action: \'index\'';
baa8cdf @larzconwell Updated /site to reflect new features related to generators and template...
larzconwell authored
294 } else {
295 newRoute = 'router.resource \'' + names.filename.plural + '\'';
296 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
297 } else if(routerPath.match('.js')) {
298 if(options.bare) {
299 newRoute = 'router.match(\'/' + names.filename.plural +
300 '\').to({controller: \'' + names.constructor.plural +
301 '\', action: \'index\'});';
baa8cdf @larzconwell Updated /site to reflect new features related to generators and template...
larzconwell authored
302 } else {
303 newRoute = 'router.resource(\'' + names.filename.plural + '\');';
304 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
305 }
306
307 // Don't add the same route over and over
308 if(text.indexOf(newRoute) == -1) {
309 // Add the new resource route just above the export
310 routerArr = text.split('exports.router');
311 routerArr[0] += newRoute + '\n';
312
313 text = routerArr.join('exports.router');
314 fs.writeFileSync(routerPath, text, 'utf8');
315
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
316 console.log('[Added] ' + routeType + ' ' + names.filename.plural +
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
317 ' route added to ' + routerPath);
318 }
319 else {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
320 console.log(routeType + ' ' + names.filename.plural + ' route already defined in ' +
321 routerPath);
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
322 }
323
baa8cdf @larzconwell Updated /site to reflect new features related to generators and template...
larzconwell authored
324 } else {
325 console.log('There is no router file to add routes too');
326 }
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
327 });
328
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
329 task('views', [], function(name, options) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
330 if(!name) throw new Error('No view name specified.');
331
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
332 options = options || {};
333
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
334 // Option defaults
335 options.engine = options.engine || 'ejs';
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
336
d92f157 Get generators working
mde authored
337 var names = utils.string.getInflections(name)
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
338 , engine = options.engine
339 , appViewDir = path.join('app', 'views', names.filename.plural)
340 , templateViewDir = path.join(__dirname, 'views', engine)
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
341 , cmds = []
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
342 , ext = '.html'
343 , appLayoutPath
344 , actions
345 , addActionView;
346
347 // Set extension based on engine option
348 if(engine === 'ejs') ext += '.ejs';
349 if(engine === 'jade') ext += '.jade';
350 if(engine === 'handlebars') ext += '.hbs';
351 if(engine === 'mustache') ext += '.ms';
352
353 // Set application layout path
354 appLayoutPath = path.join('app', 'views', 'layouts', 'application');
355 // Copy template view to application path
356 addActionView = function(action) {
357 jake.cpR(path.join(templateViewDir, action + ext), appViewDir);
358 };
359
360 jake.mkdirP(appViewDir);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
361 jake.mkdirP('app/views/layouts');
362 addActionView('index');
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
363
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
364 // 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
365 if(!options.bare) {
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
366 ['add', 'edit', 'show'].forEach(function (action) {
367 addActionView(action);
368 });
369 }
370
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
371 // Create default layout if one doesn't exist
372 // Hack: There should be a better way to detect if a application layout exists
40b7a7d @larzconwell Removed unused requires throughout
larzconwell authored
373 if(!utils.compat.existsSync(appLayoutPath + '.html.ejs') && !utils.compat.existsSync(appLayoutPath + '.html.jade') &&
374 !utils.compat.existsSync(appLayoutPath + '.html.hbs') && !utils.compat.existsSync(appLayoutPath + '.html.ms')) {
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
375 // Copy template layout file to apps application layout file
376 jake.cpR(path.join(templateViewDir, 'layout' + ext), appLayoutPath + ext);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
377 }
378
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
379 console.log('[Added] View templates');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
380 });
381
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
382 task('viewsScaffold', function(name, options) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
383 if(!name) {
384 throw new Error('No view name specified.');
385 }
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
386
387 options = options || {};
388
389 // Option defaults
390 options.engine = options.engine || 'ejs';
391
d92f157 Get generators working
mde authored
392 var names = utils.string.getInflections(name)
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
393 , engine = options.engine
394 , appViewDir = path.join('app', 'views', names.filename.plural)
395 , templateViewDir = path.join(__dirname, 'scaffold', 'views', engine)
396 , cmds = []
397 , ext = '.html'
398 , appLayoutPath
399 , actions
400 , addActionView
401 , text
402 , templ;
403
404 // Set extension based on engine option
405 if(engine === 'ejs') ext += '.ejs';
406 if(engine === 'jade') ext += '.jade';
407 if(engine === 'handlebars') ext += '.hbs';
408 if(engine === 'mustache') ext += '.ms';
409
410 // Set application layout path
411 appLayoutPath = path.join('app', 'views', 'layouts', 'application');
412
413 // Function to compile the template
414 addActionView = function(action) {
415 text = fs.readFileSync(path.join(templateViewDir, action + ext + '.ejs')).toString();
416
417 // Compile template text
418 templ = new adapter({data: {ext: '.ejs'}, text: text, templato: templato});
419 templ.process({names: names, properties: _formatModelProperties(options.properties)});
420
421 // Write file
422 fs.writeFileSync(path.join(appViewDir, action + ext),
423 templ.markup.replace(/<@/g, '<%').replace(/@>/g, '%>'), 'utf8');
424 };
425
426 jake.mkdirP(appViewDir);
427 jake.mkdirP('app/views/layouts');
428
429 // Add views for the other CRUD actions when doing a full-on resource
430 ['index', 'add', 'show', 'edit'].forEach(function(action) {
431 addActionView(action);
432 });
433
434 // Create default layout if one doesn't exist
435 // Hack: There should be a better way to detect if a application layout exists
40b7a7d @larzconwell Removed unused requires throughout
larzconwell authored
436 if(!utils.compat.existsSync(appLayoutPath + '.html.ejs') && !utils.compat.existsSync(appLayoutPath + '.html.jade') &&
437 !utils.compat.existsSync(appLayoutPath + '.html.hbs') && !utils.compat.existsSync(appLayoutPath + '.html.ms')) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
438 // Copy template layout file to apps application layout file
439 jake.cpR(path.join(templateViewDir, 'layout' + ext), appLayoutPath + ext);
440 }
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
441
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
442 console.log('[Added] View templates');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
443 });
444
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
445 // Generate a new application secret in environment.js
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
446 task('secret', [], function() {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
447 var jsEnvironment = path.normalize('config/environment.js')
448 , coffeeEnvironment = path.normalize('config/environment.coffee')
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
449 , secret = utils.string.uuid(128)
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
450 , environmentPath
451 , environmentArr
452 , text
453 , splitText;
454
455 // Check if the environment file exists
d92f157 Get generators working
mde authored
456 utils.file.searchParentPath(jsEnvironment, function(err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
457 if(err) {
458 var jsErr = err;
459 // If jsEnvironment wasn't found, try finding coffee variant
d92f157 Get generators working
mde authored
460 utils.file.searchParentPath(coffeeEnvironment, function(err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
461 if(err) {
462 throw jsErr;
463 } else {
464 environmentPath = coffeeEnvironment;
465 }
466 });
467 } else {
468 environmentPath = jsEnvironment;
469 }
470 });
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
471
472 if(environmentPath) {
473 text = fs.readFileSync(environmentPath, 'utf8').toString();
474
475 // Remove any old secret
476 text = text.replace(/\nconfig.secret.+\n/, '');
477
478 splitText = 'module.exports = config';
479 if(environmentPath.match('.js')) splitText += ';';
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
480
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
481 environmentArr = text.split(splitText);
482 environmentArr[0] += "config.secret = '" + secret + "'";
483 environmentArr[0] += environmentPath.match('.js') ? ';\n\n' : '\n\n';
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
484
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
485 text = environmentArr.join(splitText);
486 fs.writeFileSync(environmentPath, text, 'utf8');
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
487
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
488 console.log('[Added] Application secret to \'' + environmentPath + '\'');
489 } else console.log('There is no environment config to add the secret too');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
490 });
491
492 });
Something went wrong with that request. Please try again.