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 library analyzer_cli.src.build_mode; | 5 library analyzer_cli.src.build_mode; |
6 | 6 |
7 import 'dart:core' hide Resource; | 7 import 'dart:core' hide Resource; |
8 import 'dart:io' as io; | 8 import 'dart:io' as io; |
9 | 9 |
10 import 'package:analyzer/dart/ast/ast.dart' show CompilationUnit; | 10 import 'package:analyzer/dart/ast/ast.dart' show CompilationUnit; |
11 import 'package:analyzer/dart/element/element.dart'; | 11 import 'package:analyzer/dart/element/element.dart'; |
12 import 'package:analyzer/file_system/file_system.dart'; | 12 import 'package:analyzer/file_system/file_system.dart'; |
13 import 'package:analyzer/src/dart/sdk/sdk.dart'; | 13 import 'package:analyzer/src/dart/sdk/sdk.dart'; |
14 import 'package:analyzer/src/generated/engine.dart'; | 14 import 'package:analyzer/src/generated/engine.dart'; |
15 import 'package:analyzer/src/generated/error.dart'; | 15 import 'package:analyzer/src/generated/error.dart'; |
16 import 'package:analyzer/src/generated/java_io.dart'; | |
17 import 'package:analyzer/src/generated/sdk.dart'; | 16 import 'package:analyzer/src/generated/sdk.dart'; |
18 import 'package:analyzer/src/generated/source.dart'; | 17 import 'package:analyzer/src/generated/source.dart'; |
19 import 'package:analyzer/src/generated/source_io.dart'; | 18 import 'package:analyzer/src/generated/source_io.dart'; |
| 19 import 'package:analyzer/src/source/source_resource.dart'; |
20 import 'package:analyzer/src/summary/format.dart'; | 20 import 'package:analyzer/src/summary/format.dart'; |
21 import 'package:analyzer/src/summary/idl.dart'; | 21 import 'package:analyzer/src/summary/idl.dart'; |
22 import 'package:analyzer/src/summary/link.dart'; | 22 import 'package:analyzer/src/summary/link.dart'; |
23 import 'package:analyzer/src/summary/package_bundle_reader.dart'; | 23 import 'package:analyzer/src/summary/package_bundle_reader.dart'; |
24 import 'package:analyzer/src/summary/summarize_ast.dart'; | 24 import 'package:analyzer/src/summary/summarize_ast.dart'; |
25 import 'package:analyzer/src/summary/summarize_elements.dart'; | 25 import 'package:analyzer/src/summary/summarize_elements.dart'; |
26 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; | 26 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; |
27 import 'package:analyzer/task/dart.dart'; | 27 import 'package:analyzer/task/dart.dart'; |
28 import 'package:analyzer_cli/src/analyzer_impl.dart'; | 28 import 'package:analyzer_cli/src/analyzer_impl.dart'; |
29 import 'package:analyzer_cli/src/driver.dart'; | 29 import 'package:analyzer_cli/src/driver.dart'; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 /** | 124 /** |
125 * Analyzer used when the "--build-mode" option is supplied. | 125 * Analyzer used when the "--build-mode" option is supplied. |
126 */ | 126 */ |
127 class BuildMode { | 127 class BuildMode { |
128 final ResourceProvider resourceProvider; | 128 final ResourceProvider resourceProvider; |
129 final CommandLineOptions options; | 129 final CommandLineOptions options; |
130 final AnalysisStats stats; | 130 final AnalysisStats stats; |
131 | 131 |
132 SummaryDataStore summaryDataStore; | 132 SummaryDataStore summaryDataStore; |
133 InternalAnalysisContext context; | 133 InternalAnalysisContext context; |
134 Map<Uri, JavaFile> uriToFileMap; | 134 Map<Uri, File> uriToFileMap; |
135 final List<Source> explicitSources = <Source>[]; | 135 final List<Source> explicitSources = <Source>[]; |
136 | 136 |
137 PackageBundleAssembler assembler; | 137 PackageBundleAssembler assembler; |
138 final Set<Source> processedSources = new Set<Source>(); | 138 final Set<Source> processedSources = new Set<Source>(); |
139 final Map<Uri, UnlinkedUnit> uriToUnit = <Uri, UnlinkedUnit>{}; | 139 final Map<Uri, UnlinkedUnit> uriToUnit = <Uri, UnlinkedUnit>{}; |
140 PackageBundle sdkBundle; | 140 PackageBundle sdkBundle; |
141 | 141 |
142 BuildMode(this.resourceProvider, this.options, this.stats); | 142 BuildMode(this.resourceProvider, this.options, this.stats); |
143 | 143 |
144 bool get _shouldOutputSummary => | 144 bool get _shouldOutputSummary => |
(...skipping 15 matching lines...) Expand all Loading... |
160 io.exitCode = ErrorSeverity.ERROR.ordinal; | 160 io.exitCode = ErrorSeverity.ERROR.ordinal; |
161 return ErrorSeverity.ERROR; | 161 return ErrorSeverity.ERROR; |
162 } | 162 } |
163 | 163 |
164 // Prepare the analysis context. | 164 // Prepare the analysis context. |
165 _createContext(); | 165 _createContext(); |
166 | 166 |
167 // Add sources. | 167 // Add sources. |
168 ChangeSet changeSet = new ChangeSet(); | 168 ChangeSet changeSet = new ChangeSet(); |
169 for (Uri uri in uriToFileMap.keys) { | 169 for (Uri uri in uriToFileMap.keys) { |
170 JavaFile file = uriToFileMap[uri]; | 170 File file = uriToFileMap[uri]; |
171 if (!file.exists()) { | 171 if (!file.exists) { |
172 errorSink.writeln('File not found: ${file.getPath()}'); | 172 errorSink.writeln('File not found: ${file.path}'); |
173 io.exitCode = ErrorSeverity.ERROR.ordinal; | 173 io.exitCode = ErrorSeverity.ERROR.ordinal; |
174 return ErrorSeverity.ERROR; | 174 return ErrorSeverity.ERROR; |
175 } | 175 } |
176 Source source = new FileBasedSource(file, uri); | 176 Source source = new FileSource(file, uri); |
177 explicitSources.add(source); | 177 explicitSources.add(source); |
178 changeSet.addedSource(source); | 178 changeSet.addedSource(source); |
179 } | 179 } |
180 context.applyChanges(changeSet); | 180 context.applyChanges(changeSet); |
181 | 181 |
182 if (!options.buildSummaryOnly) { | 182 if (!options.buildSummaryOnly) { |
183 // Perform full analysis. | 183 // Perform full analysis. |
184 while (true) { | 184 while (true) { |
185 AnalysisResult analysisResult = context.performAnalysisTask(); | 185 AnalysisResult analysisResult = context.performAnalysisTask(); |
186 if (!analysisResult.hasMoreWork) { | 186 if (!analysisResult.hasMoreWork) { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 | 284 |
285 // In AST mode include SDK bundle to avoid parsing SDK sources. | 285 // In AST mode include SDK bundle to avoid parsing SDK sources. |
286 if (options.buildSummaryOnlyAst) { | 286 if (options.buildSummaryOnlyAst) { |
287 summaryDataStore.addBundle(null, sdkBundle); | 287 summaryDataStore.addBundle(null, sdkBundle); |
288 } | 288 } |
289 | 289 |
290 // Create the context. | 290 // Create the context. |
291 context = AnalysisEngine.instance.createAnalysisContext(); | 291 context = AnalysisEngine.instance.createAnalysisContext(); |
292 context.sourceFactory = new SourceFactory(<UriResolver>[ | 292 context.sourceFactory = new SourceFactory(<UriResolver>[ |
293 new DartUriResolver(sdk), | 293 new DartUriResolver(sdk), |
294 new InSummaryPackageUriResolver(summaryDataStore), | 294 new InSummaryUriResolver(resourceProvider, summaryDataStore), |
295 new ExplicitSourceResolver(uriToFileMap) | 295 new ExplicitSourceResolver(uriToFileMap) |
296 ]); | 296 ]); |
297 | 297 |
298 // Set context options. | 298 // Set context options. |
299 Driver.setAnalysisContextOptions(resourceProvider, context, options, | 299 Driver.setAnalysisContextOptions(resourceProvider, context, options, |
300 (AnalysisOptionsImpl contextOptions) { | 300 (AnalysisOptionsImpl contextOptions) { |
301 if (options.buildSummaryOnlyDiet) { | 301 if (options.buildSummaryOnlyDiet) { |
302 contextOptions.analyzeFunctionBodies = false; | 302 contextOptions.analyzeFunctionBodies = false; |
303 } | 303 } |
304 }); | 304 }); |
305 | 305 |
306 if (!options.buildSummaryOnlyAst) { | 306 if (!options.buildSummaryOnlyAst) { |
307 // Configure using summaries. | 307 // Configure using summaries. |
308 context.typeProvider = sdk.context.typeProvider; | 308 context.typeProvider = sdk.context.typeProvider; |
309 context.resultProvider = | 309 context.resultProvider = |
310 new InputPackagesResultProvider(context, summaryDataStore); | 310 new InputPackagesResultProvider(context, summaryDataStore); |
311 } | 311 } |
312 } | 312 } |
313 | 313 |
314 /** | 314 /** |
| 315 * Convert [sourceEntities] (a list of file specifications of the form |
| 316 * "$uri|$path") to a map from URI to path. If an error occurs, report the |
| 317 * error and return null. |
| 318 */ |
| 319 Map<Uri, File> _createUriToFileMap(List<String> sourceEntities) { |
| 320 Map<Uri, File> uriToFileMap = <Uri, File>{}; |
| 321 for (String sourceFile in sourceEntities) { |
| 322 int pipeIndex = sourceFile.indexOf('|'); |
| 323 if (pipeIndex == -1) { |
| 324 // TODO(paulberry): add the ability to guess the URI from the path. |
| 325 errorSink.writeln( |
| 326 'Illegal input file (must be "\$uri|\$path"): $sourceFile'); |
| 327 return null; |
| 328 } |
| 329 Uri uri = Uri.parse(sourceFile.substring(0, pipeIndex)); |
| 330 String path = sourceFile.substring(pipeIndex + 1); |
| 331 uriToFileMap[uri] = resourceProvider.getFile(path); |
| 332 } |
| 333 return uriToFileMap; |
| 334 } |
| 335 |
| 336 /** |
315 * Print errors for all explicit sources. If [outputPath] is supplied, output | 337 * Print errors for all explicit sources. If [outputPath] is supplied, output |
316 * is sent to a new file at that path. | 338 * is sent to a new file at that path. |
317 */ | 339 */ |
318 void _printErrors({String outputPath}) { | 340 void _printErrors({String outputPath}) { |
319 StringBuffer buffer = new StringBuffer(); | 341 StringBuffer buffer = new StringBuffer(); |
320 ErrorFormatter formatter = new ErrorFormatter( | 342 ErrorFormatter formatter = new ErrorFormatter( |
321 buffer, | 343 buffer, |
322 options, | 344 options, |
323 stats, | 345 stats, |
324 (AnalysisError error) => | 346 (AnalysisError error) => |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 UnlinkedUnitBuilder unlinkedUnit = serializeAstUnlinked(unit); | 392 UnlinkedUnitBuilder unlinkedUnit = serializeAstUnlinked(unit); |
371 assembler.addUnlinkedUnit(source, unlinkedUnit); | 393 assembler.addUnlinkedUnit(source, unlinkedUnit); |
372 return unlinkedUnit; | 394 return unlinkedUnit; |
373 }); | 395 }); |
374 } | 396 } |
375 | 397 |
376 Map<String, LinkedLibraryBuilder> linkResult = | 398 Map<String, LinkedLibraryBuilder> linkResult = |
377 link(sourceUris, _getDependency, _getUnit, options.strongMode); | 399 link(sourceUris, _getDependency, _getUnit, options.strongMode); |
378 linkResult.forEach(assembler.addLinkedLibrary); | 400 linkResult.forEach(assembler.addLinkedLibrary); |
379 } | 401 } |
| 402 } |
| 403 |
| 404 /** |
| 405 * Instances of the class [ExplicitSourceResolver] map URIs to files on disk |
| 406 * using a fixed mapping provided at construction time. |
| 407 */ |
| 408 class ExplicitSourceResolver extends UriResolver { |
| 409 final Map<Uri, File> uriToFileMap; |
| 410 final Map<String, Uri> pathToUriMap; |
380 | 411 |
381 /** | 412 /** |
382 * Convert [sourceEntities] (a list of file specifications of the form | 413 * Construct an [ExplicitSourceResolver] based on the given [uriToFileMap]. |
383 * "$uri|$path") to a map from URI to path. If an error occurs, report the | |
384 * error and return null. | |
385 */ | 414 */ |
386 static Map<Uri, JavaFile> _createUriToFileMap(List<String> sourceEntities) { | 415 ExplicitSourceResolver(Map<Uri, File> uriToFileMap) |
387 Map<Uri, JavaFile> uriToFileMap = <Uri, JavaFile>{}; | 416 : uriToFileMap = uriToFileMap, |
388 for (String sourceFile in sourceEntities) { | 417 pathToUriMap = _computePathToUriMap(uriToFileMap); |
389 int pipeIndex = sourceFile.indexOf('|'); | 418 |
390 if (pipeIndex == -1) { | 419 @override |
391 // TODO(paulberry): add the ability to guess the URI from the path. | 420 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
392 errorSink.writeln( | 421 File file = uriToFileMap[uri]; |
393 'Illegal input file (must be "\$uri|\$path"): $sourceFile'); | 422 actualUri ??= uri; |
394 return null; | 423 if (file == null) { |
395 } | 424 return new NonExistingSource( |
396 Uri uri = Uri.parse(sourceFile.substring(0, pipeIndex)); | 425 uri.toString(), actualUri, UriKind.fromScheme(actualUri.scheme)); |
397 String path = sourceFile.substring(pipeIndex + 1); | 426 } else { |
398 uriToFileMap[uri] = new JavaFile(path); | 427 return new FileSource(file, actualUri); |
399 } | 428 } |
400 return uriToFileMap; | 429 } |
| 430 |
| 431 @override |
| 432 Uri restoreAbsolute(Source source) { |
| 433 return pathToUriMap[source.fullName]; |
| 434 } |
| 435 |
| 436 /** |
| 437 * Build the inverse mapping of [uriToSourceMap]. |
| 438 */ |
| 439 static Map<String, Uri> _computePathToUriMap(Map<Uri, File> uriToSourceMap) { |
| 440 Map<String, Uri> pathToUriMap = <String, Uri>{}; |
| 441 uriToSourceMap.forEach((Uri uri, File file) { |
| 442 pathToUriMap[file.path] = uri; |
| 443 }); |
| 444 return pathToUriMap; |
401 } | 445 } |
402 } | 446 } |
OLD | NEW |