Skip to content

Commit 38d2afe

Browse files
committed
Support multiple external files / Build.gradle will still be introspected when external file is present
1 parent 4ff06c4 commit 38d2afe

File tree

2 files changed

+65
-52
lines changed

2 files changed

+65
-52
lines changed

README.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,28 @@ Simply run `gradle-upgrade-interactive`.
3838

3939
```
4040
Options:
41-
--help Show help [boolean]
42-
--version Show version number [boolean]
43-
--resolution, -r Controls the dependency resolution strategy.
44-
Supported options:
45-
* release: selects the latest release
46-
* milestone: select the latest version being either a
47-
milestone or a release (default)
48-
* integration: selects the latest revision of the dependency
49-
module (such as SNAPSHOT) [string]
50-
--semver, -s Which semantic version diffs to include
51-
(https://semver.org). Flag can be used multiple times.
52-
Supported options:
53-
* major: Include upgrades with a major version change
54-
* minor: Include upgrades with a minor version change
55-
* patch: Include upgrades with a patch version change[array]
56-
--debug, -d Prints debugging information, such as commands executed and
57-
current status. [boolean] [Standard: false]
58-
--no-color Disables color output
41+
--help Show help [boolean]
42+
--version Show version number [boolean]
43+
--resolution, -r Controls the dependency resolution strategy.
44+
Supported options:
45+
* release: selects the latest release
46+
* milestone: select the latest version being either a
47+
milestone or a release (default)
48+
* integration: selects the latest revision of the
49+
dependency module (such as SNAPSHOT) [string]
50+
--semver, -s Which semantic version diffs to include
51+
(https://semver.org). Flag can be used multiple times.
52+
Supported options:
53+
* major: Include upgrades with a major version change
54+
* minor: Include upgrades with a minor version change
55+
* patch: Include upgrades with a patch version change
56+
[array]
57+
--external-file, -e Points to a file where dependencies have been declared,
58+
e.g. gradle/dependencies.gradle. Option can be used
59+
multiple times. [array]
60+
--debug, -d Prints debugging information, such as commands executed
61+
and current status. [boolean] [Standard: false]
62+
--no-color Disables color output
5963
```
6064

6165
## How it works

index.js

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ const argv = require('yargs')
1818
nargs: 1,
1919
demand: false
2020
})
21+
.option('external-file', {
22+
alias: 'e',
23+
describe: 'Points to a file where dependencies have been declared, e.g. gradle/dependencies.gradle. Option can be used multiple times.',
24+
type: 'array',
25+
nargs: 1,
26+
demand: false
27+
})
2128
.option('debug', {
2229
alias: 'd',
2330
describe: 'Prints debugging information, such as commands executed and current status.',
@@ -30,13 +37,6 @@ const argv = require('yargs')
3037
nargs: 1,
3138
demand: false
3239
})
33-
.option('external-file', {
34-
alias: 'e',
35-
describe: 'Points to a file where dependencies have been declared, e.g. gradle/dependencies.gradle.',
36-
type: 'string',
37-
nargs: 1,
38-
demand: false
39-
})
4040
.argv
4141

4242
const prompts = require('prompts');
@@ -69,30 +69,37 @@ if (!gradleCommand) {
6969
return
7070
}
7171

72-
let buildFile
72+
const externalFiles = argv['external-file']
73+
const debug = argv.debug
7374

74-
const externalFile = argv['external-file']
75+
let buildFiles = []
7576

76-
if (externalFile) {
77-
if (!fs.existsSync(externalFile)) {
78-
console.log('Unable to find ' + externalFile + ' file.'.bgRed)
79-
return
80-
}
81-
buildFile = externalFile
82-
} else if (fs.existsSync('build.gradle')) {
83-
buildFile = 'build.gradle'
77+
if (externalFiles && externalFiles.length) {
78+
externalFiles.forEach(externalFile => {
79+
if (!fs.existsSync(externalFile)) {
80+
console.log('Unable to find ' + externalFile + ' file.'.bgRed)
81+
return
82+
} else {
83+
buildFiles.push(externalFile)
84+
}
85+
})
86+
87+
}
88+
89+
if (fs.existsSync('build.gradle')) {
90+
buildFiles.push('build.gradle')
8491
} else if (fs.existsSync('build.gradle.kts')) {
85-
buildFile = 'build.gradle.kts'
92+
buildFiles.push('build.gradle.kts')
8693
}
8794

88-
if (!buildFile) {
89-
console.log('Unable to find a build.gradle or build.gradle.kts file.'.bgRed)
95+
if (!buildFiles.length) {
96+
console.log('Unable to find build.gradle, build.gradle.kts or external build file.'.bgRed)
9097
return
9198
}
9299

93-
console.log('Checking for upgrades...\n')
100+
debugLog('Build files ' + buildFiles)
94101

95-
const debug = argv.debug
102+
console.log('Checking for upgrades...\n')
96103

97104
const gduArgs = ['dependencyUpdates', '-DoutputFormatter=json', '-DoutputDir=build/dependencyUpdates']
98105
const gduResolution = argv.resolution
@@ -234,22 +241,24 @@ function debugLog (message) {
234241
}
235242
}
236243

237-
debugLog('Reading Gradle build file\n')
244+
buildFiles.forEach(buildFile => {
245+
debugLog(`Reading Gradle build file ${buildFile}\n`)
238246

239-
fs.readFile(buildFile, function (err, buf) {
240-
let buildFileAsString = buf.toString()
247+
fs.readFile(buildFile, function (err, buf) {
248+
let buildFileAsString = buf.toString()
241249

242-
response.upgrades.filter(it => it !== 'gradle').forEach(it => {
243-
debugLog(`Replacing version\n${JSON.stringify(it)}\n`)
244-
buildFileAsString = ReplaceVersion.replace(buildFileAsString, it)
245-
})
250+
response.upgrades.filter(it => it !== 'gradle').forEach(it => {
251+
debugLog(`Replacing version\n${JSON.stringify(it)}\n`)
252+
buildFileAsString = ReplaceVersion.replace(buildFileAsString, it)
253+
})
246254

247-
debugLog('Writing Gradle build file\n')
248-
fs.writeFile(buildFile, buildFileAsString, 'utf8', function (err) {
249-
if (err) return console.log(`Unable to write gradle build file.\n${err}`.bgRed);
250-
});
255+
debugLog(`Writing Gradle build file ${buildFile}\n`)
256+
fs.writeFile(buildFile, buildFileAsString, 'utf8', function (err) {
257+
if (err) return console.log(`Unable to write gradle build file.\n${err}`.bgRed);
258+
});
251259

252-
});
260+
});
261+
})
253262

254263

255264
})();

0 commit comments

Comments
 (0)