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:math' as math; | 9 import 'dart:math' as math; |
10 import 'dart:html' show HttpRequest; | 10 import 'dart:html' show HttpRequest; |
11 import 'dart:typed_data'; | 11 import 'dart:typed_data'; |
12 | 12 |
13 import 'package:analyzer/dart/element/element.dart' | 13 import 'package:analyzer/dart/element/element.dart' |
14 show | 14 show |
15 LibraryElement, | 15 LibraryElement, |
16 ImportElement, | 16 ImportElement, |
17 ShowElementCombinator, | 17 ShowElementCombinator, |
18 HideElementCombinator; | 18 HideElementCombinator; |
19 import 'package:analyzer/file_system/file_system.dart' show ResourceUriResolver; | 19 import 'package:analyzer/file_system/file_system.dart' show ResourceUriResolver; |
20 import 'package:analyzer/file_system/memory_file_system.dart' | 20 import 'package:analyzer/file_system/memory_file_system.dart' |
21 show MemoryResourceProvider; | 21 show MemoryResourceProvider; |
22 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; | |
23 import 'package:analyzer/src/summary/idl.dart' show PackageBundle; | 22 import 'package:analyzer/src/summary/idl.dart' show PackageBundle; |
24 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 23 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
25 show SummaryDataStore, InSummaryUriResolver, InSummarySource; | 24 show SummaryDataStore, InSummaryUriResolver, InSummarySource; |
26 import 'package:analyzer/src/dart/resolver/scope.dart' show Scope; | 25 import 'package:analyzer/src/dart/resolver/scope.dart' show Scope; |
27 | 26 |
28 import 'package:args/command_runner.dart'; | 27 import 'package:args/command_runner.dart'; |
29 | 28 |
30 import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions; | 29 import 'package:dev_compiler/src/analyzer/context.dart' show AnalyzerOptions; |
31 import 'package:dev_compiler/src/compiler/compiler.dart' | 30 import 'package:dev_compiler/src/compiler/compiler.dart' |
32 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; | 31 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 new InSummaryUriResolver(resourceProvider, summaryDataStore); | 160 new InSummaryUriResolver(resourceProvider, summaryDataStore); |
162 | 161 |
163 var fileResolvers = [summaryResolver, resourceUriResolver]; | 162 var fileResolvers = [summaryResolver, resourceUriResolver]; |
164 | 163 |
165 var compiler = new ModuleCompiler(options, | 164 var compiler = new ModuleCompiler(options, |
166 analysisRoot: '/web-compile-root', | 165 analysisRoot: '/web-compile-root', |
167 fileResolvers: fileResolvers, | 166 fileResolvers: fileResolvers, |
168 resourceProvider: resourceProvider, | 167 resourceProvider: resourceProvider, |
169 summaryData: summaryDataStore); | 168 summaryData: summaryDataStore); |
170 | 169 |
171 var context = compiler.context as AnalysisContextImpl; | |
172 | |
173 var compilerOptions = new CompilerOptions.fromArguments(argResults); | 170 var compilerOptions = new CompilerOptions.fromArguments(argResults); |
174 | 171 |
175 var resolveFn = (String url) { | 172 var resolveFn = (String url) { |
176 var packagePrefix = 'package:'; | 173 var packagePrefix = 'package:'; |
177 var uri = Uri.parse(url); | 174 var uri = Uri.parse(url); |
178 var base = path.basename(url); | 175 var base = path.basename(url); |
179 var parts = uri.pathSegments; | 176 var parts = uri.pathSegments; |
180 var match = null; | 177 var match = null; |
181 int bestScore = 0; | 178 int bestScore = 0; |
182 for (var candidate in summaryDataStore.uriToSummaryPath.keys) { | 179 for (var candidate in summaryDataStore.uriToSummaryPath.keys) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 } | 222 } |
226 | 223 |
227 if (score > bestScore) { | 224 if (score > bestScore) { |
228 match = candidate; | 225 match = candidate; |
229 } | 226 } |
230 } | 227 } |
231 return match; | 228 return match; |
232 }; | 229 }; |
233 | 230 |
234 CompileModule compileFn = (String imports, String body, String libraryName, | 231 CompileModule compileFn = (String imports, String body, String libraryName, |
235 String existingLibrary, String fileName) { | 232 String existingLibrary, String fileName) async { |
236 // Instead of returning a single function, return a pair of functions. | 233 // Instead of returning a single function, return a pair of functions. |
237 // Create a new virtual File that contains the given Dart source. | 234 // Create a new virtual File that contains the given Dart source. |
238 String sourceCode; | 235 String sourceCode; |
239 if (existingLibrary == null) { | 236 if (existingLibrary == null) { |
240 sourceCode = imports + body; | 237 sourceCode = imports + body; |
241 } else { | 238 } else { |
242 var dir = path.dirname(existingLibrary); | 239 var dir = path.dirname(existingLibrary); |
243 // Need to pull in all the imports from the existing library and | 240 // Need to pull in all the imports from the existing library and |
244 // re-export all privates as privates in this library. | 241 // re-export all privates as privates in this library. |
245 var source = context.sourceFactory.forUri(existingLibrary); | 242 var source = compiler.driver.sourceFactory.forUri(existingLibrary); |
246 if (source == null) { | 243 if (source == null) { |
247 throw "Unable to load source for library $existingLibrary"; | 244 throw "Unable to load source for library $existingLibrary"; |
248 } | 245 } |
249 | 246 |
250 LibraryElement libraryElement = context.computeLibraryElement(source); | 247 var unitResult = await compiler.driver.getUnitElement(source.fullName); |
| 248 LibraryElement libraryElement = unitResult.element.library; |
251 if (libraryElement == null) { | 249 if (libraryElement == null) { |
252 throw "Unable to get library element."; | 250 throw "Unable to get library element."; |
253 } | 251 } |
254 var sb = new StringBuffer(imports); | 252 var sb = new StringBuffer(imports); |
255 sb.write('\n'); | 253 sb.write('\n'); |
256 | 254 |
257 // TODO(jacobr): we need to add a proper Analyzer flag specifing that | 255 // TODO(jacobr): we need to add a proper Analyzer flag specifing that |
258 // cross-library privates should be in scope instead of this hack. | 256 // cross-library privates should be in scope instead of this hack. |
259 // We set the private name prefix for scope resolution to an invalid | 257 // We set the private name prefix for scope resolution to an invalid |
260 // character code so that the analyzer ignores normal Dart private | 258 // character code so that the analyzer ignores normal Dart private |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 } | 291 } |
294 sb.write(';\n'); | 292 sb.write(';\n'); |
295 } | 293 } |
296 sb.write(body); | 294 sb.write(body); |
297 sourceCode = sb.toString(); | 295 sourceCode = sb.toString(); |
298 } | 296 } |
299 resourceProvider.newFile(fileName, sourceCode); | 297 resourceProvider.newFile(fileName, sourceCode); |
300 | 298 |
301 var unit = new BuildUnit(libraryName, "", [fileName], _moduleForLibrary); | 299 var unit = new BuildUnit(libraryName, "", [fileName], _moduleForLibrary); |
302 | 300 |
303 JSModuleFile module = compiler.compile(unit, compilerOptions); | 301 JSModuleFile module = await compiler.compile(unit, compilerOptions); |
304 | 302 |
305 var moduleCode = ''; | 303 var moduleCode = ''; |
306 if (module.isValid) { | 304 if (module.isValid) { |
307 moduleCode = module | 305 moduleCode = module |
308 .getCode(ModuleFormat.legacy, unit.name, unit.name + '.map', | 306 .getCode(ModuleFormat.legacy, unit.name, unit.name + '.map', |
309 singleOutFile: true) | 307 singleOutFile: true) |
310 .code; | 308 .code; |
311 } | 309 } |
312 | 310 |
313 return new CompileResult( | 311 return new CompileResult( |
314 code: moduleCode, isValid: module.isValid, errors: module.errors); | 312 code: moduleCode, isValid: module.isValid, errors: module.errors); |
315 }; | 313 }; |
316 | 314 |
317 return [allowInterop(compileFn), allowInterop(resolveFn)]; | 315 return [allowInterop(compileFn), allowInterop(resolveFn)]; |
318 } | 316 } |
319 } | 317 } |
320 | 318 |
321 // Given path, determine corresponding dart library. | 319 // Given path, determine corresponding dart library. |
322 String _moduleForLibrary(source) { | 320 String _moduleForLibrary(source) { |
323 if (source is InSummarySource) { | 321 if (source is InSummarySource) { |
324 return source.summaryPath.substring(1).replaceAll('.api.ds', ''); | 322 return source.summaryPath.substring(1).replaceAll('.api.ds', ''); |
325 } | 323 } |
326 return source.toString().substring(1).replaceAll('.dart', ''); | 324 return source.toString().substring(1).replaceAll('.dart', ''); |
327 } | 325 } |
328 | 326 |
329 /// Thrown when the input source code has errors. | 327 /// Thrown when the input source code has errors. |
330 class CompileErrorException implements Exception { | 328 class CompileErrorException implements Exception { |
331 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 329 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
332 } | 330 } |
OLD | NEW |