|
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
5 | 5 | import 'package:file/file.dart'; |
| 6 | +import 'package:meta/meta.dart'; |
| 7 | +import 'package:pub_semver/pub_semver.dart'; |
6 | 8 |
|
7 | 9 | import 'common/core.dart'; |
8 | 10 | import 'common/package_looping_command.dart'; |
9 | 11 | import 'common/plugin_utils.dart'; |
10 | 12 | import 'common/repository_package.dart'; |
11 | 13 |
|
| 14 | +/// The lowest `ext.kotlin_version` that example apps are allowed to use. |
| 15 | +@visibleForTesting |
| 16 | +final Version minKotlinVersion = Version(1, 7, 10); |
| 17 | + |
12 | 18 | /// A command to enforce gradle file conventions and best practices. |
13 | 19 | class GradleCheckCommand extends PackageLoopingCommand { |
14 | 20 | /// Creates an instance of the gradle check command. |
@@ -125,6 +131,9 @@ class GradleCheckCommand extends PackageLoopingCommand { |
125 | 131 | if (!_validateJavacLintConfig(package, lines)) { |
126 | 132 | succeeded = false; |
127 | 133 | } |
| 134 | + if (!_validateKotlinVersion(package, lines)) { |
| 135 | + succeeded = false; |
| 136 | + } |
128 | 137 | return succeeded; |
129 | 138 | } |
130 | 139 |
|
@@ -347,4 +356,26 @@ gradle.projectsEvaluated { |
347 | 356 | } |
348 | 357 | return true; |
349 | 358 | } |
| 359 | + |
| 360 | + /// Validates whether the given [example] has its Kotlin version set to at |
| 361 | + /// least a minimum value, if it is set at all. |
| 362 | + bool _validateKotlinVersion( |
| 363 | + RepositoryPackage example, List<String> gradleLines) { |
| 364 | + final RegExp kotlinVersionRegex = |
| 365 | + RegExp(r"ext\.kotlin_version\s*=\s*'([\d.]+)'"); |
| 366 | + RegExpMatch? match; |
| 367 | + if (gradleLines.any((String line) { |
| 368 | + match = kotlinVersionRegex.firstMatch(line); |
| 369 | + return match != null; |
| 370 | + })) { |
| 371 | + final Version version = Version.parse(match!.group(1)!); |
| 372 | + if (version < minKotlinVersion) { |
| 373 | + printError('build.gradle sets "ext.kotlin_version" to "$version". The ' |
| 374 | + 'minimum Kotlin version that can be specified is ' |
| 375 | + '$minKotlinVersion, for compatibility with modern dependencies.'); |
| 376 | + return false; |
| 377 | + } |
| 378 | + } |
| 379 | + return true; |
| 380 | + } |
350 | 381 | } |
0 commit comments