|
| 1 | +// Copyright 2013 The Flutter Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:file/file.dart'; |
| 6 | +import 'package:meta/meta.dart'; |
| 7 | +import 'package:yaml/yaml.dart'; |
| 8 | +import 'core.dart'; |
| 9 | +import 'output_utils.dart'; |
| 10 | + |
| 11 | +/// The name of the tool configuration file, at the root of the repository. |
| 12 | +const String configFilename = '.repo_tool_config.yaml'; |
| 13 | + |
| 14 | +YamlMap? _toolConfig; |
| 15 | + |
| 16 | +/// Clears the cached tool configuration. |
| 17 | +/// |
| 18 | +/// Visible for testing only. |
| 19 | +@visibleForTesting |
| 20 | +void clearToolConfigCache() { |
| 21 | + _toolConfig = null; |
| 22 | +} |
| 23 | + |
| 24 | +YamlMap _getToolConfig(Directory repoRoot) { |
| 25 | + if (_toolConfig == null) { |
| 26 | + final File configFile = repoRoot.childFile(configFilename); |
| 27 | + if (!configFile.existsSync()) { |
| 28 | + printError( |
| 29 | + 'Configuration file $configFilename not found at repository root.', |
| 30 | + ); |
| 31 | + throw ToolExit(exitInvalidArguments); |
| 32 | + } |
| 33 | + final Object yaml = loadYamlNode(configFile.readAsStringSync()); |
| 34 | + if (yaml is! YamlMap) { |
| 35 | + printError('Configuration file $configFilename is must be a map.'); |
| 36 | + throw ToolExit(exitInvalidArguments); |
| 37 | + } |
| 38 | + _toolConfig = yaml; |
| 39 | + } |
| 40 | + return _toolConfig!; |
| 41 | +} |
| 42 | + |
| 43 | +/// Returns the name of the repository. |
| 44 | +String getRepositoryName(Directory repoRoot) { |
| 45 | + final name = _getToolConfig(repoRoot)['repo_name'] as String?; |
| 46 | + if (name == null) { |
| 47 | + printError('repo_name is missing in $configFilename'); |
| 48 | + throw ToolExit(exitInvalidArguments); |
| 49 | + } |
| 50 | + return name; |
| 51 | +} |
| 52 | + |
| 53 | +/// Returns the minimum Flutter version allowed. |
| 54 | +String? getMinFlutterVersion(Directory repoRoot) { |
| 55 | + final Object? yaml = _getToolConfig(repoRoot)['min_flutter']; |
| 56 | + if (yaml == null) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + if (yaml is! String) { |
| 60 | + printError('min_flutter must be a full version string (e.g., "3.44.0").'); |
| 61 | + throw ToolExit(exitInvalidArguments); |
| 62 | + } |
| 63 | + return yaml; |
| 64 | +} |
| 65 | + |
| 66 | +/// Returns the allowed dependencies, grouped by 'pinned' and 'unpinned'. |
| 67 | +({List<String> pinned, List<String> unpinned}) getAllowedDependencies( |
| 68 | + Directory repoRoot, |
| 69 | +) { |
| 70 | + final allowedDeps = |
| 71 | + _getToolConfig(repoRoot)['allowed_dependencies'] as YamlMap?; |
| 72 | + if (allowedDeps == null) { |
| 73 | + return (pinned: <String>[], unpinned: <String>[]); |
| 74 | + } |
| 75 | + |
| 76 | + final List<String> pinned = |
| 77 | + (allowedDeps['pinned'] as YamlList?)?.map((e) => e as String).toList() ?? |
| 78 | + <String>[]; |
| 79 | + final List<String> unpinned = |
| 80 | + (allowedDeps['unpinned'] as YamlList?) |
| 81 | + ?.map((e) => e as String) |
| 82 | + .toList() ?? |
| 83 | + <String>[]; |
| 84 | + |
| 85 | + return (pinned: pinned, unpinned: unpinned); |
| 86 | +} |
| 87 | + |
| 88 | +/// Returns a map from package names to non-standard issue labels used for those |
| 89 | +/// packages. Packages that use the default label `p: <package_name>` are not |
| 90 | +/// included in the returned map. |
| 91 | +Map<String, String> getNonStandardPackageLabels(Directory repoRoot) { |
| 92 | + final customLabels = _getToolConfig(repoRoot)['package_labels'] as YamlMap?; |
| 93 | + if (customLabels == null) { |
| 94 | + return <String, String>{}; |
| 95 | + } |
| 96 | + |
| 97 | + return customLabels.map( |
| 98 | + (key, value) => MapEntry(key as String, value as String), |
| 99 | + ); |
| 100 | +} |
0 commit comments