OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 'dart:collection' show HashSet, Queue; | 5 import 'dart:collection' show HashSet, Queue; |
6 import 'dart:convert' show BASE64, JSON, UTF8; | 6 import 'dart:convert' show BASE64, JSON, UTF8; |
7 import 'dart:io' show File; | 7 import 'dart:io' show File; |
8 import 'package:analyzer/dart/element/element.dart' show LibraryElement; | 8 import 'package:analyzer/dart/element/element.dart' show LibraryElement; |
9 import 'package:analyzer/analyzer.dart' | 9 import 'package:analyzer/analyzer.dart' |
10 show AnalysisError, CompilationUnit, ErrorSeverity; | 10 show AnalysisError, CompilationUnit, ErrorSeverity; |
11 import 'package:analyzer/file_system/file_system.dart' show ResourceProvider; | 11 import 'package:analyzer/file_system/file_system.dart' show ResourceProvider; |
| 12 import 'package:analyzer/file_system/physical_file_system.dart' |
| 13 show PhysicalResourceProvider; |
| 14 import 'package:analyzer/src/context/builder.dart' show ContextBuilder; |
| 15 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; |
12 import 'package:analyzer/src/generated/engine.dart' | 16 import 'package:analyzer/src/generated/engine.dart' |
13 show AnalysisContext, AnalysisEngine; | 17 show AnalysisContext, AnalysisEngine; |
14 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; | 18 import 'package:analyzer/src/generated/sdk.dart' show DartSdkManager; |
| 19 import 'package:analyzer/src/generated/source.dart' |
| 20 show ContentCache, DartUriResolver; |
15 import 'package:analyzer/src/generated/source_io.dart' | 21 import 'package:analyzer/src/generated/source_io.dart' |
16 show Source, SourceKind, UriResolver; | 22 show Source, SourceKind, UriResolver; |
17 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 23 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
18 show InSummarySource, InputPackagesResultProvider, SummaryDataStore; | 24 show InSummarySource, InputPackagesResultProvider, SummaryDataStore; |
19 import 'package:analyzer/src/error/codes.dart' show StaticTypeWarningCode; | 25 import 'package:analyzer/src/error/codes.dart' show StaticTypeWarningCode; |
20 import 'package:args/args.dart' show ArgParser, ArgResults; | 26 import 'package:args/args.dart' show ArgParser, ArgResults; |
21 import 'package:args/src/usage_exception.dart' show UsageException; | 27 import 'package:args/src/usage_exception.dart' show UsageException; |
22 import 'package:func/func.dart' show Func1; | 28 import 'package:func/func.dart' show Func1; |
23 import 'package:path/path.dart' as path; | 29 import 'package:path/path.dart' as path; |
24 import 'package:source_maps/source_maps.dart'; | 30 import 'package:source_maps/source_maps.dart'; |
25 | 31 |
26 import '../analyzer/context.dart' | 32 import '../analyzer/context.dart' show AnalyzerOptions, createSourceFactory; |
27 show | |
28 AnalyzerOptions, | |
29 createAnalysisContext, | |
30 createSdkPathResolver, | |
31 createSourceFactory; | |
32 import '../js_ast/js_ast.dart' as JS; | 33 import '../js_ast/js_ast.dart' as JS; |
33 import 'code_generator.dart' show CodeGenerator; | 34 import 'code_generator.dart' show CodeGenerator; |
34 import 'error_helpers.dart' show errorSeverity, formatError, sortErrors; | 35 import 'error_helpers.dart' show errorSeverity, formatError, sortErrors; |
35 import 'extension_types.dart' show ExtensionTypeSet; | 36 import 'extension_types.dart' show ExtensionTypeSet; |
36 import 'js_names.dart' as JS; | 37 import 'js_names.dart' as JS; |
37 import 'module_builder.dart' show transformModuleFormat, ModuleFormat; | 38 import 'module_builder.dart' show transformModuleFormat, ModuleFormat; |
38 import 'source_map_printer.dart' show SourceMapPrintingContext; | 39 import 'source_map_printer.dart' show SourceMapPrintingContext; |
39 | 40 |
40 /// Compiles a set of Dart files into a single JavaScript module. | 41 /// Compiles a set of Dart files into a single JavaScript module. |
41 /// | 42 /// |
(...skipping 10 matching lines...) Expand all Loading... |
52 /// | 53 /// |
53 /// For all other files, it is up to the [AnalysisContext] to decide whether or | 54 /// For all other files, it is up to the [AnalysisContext] to decide whether or |
54 /// not any caching is performed. By default an analysis context will assume | 55 /// not any caching is performed. By default an analysis context will assume |
55 /// sources are immutable for the life of the context, and cache information | 56 /// sources are immutable for the life of the context, and cache information |
56 /// about them. | 57 /// about them. |
57 class ModuleCompiler { | 58 class ModuleCompiler { |
58 final AnalysisContext context; | 59 final AnalysisContext context; |
59 final SummaryDataStore summaryData; | 60 final SummaryDataStore summaryData; |
60 final ExtensionTypeSet _extensionTypes; | 61 final ExtensionTypeSet _extensionTypes; |
61 | 62 |
62 ModuleCompiler.withContext(AnalysisContext context, this.summaryData) | 63 ModuleCompiler._(AnalysisContext context, this.summaryData) |
63 : context = context, | 64 : context = context, |
64 _extensionTypes = new ExtensionTypeSet(context) { | 65 _extensionTypes = new ExtensionTypeSet(context); |
65 if (!context.analysisOptions.strongMode) { | |
66 throw new ArgumentError('AnalysisContext must be strong mode'); | |
67 } | |
68 if (!context.sourceFactory.dartSdk.context.analysisOptions.strongMode) { | |
69 throw new ArgumentError('AnalysisContext must have strong mode SDK'); | |
70 } | |
71 } | |
72 | 66 |
73 factory ModuleCompiler(AnalyzerOptions options, | 67 factory ModuleCompiler(AnalyzerOptions options, |
74 {DartUriResolver sdkResolver, | 68 {ResourceProvider resourceProvider, |
75 ResourceProvider resourceProvider, | 69 String analysisRoot, |
76 List<UriResolver> fileResolvers}) { | 70 List<UriResolver> fileResolvers}) { |
| 71 // TODO(danrubel): refactor with analyzer CLI into analyzer common code |
77 AnalysisEngine.instance.processRequiredPlugins(); | 72 AnalysisEngine.instance.processRequiredPlugins(); |
78 | 73 |
79 sdkResolver ??= | 74 resourceProvider ??= PhysicalResourceProvider.INSTANCE; |
80 createSdkPathResolver(options.dartSdkSummaryPath, options.dartSdkPath); | 75 analysisRoot ??= path.current; |
| 76 |
| 77 var contextBuilder = new ContextBuilder(resourceProvider, |
| 78 new DartSdkManager(options.dartSdkPath, true), new ContentCache(), |
| 79 options: options.contextBuilderOptions); |
| 80 |
| 81 var analysisOptions = contextBuilder.getAnalysisOptions(analysisRoot); |
| 82 var sdk = contextBuilder.findSdk(null, analysisOptions); |
| 83 |
| 84 var sdkResolver = new DartUriResolver(sdk); |
81 | 85 |
82 // Read the summaries. | 86 // Read the summaries. |
83 var summaryData = | 87 var summaryData = |
84 new SummaryDataStore(options.summaryPaths, recordDependencyInfo: true); | 88 new SummaryDataStore(options.summaryPaths, recordDependencyInfo: true); |
85 | 89 |
86 var srcFactory = createSourceFactory(options, | 90 var srcFactory = createSourceFactory(options, |
87 sdkResolver: sdkResolver, | 91 sdkResolver: sdkResolver, |
88 fileResolvers: fileResolvers, | 92 fileResolvers: fileResolvers, |
89 summaryData: summaryData, | 93 summaryData: summaryData, |
90 resourceProvider: resourceProvider); | 94 resourceProvider: resourceProvider); |
91 | 95 |
92 var context = createAnalysisContext(); | 96 var context = |
| 97 AnalysisEngine.instance.createAnalysisContext() as AnalysisContextImpl; |
| 98 context.analysisOptions = analysisOptions; |
93 context.sourceFactory = srcFactory; | 99 context.sourceFactory = srcFactory; |
94 context.typeProvider = sdkResolver.dartSdk.context.typeProvider; | 100 context.typeProvider = sdkResolver.dartSdk.context.typeProvider; |
95 context.resultProvider = | 101 context.resultProvider = |
96 new InputPackagesResultProvider(context, summaryData); | 102 new InputPackagesResultProvider(context, summaryData); |
97 options.declaredVariables.forEach(context.declaredVariables.define); | 103 options.declaredVariables.forEach(context.declaredVariables.define); |
98 context.declaredVariables.define('dart.isVM', 'false'); | 104 context.declaredVariables.define('dart.isVM', 'false'); |
99 | 105 |
100 // TODO(vsm): Should this be hardcoded? | 106 // TODO(vsm): Should this be hardcoded? |
101 context.declaredVariables.define('dart.library.html', 'true'); | 107 context.declaredVariables.define('dart.library.html', 'true'); |
102 context.declaredVariables.define('dart.library.io', 'false'); | 108 context.declaredVariables.define('dart.library.io', 'false'); |
103 | 109 |
104 return new ModuleCompiler.withContext(context, summaryData); | 110 if (!context.analysisOptions.strongMode) { |
| 111 throw new ArgumentError('AnalysisContext must be strong mode'); |
| 112 } |
| 113 if (!context.sourceFactory.dartSdk.context.analysisOptions.strongMode) { |
| 114 throw new ArgumentError('AnalysisContext must have strong mode SDK'); |
| 115 } |
| 116 |
| 117 return new ModuleCompiler._(context, summaryData); |
105 } | 118 } |
106 | 119 |
107 bool _isFatalError(AnalysisError e, CompilerOptions options) { | 120 bool _isFatalError(AnalysisError e, CompilerOptions options) { |
108 if (errorSeverity(context, e) != ErrorSeverity.ERROR) return false; | 121 if (errorSeverity(context, e) != ErrorSeverity.ERROR) return false; |
109 | 122 |
110 // These errors are not fatal in the REPL compile mode as we | 123 // These errors are not fatal in the REPL compile mode as we |
111 // allow access to private members across library boundaries | 124 // allow access to private members across library boundaries |
112 // and those accesses will show up as undefined members unless | 125 // and those accesses will show up as undefined members unless |
113 // additional analyzer changes are made to support them. | 126 // additional analyzer changes are made to support them. |
114 // TODO(jacobr): consider checking that the identifier name | 127 // TODO(jacobr): consider checking that the identifier name |
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 // Fall back to a relative path. | 571 // Fall back to a relative path. |
559 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString(); | 572 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString(); |
560 } | 573 } |
561 | 574 |
562 for (int i = 0; i < list.length; i++) { | 575 for (int i = 0; i < list.length; i++) { |
563 list[i] = transformUri(list[i]); | 576 list[i] = transformUri(list[i]); |
564 } | 577 } |
565 map['file'] = transformUri(map['file']); | 578 map['file'] = transformUri(map['file']); |
566 return map; | 579 return map; |
567 } | 580 } |
OLD | NEW |