Skip to content

HTTPS clone URL

Subversion checkout URL

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