OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'package:args/args.dart' show ArgParser, ArgResults; | 5 import 'package:args/args.dart' show ArgParser, ArgResults; |
6 import 'package:analyzer/file_system/file_system.dart' | 6 import 'package:analyzer/file_system/file_system.dart' |
7 show ResourceProvider, ResourceUriResolver; | 7 show ResourceProvider, ResourceUriResolver; |
8 import 'package:analyzer/file_system/physical_file_system.dart' | 8 import 'package:analyzer/file_system/physical_file_system.dart' |
9 show PhysicalResourceProvider; | 9 show PhysicalResourceProvider; |
10 import 'package:analyzer/source/custom_resolver.dart'; | 10 import 'package:analyzer/source/custom_resolver.dart'; |
11 import 'package:analyzer/source/package_map_resolver.dart'; | 11 import 'package:analyzer/source/package_map_resolver.dart'; |
12 import 'package:analyzer/src/context/builder.dart'; | 12 import 'package:analyzer/src/context/builder.dart'; |
13 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; | 13 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; |
14 import 'package:analyzer/src/dart/sdk/sdk.dart' show FolderBasedDartSdk; | 14 import 'package:analyzer/src/dart/sdk/sdk.dart' show FolderBasedDartSdk; |
15 import 'package:analyzer/src/generated/engine.dart' | 15 import 'package:analyzer/src/generated/engine.dart' |
16 show AnalysisEngine, AnalysisOptionsImpl; | 16 show AnalysisEngine, AnalysisOptionsImpl; |
17 import 'package:analyzer/src/generated/source.dart' | 17 import 'package:analyzer/src/generated/source.dart' |
18 show DartUriResolver, SourceFactory, UriResolver; | 18 show DartUriResolver, SourceFactory, UriResolver; |
19 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 19 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
20 show InSummaryUriResolver, SummaryDataStore; | 20 show InSummaryUriResolver, SummaryDataStore; |
21 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; | 21 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; |
22 import 'package:cli_util/cli_util.dart' show getSdkDir; | 22 import 'package:cli_util/cli_util.dart' show getSdkDir; |
23 import 'package:path/path.dart' as path; | 23 import 'package:path/path.dart' as path; |
24 | 24 |
25 import 'multi_package_resolver.dart' show MultiPackageResolver; | |
26 | |
27 /// Options used to set up Source URI resolution in the analysis context. | 25 /// Options used to set up Source URI resolution in the analysis context. |
28 class AnalyzerOptions { | 26 class AnalyzerOptions { |
29 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" | 27 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" |
30 final Map<String, String> customUrlMappings; | 28 final Map<String, String> customUrlMappings; |
31 | 29 |
32 /// Package root when resolving 'package:' urls the standard way. | 30 /// Package root when resolving 'package:' urls the standard way. |
33 final String packageRoot; | 31 final String packageRoot; |
34 | 32 |
35 /// List of summary file paths. | 33 /// List of summary file paths. |
36 final List<String> summaryPaths; | 34 final List<String> summaryPaths; |
37 | 35 |
38 /// List of paths used for the multi-package resolver. | |
39 final List<String> packagePaths; | |
40 | |
41 /// Path to the dart-sdk. Null if `useMockSdk` is true or if the path couldn't | 36 /// Path to the dart-sdk. Null if `useMockSdk` is true or if the path couldn't |
42 /// be determined | 37 /// be determined |
43 final String dartSdkPath; | 38 final String dartSdkPath; |
44 | 39 |
45 /// Path to the dart-sdk summary. If this is set, it will be used in favor | 40 /// Path to the dart-sdk summary. If this is set, it will be used in favor |
46 /// of the unsummarized one. | 41 /// of the unsummarized one. |
47 final String dartSdkSummaryPath; | 42 final String dartSdkSummaryPath; |
48 | 43 |
49 /// Defined variables used by `bool.fromEnvironment` etc. | 44 /// Defined variables used by `bool.fromEnvironment` etc. |
50 final Map<String, String> declaredVariables; | 45 final Map<String, String> declaredVariables; |
51 | 46 |
52 AnalyzerOptions( | 47 AnalyzerOptions( |
53 {this.summaryPaths: const [], | 48 {this.summaryPaths: const [], |
54 String dartSdkPath, | 49 String dartSdkPath, |
55 this.dartSdkSummaryPath, | 50 this.dartSdkSummaryPath, |
56 this.customUrlMappings: const {}, | 51 this.customUrlMappings: const {}, |
57 this.packageRoot: null, | 52 this.packageRoot: null, |
58 this.packagePaths: const [], | |
59 this.declaredVariables: const {}}) | 53 this.declaredVariables: const {}}) |
60 : dartSdkPath = dartSdkPath ?? getSdkDir().path; | 54 : dartSdkPath = dartSdkPath ?? getSdkDir().path; |
61 | 55 |
62 factory AnalyzerOptions.fromArguments( | 56 factory AnalyzerOptions.fromArguments( |
63 ArgResults args, Map<String, String> declaredVariables) { | 57 ArgResults args, Map<String, String> declaredVariables) { |
64 var sdkPath = args['dart-sdk'] ?? getSdkDir().path; | 58 var sdkPath = args['dart-sdk'] ?? getSdkDir().path; |
65 var sdkSummaryPath = args['dart-sdk-summary']; | 59 var sdkSummaryPath = args['dart-sdk-summary']; |
66 | 60 |
67 if (sdkSummaryPath == null) { | 61 if (sdkSummaryPath == null) { |
68 sdkSummaryPath = path.join(sdkPath, 'lib', '_internal', 'ddc_sdk.sum'); | 62 sdkSummaryPath = path.join(sdkPath, 'lib', '_internal', 'ddc_sdk.sum'); |
69 } else if (sdkSummaryPath == 'build') { | 63 } else if (sdkSummaryPath == 'build') { |
70 // For building the SDK, we explicitly set the path to none. | 64 // For building the SDK, we explicitly set the path to none. |
71 sdkSummaryPath = null; | 65 sdkSummaryPath = null; |
72 } | 66 } |
73 | 67 |
74 return new AnalyzerOptions( | 68 return new AnalyzerOptions( |
75 summaryPaths: args['summary'] as List<String>, | 69 summaryPaths: args['summary'] as List<String>, |
76 dartSdkPath: sdkPath, | 70 dartSdkPath: sdkPath, |
77 dartSdkSummaryPath: sdkSummaryPath, | 71 dartSdkSummaryPath: sdkSummaryPath, |
78 customUrlMappings: _parseUrlMappings(args['url-mapping']), | 72 customUrlMappings: _parseUrlMappings(args['url-mapping']), |
79 packageRoot: args['package-root'], | 73 packageRoot: args['package-root'], |
80 packagePaths: (args['package-paths'] as String)?.split(',') ?? [], | |
81 declaredVariables: declaredVariables); | 74 declaredVariables: declaredVariables); |
82 } | 75 } |
83 | 76 |
84 /// Whether to resolve 'package:' uris using the multi-package resolver. | |
85 bool get useMultiPackage => packagePaths.isNotEmpty; | |
86 | |
87 static void addArguments(ArgParser parser) { | 77 static void addArguments(ArgParser parser) { |
88 parser | 78 parser |
89 ..addOption('summary', | 79 ..addOption('summary', |
90 abbr: 's', help: 'summary file(s) to include', allowMultiple: true) | 80 abbr: 's', help: 'summary file(s) to include', allowMultiple: true) |
91 ..addOption('dart-sdk', | 81 ..addOption('dart-sdk', |
92 help: 'Dart SDK Path', defaultsTo: null, hide: true) | 82 help: 'Dart SDK Path', defaultsTo: null, hide: true) |
93 ..addOption('dart-sdk-summary', | 83 ..addOption('dart-sdk-summary', |
94 help: 'Dart SDK Summary Path', defaultsTo: null, hide: true) | 84 help: 'Dart SDK Summary Path', defaultsTo: null, hide: true) |
95 ..addOption('package-root', | 85 ..addOption('package-root', |
96 abbr: 'p', help: 'Package root to resolve "package:" imports') | 86 abbr: 'p', help: 'Package root to resolve "package:" imports') |
97 ..addOption('url-mapping', | 87 ..addOption('url-mapping', |
98 help: '--url-mapping=libraryUri,/path/to/library.dart uses\n' | 88 help: '--url-mapping=libraryUri,/path/to/library.dart uses\n' |
99 'library.dart as the source for an import of of "libraryUri".', | 89 'library.dart as the source for an import of of "libraryUri".', |
100 allowMultiple: true, | 90 allowMultiple: true, |
101 splitCommas: false) | 91 splitCommas: false); |
102 ..addOption('package-paths', | |
103 help: 'use a list of directories to resolve "package:" imports'); | |
104 } | 92 } |
105 | 93 |
106 static Map<String, String> _parseUrlMappings(Iterable argument) { | 94 static Map<String, String> _parseUrlMappings(Iterable argument) { |
107 var mappings = <String, String>{}; | 95 var mappings = <String, String>{}; |
108 for (var mapping in argument) { | 96 for (var mapping in argument) { |
109 var splitMapping = mapping.split(','); | 97 var splitMapping = mapping.split(','); |
110 if (splitMapping.length >= 2) { | 98 if (splitMapping.length >= 2) { |
111 mappings[splitMapping[0]] = path.absolute(splitMapping[1]); | 99 mappings[splitMapping[0]] = path.absolute(splitMapping[1]); |
112 } | 100 } |
113 } | 101 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 ContextBuilderOptions builderOptions = new ContextBuilderOptions(); | 149 ContextBuilderOptions builderOptions = new ContextBuilderOptions(); |
162 if (options.packageRoot != null) { | 150 if (options.packageRoot != null) { |
163 builderOptions.defaultPackagesDirectoryPath = options.packageRoot; | 151 builderOptions.defaultPackagesDirectoryPath = options.packageRoot; |
164 } | 152 } |
165 ContextBuilder builder = new ContextBuilder(resourceProvider, null, null, | 153 ContextBuilder builder = new ContextBuilder(resourceProvider, null, null, |
166 options: builderOptions); | 154 options: builderOptions); |
167 return new PackageMapUriResolver(resourceProvider, | 155 return new PackageMapUriResolver(resourceProvider, |
168 builder.convertPackagesToMap(builder.createPackageMap(''))); | 156 builder.convertPackagesToMap(builder.createPackageMap(''))); |
169 } | 157 } |
170 | 158 |
171 return [ | 159 return [new ResourceUriResolver(resourceProvider), packageResolver()]; |
172 new ResourceUriResolver(resourceProvider), | |
173 options.useMultiPackage | |
174 ? new MultiPackageResolver(options.packagePaths) | |
175 : packageResolver() | |
176 ]; | |
177 } | 160 } |
178 | 161 |
179 FolderBasedDartSdk _createFolderBasedDartSdk(String sdkPath) { | 162 FolderBasedDartSdk _createFolderBasedDartSdk(String sdkPath) { |
180 var resourceProvider = PhysicalResourceProvider.INSTANCE; | 163 var resourceProvider = PhysicalResourceProvider.INSTANCE; |
181 var sdk = new FolderBasedDartSdk(resourceProvider, | 164 var sdk = new FolderBasedDartSdk(resourceProvider, |
182 resourceProvider.getFolder(sdkPath), /*useDart2jsPaths:*/ true); | 165 resourceProvider.getFolder(sdkPath), /*useDart2jsPaths:*/ true); |
183 sdk.useSummary = true; | 166 sdk.useSummary = true; |
184 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true; | 167 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true; |
185 return sdk; | 168 return sdk; |
186 } | 169 } |
(...skipping 20 matching lines...) Expand all Loading... |
207 // The format for defined variables is: | 190 // The format for defined variables is: |
208 // -D<name>=<value> | 191 // -D<name>=<value> |
209 var parts = arg.substring(2).split('='); | 192 var parts = arg.substring(2).split('='); |
210 declaredVars[parts[0]] = parts.length > 1 ? parts[1] : ''; | 193 declaredVars[parts[0]] = parts.length > 1 ? parts[1] : ''; |
211 } else { | 194 } else { |
212 remainingArgs.add(arg); | 195 remainingArgs.add(arg); |
213 } | 196 } |
214 } | 197 } |
215 return remainingArgs; | 198 return remainingArgs; |
216 } | 199 } |
OLD | NEW |