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:source_span/source_span.dart'; | 26 import 'package:source_span/source_span.dart'; |
26 | 27 |
27 bool isDartPrivateLibrary(LibraryElement library) { | 28 bool isDartPrivateLibrary(LibraryElement library) { |
28 var uri = library.source.uri; | 29 var uri = library.source.uri; |
29 if (uri.scheme != "dart") return false; | 30 if (uri.scheme != "dart") return false; |
30 return Identifier.isPrivateName(uri.path); | 31 return Identifier.isPrivateName(uri.path); |
31 } | 32 } |
32 | 33 |
33 /// Choose a canonical name from the library element. This is safe to use as a | 34 /// Choose a canonical name from the library element. This is safe to use as a |
34 /// namespace in JS and Dart code generation. This never uses the library's | 35 /// namespace in JS and Dart code generation. This never uses the library's |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 return _RED_COLOR; | 177 return _RED_COLOR; |
177 } | 178 } |
178 if (levelName == 'warning') return _MAGENTA_COLOR; | 179 if (levelName == 'warning') return _MAGENTA_COLOR; |
179 if (levelName == 'info') return _CYAN_COLOR; | 180 if (levelName == 'info') return _CYAN_COLOR; |
180 return null; | 181 return null; |
181 } | 182 } |
182 | 183 |
183 const String _RED_COLOR = '\u001b[31m'; | 184 const String _RED_COLOR = '\u001b[31m'; |
184 const String _MAGENTA_COLOR = '\u001b[35m'; | 185 const String _MAGENTA_COLOR = '\u001b[35m'; |
185 const String _CYAN_COLOR = '\u001b[36m'; | 186 const String _CYAN_COLOR = '\u001b[36m'; |
| 187 const String GREEN_COLOR = '\u001b[32m'; |
| 188 const String NO_COLOR = '\u001b[0m'; |
186 | 189 |
187 class OutWriter { | 190 class OutWriter { |
188 final String _path; | 191 final String _path; |
189 final StringBuffer _sb = new StringBuffer(); | 192 final StringBuffer _sb = new StringBuffer(); |
190 int _indent = 0; | 193 int _indent = 0; |
191 String _prefix = ""; | 194 String _prefix = ""; |
192 bool _needsIndent = true; | 195 bool _needsIndent = true; |
193 | 196 |
194 OutWriter(this._path); | 197 OutWriter(this._path); |
195 | 198 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 var currentToken = node is AnnotatedNode | 252 var currentToken = node is AnnotatedNode |
250 ? node.firstTokenAfterCommentAndMetadata | 253 ? node.firstTokenAfterCommentAndMetadata |
251 : node.beginToken; | 254 : node.beginToken; |
252 var begin = currentToken.offset; | 255 var begin = currentToken.offset; |
253 var end = node.end; | 256 var end = node.end; |
254 var text = source.contents.data.substring(begin, end); | 257 var text = source.contents.data.substring(begin, end); |
255 var uri = source.uri; | 258 var uri = source.uri; |
256 return new SourceSpan(locationForOffset(unit, uri, begin), | 259 return new SourceSpan(locationForOffset(unit, uri, begin), |
257 locationForOffset(unit, uri, end), '$text'); | 260 locationForOffset(unit, uri, end), '$text'); |
258 } | 261 } |
| 262 |
| 263 /// Computes a hash for the given contents. |
| 264 String computeHash(String contents) { |
| 265 if (contents == null || contents == '') return null; |
| 266 return CryptoUtils.bytesToHex((new MD5()..add(contents.codeUnits)).close()); |
| 267 } |
| 268 |
| 269 String resourceOutputPath(Uri resourceUri) => resourceUri.path; |
OLD | NEW |