-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJakefile
169 lines (143 loc) · 3.9 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
var path = require('path')
, fs = require('fs')
, cwd = process.cwd()
, utilities = require('utilities')
, genutils = require('geddy-genutils')
, exec = require('child_process').exec
, genDirname = __dirname;
// Load the basic Geddy toolkit
genutils.loadGeddy();
var utils = genutils.loadGeddyUtils();
function getGenPath(genName)
{
return path.join(process.cwd(), 'geddy-gen-' + genName);
}
// Tasks
task('default', {async: true}, function(genName, taskRunner) {
var self = this;
var t = jake.Task.create;
t.reenable();
t.once('done', function() {
complete();
self.emit('done');
});
t.invoke(genName, taskRunner);
});
task('create', {async: true}, function(genName, taskRunner) {
var self = this;
if (!genName) {
fail('Generator name missing.');
return;
}
// sanitize the gen name
genName = genName.toLowerCase().replace(/\s|_/g, '-');
// copy and parse template files
var t = jake.Task['copy-template'];
t.reenable();
t.invoke.call(t, genName, taskRunner);
// install node modules for generator
t = jake.Task['npm-install'];
t.reenable();
t.once('done', function() {
complete();
self.emit('done');
});
t.invoke.call(t, genName);
});
task('copy-template', function(genName, taskRunner) {
if (!taskRunner) {
taskRunner = 'jake';
console.log('No task runner given, using "jake" ...');
}
else {
taskRunner = taskRunner.toLowerCase();
}
var supportedTaskRunners = ['jake', 'grunt', 'gulp', 'none'];
if (supportedTaskRunners.indexOf(taskRunner) === -1) {
fail('The task runner "' + taskRunner + '" is not supported.');
return;
}
// create gen dir
var genPath = getGenPath(genName);
jake.mkdirP(genPath);
// copy and parse shared templates
var sharedPath = path.join(__dirname, 'template/shared');
var sharedFiles = fs.readdirSync(sharedPath);
sharedFiles.forEach(function(sharedFile) {
var ext = path.extname(sharedFile).toLowerCase();
if (ext === '.ejs') {
genutils.template.write(
path.join(sharedPath, sharedFile),
path.join(genPath, path.basename(sharedFile, ext)),
{
genName: genName,
genPath: genPath,
taskRunner: taskRunner
}
);
}
else {
fs.writeFileSync(
path.join(genPath, sharedFile),
fs.readFileSync(path.join(sharedPath, sharedFile))
);
}
});
// copy task runner specifique files
var taskRunnerPath = path.join(__dirname, 'template', taskRunner);
var files = fs.readdirSync(taskRunnerPath);
files.forEach(function(relPath) {
var fullPath = path.join(taskRunnerPath, relPath);
if (fs.statSync(fullPath).isDirectory()) {
jake.cpR(fullPath, genPath, { silent: true });
}
else {
fs.writeFileSync(
path.join(genPath, relPath),
fs.readFileSync(fullPath)
);
}
});
});
task('npm-install', {async: true}, function(genName) {
var self = this;
if (!genName) {
fail('Generator name missing.');
return;
}
var genPath = getGenPath(genName);
// install node modules
var cmd = exec('npm install', {
cwd: genPath
}, function(err) {
if (err) {
fail(err.message);
return;
}
console.log('\ngenerated generator "' + genName + '" in ' + genPath);
complete();
self.emit('done');
});
cmd.stderr.pipe(process.stderr);
cmd.stdout.pipe(process.stdout);
});
task('help', function() {
console.log(
fs.readFileSync(
path.join(__dirname, 'help.txt'),
{encoding: 'utf8'}
)
);
});
testTask('Gens', ['clean'], function() {
this.testFiles.exclude('test/helpers/**');
this.testFiles.exclude('test/tmp/**');
this.testFiles.include('test/**/*.js');
});
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);
});