From 2b890af9391a869b8fa8762ceab503c01f73da5d Mon Sep 17 00:00:00 2001 From: Reid Baker Date: Tue, 16 Jan 2024 14:29:26 -0500 Subject: [PATCH] handle rc versions of gradle in version compare (#141612) - handle number format exceptions and strip rc information from version compare - add test that handles rc format part 2/n https://github.com/flutter/flutter/issues/138523 Helpfully pointed out by [asaarnak](https://github.com/asaarnak) https://github.com/flutter/flutter/pull/139325#issuecomment-1892554584 --- dev/devicelab/bin/tasks/module_test.dart | 1 + .../gradle/src/main/groovy/flutter.groovy | 29 ++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/dev/devicelab/bin/tasks/module_test.dart b/dev/devicelab/bin/tasks/module_test.dart index 1aeb86a14c..9d5f8f8137 100644 --- a/dev/devicelab/bin/tasks/module_test.dart +++ b/dev/devicelab/bin/tasks/module_test.dart @@ -450,5 +450,6 @@ Future main() async { await task(combine([ // ignore: avoid_redundant_argument_values ModuleTest('module-gradle-7.6', gradleVersion: '7.6.3').call, + ModuleTest('module-gradle-7.6', gradleVersion: '7.6-rc-2').call, ])); } diff --git a/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy b/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy index 06e34e8b52..51d8218b09 100644 --- a/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy +++ b/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy @@ -1423,6 +1423,7 @@ class FlutterPlugin implements Plugin { // compareTo implementation of version strings in the format of ints and periods // Requires non null objects. + // Will not crash on RC candidate strings but considers all RC candidates the same version. static int compareVersionStrings(String firstString, String secondString) { List firstVersion = firstString.tokenize(".") List secondVersion = secondString.tokenize(".") @@ -1430,12 +1431,32 @@ class FlutterPlugin implements Plugin { def commonIndices = Math.min(firstVersion.size(), secondVersion.size()) for (int i = 0; i < commonIndices; i++) { - def firstAtIndex = firstVersion[i].toInteger() - def secondAtIndex = secondVersion[i].toInteger() + String firstAtIndex = firstVersion[i] + String secondAtIndex = secondVersion[i] + int firstInt = 0; + int secondInt = 0 + try { + if (firstAtIndex.contains("-")) { + // Strip any chars after "-". For example "8.6-rc-2" + firstAtIndex = firstAtIndex.substring(0, firstAtIndex.indexOf('-')) + } + firstInt = firstAtIndex.toInteger() + } catch (NumberFormatException nfe) { + println(nfe) + } + try { + if (firstAtIndex.contains("-")) { + // Strip any chars after "-". For example "8.6-rc-2" + secondAtIndex = secondAtIndex.substring(0, secondAtIndex.indexOf('-')) + } + secondInt = secondAtIndex.toInteger() + } catch (NumberFormatException nfe) { + println(nfe) + } - if (firstAtIndex != secondAtIndex) { + if (firstInt != secondInt) { // <=> in groovy delegates to compareTo - return firstAtIndex <=> secondAtIndex + return firstInt <=> secondInt } }