-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jakefile
177 lines (149 loc) · 4.54 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var path = require('path')
, fs = require('fs')
, cwd = process.cwd()
, utilities = require('utilities')
, genutils = require('geddy-genutils')
, genDirname = __dirname;
var ns = 'scaffold';
// Load the basic Geddy toolkit
genutils.loadGeddy();
var utils = genutils.loadGeddyUtils();
// Tasks
task('default', function() {
var self = this;
var t = jake.Task['scaffold:create'];
t.reenable();
t.invoke.apply(t, Array.prototype.slice.call(arguments));
});
namespace(ns, function() {
task('help', function() {
console.log(
fs.readFileSync(
path.join(__dirname, 'help.txt'),
{encoding: 'utf8'}
)
);
});
desc('Creates a model scaffold.');
task('create', {async: true}, function(/*name, modelProperties*/) {
if (!genutils.inAppRoot()) {
fail('You must run this generator from the root of your application.');
return;
}
var args = Array.prototype.slice.call(arguments);
var name = args.shift();
var modelProperties = (args.length > 0) ? args : null;
if (!name) {
fail('Missing model name.');
return;
}
var engine = process.env['engine'] || 'ejs';
var framework = process.env['framework'] || 'bootstrap/none';
// parse the framework
framework = framework.split('/', 2);
framework = {
css: framework[0],
js: (framework.length == 2) ? framework[1] : 'none'
};
if (!templatesDir) {
var templatesDir = process.env['templates'] || path.join(__dirname, 'template');
}
var bower = genutils.flagSet(null, '--bower');
var ext = genutils.template.getExtFromEngine(engine);
// get model gen helpers
var modelHelpers = require(path.join(path.dirname(require.resolve('geddy-gen-model')), 'helpers'));
var properties = modelHelpers.formatModelProperties(modelProperties);
var names = utilities.string.getInflections(name);
var viewData = {
engine: engine,
framework: framework,
name: name,
properties: properties,
names: names,
genutils: genutils
};
// create resource
genutils.runGen('geddy-gen-resource', [name].concat(modelProperties), onResourceGen);
function onResourceGen(err)
{
if (err) {
console.error(err);
fail('Error creating the resource.');
return;
}
if (bower) {
setupBower(function() {
createViews();
complete();
});
}
else {
createViews();
complete();
}
}
function setupBower(cb)
{
genutils.jake.loadFiles(genutils.getGenDir('geddy-gen-app'));
var t = jake.Task['app:bower'];
t.on('complete', function() {
var deps = ['jquery'];
console.log('installing bower components now ...');
var deps = ['jquery'];
if (['bootstrap', 'foundation'].indexOf(framework.css) !== -1) {
deps.push(framework.css);
}
genutils.bower.install(deps, function() {
cb();
});
});
t.invoke();
}
function createViews()
{
// create views
genutils.jake.loadFiles(genutils.getGenDir('geddy-gen-view'));
var viewTask = jake.Task['view:create'];
// index
var files = new jake.FileList();
files.include(templatesDir + '/**/*.html.ejs');
files.toArray().forEach(function(file) {
viewTask.reenable();
viewTask.invoke(
path.join(names.filename.plural, path.basename(file, path.extname(file)) + ext),
file,
viewData,
transformTemplate
);
});
}
function transformTemplate(content)
{
return content.replace(/\<\@/g, '<%').replace(/\@\>/g, '%>').trim();
}
});
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('Scaffold', ['scaffold:clean', 'scaffold: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');
});