Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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