refactor: simplify doFindLatestInstalledBuildTools (#900)

* chore: update com.g00fy2:versioncompare to 1.3.4

* refactor: flatten error handling in doFindLatestInstalledBuildTools

* refactor: inline & simplify getAvailableBuildTools

* refactor: use string interpolation for error messages
This commit is contained in:
Raphael von der Grün 2020-01-17 13:43:25 +01:00 committed by GitHub
parent 60e022fedd
commit 66ad2c948e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,40 +40,42 @@ String doGetProjectTarget() {
return doEnsureValueExists('project.properties', props, 'target') return doEnsureValueExists('project.properties', props, 'target')
} }
Version[] getAvailableBuildTools() {
def buildToolsDir = new File(getAndroidSdkDir(), "build-tools")
buildToolsDir.list()
.collect { new Version(it) } // Invalid inputs will be handled as 0.0.0
.findAll { it.isHigherThan('0.0.0') }
.sort { a, b -> a.isHigherThan(b) ? -1 : 1 }
}
Boolean isVersionValid(version) { Boolean isVersionValid(version) {
return !(new Version(version)).isEqual('0.0.0') return !(new Version(version)).isEqual('0.0.0')
} }
String doFindLatestInstalledBuildTools(String minBuildToolsVersionString) { String doFindLatestInstalledBuildTools(String minBuildToolsVersionString) {
def availableBuildToolsVersions def buildToolsDirContents
try { try {
availableBuildToolsVersions = getAvailableBuildTools() def buildToolsDir = new File(getAndroidSdkDir(), "build-tools")
buildToolsDirContents = buildToolsDir.list()
} catch (e) { } catch (e) {
println "An exception occurred while trying to find the Android build tools." println "An exception occurred while trying to find the Android build tools."
throw e throw e
} }
if (availableBuildToolsVersions.length > 0) {
def highestBuildToolsVersion = availableBuildToolsVersions[0] def highestBuildToolsVersion = buildToolsDirContents
.collect { new Version(it) }
// Invalid inputs will be handled as 0.0.0
.findAll { it.isHigherThan('0.0.0') }
.max()
if (highestBuildToolsVersion == null) {
throw new RuntimeException("""
No installed build tools found. Install the Android build tools
version ${minBuildToolsVersionString} or higher.
""".replaceAll(/\s+/, ' ').trim())
}
if (highestBuildToolsVersion.isLowerThan(minBuildToolsVersionString)) { if (highestBuildToolsVersion.isLowerThan(minBuildToolsVersionString)) {
throw new RuntimeException( throw new RuntimeException("""
"No usable Android build tools found. Highest installed version is " + No usable Android build tools found. Highest installed version is
highestBuildToolsVersion.getOriginalString() + "; minimum version required is " + ${highestBuildToolsVersion.getOriginalString()}; minimum version
minBuildToolsVersionString + ".") required is ${minBuildToolsVersionString}.
""".replaceAll(/\s+/, ' ').trim())
} }
highestBuildToolsVersion.getOriginalString() highestBuildToolsVersion.getOriginalString()
} else {
throw new RuntimeException(
"No installed build tools found. Install the Android build tools version " +
minBuildToolsVersionString + " or higher.")
}
} }
String getAndroidSdkDir() { String getAndroidSdkDir() {
@ -194,6 +196,6 @@ buildscript {
} }
dependencies { dependencies {
classpath 'com.g00fy2:versioncompare:1.3.1@jar' classpath 'com.g00fy2:versioncompare:1.3.4@jar'
} }
} }