Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Got generators working.

  • Loading branch information...
commit 9e1fe0461e6bfb14540aad3b3d4e5a85c683f5d0 1 parent edf26c7
mde authored
View
8 bin/cli.js
@@ -144,9 +144,13 @@ if(cmds.length) {
// Add Jake argument based on commands
switch(cmds[0]) {
case 'db:init':
- // Generating application
+ // Create DBs
cmd += '"db:init"';
break;
+ case 'db:createTable':
+ // Create DBs
+ cmd += '"db:createTable[' + cmds[1] + ']"';
+ break;
case 'app':
// Generating application
cmd += '"gen:app[' + cmds[1] + engineCmd + ']"';
@@ -175,7 +179,7 @@ if(cmds.length) {
die(cmds[0] + ' is not a Geddy command.');
}
- //cmd += ' --quiet';
+ cmd += ' --quiet';
exec(cmd, function(err, stdout, stderr) {
if(err) {
throw err;
View
3  lib/app.js
@@ -48,6 +48,9 @@ geddy.model = model;
geddy.utils = utils;
geddy.errors = errors;
+// Use Geddy logger as the utility logger
+utils.log.registerLogger(geddy.log);
+
var App = function () {
var JSPAT = /\.(js|coffee)$/;
View
9 lib/init/model.js
@@ -44,9 +44,6 @@ module.exports = new (function () {
, appAdaptersPath = path.join(cwd, modelDir, 'adapters')
, ctors;
- // FIXME: Do this in app.js
- utils.log.registerLogger(geddy.log);
-
// May be running totally model-less
if (!utils.file.existsSync(path.join(cwd, modelDir))) {
return callback();
@@ -70,7 +67,7 @@ module.exports = new (function () {
// If the model doesn't exist, something is fucked up
if (!model[name]) {
- throw new Error('Model ' + m.ctorName + ' did not get registered properly.');
+ throw new Error('Model ' + name + ' did not get registered properly.');
}
adapterName = model[name].adapter;
@@ -102,8 +99,8 @@ module.exports = new (function () {
// No adapter, log an error
if (!adapterPath) {
- geddy.log.error('Adapter not found for ' + name +
- '. Make sure your adapter is in app/models/adapters');
+ geddy.log.error('Model adapter not found for ' + name +
+ '.\nSet .adapter for this model, or set model.defaultAdapter.');
}
else {
adapter = model.loadedAdapters[adapterName];
View
244 templates/Jakefile
@@ -10,15 +10,19 @@ var fs = require('fs')
namespace('env', function () {
task('init', function () {
- geddy.config = require('../lib/config').readConfig();
- geddy.model = require('model');
-
- require('../lib/init').init(complete);
jake.addListener('complete', function (e) {
jake.Task['env:cleanup'].invoke();
});
- });
+
+ geddy.config = require('../lib/config').readConfig();
+ geddy.model = require('model');
+
+ require('../lib/init').init(function () {
+ complete();
+ });
+
+ }, {async: true});
task('cleanup', function () {
// Disconnect all the adapters
@@ -37,19 +41,43 @@ namespace('env', function () {
namespace('db', function () {
task('createTable', ['env:init'], function (name) {
- var modelName = typeof name == 'string' ? [name] : name
+ var modelName
, createTable
, adapters
- , adapter
- , modelNames = Object.keys(geddy.model.descriptionRegistry);
+ , adapter;
+
+ if (typeof name == 'string') {
+ if (name.indexOf(',') > -1) {
+ modelNames = name.split(',');
+ }
+ else {
+ modelNames = [name];
+ }
+ }
+ else {
+ modelNames = name;
+ }
+
createTable = function () {
if ((m = modelNames.shift())) {
- console.log('Creating table for ' + m);
+
+ // Make sure this is a correct model-name
+ m = utils.string.getInflections(m).constructor.singular;
+ if (!geddy.model[m]) {
+ throw new Error(m + ' is not a known model.');
+ }
+
adapter = geddy.model.adapters[m];
- adapter.createTable(m, function (err, data) {
- if (err) { throw err }
+ if (adapter) {
+ console.log('Creating table for ' + m);
+ adapter.createTable(m, function (err, data) {
+ if (err) { throw err }
+ createTable();
+ });
+ }
+ else {
createTable();
- });
+ }
}
else {
complete();
@@ -94,7 +122,9 @@ namespace('gen', function () {
var _formatModelProperties = function (properties) {
var obj = {'default': {name: 'id', type: 'string'}};
- if (!properties) return obj;
+ if (!properties) {
+ return obj;
+ }
var itemsArr = properties.split(' ')
, name
@@ -113,7 +143,7 @@ namespace('gen', function () {
// Take off any args on the type
type = type.replace(/:.*/g, '');
- // Defaults and alias's
+ // Defaults and aliases
if (!type) type = 'string';
if (type === 'integer') type = 'int';
if (type === 'bool') type = 'boolean';
@@ -130,12 +160,16 @@ namespace('gen', function () {
obj['default'] = {name: name, type: type};
// Add the original ID property
obj['id'] = obj['id'] || {name: 'id', type: 'string'};
- } else {
+ }
+ else {
// If ID property is given and it matches the default
// then rewrite the default with the new ID property
if (name === 'id' && obj['default'].name === 'id') {
obj['default'] = {name: name, type: type};
- } else obj[name] = {name: name, type: type};
+ }
+ else {
+ obj[name] = {name: name, type: type};
+ }
}
}
@@ -143,9 +177,13 @@ namespace('gen', function () {
};
// Creates a new Geddy app scaffold
- task('app', [], function (name, engine) {
- if (!name) throw new Error('No app name specified.');
- if (!engine) engine = 'ejs';
+ task('app', function (name, engine) {
+ if (!name) {
+ throw new Error('No app name specified.');
+ }
+ if (!engine) {
+ engine = 'ejs';
+ }
var mkdirs = [
''
@@ -181,121 +219,87 @@ namespace('gen', function () {
// Creates a resource with a model, controller and a resource route
task('resource', function (name, modelProperties) {
- if (!name) throw new Error('No resource name specified.');
- var names = utils.string.getInflections(name);
+ var names
+ , modelTask = jake.Task['gen:model'];
- jake.Task['gen:model'].invoke(name, modelProperties);
- jake.Task['gen:controller'].invoke(name);
- jake.Task['gen:route'].invoke(name);
+ if (!name) {
+ throw new Error('No resource name specified.');
+ }
- // Create views folder but not actions
- jake.mkdirP(path.join('app', 'views', names.filename.plural));
+ modelTask.on('complete', function () {
+ jake.Task['gen:controller'].invoke(name);
+ jake.Task['gen:route'].invoke(name);
+ names = utils.string.getInflections(name);
+ // Create views folder but not actions
+ jake.mkdirP(path.join('app', 'views', names.filename.plural));
+ console.log('[Added] ' + names.filename.plural + ' view directory');
+ complete();
+ });
+ modelTask.invoke(name, modelProperties);
- console.log('[Added] ' + names.filename.plural + ' view directory');
- });
+ }, {async: true});
// Creates a full scaffold with views, a model, controller and a resource route
task('scaffold', function (name, engine, modelProperties) {
+ var modelTask = jake.Task['gen:model'];
+
if (!modelProperties && engine) {
modelProperties = engine;
engine = '';
}
- if (!name) throw new Error('No scaffold name specified.');
- if (!engine) engine = 'ejs';
-
- var names = utils.string.getInflections(name)
- , jsEnvironment = path.normalize('config/environment.js')
- , coffeeEnvironment = path.normalize('config/environment.coffee')
- , environmentPath
- , environmentArr
- , text
- , splitText
- , dbContent = '';
+ if (!name) {
+ throw new Error('No scaffold name specified.');
+ }
+ if (!engine) {
+ engine = 'ejs';
+ }
- // Check if the environment file exists
- utils.file.searchParentPath(jsEnvironment, function (err) {
- if (err) {
- var jsErr = err;
- // If jsEnvironment wasn't found, try finding coffee variant
- utils.file.searchParentPath(coffeeEnvironment, function (err) {
- if (err) {
- throw jsErr;
- } else {
- environmentPath = coffeeEnvironment;
- }
- });
- } else {
- environmentPath = jsEnvironment;
- }
+ modelTask.on('complete', function () {
+ jake.Task['gen:controllerScaffold'].invoke(name,
+ {properties: modelProperties});
+ jake.Task['gen:route'].invoke(name);
+ jake.Task['gen:viewsScaffold'].invoke(name,
+ {engine: engine, properties: modelProperties});
+ complete();
});
+ modelTask.invoke(name, modelProperties);
- jake.Task['gen:modelScaffold'].invoke(name, modelProperties);
- jake.Task['gen:controllerScaffold'].invoke(name, {properties: modelProperties});
- jake.Task['gen:route'].invoke(name);
- jake.Task['gen:viewsScaffold'].invoke(name, {engine: engine, properties: modelProperties});
-
- // Add the following line to `config/environment.js/.coffee`
- if (environmentPath) {
- text = fs.readFileSync(environmentPath, 'utf8').toString();
-
- // Create text to add to environment
- if (environmentPath.match('.coffee')) {
- dbContent = [
- ' db:'
- , ' mongo:'
- , ' db: \'' + names.property.plural + '\''
- ].join('\n');
- splitText = 'config = ';
- } else if (environmentPath.match('.js')) {
- dbContent = [
- ' db: {'
- , ' mongo: {'
- , ' db: \'' + names.property.plural + '\''
- , ' }'
- , ' }'
- ].join('\n');
- splitText = 'var config = {';
- }
-
- // Don't add the db config over and over
- if (text.indexOf('db:') == -1) {
- environmentArr = text.split(splitText);
- environmentArr[0] += splitText + '\n' + dbContent + '\n';
-
- text = environmentArr.join('');
- fs.writeFileSync(environmentPath, text, 'utf8');
+ }, {async: true});
- console.log('[Added] Database configuration to ' + environmentPath);
- } else console.log('Database configuration already set in ' + environmentPath);
+ task('model', function (name, properties) {
+ var createTableTask;
+ if (!name) {
+ throw new Error('No model name specified.');
}
- });
-
- task('model', [], function (name, properties) {
- if (!name) throw new Error('No model name specified.');
_writeTemplate(name, 'resource/model', 'models', {
inflection: 'singular'
, properties: _formatModelProperties(properties)
});
- });
-
- task('modelScaffold', [], function (name, properties) {
- if (!name) throw new Error('No model name specified.');
- _writeTemplate(name, 'scaffold/model', 'models', {
- inflection: 'singular'
- , properties: _formatModelProperties(properties)
+ // Try to create a table -- should be a no-op if an
+ // appropriate DB adapter can't be found
+ createTableTask = jake.Task['db:createTable'];
+ createTableTask.on('complete', function () {
+ complete();
});
- });
+ createTableTask.invoke(name);
- task('controller', [], function (name) {
- if (!name) throw new Error('No controller name specified.');
+ }, {async: true});
- _writeTemplate(name, 'resource/controller', 'controllers', {inflection: 'plural', bare: false});
+ task('controller', function (name) {
+ if (!name) {
+ throw new Error('No controller name specified.');
+ }
+
+ _writeTemplate(name, 'resource/controller', 'controllers',
+ {inflection: 'plural', bare: false});
});
- task('controllerScaffold', [], function (name, options) {
- if (!name) throw new Error('No controller name specified.');
+ task('controllerScaffold', function (name, options) {
+ if (!name) {
+ throw new Error('No controller name specified.');
+ }
options = options || {};
_writeTemplate(name, 'scaffold/controller', 'controllers', {
@@ -305,17 +309,23 @@ namespace('gen', function () {
});
});
- task('bareController', [], function (name, engine) {
- if (!name) throw new Error('No controller name specified.');
- if (!engine) engine = 'ejs';
+ task('bareController', function (name, engine) {
+ if (!name) {
+ throw new Error('No controller name specified.');
+ }
+ if (!engine) {
+ engine = 'ejs';
+ }
_writeTemplate(name, 'resource/controller', 'controllers', {inflection: 'plural', bare: true});
jake.Task['gen:route'].invoke(name, {bare: true});
jake.Task['gen:views'].invoke(name, {bare: true, engine: engine});
});
- task('route', [], function (name, options) {
- if (!name) throw new Error('No route name specified.');
+ task('route', function (name, options) {
+ if (!name) {
+ throw new Error('No route name specified.');
+ }
options = options || {};
var names = utils.string.getInflections(name)
@@ -388,7 +398,7 @@ namespace('gen', function () {
}
});
- task('views', [], function (name, options) {
+ task('views', function (name, options) {
if (!name) throw new Error('No view name specified.');
options = options || {};
@@ -505,7 +515,7 @@ namespace('gen', function () {
});
// Generate a new application secret in environment.js
- task('secret', [], function () {
+ task('secret', function () {
var jsEnvironment = path.normalize('config/environment.js')
, coffeeEnvironment = path.normalize('config/environment.coffee')
, secret = utils.string.uuid(128)
@@ -549,7 +559,9 @@ namespace('gen', function () {
console.log('[Added] Application secret to \'' + environmentPath + '\'');
}
- else console.log('There is no environment config to add the app-secret to.');
+ else {
+ console.log('There is no environment config to add the app-secret to.');
+ }
});
});
View
2  templates/resource/model.ejs
@@ -3,7 +3,7 @@ var <%= names.constructor.singular %> = function () {
this.defineProperties({
<% for(var i in properties) { -%>
<% if(i === 'default') { -%>
- <%= properties[i].name %>: {type: '<%= properties[i].type %>', required: true}
+ <%# Handle optional, locally defined ids %>

The "default" doesn't change the ID attribute, it's just used as the default value to show on the views.

If I did:

$ geddy scaffold user name:default

The name attribute will be used on the views, instead of the default ID attribute.

@mde Owner
mde added a note

Ah, I had a hard time figuring out what that was supposed to be doing. The problem was this line would cause an 'id' property to be defined in the model, and that's a reserved, auto-generated (or optionally user-set) property. It should not show up as a property-definition in the model file by default. What's the best way to fix this?

@Techwraith Owner

We'll need to replace this line, then hide it if the properties name is id.
Also below on line 8 we'll need to replace the if..else to only do if (i !== 'id') { // write the property stuff }

In the controller template on line 50-52, we may need to hide the id property, but that all depends on what's changing.

We also may need to change the _formatModelProperties function in templates/Jakefile, to make some changes dealing with the ID property, because currently you can override the default ID property with the given one, and I'm sure that wouldn't work (;

ex:

geddy resource user name:def password id:int

Will override the default id property of string with an integer type. Also the name property will be used when displaying things on views, instead of the id property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
<% } else { -%>
<% if(i === 'id') { -%>
, <%= properties[i].name %>: {type: '<%= properties[i].type %>', required: true}
View
56 templates/scaffold/model.ejs
@@ -1,56 +0,0 @@
-var <%= names.constructor.singular %> = function () {
-
- this.defineProperties({
-<% for(var i in properties) { -%>
-<% if(i === 'default') { -%>
- <%= properties[i].name %>: {type: '<%= properties[i].type %>', required: true}
-<% } else { -%>
-<% if(i === 'id') { -%>
- , <%= properties[i].name %>: {type: '<%= properties[i].type %>', required: true}
-<% } else { -%>
- , <%= properties[i].name %>: {type: '<%= properties[i].type %>'}
-<% } -%>
-<% } -%>
-<% } -%>
- });
-
- this.adapter = "Mongo";
-
- /*
- this.property('login', 'string', {required: true});
- this.property('password', 'string', {required: true});
- this.property('lastName', 'string');
- this.property('firstName', 'string');
-
- this.validatesPresent('login');
- this.validatesFormat('login', /[a-z]+/, {message: 'Subdivisions!'});
- this.validatesLength('login', {min: 3});
- // Use with the name of the other parameter to compare with
- this.validatesConfirmed('password', 'confirmPassword');
- // Use with any function that returns a Boolean
- this.validatesWithFunction('password', function (s) {
- return s.length > 0;
- });
-
- // Can define methods for instances like this
- this.someMethod = function () {
- // Do some stuff
- };
- */
-
-};
-
-/*
-// Can also define them on the prototype
-<%= names.constructor.singular %>.prototype.someOtherMethod = function () {
- // Do some other stuff
-};
-// Can also define static methods and properties
-<%= names.constructor.singular %>.someStaticMethod = function () {
- // Do some other stuff
-};
-<%= names.constructor.singular %>.someStaticProperty = 'YYZ';
-*/
-
-<%= names.constructor.singular %> = geddy.model.register('<%= names.constructor.singular %>', <%= names.constructor.singular %>);
-
Please sign in to comment.
Something went wrong with that request. Please try again.