Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 568 lines (474 sloc) 16.996 kb
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
1 // Load the basic Geddy toolkit
2 require('../lib/geddy')
3
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
4 // Dependencies
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
5 var fs = require('fs')
6 , path = require('path')
d278a7e Get example app working with external model lib
mde authored
7 , utils = require('../lib/utils')
3b29a7e @larzconwell Fixed bug where apps couldn't be generated and resources couldn't be …
larzconwell authored
8 , adapter = require('../lib/template/adapters').Adapter
9 , templato = new (require('../deps/templato'));
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
10
d278a7e Get example app working with external model lib
mde authored
11 namespace('env', function () {
12 task('init', function () {
13
14 jake.addListener('complete', function (e) {
15 jake.Task['env:cleanup'].invoke();
16 });
9e1fe04 Got generators working.
mde authored
17
18 geddy.config = require('../lib/config').readConfig();
19 geddy.model = require('model');
20
21 require('../lib/init').init(function () {
22 complete();
23 });
24
25 }, {async: true});
d278a7e Get example app working with external model lib
mde authored
26
27 task('cleanup', function () {
28 // Disconnect all the adapters
48fb464 Fixed DB adapter connect/disconnect
mde authored
29 adapters = geddy.model.loadedAdapters
30 , adapter;
d278a7e Get example app working with external model lib
mde authored
31 for (var p in adapters) {
48fb464 Fixed DB adapter connect/disconnect
mde authored
32 adapter = adapters[p];
33 if (typeof adapter.disconnect == 'function') {
34 adapter.disconnect();
35 }
d278a7e Get example app working with external model lib
mde authored
36 }
37 });
38
39 }, {async: true});
40
41 namespace('db', function () {
48fb464 Fixed DB adapter connect/disconnect
mde authored
42
43 task('createTable', ['env:init'], function (name) {
9e1fe04 Got generators working.
mde authored
44 var modelName
48fb464 Fixed DB adapter connect/disconnect
mde authored
45 , createTable
d278a7e Get example app working with external model lib
mde authored
46 , adapters
9e1fe04 Got generators working.
mde authored
47 , adapter;
48
49 if (typeof name == 'string') {
50 if (name.indexOf(',') > -1) {
51 modelNames = name.split(',');
52 }
53 else {
54 modelNames = [name];
55 }
56 }
57 else {
58 modelNames = name;
59 }
60
d278a7e Get example app working with external model lib
mde authored
61 createTable = function () {
48fb464 Fixed DB adapter connect/disconnect
mde authored
62 if ((m = modelNames.shift())) {
9e1fe04 Got generators working.
mde authored
63
64 // Make sure this is a correct model-name
65 m = utils.string.getInflections(m).constructor.singular;
66 if (!geddy.model[m]) {
67 throw new Error(m + ' is not a known model.');
68 }
69
d278a7e Get example app working with external model lib
mde authored
70 adapter = geddy.model.adapters[m];
9e1fe04 Got generators working.
mde authored
71 if (adapter) {
72 console.log('Creating table for ' + m);
73 adapter.createTable(m, function (err, data) {
74 if (err) { throw err }
75 createTable();
76 });
77 }
78 else {
d278a7e Get example app working with external model lib
mde authored
79 createTable();
9e1fe04 Got generators working.
mde authored
80 }
d278a7e Get example app working with external model lib
mde authored
81 }
82 else {
83 complete();
84 }
85 };
86 createTable();
48fb464 Fixed DB adapter connect/disconnect
mde authored
87
88 }, {async: true});
89
90 task('retrofit', ['env:init'], function () {
91 var modelNames = Object.keys(geddy.model.descriptionRegistry)
92 , createTask = jake.Task['db:createTable'];
93 createTask.once('complete', function () {
94 complete();
95 });
96 createTask.invoke(modelNames);
d278a7e Get example app working with external model lib
mde authored
97 }, {async: true});
98
99 });
100
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
101 namespace('gen', function () {
102
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
103 var _writeTemplate = function (name, filename, dirname, options) {
104 options = options || {};
105
d92f157 Get generators working
mde authored
106 var names = utils.string.getInflections(name)
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
107 , text = fs.readFileSync(path.join(__dirname, filename + '.ejs'), 'utf8').toString()
cf999ea @larzconwell Deleted bare controller template
larzconwell authored
108 , bare = options.bare || false // Default to full controller
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
109 , templ
110 , filePath;
111
112 // Render with the right model name
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
113 templ = new adapter({data: {ext: '.ejs'}, text: text, templato: templato});
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
114 templ.process({names: names, bare: bare, properties: options.properties});
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
115
116 // Write file
117 filePath = path.join('app', dirname, names.filename[options.inflection] + '.js');
118 fs.writeFileSync(filePath, templ.markup, 'utf8');
119
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
120 console.log('[Added] ' + filePath);
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
121 };
122
edf26c7 Formatting
mde authored
123 var _formatModelProperties = function (properties) {
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
124 var obj = {'default': {name: 'id', type: 'string'}};
9e1fe04 Got generators working.
mde authored
125 if (!properties) {
126 return obj;
127 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
128
129 var itemsArr = properties.split(' ')
130 , name
131 , type
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
132 , args
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
133 , i
134 , value;
135
26600c5 @larzconwell Just made a few loop optimizations for Array loops
larzconwell authored
136 i = -1;
137 while(++i < itemsArr.length) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
138 value = itemsArr[i];
139 name = value.replace(/:.*/g, '');
140 type = value.replace(/[a-zA-Z]*:?/, '');
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
141 args = type.replace(/[a-zA-Z]*:?/, '');
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
142
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
143 // Take off any args on the type
144 type = type.replace(/:.*/g, '');
145
9e1fe04 Got generators working.
mde authored
146 // Defaults and aliases
6967867 Misc. fixes to get generators working
mde authored
147 if (!type) type = 'string';
148 if (type === 'integer') type = 'int';
149 if (type === 'bool') type = 'boolean';
150 if (args === 'def') args = 'default';
151 if (type === 'default' || type === 'def') {
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
152 // If the type is `default` assume they want `string`
153 // type and want this property to be default
154 type = 'string';
155 args = 'default';
156 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
157
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
158 // Set it as default if given the `default` argument
6967867 Misc. fixes to get generators working
mde authored
159 if (args === 'default') {
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
160 obj['default'] = {name: name, type: type};
161 // Add the original ID property
162 obj['id'] = obj['id'] || {name: 'id', type: 'string'};
9e1fe04 Got generators working.
mde authored
163 }
164 else {
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
165 // If ID property is given and it matches the default
166 // then rewrite the default with the new ID property
6967867 Misc. fixes to get generators working
mde authored
167 if (name === 'id' && obj['default'].name === 'id') {
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
168 obj['default'] = {name: name, type: type};
9e1fe04 Got generators working.
mde authored
169 }
170 else {
171 obj[name] = {name: name, type: type};
172 }
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
173 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
174 }
175
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
176 return obj;
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
177 };
178
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
179 // Creates a new Geddy app scaffold
9e1fe04 Got generators working.
mde authored
180 task('app', function (name, engine) {
181 if (!name) {
182 throw new Error('No app name specified.');
183 }
184 if (!engine) {
185 engine = 'ejs';
186 }
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
187
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
188 var mkdirs = [
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
189 ''
190 , 'config'
191 , 'app/models'
192 , 'app/controllers'
193 , 'lib'
194 , 'log'
195 , 'node_modules'
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
196 ]
197 , cps = [
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
198 ['views/' + engine, 'app/views']
199 , ['public', '']
200 , ['router.js', 'config']
201 , ['init.js', 'config']
202 , ['environment.js', 'config']
203 , ['development.js', 'config']
204 , ['production.js', 'config']
205 , ['main.js', 'app/controllers']
206 , ['application.js', 'app/controllers']
207 , ['favicon.ico', 'public']
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
208 ];
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
209
edf26c7 Formatting
mde authored
210 mkdirs.forEach(function (dir) {
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
211 jake.mkdirP(path.join(name, dir));
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
212 });
edf26c7 Formatting
mde authored
213 cps.forEach(function (cp) {
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
214 jake.cpR(path.join(__dirname, 'base', cp[0]), path.join(name, cp[1]));
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
215 });
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
216
217 console.log('Created app ' + name + '.');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
218 });
219
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
220 // Creates a resource with a model, controller and a resource route
edf26c7 Formatting
mde authored
221 task('resource', function (name, modelProperties) {
9e1fe04 Got generators working.
mde authored
222 var names
223 , modelTask = jake.Task['gen:model'];
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
224
9e1fe04 Got generators working.
mde authored
225 if (!name) {
226 throw new Error('No resource name specified.');
227 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
228
9e1fe04 Got generators working.
mde authored
229 modelTask.on('complete', function () {
230 jake.Task['gen:controller'].invoke(name);
231 jake.Task['gen:route'].invoke(name);
232 names = utils.string.getInflections(name);
233 // Create views folder but not actions
234 jake.mkdirP(path.join('app', 'views', names.filename.plural));
235 console.log('[Added] ' + names.filename.plural + ' view directory');
236 complete();
237 });
238 modelTask.invoke(name, modelProperties);
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
239
9e1fe04 Got generators working.
mde authored
240 }, {async: true});
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
241
242 // Creates a full scaffold with views, a model, controller and a resource route
edf26c7 Formatting
mde authored
243 task('scaffold', function (name, engine, modelProperties) {
9e1fe04 Got generators working.
mde authored
244 var modelTask = jake.Task['gen:model'];
245
6967867 Misc. fixes to get generators working
mde authored
246 if (!modelProperties && engine) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
247 modelProperties = engine;
248 engine = '';
249 }
9e1fe04 Got generators working.
mde authored
250 if (!name) {
251 throw new Error('No scaffold name specified.');
252 }
253 if (!engine) {
254 engine = 'ejs';
255 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
256
9e1fe04 Got generators working.
mde authored
257 modelTask.on('complete', function () {
258 jake.Task['gen:controllerScaffold'].invoke(name,
259 {properties: modelProperties});
260 jake.Task['gen:route'].invoke(name);
261 jake.Task['gen:viewsScaffold'].invoke(name,
262 {engine: engine, properties: modelProperties});
263 complete();
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up t…
larzconwell authored
264 });
9e1fe04 Got generators working.
mde authored
265 modelTask.invoke(name, modelProperties);
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
266
9e1fe04 Got generators working.
mde authored
267 }, {async: true});
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
268
9e1fe04 Got generators working.
mde authored
269 task('model', function (name, properties) {
270 var createTableTask;
271 if (!name) {
272 throw new Error('No model name specified.');
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
273 }
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
274
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
275 _writeTemplate(name, 'resource/model', 'models', {
276 inflection: 'singular'
277 , properties: _formatModelProperties(properties)
278 });
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
279
9e1fe04 Got generators working.
mde authored
280 // Try to create a table -- should be a no-op if an
281 // appropriate DB adapter can't be found
282 createTableTask = jake.Task['db:createTable'];
283 createTableTask.on('complete', function () {
284 complete();
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
285 });
9e1fe04 Got generators working.
mde authored
286 createTableTask.invoke(name);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
287
9e1fe04 Got generators working.
mde authored
288 }, {async: true});
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
289
9e1fe04 Got generators working.
mde authored
290 task('controller', function (name) {
291 if (!name) {
292 throw new Error('No controller name specified.');
293 }
294
295 _writeTemplate(name, 'resource/controller', 'controllers',
296 {inflection: 'plural', bare: false});
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
297 });
298
9e1fe04 Got generators working.
mde authored
299 task('controllerScaffold', function (name, options) {
300 if (!name) {
301 throw new Error('No controller name specified.');
302 }
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
303 options = options || {};
304
305 _writeTemplate(name, 'scaffold/controller', 'controllers', {
306 inflection: 'plural'
307 , bare: false
308 , properties: _formatModelProperties(options.properties)
309 });
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
310 });
311
9e1fe04 Got generators working.
mde authored
312 task('bareController', function (name, engine) {
313 if (!name) {
314 throw new Error('No controller name specified.');
315 }
316 if (!engine) {
317 engine = 'ejs';
318 }
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
319
320 _writeTemplate(name, 'resource/controller', 'controllers', {inflection: 'plural', bare: true});
321 jake.Task['gen:route'].invoke(name, {bare: true});
322 jake.Task['gen:views'].invoke(name, {bare: true, engine: engine});
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
323 });
324
9e1fe04 Got generators working.
mde authored
325 task('route', function (name, options) {
326 if (!name) {
327 throw new Error('No route name specified.');
328 }
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
329 options = options || {};
330
d92f157 Get generators working
mde authored
331 var names = utils.string.getInflections(name)
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
332 , routeType = options.bare ? 'Bare' : 'Resource'
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
333 , jsRouter = path.normalize('config/router.js')
334 , coffeeRouter = path.normalize('config/router.coffee')
335 , routerPath
336 , routerArr
337 , text
338 , splitText
339 , newRoute;
340
341 // Check if the router file exists
edf26c7 Formatting
mde authored
342 utils.file.searchParentPath(jsRouter, function (err) {
6967867 Misc. fixes to get generators working
mde authored
343 if (err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up t…
larzconwell authored
344 var jsErr = err;
345 // If jsEnvironment wasn't found, try finding coffee variant
edf26c7 Formatting
mde authored
346 utils.file.searchParentPath(coffeeRouter, function (err) {
6967867 Misc. fixes to get generators working
mde authored
347 if (err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up t…
larzconwell authored
348 throw jsErr;
349 } else {
350 routerPath = coffeeRouter;
351 }
352 });
353 } else {
354 routerPath = jsRouter;
355 }
356 });
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
357
6967867 Misc. fixes to get generators working
mde authored
358 if (routerPath) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
359 text = fs.readFileSync(routerPath, 'utf8');
360
6967867 Misc. fixes to get generators working
mde authored
361 if (routerPath.match('.coffee')) {
362 if (options.bare) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
363 newRoute = 'router.match(\'/' + names.filename.plural +
364 '\').to controller: \'' + names.constructor.plural +
365 '\', action: \'index\'';
baa8cdf @larzconwell Updated /site to reflect new features related to generators and templ…
larzconwell authored
366 } else {
367 newRoute = 'router.resource \'' + names.filename.plural + '\'';
368 }
6967867 Misc. fixes to get generators working
mde authored
369 } else if (routerPath.match('.js')) {
370 if (options.bare) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
371 newRoute = 'router.match(\'/' + names.filename.plural +
372 '\').to({controller: \'' + names.constructor.plural +
373 '\', action: \'index\'});';
baa8cdf @larzconwell Updated /site to reflect new features related to generators and templ…
larzconwell authored
374 } else {
375 newRoute = 'router.resource(\'' + names.filename.plural + '\');';
376 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
377 }
378
379 // Don't add the same route over and over
6967867 Misc. fixes to get generators working
mde authored
380 if (text.indexOf(newRoute) == -1) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
381 // Add the new resource route just above the export
382 routerArr = text.split('exports.router');
383 routerArr[0] += newRoute + '\n';
384
385 text = routerArr.join('exports.router');
386 fs.writeFileSync(routerPath, text, 'utf8');
387
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
388 console.log('[Added] ' + routeType + ' ' + names.filename.plural +
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
389 ' route added to ' + routerPath);
390 }
391 else {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
392 console.log(routeType + ' ' + names.filename.plural + ' route already defined in ' +
393 routerPath);
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
394 }
395
baa8cdf @larzconwell Updated /site to reflect new features related to generators and templ…
larzconwell authored
396 } else {
397 console.log('There is no router file to add routes too');
398 }
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
399 });
400
9e1fe04 Got generators working.
mde authored
401 task('views', function (name, options) {
6967867 Misc. fixes to get generators working
mde authored
402 if (!name) throw new Error('No view name specified.');
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
403
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
404 options = options || {};
405
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
406 // Option defaults
407 options.engine = options.engine || 'ejs';
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
408
d92f157 Get generators working
mde authored
409 var names = utils.string.getInflections(name)
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
410 , engine = options.engine
411 , appViewDir = path.join('app', 'views', names.filename.plural)
412 , templateViewDir = path.join(__dirname, 'views', engine)
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
413 , cmds = []
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
414 , ext = '.html'
415 , appLayoutPath
416 , actions
417 , addActionView;
418
419 // Set extension based on engine option
6967867 Misc. fixes to get generators working
mde authored
420 if (engine === 'ejs') ext += '.ejs';
421 if (engine === 'jade') ext += '.jade';
422 if (engine === 'handlebars') ext += '.hbs';
423 if (engine === 'mustache') ext += '.ms';
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
424
425 // Set application layout path
426 appLayoutPath = path.join('app', 'views', 'layouts', 'application');
427 // Copy template view to application path
edf26c7 Formatting
mde authored
428 addActionView = function (action) {
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
429 jake.cpR(path.join(templateViewDir, action + ext), appViewDir);
430 };
431
432 jake.mkdirP(appViewDir);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
433 jake.mkdirP('app/views/layouts');
434 addActionView('index');
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
435
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
436 // Add views for the other CRUD actions when doing a full-on resource
6967867 Misc. fixes to get generators working
mde authored
437 if (!options.bare) {
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
438 ['add', 'edit', 'show'].forEach(function (action) {
439 addActionView(action);
440 });
441 }
442
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
443 // Create default layout if one doesn't exist
444 // Hack: There should be a better way to detect if a application layout exists
6967867 Misc. fixes to get generators working
mde authored
445 if (!utils.file.existsSync(appLayoutPath + '.html.ejs') && !utils.file.existsSync(appLayoutPath + '.html.jade') &&
446 !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
447 // Copy template layout file to apps application layout file
448 jake.cpR(path.join(templateViewDir, 'layout' + ext), appLayoutPath + ext);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
449 }
450
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
451 console.log('[Added] View templates');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
452 });
453
edf26c7 Formatting
mde authored
454 task('viewsScaffold', function (name, options) {
6967867 Misc. fixes to get generators working
mde authored
455 if (!name) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up t…
larzconwell authored
456 throw new Error('No view name specified.');
457 }
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
458
459 options = options || {};
460
461 // Option defaults
462 options.engine = options.engine || 'ejs';
463
d92f157 Get generators working
mde authored
464 var names = utils.string.getInflections(name)
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
465 , engine = options.engine
466 , appViewDir = path.join('app', 'views', names.filename.plural)
467 , templateViewDir = path.join(__dirname, 'scaffold', 'views', engine)
468 , cmds = []
469 , ext = '.html'
470 , appLayoutPath
471 , actions
472 , addActionView
473 , text
474 , templ;
475
476 // Set extension based on engine option
6967867 Misc. fixes to get generators working
mde authored
477 if (engine === 'ejs') ext += '.ejs';
478 if (engine === 'jade') ext += '.jade';
479 if (engine === 'handlebars') ext += '.hbs';
480 if (engine === 'mustache') ext += '.ms';
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
481
482 // Set application layout path
483 appLayoutPath = path.join('app', 'views', 'layouts', 'application');
484
485 // Function to compile the template
edf26c7 Formatting
mde authored
486 addActionView = function (action) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
487 text = fs.readFileSync(path.join(templateViewDir, action + ext + '.ejs')).toString();
488
489 // Compile template text
490 templ = new adapter({data: {ext: '.ejs'}, text: text, templato: templato});
491 templ.process({names: names, properties: _formatModelProperties(options.properties)});
492
493 // Write file
494 fs.writeFileSync(path.join(appViewDir, action + ext),
495 templ.markup.replace(/<@/g, '<%').replace(/@>/g, '%>'), 'utf8');
496 };
497
498 jake.mkdirP(appViewDir);
499 jake.mkdirP('app/views/layouts');
500
501 // Add views for the other CRUD actions when doing a full-on resource
edf26c7 Formatting
mde authored
502 ['index', 'add', 'show', 'edit'].forEach(function (action) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
503 addActionView(action);
504 });
505
506 // Create default layout if one doesn't exist
507 // Hack: There should be a better way to detect if a application layout exists
6967867 Misc. fixes to get generators working
mde authored
508 if (!utils.file.existsSync(appLayoutPath + '.html.ejs') && !utils.file.existsSync(appLayoutPath + '.html.jade') &&
509 !utils.file.existsSync(appLayoutPath + '.html.hbs') && !utils.file.existsSync(appLayoutPath + '.html.ms')) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
510 // Copy template layout file to apps application layout file
511 jake.cpR(path.join(templateViewDir, 'layout' + ext), appLayoutPath + ext);
512 }
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
513
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
514 console.log('[Added] View templates');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
515 });
516
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
517 // Generate a new application secret in environment.js
9e1fe04 Got generators working.
mde authored
518 task('secret', function () {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
519 var jsEnvironment = path.normalize('config/environment.js')
520 , coffeeEnvironment = path.normalize('config/environment.coffee')
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
521 , secret = utils.string.uuid(128)
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
522 , environmentPath
523 , environmentArr
524 , text
525 , splitText;
526
527 // Check if the environment file exists
edf26c7 Formatting
mde authored
528 utils.file.searchParentPath(jsEnvironment, function (err) {
6967867 Misc. fixes to get generators working
mde authored
529 if (err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up t…
larzconwell authored
530 var jsErr = err;
531 // If jsEnvironment wasn't found, try finding coffee variant
edf26c7 Formatting
mde authored
532 utils.file.searchParentPath(coffeeEnvironment, function (err) {
6967867 Misc. fixes to get generators working
mde authored
533 if (err) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up t…
larzconwell authored
534 throw jsErr;
535 } else {
536 environmentPath = coffeeEnvironment;
537 }
538 });
539 } else {
540 environmentPath = jsEnvironment;
541 }
542 });
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
543
6967867 Misc. fixes to get generators working
mde authored
544 if (environmentPath) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
545 text = fs.readFileSync(environmentPath, 'utf8').toString();
546
547 // Remove any old secret
548 text = text.replace(/\nconfig.secret.+\n/, '');
549
550 splitText = 'module.exports = config';
6967867 Misc. fixes to get generators working
mde authored
551 if (environmentPath.match('.js')) splitText += ';';
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
552
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
553 environmentArr = text.split(splitText);
554 environmentArr[0] += "config.secret = '" + secret + "'";
555 environmentArr[0] += environmentPath.match('.js') ? ';\n\n' : '\n\n';
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
556
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
557 text = environmentArr.join(splitText);
558 fs.writeFileSync(environmentPath, text, 'utf8');
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
559
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
560 console.log('[Added] Application secret to \'' + environmentPath + '\'');
6967867 Misc. fixes to get generators working
mde authored
561 }
9e1fe04 Got generators working.
mde authored
562 else {
563 console.log('There is no environment config to add the app-secret to.');
564 }
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
565 });
566
567 });
Something went wrong with that request. Please try again.