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