From 1d89ae3f6555028e983e5e567aaabeab153596a9 Mon Sep 17 00:00:00 2001 From: Gray Mackall <34871572+gmackall@users.noreply.github.com> Date: Thu, 4 Apr 2024 11:40:07 -0700 Subject: [PATCH] Give `generate_gradle_lockfiles.dart` functionality to exclude certain subdirectories (#146228) Fixes https://github.com/flutter/flutter/issues/146216. --- dev/tools/bin/config/lockfile_exclusion.yaml | 13 ++++++ dev/tools/bin/generate_gradle_lockfiles.dart | 45 ++++++++++++++++++-- dev/tools/pubspec.yaml | 2 +- 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 dev/tools/bin/config/lockfile_exclusion.yaml diff --git a/dev/tools/bin/config/lockfile_exclusion.yaml b/dev/tools/bin/config/lockfile_exclusion.yaml new file mode 100644 index 0000000000..9b942081ed --- /dev/null +++ b/dev/tools/bin/config/lockfile_exclusion.yaml @@ -0,0 +1,13 @@ +# Tests that are excluded from batch Gradle updating and lockfile generation. +# Each exclusion must have an associated reason. Please avoid adding to this list unless necessary, +# as each exclusion must be updated by hand. + +# flutter_gallery uses discontinued plugins with old AGP versions that block the app itself using +# a newer AGP version. +- dev/integration_tests/flutter_gallery/android + +# gradle_deprecated_settings intentionally uses a Gradle file structure matching an old output of +# `flutter create`. It must be updated manually, by changing the desired versions in the Gradle +# files, and then running `rm buildscript-gradle.lockfile && ./gradlew :generateLockfiles` from +# its android subdirectory. +- dev/integration_tests/gradle_deprecated_settings/android diff --git a/dev/tools/bin/generate_gradle_lockfiles.dart b/dev/tools/bin/generate_gradle_lockfiles.dart index 4695b18813..b2b9f5c16b 100644 --- a/dev/tools/bin/generate_gradle_lockfiles.dart +++ b/dev/tools/bin/generate_gradle_lockfiles.dart @@ -2,29 +2,40 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// This script generates `android/build.gradle` for each directory specify in the stdin. +// For each directory specified in the stdin, this script generates: +// 1. The top-level build.gradle (android/build.gradle). +// 2. The top level settings.gradle (android/settings.gradle). +// 3. The gradle wrapper file (android/gradle/wrapper/gradle-wrapper.properties). // Then it generate the lockfiles for each Gradle project. -// To regenerate these files, run `find . -type d -name 'android' | dart dev/tools/bin/generate_gradle_lockfiles.dart` +// To regenerate these files, run `find . -type d -name 'android' | dart dev/tools/bin/generate_gradle_lockfiles.dart`. +import 'dart:collection'; import 'dart:io'; import 'package:args/args.dart'; import 'package:file/file.dart'; import 'package:file/local.dart'; import 'package:path/path.dart' as path; +import 'package:yaml/yaml.dart'; void main(List arguments) { const String usageMessage = "Usage: find . -type d -name 'android' | dart dev/tools/bin/generate_gradle_lockfiles.dart\n" 'If you would rather enter the files manually, just run `dart dev/tools/bin/generate_gradle_lockfiles.dart`,\n' "enter the absolute paths to the app's android directory, then press CTRL-D.\n" "If you don't wish to re-generate the settings.gradle, build.gradle, and gradle-wrapper.properties files,\n" - "add the flag '--no-gradle-generation'"; + 'add the flag `--no-gradle-generation`.\n' + 'This tool automatically excludes a set of android subdirectories, defined at dev/tools/bin/config/lockfile_exclusion.yaml.\n' + 'To disable this behavior, run with `--no-exclusion`.\n'; final ArgParser argParser = ArgParser() ..addFlag( 'gradle-generation', help: 'Re-generate gradle files in each processed directory.', defaultsTo: true, + )..addFlag( + 'exclusion', + help: 'Run the script using the config file at ./configs/lockfile_exclusion.yaml to skip the specified subdirectories.', + defaultsTo: true, ); ArgResults args; @@ -41,9 +52,32 @@ void main(List arguments) { /// Re-generate gradle files in each processed directory. final bool gradleGeneration = (args['gradle-generation'] as bool?) ?? true; + // Skip android subdirectories specified in the ./config/lockfile_exclusion.yaml file. + final bool useExclusion = (args['exclusion'] as bool?) ?? true; + const FileSystem fileSystem = LocalFileSystem(); final List androidDirectories = getFilesFromStdin(); + final File exclusionFile = fileSystem + .currentDirectory.childDirectory('dev').childDirectory('tools').childDirectory('bin') + .childDirectory('config') + .childFile('lockfile_exclusion.yaml'); + + // Load the exclusion set, or make an empty exclusion set. + final Set exclusionSet; + if (useExclusion) { + exclusionSet = HashSet.from( + (loadYaml(exclusionFile.readAsStringSync()) as YamlList) + .toList() + .cast() + ); + print('Loaded exclusion file from ${exclusionFile.path}.'); + } else { + exclusionSet = {}; + print('Running without exclusion.'); + } + + for (final String androidDirectoryPath in androidDirectories) { final Directory androidDirectory = fileSystem.directory(path.normalize(androidDirectoryPath)); @@ -51,6 +85,11 @@ void main(List arguments) { throw '$androidDirectory does not exist'; } + if (exclusionSet.contains(androidDirectory.path)) { + print('${androidDirectory.path} is included in the exclusion config file at ${exclusionFile.path} - skipping'); + continue; + } + final File rootBuildGradle = androidDirectory.childFile('build.gradle'); if (!rootBuildGradle.existsSync()) { print('${rootBuildGradle.path} does not exist - skipping'); diff --git a/dev/tools/pubspec.yaml b/dev/tools/pubspec.yaml index a77ac762bb..aed43be3fc 100644 --- a/dev/tools/pubspec.yaml +++ b/dev/tools/pubspec.yaml @@ -13,6 +13,7 @@ dependencies: path: 1.9.0 process: 5.0.2 pub_semver: 2.1.4 + yaml: 3.1.2 async: 2.11.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" clock: 1.1.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" @@ -60,6 +61,5 @@ dev_dependencies: web: 0.5.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" web_socket_channel: 2.4.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" webkit_inspection_protocol: 1.2.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" - yaml: 3.1.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" # PUBSPEC CHECKSUM: 146d