Skip to content

Commit

Permalink
Misc. fixes to get generators working
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Aug 16, 2012
1 parent 48fb464 commit 6967867
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 62 deletions.
2 changes: 1 addition & 1 deletion bin/cli.js
Expand Up @@ -175,7 +175,7 @@ if(cmds.length) {
die(cmds[0] + ' is not a Geddy command.'); die(cmds[0] + ' is not a Geddy command.');
} }


cmd += ' --quiet'; //cmd += ' --quiet';
exec(cmd, function(err, stdout, stderr) { exec(cmd, function(err, stdout, stderr) {
if(err) { if(err) {
throw err; throw err;
Expand Down
111 changes: 56 additions & 55 deletions templates/Jakefile
Expand Up @@ -94,7 +94,7 @@ namespace('gen', function () {


var _formatModelProperties = function(properties) { var _formatModelProperties = function(properties) {
var obj = {'default': {name: 'id', type: 'string'}}; var obj = {'default': {name: 'id', type: 'string'}};
if(!properties) return obj; if (!properties) return obj;


var itemsArr = properties.split(' ') var itemsArr = properties.split(' ')
, name , name
Expand All @@ -114,26 +114,26 @@ namespace('gen', function () {
type = type.replace(/:.*/g, ''); type = type.replace(/:.*/g, '');


// Defaults and alias's // Defaults and alias's
if(!type) type = 'string'; if (!type) type = 'string';
if(type === 'integer') type = 'int'; if (type === 'integer') type = 'int';
if(type === 'bool') type = 'boolean'; if (type === 'bool') type = 'boolean';
if(args === 'def') args = 'default'; if (args === 'def') args = 'default';
if(type === 'default' || type === 'def') { if (type === 'default' || type === 'def') {
// If the type is `default` assume they want `string` // If the type is `default` assume they want `string`
// type and want this property to be default // type and want this property to be default
type = 'string'; type = 'string';
args = 'default'; args = 'default';
} }


// Set it as default if given the `default` argument // Set it as default if given the `default` argument
if(args === 'default') { if (args === 'default') {
obj['default'] = {name: name, type: type}; obj['default'] = {name: name, type: type};
// Add the original ID property // Add the original ID property
obj['id'] = obj['id'] || {name: 'id', type: 'string'}; obj['id'] = obj['id'] || {name: 'id', type: 'string'};
} else { } else {
// If ID property is given and it matches the default // If ID property is given and it matches the default
// then rewrite the default with the new ID property // then rewrite the default with the new ID property
if(name === 'id' && obj['default'].name === 'id') { if (name === 'id' && obj['default'].name === 'id') {
obj['default'] = {name: name, type: type}; obj['default'] = {name: name, type: type};
} else obj[name] = {name: name, type: type}; } else obj[name] = {name: name, type: type};
} }
Expand All @@ -144,8 +144,8 @@ namespace('gen', function () {


// Creates a new Geddy app scaffold // Creates a new Geddy app scaffold
task('app', [], function(name, engine) { task('app', [], function(name, engine) {
if(!name) throw new Error('No app name specified.'); if (!name) throw new Error('No app name specified.');
if(!engine) engine = 'ejs'; if (!engine) engine = 'ejs';


var mkdirs = [ var mkdirs = [
'' ''
Expand Down Expand Up @@ -181,7 +181,7 @@ namespace('gen', function () {


// Creates a resource with a model, controller and a resource route // Creates a resource with a model, controller and a resource route
task('resource', function(name, modelProperties) { task('resource', function(name, modelProperties) {
if(!name) throw new Error('No resource name specified.'); if (!name) throw new Error('No resource name specified.');
var names = utils.string.getInflections(name); var names = utils.string.getInflections(name);


jake.Task['gen:model'].invoke(name, modelProperties); jake.Task['gen:model'].invoke(name, modelProperties);
Expand All @@ -196,12 +196,12 @@ namespace('gen', function () {


// Creates a full scaffold with views, a model, controller and a resource route // Creates a full scaffold with views, a model, controller and a resource route
task('scaffold', function(name, engine, modelProperties) { task('scaffold', function(name, engine, modelProperties) {
if(!modelProperties && engine) { if (!modelProperties && engine) {
modelProperties = engine; modelProperties = engine;
engine = ''; engine = '';
} }
if(!name) throw new Error('No scaffold name specified.'); if (!name) throw new Error('No scaffold name specified.');
if(!engine) engine = 'ejs'; if (!engine) engine = 'ejs';


var names = utils.string.getInflections(name) var names = utils.string.getInflections(name)
, jsEnvironment = path.normalize('config/environment.js') , jsEnvironment = path.normalize('config/environment.js')
Expand All @@ -214,11 +214,11 @@ namespace('gen', function () {


// Check if the environment file exists // Check if the environment file exists
utils.file.searchParentPath(jsEnvironment, function(err) { utils.file.searchParentPath(jsEnvironment, function(err) {
if(err) { if (err) {
var jsErr = err; var jsErr = err;
// If jsEnvironment wasn't found, try finding coffee variant // If jsEnvironment wasn't found, try finding coffee variant
utils.file.searchParentPath(coffeeEnvironment, function(err) { utils.file.searchParentPath(coffeeEnvironment, function(err) {
if(err) { if (err) {
throw jsErr; throw jsErr;
} else { } else {
environmentPath = coffeeEnvironment; environmentPath = coffeeEnvironment;
Expand All @@ -235,18 +235,18 @@ namespace('gen', function () {
jake.Task['gen:viewsScaffold'].invoke(name, {engine: engine, properties: modelProperties}); jake.Task['gen:viewsScaffold'].invoke(name, {engine: engine, properties: modelProperties});


// Add the following line to `config/environment.js/.coffee` // Add the following line to `config/environment.js/.coffee`
if(environmentPath) { if (environmentPath) {
text = fs.readFileSync(environmentPath, 'utf8').toString(); text = fs.readFileSync(environmentPath, 'utf8').toString();


// Create text to add to environment // Create text to add to environment
if(environmentPath.match('.coffee')) { if (environmentPath.match('.coffee')) {
dbContent = [ dbContent = [
' db:' ' db:'
, ' mongo:' , ' mongo:'
, ' db: \'' + names.property.plural + '\'' , ' db: \'' + names.property.plural + '\''
].join('\n'); ].join('\n');
splitText = 'config = '; splitText = 'config = ';
} else if(environmentPath.match('.js')) { } else if (environmentPath.match('.js')) {
dbContent = [ dbContent = [
' db: {' ' db: {'
, ' mongo: {' , ' mongo: {'
Expand All @@ -258,7 +258,7 @@ namespace('gen', function () {
} }


// Don't add the db config over and over // Don't add the db config over and over
if(text.indexOf('db:') == -1) { if (text.indexOf('db:') == -1) {
environmentArr = text.split(splitText); environmentArr = text.split(splitText);
environmentArr[0] += splitText + '\n' + dbContent + '\n'; environmentArr[0] += splitText + '\n' + dbContent + '\n';


Expand All @@ -271,7 +271,7 @@ namespace('gen', function () {
}); });


task('model', [], function(name, properties) { task('model', [], function(name, properties) {
if(!name) throw new Error('No model name specified.'); if (!name) throw new Error('No model name specified.');


_writeTemplate(name, 'resource/model', 'models', { _writeTemplate(name, 'resource/model', 'models', {
inflection: 'singular' inflection: 'singular'
Expand All @@ -280,7 +280,7 @@ namespace('gen', function () {
}); });


task('modelScaffold', [], function(name, properties) { task('modelScaffold', [], function(name, properties) {
if(!name) throw new Error('No model name specified.'); if (!name) throw new Error('No model name specified.');


_writeTemplate(name, 'scaffold/model', 'models', { _writeTemplate(name, 'scaffold/model', 'models', {
inflection: 'singular' inflection: 'singular'
Expand All @@ -289,13 +289,13 @@ namespace('gen', function () {
}); });


task('controller', [], function(name) { task('controller', [], function(name) {
if(!name) throw new Error('No controller name specified.'); if (!name) throw new Error('No controller name specified.');


_writeTemplate(name, 'resource/controller', 'controllers', {inflection: 'plural', bare: false}); _writeTemplate(name, 'resource/controller', 'controllers', {inflection: 'plural', bare: false});
}); });


task('controllerScaffold', [], function(name, options) { task('controllerScaffold', [], function(name, options) {
if(!name) throw new Error('No controller name specified.'); if (!name) throw new Error('No controller name specified.');
options = options || {}; options = options || {};


_writeTemplate(name, 'scaffold/controller', 'controllers', { _writeTemplate(name, 'scaffold/controller', 'controllers', {
Expand All @@ -306,16 +306,16 @@ namespace('gen', function () {
}); });


task('bareController', [], function(name, engine) { task('bareController', [], function(name, engine) {
if(!name) throw new Error('No controller name specified.'); if (!name) throw new Error('No controller name specified.');
if(!engine) engine = 'ejs'; if (!engine) engine = 'ejs';


_writeTemplate(name, 'resource/controller', 'controllers', {inflection: 'plural', bare: true}); _writeTemplate(name, 'resource/controller', 'controllers', {inflection: 'plural', bare: true});
jake.Task['gen:route'].invoke(name, {bare: true}); jake.Task['gen:route'].invoke(name, {bare: true});
jake.Task['gen:views'].invoke(name, {bare: true, engine: engine}); jake.Task['gen:views'].invoke(name, {bare: true, engine: engine});
}); });


task('route', [], function(name, options) { task('route', [], function(name, options) {
if(!name) throw new Error('No route name specified.'); if (!name) throw new Error('No route name specified.');
options = options || {}; options = options || {};


var names = utils.string.getInflections(name) var names = utils.string.getInflections(name)
Expand All @@ -330,11 +330,11 @@ namespace('gen', function () {


// Check if the router file exists // Check if the router file exists
utils.file.searchParentPath(jsRouter, function(err) { utils.file.searchParentPath(jsRouter, function(err) {
if(err) { if (err) {
var jsErr = err; var jsErr = err;
// If jsEnvironment wasn't found, try finding coffee variant // If jsEnvironment wasn't found, try finding coffee variant
utils.file.searchParentPath(coffeeRouter, function(err) { utils.file.searchParentPath(coffeeRouter, function(err) {
if(err) { if (err) {
throw jsErr; throw jsErr;
} else { } else {
routerPath = coffeeRouter; routerPath = coffeeRouter;
Expand All @@ -345,19 +345,19 @@ namespace('gen', function () {
} }
}); });


if(routerPath) { if (routerPath) {
text = fs.readFileSync(routerPath, 'utf8'); text = fs.readFileSync(routerPath, 'utf8');


if(routerPath.match('.coffee')) { if (routerPath.match('.coffee')) {
if(options.bare) { if (options.bare) {
newRoute = 'router.match(\'/' + names.filename.plural + newRoute = 'router.match(\'/' + names.filename.plural +
'\').to controller: \'' + names.constructor.plural + '\').to controller: \'' + names.constructor.plural +
'\', action: \'index\''; '\', action: \'index\'';
} else { } else {
newRoute = 'router.resource \'' + names.filename.plural + '\''; newRoute = 'router.resource \'' + names.filename.plural + '\'';
} }
} else if(routerPath.match('.js')) { } else if (routerPath.match('.js')) {
if(options.bare) { if (options.bare) {
newRoute = 'router.match(\'/' + names.filename.plural + newRoute = 'router.match(\'/' + names.filename.plural +
'\').to({controller: \'' + names.constructor.plural + '\').to({controller: \'' + names.constructor.plural +
'\', action: \'index\'});'; '\', action: \'index\'});';
Expand All @@ -367,7 +367,7 @@ namespace('gen', function () {
} }


// Don't add the same route over and over // Don't add the same route over and over
if(text.indexOf(newRoute) == -1) { if (text.indexOf(newRoute) == -1) {
// Add the new resource route just above the export // Add the new resource route just above the export
routerArr = text.split('exports.router'); routerArr = text.split('exports.router');
routerArr[0] += newRoute + '\n'; routerArr[0] += newRoute + '\n';
Expand All @@ -389,7 +389,7 @@ namespace('gen', function () {
}); });


task('views', [], function(name, options) { task('views', [], function(name, options) {
if(!name) throw new Error('No view name specified.'); if (!name) throw new Error('No view name specified.');


options = options || {}; options = options || {};


Expand All @@ -407,10 +407,10 @@ namespace('gen', function () {
, addActionView; , addActionView;


// Set extension based on engine option // Set extension based on engine option
if(engine === 'ejs') ext += '.ejs'; if (engine === 'ejs') ext += '.ejs';
if(engine === 'jade') ext += '.jade'; if (engine === 'jade') ext += '.jade';
if(engine === 'handlebars') ext += '.hbs'; if (engine === 'handlebars') ext += '.hbs';
if(engine === 'mustache') ext += '.ms'; if (engine === 'mustache') ext += '.ms';


// Set application layout path // Set application layout path
appLayoutPath = path.join('app', 'views', 'layouts', 'application'); appLayoutPath = path.join('app', 'views', 'layouts', 'application');
Expand All @@ -424,16 +424,16 @@ namespace('gen', function () {
addActionView('index'); addActionView('index');


// Add views for the other CRUD actions when doing a full-on resource // Add views for the other CRUD actions when doing a full-on resource
if(!options.bare) { if (!options.bare) {
['add', 'edit', 'show'].forEach(function (action) { ['add', 'edit', 'show'].forEach(function (action) {
addActionView(action); addActionView(action);
}); });
} }


// Create default layout if one doesn't exist // Create default layout if one doesn't exist
// Hack: There should be a better way to detect if a application layout exists // Hack: There should be a better way to detect if a application layout exists
if(!utils.compat.existsSync(appLayoutPath + '.html.ejs') && !utils.compat.existsSync(appLayoutPath + '.html.jade') && if (!utils.file.existsSync(appLayoutPath + '.html.ejs') && !utils.file.existsSync(appLayoutPath + '.html.jade') &&
!utils.compat.existsSync(appLayoutPath + '.html.hbs') && !utils.compat.existsSync(appLayoutPath + '.html.ms')) { !utils.file.existsSync(appLayoutPath + '.html.hbs') && !utils.file.existsSync(appLayoutPath + '.html.ms')) {
// Copy template layout file to apps application layout file // Copy template layout file to apps application layout file
jake.cpR(path.join(templateViewDir, 'layout' + ext), appLayoutPath + ext); jake.cpR(path.join(templateViewDir, 'layout' + ext), appLayoutPath + ext);
} }
Expand All @@ -442,7 +442,7 @@ namespace('gen', function () {
}); });


task('viewsScaffold', function(name, options) { task('viewsScaffold', function(name, options) {
if(!name) { if (!name) {
throw new Error('No view name specified.'); throw new Error('No view name specified.');
} }


Expand All @@ -464,10 +464,10 @@ namespace('gen', function () {
, templ; , templ;


// Set extension based on engine option // Set extension based on engine option
if(engine === 'ejs') ext += '.ejs'; if (engine === 'ejs') ext += '.ejs';
if(engine === 'jade') ext += '.jade'; if (engine === 'jade') ext += '.jade';
if(engine === 'handlebars') ext += '.hbs'; if (engine === 'handlebars') ext += '.hbs';
if(engine === 'mustache') ext += '.ms'; if (engine === 'mustache') ext += '.ms';


// Set application layout path // Set application layout path
appLayoutPath = path.join('app', 'views', 'layouts', 'application'); appLayoutPath = path.join('app', 'views', 'layouts', 'application');
Expand Down Expand Up @@ -495,8 +495,8 @@ namespace('gen', function () {


// Create default layout if one doesn't exist // Create default layout if one doesn't exist
// Hack: There should be a better way to detect if a application layout exists // Hack: There should be a better way to detect if a application layout exists
if(!utils.compat.existsSync(appLayoutPath + '.html.ejs') && !utils.compat.existsSync(appLayoutPath + '.html.jade') && if (!utils.file.existsSync(appLayoutPath + '.html.ejs') && !utils.file.existsSync(appLayoutPath + '.html.jade') &&
!utils.compat.existsSync(appLayoutPath + '.html.hbs') && !utils.compat.existsSync(appLayoutPath + '.html.ms')) { !utils.file.existsSync(appLayoutPath + '.html.hbs') && !utils.file.existsSync(appLayoutPath + '.html.ms')) {
// Copy template layout file to apps application layout file // Copy template layout file to apps application layout file
jake.cpR(path.join(templateViewDir, 'layout' + ext), appLayoutPath + ext); jake.cpR(path.join(templateViewDir, 'layout' + ext), appLayoutPath + ext);
} }
Expand All @@ -516,11 +516,11 @@ namespace('gen', function () {


// Check if the environment file exists // Check if the environment file exists
utils.file.searchParentPath(jsEnvironment, function(err) { utils.file.searchParentPath(jsEnvironment, function(err) {
if(err) { if (err) {
var jsErr = err; var jsErr = err;
// If jsEnvironment wasn't found, try finding coffee variant // If jsEnvironment wasn't found, try finding coffee variant
utils.file.searchParentPath(coffeeEnvironment, function(err) { utils.file.searchParentPath(coffeeEnvironment, function(err) {
if(err) { if (err) {
throw jsErr; throw jsErr;
} else { } else {
environmentPath = coffeeEnvironment; environmentPath = coffeeEnvironment;
Expand All @@ -531,14 +531,14 @@ namespace('gen', function () {
} }
}); });


if(environmentPath) { if (environmentPath) {
text = fs.readFileSync(environmentPath, 'utf8').toString(); text = fs.readFileSync(environmentPath, 'utf8').toString();


// Remove any old secret // Remove any old secret
text = text.replace(/\nconfig.secret.+\n/, ''); text = text.replace(/\nconfig.secret.+\n/, '');


splitText = 'module.exports = config'; splitText = 'module.exports = config';
if(environmentPath.match('.js')) splitText += ';'; if (environmentPath.match('.js')) splitText += ';';


environmentArr = text.split(splitText); environmentArr = text.split(splitText);
environmentArr[0] += "config.secret = '" + secret + "'"; environmentArr[0] += "config.secret = '" + secret + "'";
Expand All @@ -548,7 +548,8 @@ namespace('gen', function () {
fs.writeFileSync(environmentPath, text, 'utf8'); fs.writeFileSync(environmentPath, text, 'utf8');


console.log('[Added] Application secret to \'' + environmentPath + '\''); console.log('[Added] Application secret to \'' + environmentPath + '\'');
} else console.log('There is no environment config to add the secret too'); }
else console.log('There is no environment config to add the app-secret to.');
}); });


}); });
2 changes: 1 addition & 1 deletion templates/scaffold/views/ejs/add.html.ejs.ejs
@@ -1,5 +1,5 @@
<div class="hero-unit"> <div class="hero-unit">
<form id="<%= names.property.singular %>-form" class="form-horizontal" action="/<%= names.url.plural %>" method="POST"> <form id="<%= names.property.singular %>-form" class="form-horizontal" action="/<%= names.filename.plural %>" method="POST">
<fieldset> <fieldset>
<legend>Create a new <%= names.constructor.singular %></legend> <legend>Create a new <%= names.constructor.singular %></legend>
<@ if(params.errors) { @> <@ if(params.errors) { @>
Expand Down
4 changes: 2 additions & 2 deletions templates/scaffold/views/ejs/edit.html.ejs.ejs
@@ -1,5 +1,5 @@
<div class="hero-unit"> <div class="hero-unit">
<form id="<%= names.property.singular %>-form" class="form-horizontal" action="/<%= names.url.plural %>/<@= params.id @>?_method=PUT" method="POST"> <form id="<%= names.property.singular %>-form" class="form-horizontal" action="/<%= names.filename.plural %>/<@= params.id @>?_method=PUT" method="POST">
<fieldset> <fieldset>
<legend>Update this <%= names.constructor.singular %></legend> <legend>Update this <%= names.constructor.singular %></legend>
<@ if(params.errors) { @> <@ if(params.errors) { @>
Expand Down Expand Up @@ -47,7 +47,7 @@
<% } -%> <% } -%>
<div class="form-actions"> <div class="form-actions">
<@- contentTag('input', 'Save', {type: 'submit', class: 'btn btn-primary'}) @> <@- contentTag('input', 'Save', {type: 'submit', class: 'btn btn-primary'}) @>
<@- contentTag('button', 'Remove', {type: 'submit', formaction: '/<%= names.url.plural %>/' + params.id + '?_method=DELETE', formmethod: 'POST', class: 'btn btn-danger'}) @> <@- contentTag('button', 'Remove', {type: 'submit', formaction: '/<%= names.filename.plural %>/' + params.id + '?_method=DELETE', formmethod: 'POST', class: 'btn btn-danger'}) @>
</div> </div>
</fieldset> </fieldset>
</form> </form>
Expand Down
2 changes: 1 addition & 1 deletion templates/scaffold/views/jade/add.html.jade.ejs
@@ -1,5 +1,5 @@
.hero-unit .hero-unit
form#<%= names.property.singular %>-form.form-horizontal(action="/<%= names.url.plural %>", method="POST") form#<%= names.property.singular %>-form.form-horizontal(action="/<%= names.filename.plural %>", method="POST")
fieldset fieldset
legend Create a new <%= names.constructor.singular %> legend Create a new <%= names.constructor.singular %>
if params.errors if params.errors
Expand Down
4 changes: 2 additions & 2 deletions templates/scaffold/views/jade/edit.html.jade.ejs
@@ -1,5 +1,5 @@
.hero-unit .hero-unit
form#<%= names.property.singular %>-form.form-horizontal(action="/<%= names.url.plural %>/" + params.id + "?_method=PUT", method="POST") form#<%= names.property.singular %>-form.form-horizontal(action="/<%= names.filename.plural %>/" + params.id + "?_method=PUT", method="POST")
fieldset fieldset
legend Update this <%= names.constructor.singular %> legend Update this <%= names.constructor.singular %>
if params.errors if params.errors
Expand Down Expand Up @@ -40,4 +40,4 @@
<% } -%> <% } -%>
.form-actions .form-actions
!= contentTag('input', 'Save', {type: 'submit', class: 'btn btn-primary'}) != contentTag('input', 'Save', {type: 'submit', class: 'btn btn-primary'})
!= contentTag('button', 'Remove', {type: 'submit', formaction: '/<%= names.url.plural %>/' + params.id + '?_method=DELETE', formmethod: 'POST', class: 'btn btn-danger'}) != contentTag('button', 'Remove', {type: 'submit', formaction: '/<%= names.filename.plural %>/' + params.id + '?_method=DELETE', formmethod: 'POST', class: 'btn btn-danger'})

0 comments on commit 6967867

Please sign in to comment.