Chromium Code Reviews

Side by Side Diff: pkg/dev_compiler/lib/src/analyzer/context.dart

Issue 2598593003: support --options flag and other analysis options flags in DDC (Closed)
Patch Set: merge Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
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()..strongMode = true)
62 ..dartSdkSummaryPath = dartSdkSummaryPath;
66 63
67 return new AnalyzerOptions._( 64 return new AnalyzerOptions._(
68 contextBuilderOptions: contextBuilderOptions, 65 contextBuilderOptions: contextBuilderOptions,
69 dartSdkPath: dartSdkPath, 66 dartSdkPath: dartSdkPath,
70 summaryPaths: summaryPaths); 67 summaryPaths: summaryPaths);
71 } 68 }
72 69
73 factory AnalyzerOptions.fromArguments(ArgResults args, 70 factory AnalyzerOptions.fromArguments(ArgResults args,
74 {String dartSdkSummaryPath, List<String> summaryPaths}) { 71 {String dartSdkSummaryPath, List<String> summaryPaths}) {
75 var contextBuilderOptions = createContextBuilderOptions(args); 72 var contextBuilderOptions = createContextBuilderOptions(args,
73 strongMode: true, trackCacheDependencies: false);
76 74
77 if (dartSdkSummaryPath != null) 75 var dartSdkPath = args['dart-sdk'] ?? getSdkDir().path;
78 contextBuilderOptions.dartSdkSummaryPath = dartSdkSummaryPath; 76
79 contextBuilderOptions.dartSdkSummaryPath ??= 77 dartSdkSummaryPath ??= contextBuilderOptions.dartSdkSummaryPath;
80 path.join(args['dart-sdk'], 'lib', '_internal', 'ddc_sdk.sum'); 78 dartSdkSummaryPath ??=
81 if (contextBuilderOptions.dartSdkSummaryPath == 'build') { 79 path.join(dartSdkPath, 'lib', '_internal', 'ddc_sdk.sum');
82 // For building the SDK, we explicitly set the path to none. 80 // For building the SDK, we explicitly set the path to none.
83 contextBuilderOptions.dartSdkSummaryPath = null; 81 if (dartSdkSummaryPath == 'build') dartSdkSummaryPath = null;
84 } 82 contextBuilderOptions.dartSdkSummaryPath = dartSdkSummaryPath;
85 83
86 return new AnalyzerOptions._( 84 return new AnalyzerOptions._(
87 contextBuilderOptions: contextBuilderOptions, 85 contextBuilderOptions: contextBuilderOptions,
88 summaryPaths: summaryPaths ?? args['summary'] as List<String>, 86 summaryPaths: summaryPaths ?? args['summary'] as List<String>,
89 dartSdkPath: args['dart-sdk'], 87 dartSdkPath: dartSdkPath,
90 customUrlMappings: _parseUrlMappings(args['url-mapping'])); 88 customUrlMappings: _parseUrlMappings(args['url-mapping']));
91 } 89 }
92 90
93 static void addArguments(ArgParser parser, {bool hide: true}) { 91 static void addArguments(ArgParser parser, {bool hide: true}) {
94 parser 92 parser
95 ..addOption('summary', 93 ..addOption('summary',
96 abbr: 's', help: 'summary file(s) to include', allowMultiple: true) 94 abbr: 's', help: 'summary file(s) to include', allowMultiple: true)
97 ..addOption('url-mapping', 95 ..addOption('url-mapping',
98 help: '--url-mapping=libraryUri,/path/to/library.dart uses\n' 96 help: '--url-mapping=libraryUri,/path/to/library.dart uses\n'
99 'library.dart as the source for an import of of "libraryUri".', 97 'library.dart as the source for an import of of "libraryUri".',
100 allowMultiple: true, 98 allowMultiple: true,
101 splitCommas: false); 99 splitCommas: false);
102 } 100 }
103 101
104 static Map<String, String> _parseUrlMappings(Iterable argument) { 102 static Map<String, String> _parseUrlMappings(Iterable argument) {
105 var mappings = <String, String>{}; 103 var mappings = <String, String>{};
106 for (var mapping in argument) { 104 for (var mapping in argument) {
107 var splitMapping = mapping.split(','); 105 var splitMapping = mapping.split(',');
108 if (splitMapping.length >= 2) { 106 if (splitMapping.length >= 2) {
109 mappings[splitMapping[0]] = path.absolute(splitMapping[1]); 107 mappings[splitMapping[0]] = path.absolute(splitMapping[1]);
110 } 108 }
111 } 109 }
112 return mappings; 110 return mappings;
113 } 111 }
114 } 112 }
115 113
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]. 114 /// Creates a SourceFactory configured by the [options].
126 /// 115 ///
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 116 /// If supplied, [fileResolvers] will override the default `file:` and
131 /// `package:` URI resolvers. 117 /// `package:` URI resolvers.
132 SourceFactory createSourceFactory(AnalyzerOptions options, 118 SourceFactory createSourceFactory(AnalyzerOptions options,
133 {DartUriResolver sdkResolver, 119 {DartUriResolver sdkResolver,
134 List<UriResolver> fileResolvers, 120 List<UriResolver> fileResolvers,
135 SummaryDataStore summaryData, 121 SummaryDataStore summaryData,
136 ResourceProvider resourceProvider}) { 122 ResourceProvider resourceProvider}) {
137 resourceProvider ??= PhysicalResourceProvider.INSTANCE; 123 resourceProvider ??= PhysicalResourceProvider.INSTANCE;
138 var resolvers = <UriResolver>[]; 124 var resolvers = <UriResolver>[];
139 if (options.customUrlMappings.isNotEmpty) { 125 if (options.customUrlMappings.isNotEmpty) {
(...skipping 21 matching lines...)
161 builderOptions.defaultPackagesDirectoryPath = options.packageRoot; 147 builderOptions.defaultPackagesDirectoryPath = options.packageRoot;
162 } 148 }
163 ContextBuilder builder = new ContextBuilder(resourceProvider, null, null, 149 ContextBuilder builder = new ContextBuilder(resourceProvider, null, null,
164 options: builderOptions); 150 options: builderOptions);
165 return new PackageMapUriResolver(resourceProvider, 151 return new PackageMapUriResolver(resourceProvider,
166 builder.convertPackagesToMap(builder.createPackageMap(''))); 152 builder.convertPackagesToMap(builder.createPackageMap('')));
167 } 153 }
168 154
169 return [new ResourceUriResolver(resourceProvider), packageResolver()]; 155 return [new ResourceUriResolver(resourceProvider), packageResolver()];
170 } 156 }
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 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/command_line/arguments.dart ('k') | pkg/dev_compiler/lib/src/compiler/command.dart » ('j') | no next file with comments »

Powered by Google App Engine