Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 592 lines (497 sloc) 17.533 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')
d278a7e Get example app working with external model lib
mde authored
7 , utils = require('../lib/utils')
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
d278a7e Get example app working with external model lib
mde authored
11 namespace('env', function () {
12 task('init', function () {
13
14 jake.addListener('complete', function (e) {
15 jake.Task['env:cleanup'].invoke();
16 });
9e1fe04 Got generators working.
mde authored
17
18 geddy.config = require('../lib/config').readConfig();
19 geddy.model = require('model');
20
5da7b94 Cleaned up env init process a little more
mde authored
21 require('../lib/init').init(geddy, function () {
9e1fe04 Got generators working.
mde authored
22 complete();
23 });
24
25 }, {async: true});
d278a7e Get example app working with external model lib
mde authored
26
27 task('cleanup', function () {
28 // Disconnect all the adapters
48fb464 Fixed DB adapter connect/disconnect
mde authored
29 adapters = geddy.model.loadedAdapters
30 , adapter;
d278a7e Get example app working with external model lib
mde authored
31 for (var p in adapters) {
48fb464 Fixed DB adapter connect/disconnect
mde authored
32 adapter = adapters[p];
33 if (typeof adapter.disconnect == 'function') {
34 adapter.disconnect();
35 }
d278a7e Get example app working with external model lib
mde authored
36 }
37 });
38
39 }, {async: true});
40
41 namespace('db', function () {
48fb464 Fixed DB adapter connect/disconnect
mde authored
42
43 task('createTable', ['env:init'], function (name) {
9e1fe04 Got generators working.
mde authored
44 var modelName
48fb464 Fixed DB adapter connect/disconnect
mde authored
45 , createTable
d278a7e Get example app working with external model lib
mde authored
46 , adapters
9e1fe04 Got generators working.
mde authored
47 , adapter;
48
49 if (typeof name == 'string') {
50 if (name.indexOf(',') > -1) {
51 modelNames = name.split(',');
52 }
53 else {
54 modelNames = [name];
55 }
56 }
57 else {
58 modelNames = name;
59 }
60
d278a7e Get example app working with external model lib
mde authored
61 createTable = function () {
48fb464 Fixed DB adapter connect/disconnect
mde authored
62 if ((m = modelNames.shift())) {
9e1fe04 Got generators working.
mde authored
63
64 // Make sure this is a correct model-name
65 m = utils.string.getInflections(m).constructor.singular;
66 if (!geddy.model[m]) {
67 throw new Error(m + ' is not a known model.');
68 }
69
d278a7e Get example app working with external model lib
mde authored
70 adapter = geddy.model.adapters[m];
9e1fe04 Got generators working.
mde authored
71 if (adapter) {
72 console.log('Creating table for ' + m);
73 adapter.createTable(m, function (err, data) {
74 if (err) { throw err }
75 createTable();
76 });
77 }
78 else {
d278a7e Get example app working with external model lib
mde authored
79 createTable();
9e1fe04 Got generators working.
mde authored
80 }
d278a7e Get example app working with external model lib
mde authored
81 }
82 else {
83 complete();
84 }
85 };
86 createTable();
48fb464 Fixed DB adapter connect/disconnect
mde authored
87
88 }, {async: true});
89
90 task('retrofit', ['env:init'], function () {
91 var modelNames = Object.keys(geddy.model.descriptionRegistry)
92 , createTask = jake.Task['db:createTable'];
93 createTask.once('complete', function () {
94 complete();
95 });
96 createTask.invoke(modelNames);
d278a7e Get example app working with external model lib
mde authored
97 }, {async: true});
98
99 });
100
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
101 namespace('gen', function () {
102
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the defaul...
larzconwell authored
103 var _writeTemplate = function (name, filename, dirname, opts) {
104 var options = opts || {}
105 , names = utils.string.getInflections(name)
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
106 , text = fs.readFileSync(path.join(__dirname, filename + '.ejs'), 'utf8').toString()
cf999ea @larzconwell Deleted bare controller template
larzconwell authored
107 , bare = options.bare || false // Default to full controller
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
108 , templ
109 , filePath;
110
111 // Render with the right model name
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
112 templ = new adapter({data: {ext: '.ejs'}, text: text, templato: templato});
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
113 templ.process({names: names, bare: bare, properties: options.properties});
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
114
115 // Write file
116 filePath = path.join('app', dirname, names.filename[options.inflection] + '.js');
117 fs.writeFileSync(filePath, templ.markup, 'utf8');
118
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
119 console.log('[Added] ' + filePath);
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
120 };
121
edf26c7 Formatting
mde authored
122 var _formatModelProperties = function (properties) {
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the defaul...
larzconwell authored
123 var obj = {'default': {name: 'id', type: 'int'}};
9e1fe04 Got generators working.
mde authored
124 if (!properties) {
125 return obj;
126 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
127
128 var itemsArr = properties.split(' ')
129 , name
130 , type
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
131 , args
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
132 , i
133 , value;
134
26600c5 @larzconwell Just made a few loop optimizations for Array loops
larzconwell authored
135 i = -1;
136 while(++i < itemsArr.length) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
137 value = itemsArr[i];
138 name = value.replace(/:.*/g, '');
139 type = value.replace(/[a-zA-Z]*:?/, '');
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
140 args = type.replace(/[a-zA-Z]*:?/, '');
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
141
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
142 // Take off any args on the type
143 type = type.replace(/:.*/g, '');
144
9e1fe04 Got generators working.
mde authored
145 // Defaults and aliases
6967867 Misc. fixes to get generators working
mde authored
146 if (!type) type = 'string';
147 if (type === 'integer') type = 'int';
148 if (type === 'bool') type = 'boolean';
149 if (args === 'def') args = 'default';
150 if (type === 'default' || type === 'def') {
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
151 // If the type is `default` assume they want `string`
152 // type and want this property to be default
153 type = 'string';
9e2a69d @larzconwell If text attribute is given it switches to string, also took out placehol...
larzconwell authored
154 }
155 if (args === 'def') {
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
156 args = 'default';
157 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
158
f835d2f @larzconwell Move type checks for generatirs into a switch statement so can be easily...
larzconwell authored
159 switch(type) {
160 case 'text':
161 type = 'string';
162 break;
163 case 'integer':
164 type = 'int';
165 break;
166 case 'bool':
167 type = 'boolean';
168 break;
169 case 'default':
170 case 'def':
171 type = 'string';
172 args = 'default';
173 break;
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
174 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
175
f835d2f @larzconwell Move type checks for generatirs into a switch statement so can be easily...
larzconwell authored
176 // Set property as default if given the `default` argument
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
177 if(args === 'default') {
178 obj['default'] = {name: name, type: type};
179 // Add the original ID property
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the defaul...
larzconwell authored
180 obj['id'] = obj['id'] || {name: 'id', type: 'int'};
9e1fe04 Got generators working.
mde authored
181 }
182 else {
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
183 // If ID property is given and it matches the default
184 // then rewrite the default with the new ID property
6967867 Misc. fixes to get generators working
mde authored
185 if (name === 'id' && obj['default'].name === 'id') {
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
186 obj['default'] = {name: name, type: type};
9e1fe04 Got generators working.
mde authored
187 }
188 else {
f835d2f @larzconwell Move type checks for generatirs into a switch statement so can be easily...
larzconwell authored
189 // Add the property normally
9e1fe04 Got generators working.
mde authored
190 obj[name] = {name: name, type: type};
191 }
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
192 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
193 }
194
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
195 return obj;
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
196 };
197
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
198 // Creates a new Geddy app scaffold
9e1fe04 Got generators working.
mde authored
199 task('app', function (name, engine) {
200 if (!name) {
201 throw new Error('No app name specified.');
202 }
203 if (!engine) {
204 engine = 'ejs';
205 }
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
206
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
207 var mkdirs = [
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
208 ''
209 , 'config'
210 , 'app/models'
211 , 'app/controllers'
212 , 'lib'
213 , 'log'
214 , 'node_modules'
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
215 ]
216 , cps = [
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
217 ['views/' + engine, 'app/views']
218 , ['public', '']
219 , ['router.js', 'config']
220 , ['init.js', 'config']
221 , ['environment.js', 'config']
222 , ['development.js', 'config']
223 , ['production.js', 'config']
224 , ['main.js', 'app/controllers']
225 , ['application.js', 'app/controllers']
226 , ['favicon.ico', 'public']
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
227 ];
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
228
edf26c7 Formatting
mde authored
229 mkdirs.forEach(function (dir) {
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
230 jake.mkdirP(path.join(name, dir));
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
231 });
edf26c7 Formatting
mde authored
232 cps.forEach(function (cp) {
229b0bd @larzconwell Made app copying silent, and hid ID property in model and controller tem...
larzconwell authored
233 jake.cpR(path.join(__dirname, 'base', cp[0]), path.join(name, cp[1]), {silent: true});
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
234 });
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
235
236 console.log('Created app ' + name + '.');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
237 });
238
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
239 // Creates a resource with a model, controller and a resource route
edf26c7 Formatting
mde authored
240 task('resource', function (name, modelProperties) {
9e1fe04 Got generators working.
mde authored
241 var names
242 , modelTask = jake.Task['gen:model'];
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
243
9e1fe04 Got generators working.
mde authored
244 if (!name) {
245 throw new Error('No resource name specified.');
246 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
247
9e1fe04 Got generators working.
mde authored
248 modelTask.on('complete', function () {
249 jake.Task['gen:controller'].invoke(name);
250 jake.Task['gen:route'].invoke(name);
251 names = utils.string.getInflections(name);
252 // Create views folder but not actions
253 jake.mkdirP(path.join('app', 'views', names.filename.plural));
254 console.log('[Added] ' + names.filename.plural + ' view directory');
255 complete();
256 });
257 modelTask.invoke(name, modelProperties);
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
258
9e1fe04 Got generators working.
mde authored
259 }, {async: true});
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
260
229b0bd @larzconwell Made app copying silent, and hid ID property in model and controller tem...
larzconwell authored
261
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
262 // Creates a full scaffold with views, a model, controller and a resource route
edf26c7 Formatting
mde authored
263 task('scaffold', function (name, engine, modelProperties) {
9e1fe04 Got generators working.
mde authored
264 var modelTask = jake.Task['gen:model'];
265
6967867 Misc. fixes to get generators working
mde authored
266 if (!modelProperties && engine) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
267 modelProperties = engine;
268 engine = '';
269 }
9e1fe04 Got generators working.
mde authored
270 if (!name) {
271 throw new Error('No scaffold name specified.');
272 }
273 if (!engine) {
274 engine = 'ejs';
275 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
276
9e1fe04 Got generators working.
mde authored
277 modelTask.on('complete', function () {
278 jake.Task['gen:controllerScaffold'].invoke(name,
279 {properties: modelProperties});
280 jake.Task['gen:route'].invoke(name);
281 jake.Task['gen:viewsScaffold'].invoke(name,
282 {engine: engine, properties: modelProperties});
283 complete();
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
284 });
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the defaul...
larzconwell authored
285 modelTask.invoke(name, modelProperties, 'scaffold');
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
286
9e1fe04 Got generators working.
mde authored
287 }, {async: true});
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
288
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the defaul...
larzconwell authored
289 task('model', function (name, properties, modelPath) {
9e1fe04 Got generators working.
mde authored
290 var createTableTask;
291 if (!name) {
292 throw new Error('No model name specified.');
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
293 }
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the defaul...
larzconwell authored
294 if (!modelPath) {
295 modelPath = 'resource';
296 }
297 modelPath += '/model';
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
298
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the defaul...
larzconwell authored
299 _writeTemplate(name, modelPath, 'models', {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
300 inflection: 'singular'
301 , properties: _formatModelProperties(properties)
302 });
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
303
9e1fe04 Got generators working.
mde authored
304 // Try to create a table -- should be a no-op if an
305 // appropriate DB adapter can't be found
306 createTableTask = jake.Task['db:createTable'];
307 createTableTask.on('complete', function () {
308 complete();
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
309 });
9e1fe04 Got generators working.
mde authored
310 createTableTask.invoke(name);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
311
9e1fe04 Got generators working.
mde authored
312 }, {async: true});
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
313
9e1fe04 Got generators working.
mde authored
314 task('controller', function (name) {
315 if (!name) {
316 throw new Error('No controller name specified.');
317 }
318
319 _writeTemplate(name, 'resource/controller', 'controllers',
320 {inflection: 'plural', bare: false});
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
321 });
322
9e1fe04 Got generators working.
mde authored
323 task('controllerScaffold', function (name, options) {
324 if (!name) {
325 throw new Error('No controller name specified.');
326 }
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
327 options = options || {};
328
329 _writeTemplate(name, 'scaffold/controller', 'controllers', {
330 inflection: 'plural'
331 , bare: false
332 , properties: _formatModelProperties(options.properties)
333 });
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
334 });
335
9e1fe04 Got generators working.
mde authored
336 task('bareController', function (name, engine) {
337 if (!name) {
338 throw new Error('No controller name specified.');
339 }
340 if (!engine) {
341 engine = 'ejs';
342 }
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
343
344 _writeTemplate(name, 'resource/controller', 'controllers', {inflection: 'plural', bare: true});
345 jake.Task['gen:route'].invoke(name, {bare: true});
346 jake.Task['gen:views'].invoke(name, {bare: true, engine: engine});
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
347 });
348
9e1fe04 Got generators working.
mde authored
349 task('route', function (name, options) {
350 if (!name) {
351 throw new Error('No route name specified.');
352 }
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
353 options = options || {};
354
d92f157 Get generators working
mde authored
355 var names = utils.string.getInflections(name)
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
356 , routeType = options.bare ? 'Bare' : 'Resource'
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
357 , jsRouter = path.normalize('config/router.js')
358 , coffeeRouter = path.normalize('config/router.coffee')
359 , routerPath
360 , routerArr
361 , text
362 , splitText
363 , newRoute;
364
365 // Check if the router file exists
edf26c7 Formatting
mde authored
366 utils.file.searchParentPath(jsRouter, function (err) {
6967867 Misc. fixes to get generators working
mde authored
367 if (err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
368 var jsErr = err;
369 // If jsEnvironment wasn't found, try finding coffee variant
edf26c7 Formatting
mde authored
370 utils.file.searchParentPath(coffeeRouter, function (err) {
6967867 Misc. fixes to get generators working
mde authored
371 if (err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
372 throw jsErr;
373 } else {
374 routerPath = coffeeRouter;
375 }
376 });
377 } else {
378 routerPath = jsRouter;
379 }
380 });
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
381
6967867 Misc. fixes to get generators working
mde authored
382 if (routerPath) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
383 text = fs.readFileSync(routerPath, 'utf8');
384
6967867 Misc. fixes to get generators working
mde authored
385 if (routerPath.match('.coffee')) {
386 if (options.bare) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
387 newRoute = 'router.match(\'/' + names.filename.plural +
388 '\').to controller: \'' + names.constructor.plural +
389 '\', action: \'index\'';
baa8cdf @larzconwell Updated /site to reflect new features related to generators and template...
larzconwell authored
390 } else {
391 newRoute = 'router.resource \'' + names.filename.plural + '\'';
392 }
6967867 Misc. fixes to get generators working
mde authored
393 } else if (routerPath.match('.js')) {
394 if (options.bare) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
395 newRoute = 'router.match(\'/' + names.filename.plural +
396 '\').to({controller: \'' + names.constructor.plural +
397 '\', action: \'index\'});';
baa8cdf @larzconwell Updated /site to reflect new features related to generators and template...
larzconwell authored
398 } else {
399 newRoute = 'router.resource(\'' + names.filename.plural + '\');';
400 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
401 }
402
403 // Don't add the same route over and over
6967867 Misc. fixes to get generators working
mde authored
404 if (text.indexOf(newRoute) == -1) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
405 // Add the new resource route just above the export
406 routerArr = text.split('exports.router');
407 routerArr[0] += newRoute + '\n';
408
409 text = routerArr.join('exports.router');
410 fs.writeFileSync(routerPath, text, 'utf8');
411
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
412 console.log('[Added] ' + routeType + ' ' + names.filename.plural +
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
413 ' route added to ' + routerPath);
414 }
415 else {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
416 console.log(routeType + ' ' + names.filename.plural + ' route already defined in ' +
417 routerPath);
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
418 }
419
baa8cdf @larzconwell Updated /site to reflect new features related to generators and template...
larzconwell authored
420 } else {
421 console.log('There is no router file to add routes too');
422 }
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
423 });
424
9e1fe04 Got generators working.
mde authored
425 task('views', function (name, options) {
6967867 Misc. fixes to get generators working
mde authored
426 if (!name) throw new Error('No view name specified.');
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
427
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
428 options = options || {};
429
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
430 // Option defaults
431 options.engine = options.engine || 'ejs';
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
432
d92f157 Get generators working
mde authored
433 var names = utils.string.getInflections(name)
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
434 , engine = options.engine
435 , appViewDir = path.join('app', 'views', names.filename.plural)
436 , templateViewDir = path.join(__dirname, 'views', engine)
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
437 , cmds = []
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
438 , ext = '.html'
439 , appLayoutPath
440 , actions
441 , addActionView;
442
443 // Set extension based on engine option
6967867 Misc. fixes to get generators working
mde authored
444 if (engine === 'ejs') ext += '.ejs';
445 if (engine === 'jade') ext += '.jade';
446 if (engine === 'handlebars') ext += '.hbs';
447 if (engine === 'mustache') ext += '.ms';
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
448
449 // Set application layout path
450 appLayoutPath = path.join('app', 'views', 'layouts', 'application');
451 // Copy template view to application path
edf26c7 Formatting
mde authored
452 addActionView = function (action) {
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
453 jake.cpR(path.join(templateViewDir, action + ext), appViewDir);
454 };
455
456 jake.mkdirP(appViewDir);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
457 jake.mkdirP('app/views/layouts');
458 addActionView('index');
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
459
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
460 // Add views for the other CRUD actions when doing a full-on resource
6967867 Misc. fixes to get generators working
mde authored
461 if (!options.bare) {
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
462 ['add', 'edit', 'show'].forEach(function (action) {
463 addActionView(action);
464 });
465 }
466
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
467 // Create default layout if one doesn't exist
468 // Hack: There should be a better way to detect if a application layout exists
6967867 Misc. fixes to get generators working
mde authored
469 if (!utils.file.existsSync(appLayoutPath + '.html.ejs') && !utils.file.existsSync(appLayoutPath + '.html.jade') &&
470 !utils.file.existsSync(appLayoutPath + '.html.hbs') && !utils.file.existsSync(appLayoutPath + '.html.ms')) {
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
471 // Copy template layout file to apps application layout file
472 jake.cpR(path.join(templateViewDir, 'layout' + ext), appLayoutPath + ext);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
473 }
474
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
475 console.log('[Added] View templates');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
476 });
477
edf26c7 Formatting
mde authored
478 task('viewsScaffold', function (name, options) {
6967867 Misc. fixes to get generators working
mde authored
479 if (!name) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
480 throw new Error('No view name specified.');
481 }
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
482
483 options = options || {};
484
485 // Option defaults
486 options.engine = options.engine || 'ejs';
487
d92f157 Get generators working
mde authored
488 var names = utils.string.getInflections(name)
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
489 , engine = options.engine
490 , appViewDir = path.join('app', 'views', names.filename.plural)
491 , templateViewDir = path.join(__dirname, 'scaffold', 'views', engine)
492 , cmds = []
493 , ext = '.html'
494 , appLayoutPath
495 , actions
496 , addActionView
497 , text
498 , templ;
499
500 // Set extension based on engine option
6967867 Misc. fixes to get generators working
mde authored
501 if (engine === 'ejs') ext += '.ejs';
502 if (engine === 'jade') ext += '.jade';
503 if (engine === 'handlebars') ext += '.hbs';
504 if (engine === 'mustache') ext += '.ms';
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
505
506 // Set application layout path
507 appLayoutPath = path.join('app', 'views', 'layouts', 'application');
508
509 // Function to compile the template
edf26c7 Formatting
mde authored
510 addActionView = function (action) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
511 text = fs.readFileSync(path.join(templateViewDir, action + ext + '.ejs')).toString();
512
513 // Compile template text
514 templ = new adapter({data: {ext: '.ejs'}, text: text, templato: templato});
515 templ.process({names: names, properties: _formatModelProperties(options.properties)});
516
517 // Write file
518 fs.writeFileSync(path.join(appViewDir, action + ext),
519 templ.markup.replace(/<@/g, '<%').replace(/@>/g, '%>'), 'utf8');
520 };
521
522 jake.mkdirP(appViewDir);
523 jake.mkdirP('app/views/layouts');
524
525 // Add views for the other CRUD actions when doing a full-on resource
edf26c7 Formatting
mde authored
526 ['index', 'add', 'show', 'edit'].forEach(function (action) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
527 addActionView(action);
528 });
529
530 // Create default layout if one doesn't exist
531 // Hack: There should be a better way to detect if a application layout exists
6967867 Misc. fixes to get generators working
mde authored
532 if (!utils.file.existsSync(appLayoutPath + '.html.ejs') && !utils.file.existsSync(appLayoutPath + '.html.jade') &&
533 !utils.file.existsSync(appLayoutPath + '.html.hbs') && !utils.file.existsSync(appLayoutPath + '.html.ms')) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
534 // Copy template layout file to apps application layout file
535 jake.cpR(path.join(templateViewDir, 'layout' + ext), appLayoutPath + ext);
536 }
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
537
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
538 console.log('[Added] View templates');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
539 });
540
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
541 // Generate a new application secret in environment.js
9e1fe04 Got generators working.
mde authored
542 task('secret', function () {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
543 var jsEnvironment = path.normalize('config/environment.js')
544 , coffeeEnvironment = path.normalize('config/environment.coffee')
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
545 , secret = utils.string.uuid(128)
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
546 , environmentPath
547 , environmentArr
548 , text
549 , splitText;
550
551 // Check if the environment file exists
edf26c7 Formatting
mde authored
552 utils.file.searchParentPath(jsEnvironment, function (err) {
6967867 Misc. fixes to get generators working
mde authored
553 if (err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
554 var jsErr = err;
555 // If jsEnvironment wasn't found, try finding coffee variant
edf26c7 Formatting
mde authored
556 utils.file.searchParentPath(coffeeEnvironment, function (err) {
6967867 Misc. fixes to get generators working
mde authored
557 if (err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
558 throw jsErr;
559 } else {
560 environmentPath = coffeeEnvironment;
561 }
562 });
563 } else {
564 environmentPath = jsEnvironment;
565 }
566 });
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
567
6967867 Misc. fixes to get generators working
mde authored
568 if (environmentPath) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
569 text = fs.readFileSync(environmentPath, 'utf8').toString();
570
571 // Remove any old secret
572 text = text.replace(/\nconfig.secret.+\n/, '');
573
574 splitText = 'module.exports = config';
6967867 Misc. fixes to get generators working
mde authored
575 if (environmentPath.match('.js')) splitText += ';';
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
576
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
577 environmentArr = text.split(splitText);
578 environmentArr[0] += "config.secret = '" + secret + "'";
579 environmentArr[0] += environmentPath.match('.js') ? ';\n\n' : '\n\n';
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
580
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
581 text = environmentArr.join(splitText);
582 fs.writeFileSync(environmentPath, text, 'utf8');
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
583
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
584 console.log('[Added] Application secret to \'' + environmentPath + '\'');
6967867 Misc. fixes to get generators working
mde authored
585 }
9e1fe04 Got generators working.
mde authored
586 else {
587 console.log('There is no environment config to add the app-secret to.');
588 }
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
589 });
590
591 });
Something went wrong with that request. Please try again.