Chromium Code Reviews| 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; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:io'; | |
| 9 | |
| 10 // TODO(rnystrom): Is this the prefix we want to use? | |
|
nweiz
2013/06/14 00:57:57
yes
Bob Nystrom
2013/06/17 23:35:05
Done.
| |
| 11 import 'package:pathos/path.dart' as pathos; | |
| 12 | |
| 13 /// Identifies some asset within a package. | |
| 14 class AssetId { | |
|
nweiz
2013/06/14 00:57:57
I'd rather see each of these classes in its own li
Bob Nystrom
2013/06/17 23:35:05
Done.
| |
| 15 /// Parses an [AssetId] string of the form "package|path/to/asset.txt". | |
|
nweiz
2013/06/14 00:57:57
Why "|"? Why not ":"?
Bob Nystrom
2013/06/17 23:35:05
I did ":" at first, but I worried that it makes as
nweiz
2013/06/18 23:14:45
I've found the look of these IDs very odd. I was e
Bob Nystrom
2013/06/20 00:23:59
At least right now, this method is only really bei
nweiz
2013/06/20 23:06:08
Okay, sounds good.
| |
| 16 static AssetId parse(String description) { | |
|
nweiz
2013/06/14 00:57:57
Why isn't this a constructor? I thought we decided
Bob Nystrom
2013/06/17 23:35:05
Done.
| |
| 17 var parts = description.split("|"); | |
|
nweiz
2013/06/14 00:57:57
Assert that there's only two parts here.
Bob Nystrom
2013/06/17 23:35:05
Done.
| |
| 18 return new AssetId(parts[0], parts[1]); | |
| 19 } | |
| 20 | |
| 21 final String package; | |
| 22 final String path; | |
| 23 | |
| 24 String get extension => pathos.extension(path); | |
| 25 | |
| 26 AssetId(this.package, String path) | |
| 27 : path = pathos.normalize(path); | |
|
nweiz
2013/06/14 00:57:57
This will break on Windows, since the asset paths
Bob Nystrom
2013/06/17 23:35:05
Done.
| |
| 28 | |
| 29 AssetId.deserialize(data) | |
| 30 : package = data[0], | |
| 31 path = data[1]; | |
| 32 | |
| 33 operator ==(other) { | |
| 34 if (other is! AssetId) return false; | |
|
nweiz
2013/06/14 00:57:57
Style nit: fold this into the returned expression.
Bob Nystrom
2013/06/17 23:35:05
Done.
| |
| 35 return package == other.package && | |
| 36 path == other.path; | |
| 37 } | |
| 38 | |
| 39 int get hashCode => package.hashCode ^ path.hashCode; | |
| 40 | |
| 41 AssetId addExtension(String extension) { | |
| 42 return new AssetId(package, "$path.$extension"); | |
|
nweiz
2013/06/14 00:57:57
Style nit: =>
Bob Nystrom
2013/06/17 23:35:05
Done.
| |
| 43 } | |
| 44 | |
| 45 AssetId changeExtension(String newExtension) { | |
| 46 var newPath = path.substring(0, path.length - extension.length) + | |
|
nweiz
2013/06/14 00:57:57
pathos.withoutExtension
Bob Nystrom
2013/06/17 23:35:05
Done.
| |
| 47 newExtension; | |
| 48 return new AssetId(package, newPath); | |
| 49 } | |
| 50 | |
| 51 String toString() => "$package|$path"; | |
| 52 | |
| 53 serialize() => [package, path]; | |
| 54 } | |
|
nweiz
2013/06/14 00:57:57
All these members will need documentation.
Bob Nystrom
2013/06/17 23:35:05
Done.
| |
| 55 | |
| 56 /// An identifiable blob of data. Assets may come from the file system, or | |
|
nweiz
2013/06/14 00:57:57
Clarify what "identifiable" means.
Also, move the
Bob Nystrom
2013/06/17 23:35:05
Done.
nweiz
2013/06/18 23:14:45
Most of these comments still have more than one se
Bob Nystrom
2013/06/20 00:23:59
Done.
| |
| 57 /// from some generation process. | |
| 58 abstract class Asset { | |
| 59 Asset(); | |
|
nweiz
2013/06/14 00:57:57
Why does this class have a constructor? It seems l
Bob Nystrom
2013/06/17 23:35:05
Because it has other named constructors, it doesn'
nweiz
2013/06/18 23:14:45
But why have subclasses at all? Why not just make
Bob Nystrom
2013/06/20 00:23:59
Oh, duh. Right. I think at some point there used t
| |
| 60 | |
| 61 factory Asset.fromFile(File file) { | |
| 62 return new _FileAsset(file); | |
| 63 } | |
| 64 | |
| 65 factory Asset.fromString(String content) { | |
| 66 return new _StringAsset(content); | |
| 67 } | |
| 68 | |
| 69 factory Asset.fromPath(String path) { | |
| 70 return new _FileAsset(new File(path)); | |
| 71 } | |
| 72 | |
| 73 factory Asset.deserialize(data) { | |
|
nweiz
2013/06/14 00:57:57
The rest of this class makes me think that Asset c
Bob Nystrom
2013/06/17 23:35:05
That is a use case I had in mind.
| |
| 74 // TODO(rnystrom): Handle errors. | |
| 75 switch (data[0]) { | |
| 76 case "file": return new _FileAsset(new File(data[1])); break; | |
| 77 case "string": return new _StringAsset(data[1]); break; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 String readAsString(); | |
|
nweiz
2013/06/14 00:57:57
This method seems weird to me. Why do the assets n
Bob Nystrom
2013/06/17 23:35:05
Convenience.
| |
| 82 Stream<List<int>> read(); | |
| 83 | |
| 84 serialize(); | |
| 85 } | |
| 86 | |
| 87 /// An asset backed by a file on the local file system. | |
| 88 class _FileAsset extends Asset { | |
| 89 final File _file; | |
|
nweiz
2013/06/14 00:57:57
I don't like storing a [File] object. I like the p
Bob Nystrom
2013/06/17 23:35:05
File is just a wrapper around a path. If I don't s
nweiz
2013/06/18 23:14:45
I prefer creating a new [File] each time; I feel l
Bob Nystrom
2013/06/20 00:23:59
I kind of like it this way. I'll leave it for now
| |
| 90 _FileAsset(this._file); | |
| 91 | |
| 92 String readAsString() => _file.readAsStringSync(); | |
| 93 Stream<List<int>> read() => _file.openRead(); | |
| 94 | |
| 95 String toString() => 'File "${_file.path}"'; | |
| 96 | |
| 97 Object serialize() => ["file", _file.path]; | |
| 98 } | |
| 99 | |
| 100 /// An asset whose data is stored in a string. | |
| 101 class _StringAsset extends Asset { | |
|
nweiz
2013/06/14 00:57:57
What about in-memory binary assets? It seems like
Bob Nystrom
2013/06/17 23:35:05
Yeah, I don't have any real support for binary ass
| |
| 102 final String _contents; | |
| 103 | |
| 104 _StringAsset(this._contents); | |
| 105 | |
| 106 String readAsString() => _contents; | |
| 107 // TODO(rnystrom): Handle encoding? | |
|
nweiz
2013/06/14 00:57:57
Absolutely!
Bob Nystrom
2013/06/17 23:35:05
Done.
| |
| 108 Stream<List<int>> read() => new Stream<List<int>>.fromIterable([_contents.code Units]); | |
|
nweiz
2013/06/14 00:57:57
This is broken for a lot of non-ASCII text. [codeU
Bob Nystrom
2013/06/17 23:35:05
This code path isn't being used or tested yet anyw
| |
| 109 | |
| 110 String toString() { | |
| 111 // Don't show the whole string if it's long. | |
| 112 var contents = _contents; | |
| 113 if (contents.length > 40) { | |
| 114 contents = contents.substring(0, 20) + " ... " + | |
| 115 contents.substring(contents.length - 20); | |
| 116 } | |
| 117 | |
| 118 contents = _escape(contents); | |
| 119 return 'String "$contents"'; | |
| 120 } | |
| 121 | |
| 122 Object serialize() => ["string", _contents]; | |
| 123 | |
| 124 String _escape(String string) { | |
| 125 return string | |
| 126 .replaceAll("\"", r'\"') | |
| 127 .replaceAll("\n", r"\n") | |
| 128 .replaceAll("\r", r"\r") | |
| 129 .replaceAll("\t", r"\t"); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 /// API for locating and accessing packages on disc. Implemented by pub and | |
|
nweiz
2013/06/14 00:57:57
"disc" -> "disk"
Bob Nystrom
2013/06/17 23:35:05
Done.
| |
| 134 /// provided to barback so that it isn't coupled directly to pub. | |
| 135 abstract class AssetProvider { | |
| 136 /// The names of all packages that can be provided by this provider. This will be the | |
|
nweiz
2013/06/14 00:57:57
Long line.
Bob Nystrom
2013/06/17 23:35:05
Done.
| |
| 137 /// transitive dependency graph of the entrypoint package. | |
| 138 Iterable<String> get packages; | |
| 139 | |
| 140 // TODO(rnystrom): Make this async. | |
| 141 /// The paths of all available asset files in [package], relative to the | |
| 142 /// package's root directory. | |
| 143 /// | |
| 144 /// You can pass [within], which should be the relative path to a directory | |
| 145 /// within the package, to only return the files within that subdirectory. | |
|
nweiz
2013/06/14 00:57:57
Does this give access to all files in the package,
Bob Nystrom
2013/06/17 23:35:05
All files. This lets it access stuff in asset/ but
nweiz
2013/06/18 23:14:45
My intuition is that we'll eventually have to deci
Bob Nystrom
2013/06/20 00:23:59
Agreed. I don't intend to try hard to keep it unta
| |
| 146 List<String> listFiles(String package, {String within}); | |
|
nweiz
2013/06/14 00:57:57
It seems like this should be "listAssets".
Bob Nystrom
2013/06/17 23:35:05
Done. Made it return AssetIds too.
| |
| 147 | |
| 148 Future<Asset> loadAsset(AssetId id); | |
|
nweiz
2013/06/14 00:57:57
This name is weird, since it doesn't actually load
Bob Nystrom
2013/06/17 23:35:05
Changed to getAsset() to be a bit more oblique. Fr
| |
| 149 } | |
| 150 | |
| 151 /// Error thrown when an asset with [id] cannot be found. | |
| 152 class AssetNotFoundException implements Exception { | |
| 153 final AssetId id; | |
| 154 | |
| 155 AssetNotFoundException(this.id); | |
| 156 | |
| 157 String toString() => "Could not find asset $id."; | |
| 158 } | |
| 159 | |
| 160 /// Error thrown when two transformers both output an asset with [id]. | |
| 161 class AssetCollisionException implements Exception { | |
| 162 final AssetId id; | |
| 163 | |
| 164 AssetCollisionException(this.id); | |
| 165 | |
| 166 String toString() => "Got collision on asset $id."; | |
| 167 } | |
| 168 | |
| 169 /// Error thrown when a transformer requests an input [id] which cannot be | |
| 170 /// found. | |
| 171 class MissingInputException implements Exception { | |
| 172 final AssetId id; | |
| 173 | |
| 174 MissingInputException(this.id); | |
| 175 | |
| 176 String toString() => "Missing input $id."; | |
| 177 } | |
| OLD | NEW |