Skip to content

Commit ef7e0d5

Browse files
authored
[tool] Refactor args of strings or YAML file lists (#8513)
Multiple commands in the Flutter plugin tool have arguments that accepts a list of strings or a list of YAML files that contain a list of strings. This introduces a `getYamlListArg` helper so that this logic doesn't need to be duplicated multiple times. In a subsequent pull requests, this will be used to add a list of packages that are allowed to have Xcode warnings. This will be used by the google_sign_in_ios package which will have warnings when using SwiftPM. Part of: flutter/flutter#146904
1 parent 2a4beb2 commit ef7e0d5

8 files changed

+44
-62
lines changed

script/configs/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,3 @@ Expected format:
1212
# Reason for exclusion
1313
- name_of_package
1414
```
15-
16-
If there are no exclusions there should be an file with [].

script/configs/dart_unit_tests_wasm_exceptions.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,3 @@
66
#
77
# This is only used for the rare case where a package fails in Wasm, but works
88
# in JS mode.
9-
10-
[] # Needed so the contents of this file are an empty array, not `null`!

script/configs/exclude_all_packages_app_wasm.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@
44
#
55
# This is only used for the rare case where a package fails in Wasm, but works
66
# in JS mode.
7-
8-
[] # Needed so the contents of this file are an empty array, not `null`!

script/configs/exclude_integration_web_wasm.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@
44
#
55
# This is only used for the rare case where a package fails in Wasm, but works
66
# in JS mode.
7-
8-
[] # Needed so the contents of this file are an empty array, not `null`!

script/tool/lib/src/analyze_command.dart

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import 'dart:io' as io;
66

77
import 'package:file/file.dart';
8-
import 'package:yaml/yaml.dart';
98

109
import 'common/output_utils.dart';
1110
import 'common/package_looping_command.dart';
@@ -64,7 +63,7 @@ class AnalyzeCommand extends PackageLoopingCommand {
6463
final bool hasLongOutput = false;
6564

6665
/// Checks that there are no unexpected analysis_options.yaml files.
67-
bool _hasUnexpecetdAnalysisOptions(RepositoryPackage package) {
66+
bool _hasUnexpectedAnalysisOptions(RepositoryPackage package) {
6867
final List<FileSystemEntity> files =
6968
package.directory.listSync(recursive: true, followLinks: false);
7069
for (final FileSystemEntity file in files) {
@@ -94,18 +93,7 @@ class AnalyzeCommand extends PackageLoopingCommand {
9493

9594
@override
9695
Future<void> initializeRun() async {
97-
_allowedCustomAnalysisDirectories =
98-
getStringListArg(_customAnalysisFlag).expand<String>((String item) {
99-
if (item.endsWith('.yaml')) {
100-
final File file = packagesDir.fileSystem.file(item);
101-
final Object? yaml = loadYaml(file.readAsStringSync());
102-
if (yaml == null) {
103-
return <String>[];
104-
}
105-
return (yaml as YamlList).toList().cast<String>();
106-
}
107-
return <String>[item];
108-
}).toSet();
96+
_allowedCustomAnalysisDirectories = getYamlListArg(_customAnalysisFlag);
10997

11098
// Use the Dart SDK override if one was passed in.
11199
final String? dartSdk = argResults![_analysisSdk] as String?;
@@ -161,7 +149,7 @@ class AnalyzeCommand extends PackageLoopingCommand {
161149
}
162150
}
163151

164-
if (_hasUnexpecetdAnalysisOptions(package)) {
152+
if (_hasUnexpectedAnalysisOptions(package)) {
165153
return PackageResult.fail(<String>['Unexpected local analysis options']);
166154
}
167155
final int exitCode = await processRunner.runAndStream(_dartBinaryPath,

script/tool/lib/src/common/package_command.dart

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ abstract class PackageCommand extends Command<void> {
7777
argParser.addMultiOption(
7878
_excludeArg,
7979
abbr: 'e',
80-
help: 'A list of packages to exclude from from this command.\n\n'
80+
help: 'A list of packages to exclude from this command.\n\n'
8181
'Alternately, a list of one or more YAML files that contain a list '
8282
'of packages to exclude.',
8383
defaultsTo: <String>[],
@@ -252,6 +252,22 @@ abstract class PackageCommand extends Command<void> {
252252
return List<String>.from(argResults![key] as List<String>? ?? <String>[]);
253253
}
254254

255+
/// Convenience accessor for arguments containing a list of strings and/or
256+
/// YAML files containing lists of strings.
257+
Set<String> getYamlListArg(String key) {
258+
return getStringListArg(key).expand<String>((String item) {
259+
if (item.endsWith('.yaml')) {
260+
final File file = packagesDir.fileSystem.file(item);
261+
final Object? yaml = loadYaml(file.readAsStringSync());
262+
if (yaml == null) {
263+
return const <String>[];
264+
}
265+
return (yaml as YamlList).toList().cast<String>();
266+
}
267+
return <String>[item];
268+
}).toSet();
269+
}
270+
255271
/// If true, commands should log timing information that might be useful in
256272
/// analyzing their runtime (e.g., the per-package time for multi-package
257273
/// commands).
@@ -277,25 +293,10 @@ abstract class PackageCommand extends Command<void> {
277293
_shardCount = shardCount;
278294
}
279295

280-
/// Converts a list of items which are either package names or yaml files
281-
/// containing a list of package names to a flat list of package names by
282-
/// reading all the file contents.
283-
Set<String> _expandYamlInPackageList(List<String> items) {
284-
return items.expand<String>((String item) {
285-
if (item.endsWith('.yaml')) {
286-
final File file = packagesDir.fileSystem.file(item);
287-
return (loadYaml(file.readAsStringSync()) as YamlList)
288-
.toList()
289-
.cast<String>();
290-
}
291-
return <String>[item];
292-
}).toSet();
293-
}
294-
295296
/// Returns the set of packages to exclude based on the `--exclude` argument.
296297
Set<String> getExcludedPackageNames() {
297-
final Set<String> excludedPackages = _excludedPackages ??
298-
_expandYamlInPackageList(getStringListArg(_excludeArg));
298+
final Set<String> excludedPackages =
299+
_excludedPackages ?? getYamlListArg(_excludeArg);
299300
// Cache for future calls.
300301
_excludedPackages = excludedPackages;
301302
return excludedPackages;
@@ -460,9 +461,8 @@ abstract class PackageCommand extends Command<void> {
460461

461462
final Set<String> excludedPackageNames = getExcludedPackageNames();
462463
final bool hasFilter = argResults?.wasParsed(_filterPackagesArg) ?? false;
463-
final Set<String>? excludeAllButPackageNames = hasFilter
464-
? _expandYamlInPackageList(getStringListArg(_filterPackagesArg))
465-
: null;
464+
final Set<String>? excludeAllButPackageNames =
465+
hasFilter ? getYamlListArg(_filterPackagesArg) : null;
466466
if (excludeAllButPackageNames != null &&
467467
excludeAllButPackageNames.isNotEmpty) {
468468
final List<String> sortedList = excludeAllButPackageNames.toList()

script/tool/lib/src/pubspec_check_command.dart

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,8 @@ class PubspecCheckCommand extends PackageLoopingCommand {
115115
}
116116
}
117117
// Load explicitly allowed packages.
118-
_allowedUnpinnedPackages
119-
.addAll(_getAllowedPackages(_allowDependenciesFlag));
120-
_allowedPinnedPackages
121-
.addAll(_getAllowedPackages(_allowPinnedDependenciesFlag));
122-
}
123-
124-
Iterable<String> _getAllowedPackages(String flag) {
125-
return getStringListArg(flag).expand<String>((String item) {
126-
if (item.endsWith('.yaml')) {
127-
final File file = packagesDir.fileSystem.file(item);
128-
final Object? yaml = loadYaml(file.readAsStringSync());
129-
if (yaml == null) {
130-
return <String>[];
131-
}
132-
return (yaml as YamlList).toList().cast<String>();
133-
}
134-
return <String>[item];
135-
});
118+
_allowedUnpinnedPackages.addAll(getYamlListArg(_allowDependenciesFlag));
119+
_allowedPinnedPackages.addAll(getYamlListArg(_allowPinnedDependenciesFlag));
136120
}
137121

138122
@override

script/tool/test/common/package_command_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,24 @@ void main() {
204204
expect(command.plugins, unorderedEquals(<String>[]));
205205
});
206206

207+
test('exclude accepts empty config files', () async {
208+
final RepositoryPackage plugin1 =
209+
createFakePlugin('plugin1', packagesDir);
210+
final File configFile1 = packagesDir.childFile('exclude1.yaml');
211+
configFile1.createSync();
212+
final File configFile2 = packagesDir.childFile('exclude2.yaml');
213+
configFile2.writeAsStringSync('\n');
214+
final File configFile3 = packagesDir.childFile('exclude3.yaml');
215+
configFile3.writeAsStringSync('# - plugin1');
216+
217+
await runCapturingPrint(runner, <String>[
218+
'sample',
219+
'--packages=plugin1',
220+
'--exclude=${configFile1.path},${configFile2.path},${configFile3.path}',
221+
]);
222+
expect(command.plugins, unorderedEquals(<String>[plugin1.path]));
223+
});
224+
207225
test('filter-packages-to accepts config files', () async {
208226
final RepositoryPackage plugin1 =
209227
createFakePlugin('plugin1', packagesDir);

0 commit comments

Comments
 (0)