-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
86 lines (75 loc) · 3.4 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
var fs = require('fs'),
path = require('path'),
through = require('through2'),
gutil = require('gulp-util'),
PluginError = gutil.PluginError,
exec = require('child_process').exec,
os = require('os'),
format = require('util').format;
module.exports = function (options) {
var SONAR_VERSION,
SONAR_SCANNER_HOME,
SONAR_SCANNER_JAR,
SONAR_SCANNER_COMMAND,
write,
flush;
SONAR_VERSION = "2.8";
SONAR_SCANNER_HOME = path.join(__dirname, format('/sonar-scanner-%s', SONAR_VERSION));
SONAR_SCANNER_PROPERTIES = path.join(__dirname, format('/sonar-scanner-%s', SONAR_VERSION), 'conf', 'sonar-scanner.properties');
SONAR_SCANNER_JAR = format('/lib/sonar-scanner-cli-%s.jar', SONAR_VERSION);
SONAR_SCANNER_COMMAND = 'java -jar "' + path.join(SONAR_SCANNER_HOME, SONAR_SCANNER_JAR) + '" -X -Drunner.home="' + SONAR_SCANNER_HOME + '" -Dproject.settings="' + SONAR_SCANNER_PROPERTIES + '"';
write = function (file, enc, cb) {
// do nothing with source ... not needed
cb();
};
flush = function (cb) {
var props,
process;
options = (typeof options === 'object') ? options : { sonar: {} };
options.sonar.language = options.sonar.language;
options.sonar.sourceEncoding = options.sonar.sourceEncoding || 'UTF-8';
options.sonar.host = options.sonar.host || { url: 'http://localhost:9000' };
options_exec = options.sonar.exec;
// This property is not for the sonar runner, but for the nodejs exec method, so we remove it after copied it
delete options.sonar.exec;
function objectToProps(obj, result, prefix, o, prop) {
obj = (typeof obj === 'object') ? obj : {};
result = (typeof result === 'object' && typeof result.length === 'number') ? result : [];
prefix = (typeof prefix === 'string' && prefix.length) ? prefix + '.' : '';
for (o in obj) {
if (obj.hasOwnProperty(o)) {
prop = prefix + o;
if (typeof obj[o] === 'string') {
result.push(prop + '=' + obj[o]);
} else if (typeof obj[o] === 'object') {
result = objectToProps(obj[o], result, prop);
}
}
}
return result;
}
props = objectToProps(options);
fs.writeFile(path.join(SONAR_SCANNER_HOME, '/conf/sonar-scanner.properties'), props.join(os.EOL), function (err) {
if (err) {
throw new PluginError('gulp-sonar', format('Error writing properties file: %d.', err));
} else {
process = exec(SONAR_SCANNER_COMMAND, options_exec, function () {});
process.stdout.on('data', function (c) {
gutil.log(c);
});
process.stderr.on('data', function (c) {
gutil.log(c);
});
process.on('exit', function (code) {
if (code !== 0) {
gutil.log(format('Return code: %d.', code));
throw new PluginError('gulp-sonar', format('Return code: %d.', code));
}
gutil.log(format('Return code: %d.', code));
cb();
});
}
});
};
return through.obj(write, flush);
};