Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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