Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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