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