Skip to content

HTTPS clone URL

Subversion checkout URL

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