-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jakefile
145 lines (120 loc) · 3.99 KB
/
Jakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var path = require('path')
, fs = require('fs')
, cwd = process.cwd()
, utilities = require('utilities')
, helpers = require('./helpers')
, genutils = require('geddy-genutils')
, genDirname = __dirname;
var ns = 'model';
// Load the basic Geddy toolkit
genutils.loadGeddy();
var utils = genutils.loadGeddyUtils();
var _writeTemplate = function (name, filename, dirname, opts) {
var options = opts || {}
, names = utils.string.getInflections(name)
, bare = options.bare || false; // Default to full controller
genutils.template.write(
path.join(genDirname, filename + '.ejs'),
path.join(dirname, names.filename[options.inflection] + '.js'),
{names: names, bare: bare, properties: options.properties}
);
};
// Tasks
task('default', function() {
var self = this;
var t = jake.Task['model:create'];
t.reenable();
t.invoke.apply(t, Array.prototype.slice.call(arguments));
});
namespace(ns, function() {
task('create', function () {
if (!genutils.inAppRoot()) {
fail('You must run this generator from the root of your application.');
return;
}
var self = this;
var args = Array.prototype.slice.call(arguments);
if (args.length < 1) {
fail('No model name specified.');
return;
}
var force = genutils.flagSet('-f','--force');
var name = args.shift();
var properties = (args.length > 0) ? args : null;
var props = helpers.formatModelProperties(properties)
, createTableTask;
if (!name) {
fail('No model name specified.');
return;
}
var appPath = process.cwd();
var modelsDir = path.join(appPath, 'app', 'models');
// sanitize the controller name
var modelFileName = utilities.string.getInflection(name, 'filename', 'singular')
.toLowerCase().replace(/\s|-/g, '_');
var modelFilePath = path.join(modelsDir, modelFileName + '.js');
if (!force && fs.existsSync(modelFilePath)) {
fail('Model already exists. Use -f to replace it.');
return;
}
var modelPath = path.join('template' ,'model.js');
_writeTemplate(name, modelPath, modelsDir, {
inflection: 'singular'
, properties: props
});
// Create model test scaffold
jake.Task['model:create-test'].invoke(name);
// create db directory if not existing yet
jake.mkdirP(path.join(appPath, 'db'));
// Create the corresponding migration
createTableTask = jake.Task['model:migration:createForTable'];
createTableTask.invoke(name, props);
});
// Delegate to stuff in jakelib/migration.jake
task('migration', function (name) {
if (!name) {
throw new Error('No migration name provided.');
}
var t = jake.Task['model:migration:create'];
t.invoke.apply(t, arguments);
});
task('create-test', function (name) {
if (!name) {
throw new Error('No test name specified.');
}
_writeTemplate(name, path.join('template', 'test_model.js'), path.join('test', 'models'),
{inflection: 'singular'});
});
task('help', function() {
console.log(
fs.readFileSync(
path.join(__dirname, 'help.txt'),
{encoding: 'utf8'}
)
);
});
desc('Clears the test temp directory.');
task('clean', function() {
console.log('Cleaning temp files ...');
var tmpDir = path.join(__dirname, 'test', 'tmp');
utilities.file.rmRf(tmpDir, {silent:true});
fs.mkdirSync(tmpDir);
});
desc('Copies the test app into the temp directory.');
task('prepare-test-app', function() {
console.log('Preparing test app ...');
jake.cpR(
path.join(__dirname, 'test', 'geddy-test-app'),
path.join(__dirname, 'test', 'tmp'),
{silent: true}
);
console.log('Test app prepared.');
});
});
testTask('Model', ['model:clean', 'model:prepare-test-app'], function() {
this.testFiles.exclude('test/helpers/**');
this.testFiles.exclude('test/fixtures/**');
this.testFiles.exclude('test/geddy-test-app');
this.testFiles.exclude('test/tmp/**');
this.testFiles.include('test/**/*.js');
});