| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library barback.asset; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 /// A blob of content. |
| 11 /// |
| 12 /// Assets may come from the file system, or as the output of a [Transformer]. |
| 13 /// They are identified by [AssetId]. |
| 14 abstract class Asset { |
| 15 factory Asset.fromFile(File file) { |
| 16 return new _FileAsset(file); |
| 17 } |
| 18 |
| 19 factory Asset.fromString(String content) { |
| 20 return new _StringAsset(content); |
| 21 } |
| 22 |
| 23 factory Asset.fromPath(String path) { |
| 24 return new _FileAsset(new File(path)); |
| 25 } |
| 26 |
| 27 // TODO(rnystrom): This prevents users from defining their own |
| 28 // implementations of Asset. Use serialization package instead. |
| 29 factory Asset.deserialize(data) { |
| 30 // TODO(rnystrom): Handle errors. |
| 31 switch (data[0]) { |
| 32 case "file": return new _FileAsset(new File(data[1])); break; |
| 33 case "string": return new _StringAsset(data[1]); break; |
| 34 } |
| 35 } |
| 36 |
| 37 /// Returns the contents of the asset as a string. |
| 38 // TODO(rnystrom): Figure out how binary assets should be handled. |
| 39 String readAsString(); |
| 40 |
| 41 /// Streams the contents of the asset. |
| 42 Stream<List<int>> read(); |
| 43 |
| 44 /// Serializes this [Asset] to an object that can be sent across isolates |
| 45 /// and passed to [deserialize]. |
| 46 Object serialize(); |
| 47 } |
| 48 |
| 49 /// An asset backed by a file on the local file system. |
| 50 class _FileAsset implements Asset { |
| 51 final File _file; |
| 52 _FileAsset(this._file); |
| 53 |
| 54 String readAsString() => _file.readAsStringSync(); |
| 55 Stream<List<int>> read() => _file.openRead(); |
| 56 |
| 57 String toString() => 'File "${_file.path}"'; |
| 58 |
| 59 Object serialize() => ["file", _file.path]; |
| 60 } |
| 61 |
| 62 /// An asset whose data is stored in a string. |
| 63 // TODO(rnystrom): Have something similar for in-memory binary assets. |
| 64 class _StringAsset implements Asset { |
| 65 final String _contents; |
| 66 |
| 67 _StringAsset(this._contents); |
| 68 |
| 69 String readAsString() => _contents; |
| 70 |
| 71 // TODO(rnystrom): Implement this and handle encoding. |
| 72 Stream<List<int>> read() => throw new UnimplementedError(); |
| 73 |
| 74 String toString() { |
| 75 // Don't show the whole string if it's long. |
| 76 var contents = _contents; |
| 77 if (contents.length > 40) { |
| 78 contents = contents.substring(0, 20) + " ... " + |
| 79 contents.substring(contents.length - 20); |
| 80 } |
| 81 |
| 82 contents = _escape(contents); |
| 83 return 'String "$contents"'; |
| 84 } |
| 85 |
| 86 Object serialize() => ["string", _contents]; |
| 87 |
| 88 String _escape(String string) { |
| 89 return string |
| 90 .replaceAll("\"", r'\"') |
| 91 .replaceAll("\n", r"\n") |
| 92 .replaceAll("\r", r"\r") |
| 93 .replaceAll("\t", r"\t"); |
| 94 } |
| 95 } |
| OLD | NEW |