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 @JS() | 4 @JS() |
5 library dev_compiler.web.web_command; | 5 library dev_compiler.web.web_command; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:html' show HttpRequest; | 9 import 'dart:html' show HttpRequest; |
10 | 10 |
11 import 'package:analyzer/dart/element/element.dart' | 11 import 'package:analyzer/dart/element/element.dart' |
12 show | 12 show |
13 LibraryElement, | 13 LibraryElement, |
14 ImportElement, | 14 ImportElement, |
15 ShowElementCombinator, | 15 ShowElementCombinator, |
16 HideElementCombinator; | 16 HideElementCombinator; |
17 import 'package:analyzer/file_system/file_system.dart' show ResourceUriResolver; | 17 import 'package:analyzer/file_system/file_system.dart' show ResourceUriResolver; |
18 import 'package:analyzer/file_system/memory_file_system.dart' | 18 import 'package:analyzer/file_system/memory_file_system.dart' |
19 show MemoryResourceProvider; | 19 show MemoryResourceProvider; |
20 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; | |
21 import 'package:analyzer/src/summary/idl.dart' show PackageBundle; | 20 import 'package:analyzer/src/summary/idl.dart' show PackageBundle; |
22 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 21 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
23 show SummaryDataStore, InSummaryUriResolver, InSummarySource; | 22 show SummaryDataStore, InSummaryUriResolver, InSummarySource; |
24 import 'package:analyzer/src/dart/resolver/scope.dart' show Scope; | 23 import 'package:analyzer/src/dart/resolver/scope.dart' show Scope; |
25 | 24 |
26 import 'package:args/command_runner.dart'; | 25 import 'package:args/command_runner.dart'; |
27 | 26 |
28 import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions; | 27 import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions; |
29 import 'package:dev_compiler/src/compiler/compiler.dart' | 28 import 'package:dev_compiler/src/compiler/compiler.dart' |
30 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; | 29 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 new InSummaryUriResolver(resourceProvider, summaryDataStore); | 113 new InSummaryUriResolver(resourceProvider, summaryDataStore); |
115 | 114 |
116 var fileResolvers = [summaryResolver, resourceUriResolver]; | 115 var fileResolvers = [summaryResolver, resourceUriResolver]; |
117 | 116 |
118 var compiler = new ModuleCompiler(options, | 117 var compiler = new ModuleCompiler(options, |
119 analysisRoot: '/web-compile-root', | 118 analysisRoot: '/web-compile-root', |
120 fileResolvers: fileResolvers, | 119 fileResolvers: fileResolvers, |
121 resourceProvider: resourceProvider, | 120 resourceProvider: resourceProvider, |
122 summaryData: summaryDataStore); | 121 summaryData: summaryDataStore); |
123 | 122 |
124 var context = compiler.context as AnalysisContextImpl; | |
125 | |
126 var compilerOptions = new CompilerOptions.fromArguments(argResults); | 123 var compilerOptions = new CompilerOptions.fromArguments(argResults); |
127 | 124 |
128 var resolveFn = (String url) { | 125 var resolveFn = (String url) { |
129 var packagePrefix = 'package:'; | 126 var packagePrefix = 'package:'; |
130 var uri = Uri.parse(url); | 127 var uri = Uri.parse(url); |
131 var base = path.basename(url); | 128 var base = path.basename(url); |
132 var parts = uri.pathSegments; | 129 var parts = uri.pathSegments; |
133 var match = null; | 130 var match = null; |
134 int bestScore = 0; | 131 int bestScore = 0; |
135 for (var candidate in summaryDataStore.uriToSummaryPath.keys) { | 132 for (var candidate in summaryDataStore.uriToSummaryPath.keys) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 } | 175 } |
179 | 176 |
180 if (score > bestScore) { | 177 if (score > bestScore) { |
181 match = candidate; | 178 match = candidate; |
182 } | 179 } |
183 } | 180 } |
184 return match; | 181 return match; |
185 }; | 182 }; |
186 | 183 |
187 CompileModule compileFn = (String imports, String body, String libraryName, | 184 CompileModule compileFn = (String imports, String body, String libraryName, |
188 String existingLibrary, String fileName) { | 185 String existingLibrary, String fileName) async { |
189 // Instead of returning a single function, return a pair of functions. | 186 // Instead of returning a single function, return a pair of functions. |
190 // Create a new virtual File that contains the given Dart source. | 187 // Create a new virtual File that contains the given Dart source. |
191 String sourceCode; | 188 String sourceCode; |
192 if (existingLibrary == null) { | 189 if (existingLibrary == null) { |
193 sourceCode = imports + body; | 190 sourceCode = imports + body; |
194 } else { | 191 } else { |
195 var dir = path.dirname(existingLibrary); | 192 var dir = path.dirname(existingLibrary); |
196 // Need to pull in all the imports from the existing library and | 193 // Need to pull in all the imports from the existing library and |
197 // re-export all privates as privates in this library. | 194 // re-export all privates as privates in this library. |
198 var source = context.sourceFactory.forUri(existingLibrary); | 195 var source = compiler.driver.sourceFactory.forUri(existingLibrary); |
199 if (source == null) { | 196 if (source == null) { |
200 throw "Unable to load source for library $existingLibrary"; | 197 throw "Unable to load source for library $existingLibrary"; |
201 } | 198 } |
202 | 199 |
203 LibraryElement libraryElement = context.computeLibraryElement(source); | 200 var unitResult = await compiler.driver.getUnitElement(source.fullName); |
| 201 LibraryElement libraryElement = unitResult.element.library; |
204 if (libraryElement == null) { | 202 if (libraryElement == null) { |
205 throw "Unable to get library element."; | 203 throw "Unable to get library element."; |
206 } | 204 } |
207 var sb = new StringBuffer(imports); | 205 var sb = new StringBuffer(imports); |
208 sb.write('\n'); | 206 sb.write('\n'); |
209 | 207 |
210 // TODO(jacobr): we need to add a proper Analyzer flag specifing that | 208 // TODO(jacobr): we need to add a proper Analyzer flag specifing that |
211 // cross-library privates should be in scope instead of this hack. | 209 // cross-library privates should be in scope instead of this hack. |
212 // We set the private name prefix for scope resolution to an invalid | 210 // We set the private name prefix for scope resolution to an invalid |
213 // character code so that the analyzer ignores normal Dart private | 211 // character code so that the analyzer ignores normal Dart private |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 } | 244 } |
247 sb.write(';\n'); | 245 sb.write(';\n'); |
248 } | 246 } |
249 sb.write(body); | 247 sb.write(body); |
250 sourceCode = sb.toString(); | 248 sourceCode = sb.toString(); |
251 } | 249 } |
252 resourceProvider.newFile(fileName, sourceCode); | 250 resourceProvider.newFile(fileName, sourceCode); |
253 | 251 |
254 var unit = new BuildUnit(libraryName, "", [fileName], _moduleForLibrary); | 252 var unit = new BuildUnit(libraryName, "", [fileName], _moduleForLibrary); |
255 | 253 |
256 JSModuleFile module = compiler.compile(unit, compilerOptions); | 254 JSModuleFile module = await compiler.compile(unit, compilerOptions); |
257 | 255 |
258 var moduleCode = ''; | 256 var moduleCode = ''; |
259 if (module.isValid) { | 257 if (module.isValid) { |
260 moduleCode = module | 258 moduleCode = module |
261 .getCode(ModuleFormat.legacy, unit.name, unit.name + '.map', | 259 .getCode(ModuleFormat.legacy, unit.name, unit.name + '.map', |
262 singleOutFile: true) | 260 singleOutFile: true) |
263 .code; | 261 .code; |
264 } | 262 } |
265 | 263 |
266 return new CompileResult( | 264 return new CompileResult( |
267 code: moduleCode, isValid: module.isValid, errors: module.errors); | 265 code: moduleCode, isValid: module.isValid, errors: module.errors); |
268 }; | 266 }; |
269 | 267 |
270 return [allowInterop(compileFn), allowInterop(resolveFn)]; | 268 return [allowInterop(compileFn), allowInterop(resolveFn)]; |
271 } | 269 } |
272 } | 270 } |
273 | 271 |
274 // Given path, determine corresponding dart library. | 272 // Given path, determine corresponding dart library. |
275 String _moduleForLibrary(source) { | 273 String _moduleForLibrary(source) { |
276 if (source is InSummarySource) { | 274 if (source is InSummarySource) { |
277 return source.summaryPath.substring(1).replaceAll('.api.ds', ''); | 275 return source.summaryPath.substring(1).replaceAll('.api.ds', ''); |
278 } | 276 } |
279 return source.toString().substring(1).replaceAll('.dart', ''); | 277 return source.toString().substring(1).replaceAll('.dart', ''); |
280 } | 278 } |
281 | 279 |
282 /// Thrown when the input source code has errors. | 280 /// Thrown when the input source code has errors. |
283 class CompileErrorException implements Exception { | 281 class CompileErrorException implements Exception { |
284 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 282 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
285 } | 283 } |
OLD | NEW |