| 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 /// Holds a couple utility functions used at various places in the system. | 5 /// Holds a couple utility functions used at various places in the system. |
| 6 library dev_compiler.src.utils; | 6 library dev_compiler.src.utils; |
| 7 | 7 |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| 11 import 'package:analyzer/src/generated/ast.dart' | 11 import 'package:analyzer/src/generated/ast.dart' |
| 12 show | 12 show |
| 13 ImportDirective, | 13 ImportDirective, |
| 14 ExportDirective, | 14 ExportDirective, |
| 15 PartDirective, | 15 PartDirective, |
| 16 CompilationUnit, | 16 CompilationUnit, |
| 17 Identifier, | 17 Identifier, |
| 18 AnnotatedNode, | 18 AnnotatedNode, |
| 19 AstNode; | 19 AstNode; |
| 20 import 'package:analyzer/src/generated/engine.dart' | 20 import 'package:analyzer/src/generated/engine.dart' |
| 21 show ParseDartTask, AnalysisContext; | 21 show ParseDartTask, AnalysisContext; |
| 22 import 'package:analyzer/src/generated/source.dart' show Source; | 22 import 'package:analyzer/src/generated/source.dart' show Source; |
| 23 import 'package:analyzer/src/generated/element.dart'; | 23 import 'package:analyzer/src/generated/element.dart'; |
| 24 import 'package:analyzer/analyzer.dart' show parseDirectives; | 24 import 'package:analyzer/analyzer.dart' show parseDirectives; |
| 25 import 'package:crypto/crypto.dart' show CryptoUtils, MD5; | 25 import 'package:crypto/crypto.dart' show CryptoUtils, MD5; |
| 26 import 'package:source_span/source_span.dart'; | 26 import 'package:source_span/source_span.dart'; |
| 27 import 'package:yaml/yaml.dart'; |
| 27 | 28 |
| 28 import 'js/keywords.dart'; | 29 import 'js/keywords.dart'; |
| 29 | 30 |
| 30 bool isDartPrivateLibrary(LibraryElement library) { | 31 bool isDartPrivateLibrary(LibraryElement library) { |
| 31 var uri = library.source.uri; | 32 var uri = library.source.uri; |
| 32 if (uri.scheme != "dart") return false; | 33 if (uri.scheme != "dart") return false; |
| 33 return Identifier.isPrivateName(uri.path); | 34 return Identifier.isPrivateName(uri.path); |
| 34 } | 35 } |
| 35 | 36 |
| 36 /// Choose a canonical name from the library element. This is safe to use as a | 37 /// Choose a canonical name from the library element. This is safe to use as a |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 | 310 |
| 310 if (resourceUri.scheme != 'file') return null; | 311 if (resourceUri.scheme != 'file') return null; |
| 311 var filepath = resourceUri.path; | 312 var filepath = resourceUri.path; |
| 312 var relativePath = path.relative(filepath, from: path.dirname(entryUri.path)); | 313 var relativePath = path.relative(filepath, from: path.dirname(entryUri.path)); |
| 313 | 314 |
| 314 // File:/// urls can be for resources in the same project or resources from | 315 // File:/// urls can be for resources in the same project or resources from |
| 315 // the dev_compiler package. For now we only support relative paths going | 316 // the dev_compiler package. For now we only support relative paths going |
| 316 // further inside the folder where the entrypoint is located, otherwise we | 317 // further inside the folder where the entrypoint is located, otherwise we |
| 317 // assume this is a runtime resource from the dev_compiler. | 318 // assume this is a runtime resource from the dev_compiler. |
| 318 if (!relativePath.startsWith('..')) return relativePath; | 319 if (!relativePath.startsWith('..')) return relativePath; |
| 319 var segments = resourceUri.pathSegments; | 320 |
| 320 var len = segments.length; | 321 // Expect the code to live under lib/runtime/ in the dev_compiler's folder. |
| 321 if (segments.length < 4 || | 322 var filename = path.basename(filepath); |
| 322 segments[len - 2] != 'runtime' || | 323 var dir = path.dirname(filepath); |
| 323 segments[len - 3] != 'lib' || | 324 if (path.basename(dir) != 'runtime') return null; |
| 324 // If loaded from sources this will be exactly dev_compiler, otherwise it | 325 dir = path.dirname(dir); |
| 325 // can be the name in the pub cache (typically dev_compiler-version). | 326 if (path.basename(dir) != 'lib') return null; |
| 326 !segments[len - 4].startsWith('dev_compiler')) { | 327 dir = path.dirname(dir); |
| 327 return null; | 328 var pubspec = |
| 328 } | 329 loadYaml(new File(path.join(dir, 'pubspec.yaml')).readAsStringSync()); |
| 329 return path.joinAll(['dev_compiler']..addAll(segments.skip(len - 2))); | 330 |
| 331 // Ensure this is loaded from the dev_compiler package. |
| 332 if (pubspec['name'] != 'dev_compiler') return null; |
| 333 return path.join('dev_compiler', 'runtime', filename); |
| 330 } | 334 } |
| OLD | NEW |