Skip to content

HTTPS clone URL

Subversion checkout URL

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