-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
193 lines (159 loc) · 5.39 KB
/
index.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';
var fs = require('fs');
var path = require('path');
var os = require('os');
var exec = require('child_process').exec;
var fs = require('fs-extra');
var uuid = require('uuid');
var format = require('string-format');
var temp = require('temp');
var glob = require('glob');
var Promise = require('promise');
format.extend(String.prototype);
var clobberDirName = '.clobber';
var cmdSrBuild = 'java -jar "{0}" -build {1} -label {2} -outfile {3} -logdir {4}';
var cmdSrRun = 'java -jar "{0}" -run {1} -jdbc {2} -user {3} -password {4} -logdir {5}';
exports.getInstance= function(config){
return function(changedFilePath, outcome) {
var result;
var runid = uuid.v1();
var fileRelativeDir = path.relative(config.codeSourcePath,path.dirname(changedFilePath));
var relativeFile = path.join(fileRelativeDir, path.basename(changedFilePath));
console.log("Attempting to clob "+ changedFilePath);
temp.mkdir(clobberDirName, function(err, targetWorkingDir){
if (err) throw err;
var targetBuildDir = path.join(targetWorkingDir, 'build');
var targetBuildFilePath = path.join(targetBuildDir, fileRelativeDir, path.basename(changedFilePath));
var buildLabel = 'clobber-{0}-{1}'.format(os.hostname(), runid);
var buildConfigLocation =
config.builderConfigLocation?
config.builderConfigLocation:'builder.cfg';
var buildCmd = cmdSrBuild.format(
config.jarLocation,
targetBuildDir,
buildLabel,
path.join(targetWorkingDir, buildLabel + '.zip'),
targetWorkingDir
);
var runCmd = cmdSrRun.format(
config.jarLocation,
path.join(targetWorkingDir, buildLabel + '.zip'),
config.jdbc,
config.user,
config.password,
targetWorkingDir
);
console.log("Target build directory is: "+targetBuildDir);
function createBuildFileStub(message){
return new Promise(function(resolve, reject){
fs.ensureFile(targetBuildFilePath, function(err){
if(err){
reject(err);
}
console.log("Created stub file and required directories in build directory");
resolve();
});
});
}
function copyBuildFile(){
return new Promise(function(resolve,reject){
fs.copy(changedFilePath, targetBuildFilePath, function(err){
if(err) reject(err);
console.log("Copied changed file to build directory");
resolve();
});
});
}
function copyScriptrunnerDir(){
return new Promise(function(resolve,reject){
fs.copy(
path.join(config.codeSourcePath,'ScriptRunner'),
path.join(targetBuildDir, 'ScriptRunner'),
{
filter : function(file){return file.split('.').pop() != 'mf'}
},
function(err){
var now = new Date().getTime();
while(new Date().getTime() < now + 1000){ /* do nothing */ }
if (err) reject(err);
console.log("Copied ScriptRunner branch to build dir");
resolve();
});
});
};
function findUnneededConfigFiles(){
return new Promise(function(resolve,reject){
glob(path.join(targetBuildDir,'ScriptRunner','builder*.cfg'), function(err, files){
console.log("Attempting to remove unneed config files ");
resolve(files.filter(function(fileName){
console.log("Comparing "+ path.basename(fileName) + " to " +buildConfigLocation );
return path.basename(fileName) !== buildConfigLocation
}));
});
});
}
function deleteFile(fileName){
return new Promise(function(resolve,reject){
fs.remove(fileName, function(err){
if(err) reject(err);
console.log("Deleted file:" + fileName);
resolve();
});
});
}
function deleteFiles(fileNames){
var promiseArray = fileNames.map(function(fileName){
return deleteFile(fileName);
});
return new Promise.all(promiseArray);
}
function sbuild(){
return new Promise(function(resolve,reject){
exec(buildCmd, function(err,stdout,stderr){
console.log("Building ScriptRunner archive");
if (err){
err.stderr = stdout;
reject({"stdout":stdout, "stderr":stderr});
};
resolve(stdout);
});
});
}
function srun(){
return new Promise(function(resolve,reject){
exec(runCmd, function(err,stdout,stderr){
console.log("Running scriptrunner");
if (err){
reject({"stdout":stdout, "stderr":stderr});
};
resolve(stdout);
});
});
}
var clobWork = Promise.resolve();
clobWork
.then(createBuildFileStub)
.then(copyBuildFile)
.then(copyScriptrunnerDir)
.then(findUnneededConfigFiles)
.then(deleteFiles)
.then(sbuild)
.then(srun)
.then(function(){
outcome({
"result":"success",
"clobFile":relativeFile
});
})
.catch(function(err){
console.log(JSON.stringify(err));
outcome({
"result":"failure",
"clobFile":relativeFile,
"stdout":err.stdout,
"stderr":err.stderr
});
});
});
};
};