Skip to content

Commit

Permalink
refactor: simplify doFindLatestInstalledBuildTools (#900)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
raphinesse authored Jan 17, 2020
1 parent 60e022f commit 66ad2c9
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions framework/cordova.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,42 @@ String doGetProjectTarget() {
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) {
return !(new Version(version)).isEqual('0.0.0')
}

String doFindLatestInstalledBuildTools(String minBuildToolsVersionString) {
def availableBuildToolsVersions
def buildToolsDirContents
try {
availableBuildToolsVersions = getAvailableBuildTools()
def buildToolsDir = new File(getAndroidSdkDir(), "build-tools")
buildToolsDirContents = buildToolsDir.list()
} catch (e) {
println "An exception occurred while trying to find the Android build tools."
throw e
}
if (availableBuildToolsVersions.length > 0) {
def highestBuildToolsVersion = availableBuildToolsVersions[0]
if (highestBuildToolsVersion.isLowerThan(minBuildToolsVersionString)) {
throw new RuntimeException(
"No usable Android build tools found. Highest installed version is " +
highestBuildToolsVersion.getOriginalString() + "; minimum version required is " +
minBuildToolsVersionString + ".")
}
highestBuildToolsVersion.getOriginalString()
} else {
throw new RuntimeException(
"No installed build tools found. Install the Android build tools version " +
minBuildToolsVersionString + " or higher.")

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)) {
throw new RuntimeException("""
No usable Android build tools found. Highest installed version is
${highestBuildToolsVersion.getOriginalString()}; minimum version
required is ${minBuildToolsVersionString}.
""".replaceAll(/\s+/, ' ').trim())
}

highestBuildToolsVersion.getOriginalString()
}

String getAndroidSdkDir() {
Expand Down Expand Up @@ -194,6 +196,6 @@ buildscript {
}

dependencies {
classpath 'com.g00fy2:versioncompare:1.3.1@jar'
classpath 'com.g00fy2:versioncompare:1.3.4@jar'
}
}

0 comments on commit 66ad2c9

Please sign in to comment.