|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | +import 'package:yaml/yaml.dart'; |
| 5 | + |
| 6 | +import 'file_system.dart'; |
| 7 | +import 'merge_options.dart'; |
| 8 | + |
| 9 | +/// The analysis options configuration is a dynamically-typed JSON-like data |
| 10 | +/// structure. |
| 11 | +/// |
| 12 | +/// (It's JSON-*like* and not JSON because maps in it may have non-string keys.) |
| 13 | +typedef AnalysisOptions = Map<Object?, Object?>; |
| 14 | + |
| 15 | +/// Interface for taking a "package:" URI that may appear in an analysis |
| 16 | +/// options file's "include" key and resolving it to a file path which can be |
| 17 | +/// passed to [FileSystem.join()]. |
| 18 | +typedef ResolvePackageUri = Future<String?> Function(Uri packageUri); |
| 19 | + |
| 20 | +/// Reads an `analysis_options.yaml` file in [directory] or in the nearest |
| 21 | +/// surrounding folder that contains that file using [fileSystem]. |
| 22 | +/// |
| 23 | +/// Stops walking parent directories as soon as it finds one that contains an |
| 24 | +/// `analysis_options.yaml` file. If it reaches the root directory without |
| 25 | +/// finding one, returns an empty [YamlMap]. |
| 26 | +/// |
| 27 | +/// If an `analysis_options.yaml` file is found, reads it and parses it to a |
| 28 | +/// [YamlMap]. If the map contains an `include` key whose value is a list, then |
| 29 | +/// reads any of the other referenced YAML files and merges them into this one. |
| 30 | +/// Returns the resulting map with the `include` key removed. |
| 31 | +/// |
| 32 | +/// If there any "package:" includes, then they are resolved to file paths |
| 33 | +/// using [resolvePackageUri]. If [resolvePackageUri] is omitted, an exception |
| 34 | +/// is thrown if any "package:" includes are found. |
| 35 | +Future<AnalysisOptions> findAnalysisOptions( |
| 36 | + FileSystem fileSystem, FileSystemPath directory, |
| 37 | + {ResolvePackageUri? resolvePackageUri}) async { |
| 38 | + while (true) { |
| 39 | + var optionsPath = await fileSystem.join(directory, 'analysis_options.yaml'); |
| 40 | + if (await fileSystem.fileExists(optionsPath)) { |
| 41 | + return readAnalysisOptions(fileSystem, optionsPath, |
| 42 | + resolvePackageUri: resolvePackageUri); |
| 43 | + } |
| 44 | + |
| 45 | + var parent = await fileSystem.parentDirectory(directory); |
| 46 | + if (parent == null) break; |
| 47 | + directory = parent; |
| 48 | + } |
| 49 | + |
| 50 | + // If we get here, we didn't find an analysis_options.yaml. |
| 51 | + return const {}; |
| 52 | +} |
| 53 | + |
| 54 | +/// Uses [fileSystem] to read the analysis options file at [optionsPath]. |
| 55 | +/// |
| 56 | +/// If there any "package:" includes, then they are resolved to file paths |
| 57 | +/// using [resolvePackageUri]. If [resolvePackageUri] is omitted, an exception |
| 58 | +/// is thrown if any "package:" includes are found. |
| 59 | +Future<AnalysisOptions> readAnalysisOptions( |
| 60 | + FileSystem fileSystem, FileSystemPath optionsPath, |
| 61 | + {ResolvePackageUri? resolvePackageUri}) async { |
| 62 | + var yaml = loadYamlNode(await fileSystem.readFile(optionsPath)); |
| 63 | + |
| 64 | + // If for some reason the YAML isn't a map, consider it malformed and yield |
| 65 | + // a default empty map. |
| 66 | + if (yaml is! YamlMap) return const {}; |
| 67 | + |
| 68 | + // Lower the YAML to a regular map. |
| 69 | + var options = {...yaml}; |
| 70 | + |
| 71 | + // If there is an `include:` key, then load that and merge it with these |
| 72 | + // options. |
| 73 | + if (options['include'] case String include) { |
| 74 | + options.remove('include'); |
| 75 | + |
| 76 | + // If the include path is "package:", resolve it to a file path first. |
| 77 | + var includeUri = Uri.tryParse(include); |
| 78 | + if (includeUri != null && includeUri.scheme == 'package') { |
| 79 | + if (resolvePackageUri != null) { |
| 80 | + var filePath = await resolvePackageUri(includeUri); |
| 81 | + if (filePath != null) { |
| 82 | + include = filePath; |
| 83 | + } else { |
| 84 | + throw PackageResolutionException( |
| 85 | + 'Failed to resolve package URI "$include" in include.'); |
| 86 | + } |
| 87 | + } else { |
| 88 | + throw PackageResolutionException( |
| 89 | + 'Couldn\'t resolve package URI "$include" in include because ' |
| 90 | + 'no package resolver was provided.'); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // The include path may be relative to the directory containing the current |
| 95 | + // options file. |
| 96 | + var includePath = await fileSystem.join( |
| 97 | + (await fileSystem.parentDirectory(optionsPath))!, include); |
| 98 | + var includeFile = await readAnalysisOptions(fileSystem, includePath, |
| 99 | + resolvePackageUri: resolvePackageUri); |
| 100 | + options = merge(includeFile, options) as AnalysisOptions; |
| 101 | + } |
| 102 | + |
| 103 | + return options; |
| 104 | +} |
| 105 | + |
| 106 | +/// Exception thrown when an analysis options file contains a "package:" URI in |
| 107 | +/// an include and resolving the URI to a file path failed. |
| 108 | +final class PackageResolutionException implements Exception { |
| 109 | + final String _message; |
| 110 | + |
| 111 | + PackageResolutionException(this._message); |
| 112 | + |
| 113 | + @override |
| 114 | + String toString() => _message; |
| 115 | +} |
0 commit comments