|
1 |
| -var fs = require('fs'); |
2 |
| -var path = require('path'); |
3 |
| -var os = require('os'); |
4 |
| -var exec = require('child_process').execSync; |
5 |
| -var mkdirs = require('mkdirp').sync; |
6 |
| -var extend = require('extend'); |
7 |
| -var Download = require('download'); |
8 |
| -var downloadStatus = require('download-status'); |
9 |
| -var log = require('fancy-log'); |
10 |
| -var logError = log.error; |
11 |
| -var sonarQubeParams = require('./sonarqube-scanner-params'); |
12 |
| - |
13 |
| -module.exports.prepareExecEnvironment = prepareExecEnvironment; |
14 |
| -module.exports.getSonarQubeScannerExecutable = getSonarQubeScannerExecutable; |
15 |
| -module.exports.getLocalSonarQubeScannerExecutable = getLocalSonarQubeScannerExecutable; |
| 1 | +var fs = require('fs') |
| 2 | +var path = require('path') |
| 3 | +var os = require('os') |
| 4 | +var exec = require('child_process').execSync |
| 5 | +var mkdirs = require('mkdirp').sync |
| 6 | +var extend = require('extend') |
| 7 | +var Download = require('download') |
| 8 | +var downloadStatus = require('download-status') |
| 9 | +var log = require('fancy-log') |
| 10 | +var logError = log.error |
| 11 | +var sonarQubeParams = require('./sonarqube-scanner-params') |
16 | 12 |
|
| 13 | +module.exports.prepareExecEnvironment = prepareExecEnvironment |
| 14 | +module.exports.getSonarQubeScannerExecutable = getSonarQubeScannerExecutable |
| 15 | +module.exports.getLocalSonarQubeScannerExecutable = getLocalSonarQubeScannerExecutable |
17 | 16 |
|
18 | 17 | /*
|
19 | 18 | * Prepare the executable options (including env environments) required to run the
|
20 | 19 | * SQ executable.
|
21 | 20 | */
|
22 | 21 | function prepareExecEnvironment(params, process) {
|
23 |
| - // Define what the SQ Scanner params must be |
24 |
| - var processEnvParams = {}; |
25 |
| - if (process.env.SONARQUBE_SCANNER_PARAMS) { |
26 |
| - processEnvParams = JSON.parse(process.env.SONARQUBE_SCANNER_PARAMS); |
27 |
| - } |
28 |
| - var sqScannerParams = sonarQubeParams( |
29 |
| - params, |
30 |
| - process.cwd(), |
31 |
| - processEnvParams |
32 |
| - ); |
| 22 | + // Define what the SQ Scanner params must be |
| 23 | + var processEnvParams = {} |
| 24 | + if (process.env.SONARQUBE_SCANNER_PARAMS) { |
| 25 | + processEnvParams = JSON.parse(process.env.SONARQUBE_SCANNER_PARAMS) |
| 26 | + } |
| 27 | + var sqScannerParams = sonarQubeParams(params, process.cwd(), processEnvParams) |
33 | 28 |
|
34 |
| - // We need to merge the existing env variables (process.env) with the SQ ones |
35 |
| - var mergedEnv = {}; |
36 |
| - extend(mergedEnv, process.env, { |
37 |
| - SONARQUBE_SCANNER_PARAMS: JSON.stringify(sqScannerParams) |
38 |
| - }); |
| 29 | + // We need to merge the existing env variables (process.env) with the SQ ones |
| 30 | + var mergedEnv = {} |
| 31 | + extend(mergedEnv, process.env, { |
| 32 | + SONARQUBE_SCANNER_PARAMS: JSON.stringify(sqScannerParams) |
| 33 | + }) |
39 | 34 |
|
40 |
| - // this is the actual object that the process.exec function is waiting for |
41 |
| - var options_exec = { |
42 |
| - env: mergedEnv, |
43 |
| - stdio: [0, 1, 2], |
44 |
| - // Increase the amount of data allowed on stdout or stderr |
45 |
| - // (if this value is exceeded then the child process is killed). |
46 |
| - // TODO: make this customizable |
47 |
| - maxBuffer: 1024 * 1024 |
48 |
| - }; |
| 35 | + // this is the actual object that the process.exec function is waiting for |
| 36 | + var optionsExec = { |
| 37 | + env: mergedEnv, |
| 38 | + stdio: [0, 1, 2], |
| 39 | + // Increase the amount of data allowed on stdout or stderr |
| 40 | + // (if this value is exceeded then the child process is killed). |
| 41 | + // TODO: make this customizable |
| 42 | + maxBuffer: 1024 * 1024 |
| 43 | + } |
49 | 44 |
|
50 |
| - return options_exec; |
| 45 | + return optionsExec |
51 | 46 | }
|
52 | 47 |
|
53 | 48 | /*
|
54 | 49 | * Returns the SQ Scanner executable for the current platform
|
55 | 50 | */
|
56 | 51 | function getSonarQubeScannerExecutable(passExecutableCallback) {
|
57 |
| - const platformBinariesVersion = "3.0.3.778"; |
58 |
| - var targetOS = findTargetOS(); |
59 |
| - var installFolder = path.join(os.homedir(), ".sonar", "native-sonar-scanner"); |
60 |
| - var binaryExtension = ""; |
61 |
| - if (isWindows()) { |
62 |
| - binaryExtension = ".bat"; |
63 |
| - } |
64 |
| - var platformExecutable = path.join(installFolder, |
65 |
| - `sonar-scanner-${platformBinariesVersion}-${targetOS}`, |
66 |
| - "bin", |
67 |
| - `sonar-scanner${binaryExtension}`); |
| 52 | + const platformBinariesVersion = '3.0.3.778' |
| 53 | + var targetOS = findTargetOS() |
| 54 | + var installFolder = path.join(os.homedir(), '.sonar', 'native-sonar-scanner') |
| 55 | + var binaryExtension = '' |
| 56 | + if (isWindows()) { |
| 57 | + binaryExtension = '.bat' |
| 58 | + } |
| 59 | + var platformExecutable = path.join( |
| 60 | + installFolder, |
| 61 | + `sonar-scanner-${platformBinariesVersion}-${targetOS}`, |
| 62 | + 'bin', |
| 63 | + `sonar-scanner${binaryExtension}` |
| 64 | + ) |
68 | 65 |
|
69 |
| - // #1 - Try to execute the scanner |
70 |
| - var executableFound = false; |
71 |
| - try { |
72 |
| - log("Checking if executable exists: " + platformExecutable); |
73 |
| - fs.accessSync(platformExecutable, fs.F_OK); |
74 |
| - // executable exists! |
75 |
| - log("Platform binaries for SonarQube scanner found. Using it."); |
76 |
| - executableFound = true; |
77 |
| - } catch (e) { |
78 |
| - log("Could not find executable in '" + installFolder + "'."); |
79 |
| - } |
80 |
| - if (executableFound) { |
81 |
| - passExecutableCallback(platformExecutable); |
82 |
| - return; |
83 |
| - } |
| 66 | + // #1 - Try to execute the scanner |
| 67 | + var executableFound = false |
| 68 | + try { |
| 69 | + log('Checking if executable exists: ' + platformExecutable) |
| 70 | + fs.accessSync(platformExecutable, fs.F_OK) |
| 71 | + // executable exists! |
| 72 | + log('Platform binaries for SonarQube scanner found. Using it.') |
| 73 | + executableFound = true |
| 74 | + } catch (e) { |
| 75 | + log('Could not find executable in "' + installFolder + '".') |
| 76 | + } |
| 77 | + if (executableFound) { |
| 78 | + passExecutableCallback(platformExecutable) |
| 79 | + return |
| 80 | + } |
84 | 81 |
|
85 |
| - // #2 - Download the binaries and unzip them |
86 |
| - // They are located at https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-${version}-${os}.zip |
87 |
| - log("Proceed with download of the platform binaries for SonarQube Scanner..."); |
88 |
| - log("Creating " + installFolder); |
89 |
| - mkdirs(installFolder); |
90 |
| - var baseUrl = process.env.SONAR_SCANNER_MIRROR || "https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/"; |
91 |
| - var fileName = "sonar-scanner-cli-" |
92 |
| - + platformBinariesVersion |
93 |
| - + "-" |
94 |
| - + targetOS |
95 |
| - + ".zip"; |
96 |
| - var downloadUrl = baseUrl + fileName; |
97 |
| - log(`Downloading from ${downloadUrl}`); |
98 |
| - log(`(executable will be saved in cache folder: ${installFolder})`); |
99 |
| - new Download({extract: true}) |
100 |
| - .get(downloadUrl) |
101 |
| - .dest(installFolder) |
102 |
| - .use(downloadStatus()) |
103 |
| - .run((err) => { |
104 |
| - if (err) { |
105 |
| - logError(`Impossible to download and extract binary: ${err.message}`); |
106 |
| - logError(`SonarQube Scanner binaries probably don't exist for your OS (${targetOS}).`); |
107 |
| - logError("In such situation, the best solution is to install the standard SonarQube Scanner (requires a JVM)."); |
108 |
| - logError("Check it out at https://redirect.sonarsource.com/doc/install-configure-scanner.html"); |
109 |
| - throw err; |
110 |
| - } |
111 |
| - passExecutableCallback(platformExecutable); |
112 |
| - }); |
| 82 | + // #2 - Download the binaries and unzip them |
| 83 | + // They are located at https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-${version}-${os}.zip |
| 84 | + log('Proceed with download of the platform binaries for SonarQube Scanner...') |
| 85 | + log('Creating ' + installFolder) |
| 86 | + mkdirs(installFolder) |
| 87 | + var baseUrl = process.env.SONAR_SCANNER_MIRROR || 'https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/' |
| 88 | + var fileName = 'sonar-scanner-cli-' + platformBinariesVersion + '-' + targetOS + '.zip' |
| 89 | + var downloadUrl = baseUrl + fileName |
| 90 | + log(`Downloading from ${downloadUrl}`) |
| 91 | + log(`(executable will be saved in cache folder: ${installFolder})`) |
| 92 | + new Download({ extract: true }) |
| 93 | + .get(downloadUrl) |
| 94 | + .dest(installFolder) |
| 95 | + .use(downloadStatus()) |
| 96 | + .run(err => { |
| 97 | + if (err) { |
| 98 | + logError(`Impossible to download and extract binary: ${err.message}`) |
| 99 | + logError(`SonarQube Scanner binaries probably don't exist for your OS (${targetOS}).`) |
| 100 | + logError('In such situation, the best solution is to install the standard SonarQube Scanner (requires a JVM).') |
| 101 | + logError('Check it out at https://redirect.sonarsource.com/doc/install-configure-scanner.html') |
| 102 | + throw err |
| 103 | + } |
| 104 | + passExecutableCallback(platformExecutable) |
| 105 | + }) |
113 | 106 | }
|
114 | 107 |
|
115 | 108 | /*
|
116 | 109 | * Returns the SQ Scanner executable if one available in the PATH (meaning user has also JAVA)
|
117 | 110 | */
|
118 | 111 | function getLocalSonarQubeScannerExecutable(passExecutableCallback) {
|
119 |
| - var command = "sonar-scanner"; |
120 |
| - if (isWindows()) { |
121 |
| - command += ".bat"; |
122 |
| - } |
| 112 | + var command = 'sonar-scanner' |
| 113 | + if (isWindows()) { |
| 114 | + command += '.bat' |
| 115 | + } |
123 | 116 |
|
124 |
| - // Try to execute the "sonar-scanner" command to see if it's installed locally |
125 |
| - try { |
126 |
| - log("Trying to find a local install of the SonarQube Scanner"); |
127 |
| - exec(command + " -v", {}); |
128 |
| - // if we're here, this means that the SQ Scanner can be executed |
129 |
| - // TODO: we should check that it's at least v2.8+ |
130 |
| - log("Local install of SonarQube scanner found. Using it."); |
131 |
| - passExecutableCallback(command); |
132 |
| - return; |
133 |
| - } catch (e) { |
134 |
| - // sonar-scanner is not in the PATH |
135 |
| - throw Error("Local install of SonarQube scanner not found."); |
136 |
| - } |
| 117 | + // Try to execute the 'sonar-scanner' command to see if it's installed locally |
| 118 | + try { |
| 119 | + log('Trying to find a local install of the SonarQube Scanner') |
| 120 | + exec(command + ' -v', {}) |
| 121 | + // if we're here, this means that the SQ Scanner can be executed |
| 122 | + // TODO: we should check that it's at least v2.8+ |
| 123 | + log('Local install of SonarQube scanner found. Using it.') |
| 124 | + passExecutableCallback(command) |
| 125 | + return |
| 126 | + } catch (e) { |
| 127 | + // sonar-scanner is not in the PATH |
| 128 | + throw Error('Local install of SonarQube scanner not found.') |
| 129 | + } |
137 | 130 | }
|
138 | 131 |
|
139 | 132 | /*
|
140 | 133 | * Get the target OS based on the platform name
|
141 | 134 | */
|
142 | 135 | function findTargetOS() {
|
143 |
| - if (isWindows()) { |
144 |
| - return "windows"; |
145 |
| - } |
146 |
| - if (isLinux()) { |
147 |
| - return "linux"; |
148 |
| - } |
149 |
| - if (isMac()) { |
150 |
| - return "macosx"; |
151 |
| - } |
152 |
| - throw Error(`Your platform "${process.platform}" is currently not supported.`) |
| 136 | + if (isWindows()) { |
| 137 | + return 'windows' |
| 138 | + } |
| 139 | + if (isLinux()) { |
| 140 | + return 'linux' |
| 141 | + } |
| 142 | + if (isMac()) { |
| 143 | + return 'macosx' |
| 144 | + } |
| 145 | + throw Error(`Your platform '${process.platform}' is currently not supported.`) |
153 | 146 | }
|
154 | 147 |
|
155 | 148 | /*
|
156 | 149 | * Some util functions...
|
157 | 150 | */
|
158 | 151 | function isWindows() {
|
159 |
| - return /^win/.test(process.platform); |
| 152 | + return /^win/.test(process.platform) |
160 | 153 | }
|
161 | 154 |
|
162 | 155 | function isMac() {
|
163 |
| - return /^darwin/.test(process.platform); |
| 156 | + return /^darwin/.test(process.platform) |
164 | 157 | }
|
165 | 158 |
|
166 | 159 | function isLinux() {
|
167 |
| - return /^linux/.test(process.platform); |
| 160 | + return /^linux/.test(process.platform) |
168 | 161 | }
|
0 commit comments