| 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; |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 return new SourceSpan(locationForOffset(unit, uri, begin), | 291 return new SourceSpan(locationForOffset(unit, uri, begin), |
| 292 locationForOffset(unit, uri, end), '$text'); | 292 locationForOffset(unit, uri, end), '$text'); |
| 293 } | 293 } |
| 294 | 294 |
| 295 /// Computes a hash for the given contents. | 295 /// Computes a hash for the given contents. |
| 296 String computeHash(String contents) { | 296 String computeHash(String contents) { |
| 297 if (contents == null || contents == '') return null; | 297 if (contents == null || contents == '') return null; |
| 298 return CryptoUtils.bytesToHex((new MD5()..add(contents.codeUnits)).close()); | 298 return CryptoUtils.bytesToHex((new MD5()..add(contents.codeUnits)).close()); |
| 299 } | 299 } |
| 300 | 300 |
| 301 String resourceOutputPath(Uri resourceUri) { | 301 /// Computes a hash for the given file path (reads the contents in binary form). |
| 302 String computeHashFromFile(String filepath) { |
| 303 var bytes = new File(filepath).readAsBytesSync(); |
| 304 return CryptoUtils.bytesToHex((new MD5()..add(bytes)).close()); |
| 305 } |
| 306 |
| 307 String resourceOutputPath(Uri resourceUri, Uri entryUri) { |
| 302 if (resourceUri.scheme == 'package') return resourceUri.path; | 308 if (resourceUri.scheme == 'package') return resourceUri.path; |
| 303 | 309 |
| 304 // Only support file:/// urls for resources in the dev_compiler package | |
| 305 if (resourceUri.scheme != 'file') return null; | 310 if (resourceUri.scheme != 'file') return null; |
| 311 var filepath = resourceUri.path; |
| 312 var relativePath = path.relative(filepath, from: path.dirname(entryUri.path)); |
| 313 |
| 314 // 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 // further inside the folder where the entrypoint is located, otherwise we |
| 317 // assume this is a runtime resource from the dev_compiler. |
| 318 if (!relativePath.startsWith('..')) return relativePath; |
| 306 var segments = resourceUri.pathSegments; | 319 var segments = resourceUri.pathSegments; |
| 307 var len = segments.length; | 320 var len = segments.length; |
| 308 if (segments.length < 4 || | 321 if (segments.length < 4 || |
| 309 segments[len - 2] != 'runtime' || | 322 segments[len - 2] != 'runtime' || |
| 310 segments[len - 3] != 'lib' || | 323 segments[len - 3] != 'lib' || |
| 311 // If loaded from sources this will be exactly dev_compiler, otherwise it | 324 // If loaded from sources this will be exactly dev_compiler, otherwise it |
| 312 // can be the name in the pub cache (typically dev_compiler-version). | 325 // can be the name in the pub cache (typically dev_compiler-version). |
| 313 !segments[len - 4].startsWith('dev_compiler')) { | 326 !segments[len - 4].startsWith('dev_compiler')) { |
| 314 return null; | 327 return null; |
| 315 } | 328 } |
| 316 return path.joinAll(['dev_compiler']..addAll(segments.skip(len - 2))); | 329 return path.joinAll(['dev_compiler']..addAll(segments.skip(len - 2))); |
| 317 } | 330 } |
| OLD | NEW |