-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jakefile
158 lines (135 loc) · 4.65 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
146
147
148
149
150
151
152
153
154
155
156
157
158
var path = require('path')
, fs = require('fs')
, cwd = process.cwd()
, utilities = require('utilities')
, genutils = require('geddy-genutils')
, exec = require('child_process').exec
, helpers = require('./helpers')
, genDirname = __dirname;
var ns = 'controller';
// Load the basic Geddy toolkit
genutils.loadGeddy();
var utils = genutils.loadGeddyUtils();
// Tasks
task('default', function(name) {
var t = jake.Task['controller:create'];
t.reenable();
t.invoke(name);
});
namespace(ns, function() {
task('create', function(name) {
var self = this;
if (!genutils.inAppRoot()) {
fail('You must run this generator from the root of your application.');
return;
}
if (!name) {
fail('Missing controller name.');
return;
}
var appPath = process.cwd();
var controllersDir = path.join(appPath, 'app', 'controllers');
var force = genutils.flagSet('-f','--force');
var resource = genutils.flagSet('-r', '--resource');
// sanitize the controller name
var controllerFileName = name.replace(/\s|-/g, '_');
if (resource) {
controllerFileName = utilities.string.getInflection(controllerFileName, 'filename', 'plural');
}
else {
controllerFileName = utilities.string.getInflection(controllerFileName, 'filename', 'normal');
}
var controllerFilePath = path.join(controllersDir, controllerFileName + '.js');
if (!force && fs.existsSync(controllerFilePath)) {
fail('Controller already exists. Use -f to replace it.');
return;
}
// create constructor name
var constructorName = utilities.string.capitalize(utilities.string.camelize(controllerFileName));
genutils.template.write(
path.join(__dirname, 'template', (resource) ? 'resource_controller.js.ejs' : 'controller.js.ejs'),
controllerFilePath,
{
controllersDir: controllersDir,
controllerFilePath: controllerFilePath,
constructorName: constructorName,
controllerFileName: controllerFileName
}
);
console.log('Created controller "' + constructorName + '" in app/controllers/' + controllerFileName + '.js');
// create route
jake.Task['controller:route'].invoke(name);
});
task('route', function (name) {
if (!name) {
throw new Error('No route name specified.');
}
var resource = genutils.flagSet('-r', '--resource');
var names = utils.string.getInflections(name)
, routerPath = helpers.getRouterPath()
, routeType = resource ? 'Resource' : 'Bare'
, newRoute;
if (routerPath) {
if (routerPath.match('.coffee')) {
if (!resource) {
newRoute = 'router.match(\'/' + names.filename.plural +
'\').to controller: \'' + names.constructor.plural +
'\', action: \'index\'';
} else {
newRoute = 'router.resource \'' + names.filename.plural + '\'';
}
} else if (routerPath.match('.js')) {
if (!resource) {
newRoute = 'router.match(\'/' + names.filename.plural +
'\').to({controller: \'' + names.constructor.plural +
'\', action: \'index\'});';
} else {
newRoute = 'router.resource(\'' + names.filename.plural + '\');';
}
}
if (helpers.addRoute(routerPath, newRoute)) {
console.log('[Added] ' + routeType + ' ' + names.filename.plural +
' route added to ' + routerPath);
}
else {
console.log(routeType + ' ' + names.filename.plural + ' route already defined in ' +
routerPath);
}
}
else {
console.log('There is no router file to add routes too');
}
});
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('Controller', ['controller:clean', 'controller: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');
});