-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[tool] Add initial gradle validation command #3715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9a3f1fa
8d956a1
f86e197
cafa8b7
3affe6f
fd3e047
b5a8e90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 6.0.28 | ||
|
||
* Sets an explicit Java compatibility version. | ||
|
||
## 6.0.27 | ||
|
||
* Fixes Java warnings. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:file/file.dart'; | ||
|
||
import 'common/core.dart'; | ||
import 'common/package_looping_command.dart'; | ||
import 'common/repository_package.dart'; | ||
|
||
/// A command to enforce gradle file conventions and best practices. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not true based on the implementation. I think this will only impact topline gradle files and does not check gradle files in app. Also from a structural perspective I think you might want to logically separate example gradle files from gradle files of plugins that are published. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is currently true, because the check I've added didn't seem important for apps. If that's not the case we can definitely change that.
Yes, that's why I have it only iterating over top-level packages, rather than setting the command to iterate over everything. My thought was that we could add example checks later, but I can definitely implement the structure for that now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It now iterates over everything, and just skips the current check for examples, so it will be easy to add things for all gradle files later. (Let me know if you want this specific check to run for examples; currently it's not even in the |
||
class GradleCheckCommand extends PackageLoopingCommand { | ||
/// Creates an instance of the gradle check command. | ||
GradleCheckCommand(Directory packagesDir) : super(packagesDir); | ||
|
||
@override | ||
final String name = 'gradle-check'; | ||
|
||
@override | ||
final String description = | ||
'Checks that gradle files follow repository conventions.'; | ||
|
||
@override | ||
bool get hasLongOutput => false; | ||
|
||
@override | ||
PackageLoopingType get packageLoopingType => | ||
PackageLoopingType.includeAllSubpackages; | ||
|
||
@override | ||
Future<PackageResult> runForPackage(RepositoryPackage package) async { | ||
if (!package.platformDirectory(FlutterPlatform.android).existsSync()) { | ||
return PackageResult.skip('No android/ directory.'); | ||
} | ||
|
||
const String exampleDirName = 'example'; | ||
final bool isExample = package.directory.basename == exampleDirName || | ||
package.directory.parent.basename == exampleDirName; | ||
if (!_validateBuildGradle(package, isExample: isExample)) { | ||
return PackageResult.fail(); | ||
} | ||
return PackageResult.success(); | ||
} | ||
|
||
bool _validateBuildGradle(RepositoryPackage package, | ||
{required bool isExample}) { | ||
// Currently the only check is not relevant to examples; checks that apply | ||
// to both plugins and examples should go above here. | ||
if (!isExample) { | ||
print('${indentation}Validating android/build.gradle.'); | ||
final String contents = package | ||
.platformDirectory(FlutterPlatform.android) | ||
.childFile('build.gradle') | ||
.readAsStringSync(); | ||
final List<String> lines = contents.split('\n'); | ||
|
||
if (!lines.any((String line) => | ||
line.contains('languageVersion') && | ||
!line.trim().startsWith('//')) && | ||
!lines.any((String line) => | ||
line.contains('sourceCompatibility') && | ||
!line.trim().startsWith('//'))) { | ||
const String errorMessage = ''' | ||
build.gradle must set an explicit Java compatibility version. | ||
|
||
This can be done either via "sourceCompatibility": | ||
android { | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
} | ||
|
||
or "toolchain": | ||
java { | ||
toolchain { | ||
languageVersion = JavaLanguageVersion.of(8) | ||
} | ||
} | ||
|
||
See: | ||
https://docs.gradle.org/current/userguide/java_plugin.html#toolchain_and_compatibility | ||
for more details.'''; | ||
|
||
printError( | ||
'$indentation${errorMessage.split('\n').join('\n$indentation')}'); | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know changing file names can be a pain but can we name this something more specific. I think we will have more gradle checks moving forward. Maybe
gradle_conventions_check_command
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was assuming that future gradle checks would be added here; is there a reason we'd want new commands for those instead of having them all here? If you look at things like pubspec-check, we do a bunch of different checks related to the file in the one command.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I had assumed that this command was only for the gradle files at the top of packages since that was what was parsed. Basically the implementation was the behavior you desired but the documentation was incorrect. Based on this update if all checks will live in this file then then the name makes sense.