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 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'package:path/path.dart' as path; | 8 import 'package:path/path.dart' as path; |
9 import 'package:analyzer/src/generated/ast.dart' | 9 import 'package:analyzer/src/generated/ast.dart' |
10 show | 10 show |
11 ImportDirective, | 11 ImportDirective, |
12 ExportDirective, | 12 ExportDirective, |
13 PartDirective, | 13 PartDirective, |
14 CompilationUnit, | 14 CompilationUnit, |
15 Identifier, | 15 Identifier, |
16 AnnotatedNode, | 16 AnnotatedNode, |
17 AstNode, | 17 AstNode, |
18 Expression, | 18 Expression, |
19 SimpleIdentifier, | 19 SimpleIdentifier, |
20 MethodInvocation; | 20 MethodInvocation; |
21 import 'package:analyzer/src/generated/constant.dart' show DartObject; | 21 import 'package:analyzer/src/generated/constant.dart' show DartObject; |
22 import 'package:analyzer/src/generated/element.dart'; | 22 import 'package:analyzer/src/generated/element.dart'; |
23 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; | 23 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; |
24 import 'package:analyzer/src/generated/error.dart' show ErrorCode; | 24 import 'package:analyzer/src/generated/error.dart' show ErrorCode; |
25 import 'package:analyzer/src/task/dart.dart' show ParseDartTask; | 25 import 'package:analyzer/src/task/dart.dart' show ParseDartTask; |
26 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; | 26 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; |
27 import 'package:analyzer/src/generated/source.dart' show LineInfo, Source; | 27 import 'package:analyzer/src/generated/source.dart' show LineInfo, Source; |
28 import 'package:analyzer/analyzer.dart' show parseDirectives; | 28 import 'package:analyzer/analyzer.dart' show parseDirectives; |
29 import 'package:crypto/crypto.dart' show CryptoUtils, MD5; | |
30 import 'package:source_span/source_span.dart'; | 29 import 'package:source_span/source_span.dart'; |
31 | 30 |
32 import 'codegen/js_names.dart' show invalidVariableName; | 31 import 'codegen/js_names.dart' show invalidVariableName; |
33 | 32 |
34 bool isDartPrivateLibrary(LibraryElement library) { | 33 bool isDartPrivateLibrary(LibraryElement library) { |
35 var uri = library.source.uri; | 34 var uri = library.source.uri; |
36 if (uri.scheme != "dart") return false; | 35 if (uri.scheme != "dart") return false; |
37 return Identifier.isPrivateName(uri.path); | 36 return Identifier.isPrivateName(uri.path); |
38 } | 37 } |
39 | 38 |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 new File(_path).writeAsStringSync('$_sb'); | 288 new File(_path).writeAsStringSync('$_sb'); |
290 } | 289 } |
291 } | 290 } |
292 | 291 |
293 SourceLocation locationForOffset(LineInfo lineInfo, Uri uri, int offset) { | 292 SourceLocation locationForOffset(LineInfo lineInfo, Uri uri, int offset) { |
294 var loc = lineInfo.getLocation(offset); | 293 var loc = lineInfo.getLocation(offset); |
295 return new SourceLocation(offset, | 294 return new SourceLocation(offset, |
296 sourceUrl: uri, line: loc.lineNumber - 1, column: loc.columnNumber - 1); | 295 sourceUrl: uri, line: loc.lineNumber - 1, column: loc.columnNumber - 1); |
297 } | 296 } |
298 | 297 |
299 /// Computes a hash for the given contents. | |
300 String computeHash(String contents) { | |
301 if (contents == null || contents == '') return null; | |
302 return CryptoUtils.bytesToHex((new MD5()..add(contents.codeUnits)).close()); | |
303 } | |
304 | |
305 /// Computes a hash for the given file path (reads the contents in binary form). | |
306 String computeHashFromFile(String filepath) { | |
307 var bytes = new File(filepath).readAsBytesSync(); | |
308 return CryptoUtils.bytesToHex((new MD5()..add(bytes)).close()); | |
309 } | |
310 | |
311 String resourceOutputPath(Uri resourceUri, Uri entryUri, String runtimeDir) { | 298 String resourceOutputPath(Uri resourceUri, Uri entryUri, String runtimeDir) { |
312 if (resourceUri.scheme == 'package') return resourceUri.path; | 299 if (resourceUri.scheme == 'package') return resourceUri.path; |
313 | 300 |
314 if (resourceUri.scheme != 'file') return null; | 301 if (resourceUri.scheme != 'file') return null; |
315 | 302 |
316 var entryPath = entryUri.path; | 303 var entryPath = entryUri.path; |
317 // The entry uri is either a directory or a dart/html file. If the latter, | 304 // The entry uri is either a directory or a dart/html file. If the latter, |
318 // trim the file. | 305 // trim the file. |
319 var entryDir = entryPath.endsWith('.dart') || entryPath.endsWith('.html') | 306 var entryDir = entryPath.endsWith('.dart') || entryPath.endsWith('.html') |
320 ? path.dirname(entryPath) | 307 ? path.dirname(entryPath) |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 } | 454 } |
468 | 455 |
469 void writeAsStringSync(String file, String contents) { | 456 void writeAsStringSync(String file, String contents) { |
470 _ensureParentExists(file); | 457 _ensureParentExists(file); |
471 new File(file).writeAsStringSync(contents); | 458 new File(file).writeAsStringSync(contents); |
472 } | 459 } |
473 } | 460 } |
474 | 461 |
475 DartType getStaticType(Expression e) => | 462 DartType getStaticType(Expression e) => |
476 e.staticType ?? DynamicTypeImpl.instance; | 463 e.staticType ?? DynamicTypeImpl.instance; |
OLD | NEW |