Skip to content

Commit

Permalink
feat: add version-compare library to compare build-tools versions pro…
Browse files Browse the repository at this point in the history
…perly. (#709)

Closes #708
  • Loading branch information
wmramazan authored and erisu committed Jan 14, 2020
1 parent 08ab7d4 commit f4b8f44
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions framework/cordova.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.regex.Pattern
import groovy.swing.SwingBuilder
import com.g00fy2.versioncompare.Version

String doEnsureValueExists(filePath, props, key) {
if (props.get(key) == null) {
Expand All @@ -39,14 +40,15 @@ String doGetProjectTarget() {
return doEnsureValueExists('project.properties', props, 'target')
}

String[] getAvailableBuildTools() {
Version[] getAvailableBuildTools() {
def buildToolsDir = new File(getAndroidSdkDir(), "build-tools")
buildToolsDir.list()
.findAll { it ==~ /[0-9.]+/ }
.sort { a, b -> compareVersions(b, a) }
.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 }
}

String doFindLatestInstalledBuildTools(String minBuildToolsVersion) {
String doFindLatestInstalledBuildTools(String minBuildToolsVersionString) {
def availableBuildToolsVersions
try {
availableBuildToolsVersions = getAvailableBuildTools()
Expand All @@ -56,41 +58,20 @@ String doFindLatestInstalledBuildTools(String minBuildToolsVersion) {
}
if (availableBuildToolsVersions.length > 0) {
def highestBuildToolsVersion = availableBuildToolsVersions[0]
if (compareVersions(highestBuildToolsVersion, minBuildToolsVersion) < 0) {
if (highestBuildToolsVersion.isLowerThan(minBuildToolsVersionString)) {
throw new RuntimeException(
"No usable Android build tools found. Highest installed version is " +
highestBuildToolsVersion + "; minimum version required is " +
minBuildToolsVersion + ".")
highestBuildToolsVersion.getOriginalString() + "; minimum version required is " +
minBuildToolsVersionString + ".")
}
highestBuildToolsVersion
highestBuildToolsVersion.getOriginalString()
} else {
throw new RuntimeException(
"No installed build tools found. Install the Android build tools version " +
minBuildToolsVersion + " or higher.")
minBuildToolsVersionString + " or higher.")
}
}

// Return the first non-zero result of subtracting version list elements
// pairwise. If they are all identical, return the difference in length of
// the two lists.
int compareVersionList(Collection aParts, Collection bParts) {
def pairs = ([aParts, bParts]).transpose()
pairs.findResult(aParts.size()-bParts.size()) {it[0] - it[1] != 0 ? it[0] - it[1] : null}
}

// Compare two version strings, such as "19.0.0" and "18.1.1.0". If all matched
// elements are identical, the longer version is the largest by this method.
// Examples:
// "19.0.0" > "19"
// "19.0.1" > "19.0.0"
// "19.1.0" > "19.0.1"
// "19" > "18.999.999"
int compareVersions(String a, String b) {
def aParts = a.tokenize('.').collect {it.toInteger()}
def bParts = b.tokenize('.').collect {it.toInteger()}
compareVersionList(aParts, bParts)
}

String getAndroidSdkDir() {
def rootDir = project.rootDir
def androidSdkDir = null
Expand Down Expand Up @@ -203,3 +184,12 @@ ext {
cdvHelpers.getConfigPreference = { name, defaultValue -> doGetConfigPreference(name, defaultValue) }
}

buildscript {
repositories {
jcenter()
}

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

0 comments on commit f4b8f44

Please sign in to comment.