Chromium Code Reviews| 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/src/command_line/arguments.dart'; | 6 import 'package:analyzer/src/command_line/arguments.dart'; |
| 7 import 'package:analyzer/file_system/file_system.dart' | 7 import 'package:analyzer/file_system/file_system.dart' |
| 8 show ResourceProvider, ResourceUriResolver; | 8 show ResourceProvider, ResourceUriResolver; |
| 9 import 'package:analyzer/file_system/physical_file_system.dart' | 9 import 'package:analyzer/file_system/physical_file_system.dart' |
| 10 show PhysicalResourceProvider; | 10 show PhysicalResourceProvider; |
| 11 import 'package:analyzer/source/custom_resolver.dart'; | 11 import 'package:analyzer/source/custom_resolver.dart'; |
| 12 import 'package:analyzer/source/package_map_resolver.dart'; | 12 import 'package:analyzer/source/package_map_resolver.dart'; |
| 13 import 'package:analyzer/src/context/builder.dart'; | 13 import 'package:analyzer/src/context/builder.dart'; |
| 14 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; | 14 import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl; |
| 15 import 'package:analyzer/src/dart/sdk/sdk.dart' show FolderBasedDartSdk; | |
| 16 import 'package:analyzer/src/generated/engine.dart' | |
| 17 show AnalysisEngine, AnalysisOptionsImpl; | |
| 18 import 'package:analyzer/src/generated/source.dart' | 15 import 'package:analyzer/src/generated/source.dart' |
| 19 show DartUriResolver, SourceFactory, UriResolver; | 16 show DartUriResolver, SourceFactory, UriResolver; |
| 20 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 17 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
| 21 show InSummaryUriResolver, SummaryDataStore; | 18 show InSummaryUriResolver, SummaryDataStore; |
| 22 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; | |
| 23 import 'package:cli_util/cli_util.dart' show getSdkDir; | 19 import 'package:cli_util/cli_util.dart' show getSdkDir; |
| 24 import 'package:path/path.dart' as path; | 20 import 'package:path/path.dart' as path; |
| 25 | 21 |
| 26 /// Options used to set up Source URI resolution in the analysis context. | 22 /// Options used to set up Source URI resolution in the analysis context. |
| 27 class AnalyzerOptions { | 23 class AnalyzerOptions { |
| 28 final ContextBuilderOptions contextBuilderOptions; | 24 final ContextBuilderOptions contextBuilderOptions; |
| 29 | 25 |
| 30 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" | 26 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" |
| 31 final Map<String, String> customUrlMappings; | 27 final Map<String, String> customUrlMappings; |
| 32 | 28 |
| 33 /// Package root when resolving 'package:' urls the standard way. | 29 /// Package root when resolving 'package:' urls the standard way. |
| 34 String get packageRoot => contextBuilderOptions.defaultPackagesDirectoryPath; | 30 String get packageRoot => contextBuilderOptions.defaultPackagesDirectoryPath; |
| 35 | 31 |
| 36 /// List of summary file paths. | 32 /// List of summary file paths. |
| 37 final List<String> summaryPaths; | 33 final List<String> summaryPaths; |
| 38 | 34 |
| 39 /// Path to the dart-sdk. Null if `useMockSdk` is true or if the path couldn't | 35 /// Path to the dart-sdk, or `null` if the path couldn't be determined. |
| 40 /// be determined | |
| 41 final String dartSdkPath; | 36 final String dartSdkPath; |
| 42 | 37 |
| 43 /// Path to the dart-sdk summary. If this is set, it will be used in favor | 38 /// Path to the dart-sdk summary. If this is set, it will be used in favor |
| 44 /// of the unsummarized one. | 39 /// of the unsummarized one. |
| 45 String get dartSdkSummaryPath => contextBuilderOptions.dartSdkSummaryPath; | 40 String get dartSdkSummaryPath => contextBuilderOptions.dartSdkSummaryPath; |
| 46 | 41 |
| 47 /// Defined variables used by `bool.fromEnvironment` etc. | 42 /// Defined variables used by `bool.fromEnvironment` etc. |
| 48 Map<String, String> get declaredVariables => | 43 Map<String, String> get declaredVariables => |
| 49 contextBuilderOptions.declaredVariables; | 44 contextBuilderOptions.declaredVariables; |
| 50 | 45 |
| 51 AnalyzerOptions._( | 46 AnalyzerOptions._( |
| 52 {this.contextBuilderOptions, | 47 {this.contextBuilderOptions, |
| 53 this.summaryPaths: const [], | 48 List<String> summaryPaths, |
| 54 String dartSdkPath, | 49 String dartSdkPath, |
| 55 this.customUrlMappings: const {}}) | 50 this.customUrlMappings: const {}}) |
| 56 : dartSdkPath = dartSdkPath ?? getSdkDir().path { | 51 : dartSdkPath = dartSdkPath ?? getSdkDir().path, |
| 52 summaryPaths = summaryPaths ?? const [] { | |
| 57 contextBuilderOptions.declaredVariables ??= const {}; | 53 contextBuilderOptions.declaredVariables ??= const {}; |
| 58 } | 54 } |
| 59 | 55 |
| 60 factory AnalyzerOptions.basic( | 56 factory AnalyzerOptions.basic( |
| 61 {String dartSdkPath, | 57 {String dartSdkPath, |
| 62 String dartSdkSummaryPath, | 58 String dartSdkSummaryPath, |
| 63 List<String> summaryPaths}) { | 59 List<String> summaryPaths}) { |
| 64 var contextBuilderOptions = new ContextBuilderOptions(); | 60 var contextBuilderOptions = new ContextBuilderOptions() |
| 65 contextBuilderOptions.dartSdkSummaryPath = dartSdkSummaryPath; | 61 ..defaultOptions = new AnalysisOptionsImpl() |
| 62 ..dartSdkSummaryPath = dartSdkSummaryPath; | |
| 63 (contextBuilderOptions.defaultOptions as AnalysisOptionsImpl) | |
| 64 ..strongMode = true; | |
|
vsm
2017/01/03 17:56:28
Maybe
..defaultOptions = new AnalysisOptionsImpl(
danrubel
2017/01/03 18:51:48
Good point. Restructured to avoid cast.
| |
| 66 | 65 |
| 67 return new AnalyzerOptions._( | 66 return new AnalyzerOptions._( |
| 68 contextBuilderOptions: contextBuilderOptions, | 67 contextBuilderOptions: contextBuilderOptions, |
| 69 dartSdkPath: dartSdkPath, | 68 dartSdkPath: dartSdkPath, |
| 70 summaryPaths: summaryPaths); | 69 summaryPaths: summaryPaths); |
| 71 } | 70 } |
| 72 | 71 |
| 73 factory AnalyzerOptions.fromArguments(ArgResults args, | 72 factory AnalyzerOptions.fromArguments(ArgResults args, |
| 74 {String dartSdkSummaryPath, List<String> summaryPaths}) { | 73 {String dartSdkSummaryPath, List<String> summaryPaths}) { |
| 75 var contextBuilderOptions = createContextBuilderOptions(args); | 74 var contextBuilderOptions = createContextBuilderOptions(args); |
| 75 (contextBuilderOptions.defaultOptions as AnalysisOptionsImpl) | |
| 76 ..strongMode = true; | |
|
vsm
2017/01/03 17:56:28
Would be nice to avoid the cast to the Impl type h
danrubel
2017/01/03 18:51:48
Done.
| |
| 76 | 77 |
| 77 if (dartSdkSummaryPath != null) | 78 var dartSdkPath = args['dart-sdk'] ?? getSdkDir().path; |
| 78 contextBuilderOptions.dartSdkSummaryPath = dartSdkSummaryPath; | 79 |
| 79 contextBuilderOptions.dartSdkSummaryPath ??= | 80 dartSdkSummaryPath ??= contextBuilderOptions.dartSdkSummaryPath; |
| 80 path.join(args['dart-sdk'], 'lib', '_internal', 'ddc_sdk.sum'); | 81 dartSdkSummaryPath ??= |
| 81 if (contextBuilderOptions.dartSdkSummaryPath == 'build') { | 82 path.join(dartSdkPath, 'lib', '_internal', 'ddc_sdk.sum'); |
| 82 // For building the SDK, we explicitly set the path to none. | 83 // For building the SDK, we explicitly set the path to none. |
| 83 contextBuilderOptions.dartSdkSummaryPath = null; | 84 if (dartSdkSummaryPath == 'build') dartSdkSummaryPath = null; |
| 84 } | 85 contextBuilderOptions.dartSdkSummaryPath = dartSdkSummaryPath; |
| 85 | 86 |
| 86 return new AnalyzerOptions._( | 87 return new AnalyzerOptions._( |
| 87 contextBuilderOptions: contextBuilderOptions, | 88 contextBuilderOptions: contextBuilderOptions, |
| 88 summaryPaths: summaryPaths ?? args['summary'] as List<String>, | 89 summaryPaths: summaryPaths ?? args['summary'] as List<String>, |
| 89 dartSdkPath: args['dart-sdk'], | 90 dartSdkPath: dartSdkPath, |
| 90 customUrlMappings: _parseUrlMappings(args['url-mapping'])); | 91 customUrlMappings: _parseUrlMappings(args['url-mapping'])); |
| 91 } | 92 } |
| 92 | 93 |
| 93 static void addArguments(ArgParser parser, {bool hide: true}) { | 94 static void addArguments(ArgParser parser, {bool hide: true}) { |
| 94 parser | 95 parser |
| 95 ..addOption('summary', | 96 ..addOption('summary', |
| 96 abbr: 's', help: 'summary file(s) to include', allowMultiple: true) | 97 abbr: 's', help: 'summary file(s) to include', allowMultiple: true) |
| 97 ..addOption('url-mapping', | 98 ..addOption('url-mapping', |
| 98 help: '--url-mapping=libraryUri,/path/to/library.dart uses\n' | 99 help: '--url-mapping=libraryUri,/path/to/library.dart uses\n' |
| 99 'library.dart as the source for an import of of "libraryUri".', | 100 'library.dart as the source for an import of of "libraryUri".', |
| 100 allowMultiple: true, | 101 allowMultiple: true, |
| 101 splitCommas: false); | 102 splitCommas: false); |
| 102 } | 103 } |
| 103 | 104 |
| 104 static Map<String, String> _parseUrlMappings(Iterable argument) { | 105 static Map<String, String> _parseUrlMappings(Iterable argument) { |
| 105 var mappings = <String, String>{}; | 106 var mappings = <String, String>{}; |
| 106 for (var mapping in argument) { | 107 for (var mapping in argument) { |
| 107 var splitMapping = mapping.split(','); | 108 var splitMapping = mapping.split(','); |
| 108 if (splitMapping.length >= 2) { | 109 if (splitMapping.length >= 2) { |
| 109 mappings[splitMapping[0]] = path.absolute(splitMapping[1]); | 110 mappings[splitMapping[0]] = path.absolute(splitMapping[1]); |
| 110 } | 111 } |
| 111 } | 112 } |
| 112 return mappings; | 113 return mappings; |
| 113 } | 114 } |
| 114 } | 115 } |
| 115 | 116 |
| 116 /// Creates an analysis context that contains our restricted typing rules. | |
| 117 AnalysisContextImpl createAnalysisContext() { | |
| 118 var res = AnalysisEngine.instance.createAnalysisContext(); | |
| 119 res.analysisOptions = new AnalysisOptionsImpl() | |
| 120 ..strongMode = true | |
| 121 ..trackCacheDependencies = false; | |
| 122 return res; | |
| 123 } | |
| 124 | |
| 125 /// Creates a SourceFactory configured by the [options]. | 117 /// Creates a SourceFactory configured by the [options]. |
| 126 /// | 118 /// |
| 127 /// Use [options.useMockSdk] to specify the SDK mode, or use [sdkResolver] | |
| 128 /// to entirely override the DartUriResolver. | |
| 129 /// | |
| 130 /// If supplied, [fileResolvers] will override the default `file:` and | 119 /// If supplied, [fileResolvers] will override the default `file:` and |
| 131 /// `package:` URI resolvers. | 120 /// `package:` URI resolvers. |
| 132 SourceFactory createSourceFactory(AnalyzerOptions options, | 121 SourceFactory createSourceFactory(AnalyzerOptions options, |
| 133 {DartUriResolver sdkResolver, | 122 {DartUriResolver sdkResolver, |
| 134 List<UriResolver> fileResolvers, | 123 List<UriResolver> fileResolvers, |
| 135 SummaryDataStore summaryData, | 124 SummaryDataStore summaryData, |
| 136 ResourceProvider resourceProvider}) { | 125 ResourceProvider resourceProvider}) { |
| 137 resourceProvider ??= PhysicalResourceProvider.INSTANCE; | 126 resourceProvider ??= PhysicalResourceProvider.INSTANCE; |
| 138 var resolvers = <UriResolver>[]; | 127 var resolvers = <UriResolver>[]; |
| 139 if (options.customUrlMappings.isNotEmpty) { | 128 if (options.customUrlMappings.isNotEmpty) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 161 builderOptions.defaultPackagesDirectoryPath = options.packageRoot; | 150 builderOptions.defaultPackagesDirectoryPath = options.packageRoot; |
| 162 } | 151 } |
| 163 ContextBuilder builder = new ContextBuilder(resourceProvider, null, null, | 152 ContextBuilder builder = new ContextBuilder(resourceProvider, null, null, |
| 164 options: builderOptions); | 153 options: builderOptions); |
| 165 return new PackageMapUriResolver(resourceProvider, | 154 return new PackageMapUriResolver(resourceProvider, |
| 166 builder.convertPackagesToMap(builder.createPackageMap(''))); | 155 builder.convertPackagesToMap(builder.createPackageMap(''))); |
| 167 } | 156 } |
| 168 | 157 |
| 169 return [new ResourceUriResolver(resourceProvider), packageResolver()]; | 158 return [new ResourceUriResolver(resourceProvider), packageResolver()]; |
| 170 } | 159 } |
| 171 | |
| 172 FolderBasedDartSdk _createFolderBasedDartSdk(String sdkPath) { | |
| 173 var resourceProvider = PhysicalResourceProvider.INSTANCE; | |
| 174 var sdk = new FolderBasedDartSdk(resourceProvider, | |
| 175 resourceProvider.getFolder(sdkPath), /*useDart2jsPaths:*/ true); | |
| 176 sdk.useSummary = true; | |
| 177 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true; | |
| 178 return sdk; | |
| 179 } | |
| 180 | |
| 181 /// Creates a [DartUriResolver] that uses the SDK at the given [sdkPath]. | |
| 182 DartUriResolver createSdkPathResolver(String sdkSummaryPath, String sdkPath) { | |
| 183 var sdk = (sdkSummaryPath != null) | |
| 184 ? new SummaryBasedDartSdk(sdkSummaryPath, true) | |
| 185 : _createFolderBasedDartSdk(sdkPath); | |
| 186 return new DartUriResolver(sdk); | |
| 187 } | |
| OLD | NEW |