Chromium Code Reviews| Index: pkg/barback/lib/barback.dart |
| diff --git a/pkg/barback/lib/barback.dart b/pkg/barback/lib/barback.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b30994ad5b002a0a2281574c0e9eed4a48edfdae |
| --- /dev/null |
| +++ b/pkg/barback/lib/barback.dart |
| @@ -0,0 +1,177 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library barback; |
| + |
| +import 'dart:async'; |
| +import 'dart:io'; |
| + |
| +// 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.
|
| +import 'package:pathos/path.dart' as pathos; |
| + |
| +/// Identifies some asset within a package. |
| +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.
|
| + /// 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.
|
| + 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.
|
| + 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.
|
| + return new AssetId(parts[0], parts[1]); |
| + } |
| + |
| + final String package; |
| + final String path; |
| + |
| + String get extension => pathos.extension(path); |
| + |
| + AssetId(this.package, String path) |
| + : 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.
|
| + |
| + AssetId.deserialize(data) |
| + : package = data[0], |
| + path = data[1]; |
| + |
| + operator ==(other) { |
| + 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.
|
| + return package == other.package && |
| + path == other.path; |
| + } |
| + |
| + int get hashCode => package.hashCode ^ path.hashCode; |
| + |
| + AssetId addExtension(String extension) { |
| + return new AssetId(package, "$path.$extension"); |
|
nweiz
2013/06/14 00:57:57
Style nit: =>
Bob Nystrom
2013/06/17 23:35:05
Done.
|
| + } |
| + |
| + AssetId changeExtension(String newExtension) { |
| + 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.
|
| + newExtension; |
| + return new AssetId(package, newPath); |
| + } |
| + |
| + String toString() => "$package|$path"; |
| + |
| + serialize() => [package, path]; |
| +} |
|
nweiz
2013/06/14 00:57:57
All these members will need documentation.
Bob Nystrom
2013/06/17 23:35:05
Done.
|
| + |
| +/// 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.
|
| +/// from some generation process. |
| +abstract class Asset { |
| + 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
|
| + |
| + factory Asset.fromFile(File file) { |
| + return new _FileAsset(file); |
| + } |
| + |
| + factory Asset.fromString(String content) { |
| + return new _StringAsset(content); |
| + } |
| + |
| + factory Asset.fromPath(String path) { |
| + return new _FileAsset(new File(path)); |
| + } |
| + |
| + 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.
|
| + // TODO(rnystrom): Handle errors. |
| + switch (data[0]) { |
| + case "file": return new _FileAsset(new File(data[1])); break; |
| + case "string": return new _StringAsset(data[1]); break; |
| + } |
| + } |
| + |
| + 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.
|
| + Stream<List<int>> read(); |
| + |
| + serialize(); |
| +} |
| + |
| +/// An asset backed by a file on the local file system. |
| +class _FileAsset extends Asset { |
| + 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
|
| + _FileAsset(this._file); |
| + |
| + String readAsString() => _file.readAsStringSync(); |
| + Stream<List<int>> read() => _file.openRead(); |
| + |
| + String toString() => 'File "${_file.path}"'; |
| + |
| + Object serialize() => ["file", _file.path]; |
| +} |
| + |
| +/// An asset whose data is stored in a string. |
| +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
|
| + final String _contents; |
| + |
| + _StringAsset(this._contents); |
| + |
| + String readAsString() => _contents; |
| + // TODO(rnystrom): Handle encoding? |
|
nweiz
2013/06/14 00:57:57
Absolutely!
Bob Nystrom
2013/06/17 23:35:05
Done.
|
| + Stream<List<int>> read() => new Stream<List<int>>.fromIterable([_contents.codeUnits]); |
|
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
|
| + |
| + String toString() { |
| + // Don't show the whole string if it's long. |
| + var contents = _contents; |
| + if (contents.length > 40) { |
| + contents = contents.substring(0, 20) + " ... " + |
| + contents.substring(contents.length - 20); |
| + } |
| + |
| + contents = _escape(contents); |
| + return 'String "$contents"'; |
| + } |
| + |
| + Object serialize() => ["string", _contents]; |
| + |
| + String _escape(String string) { |
| + return string |
| + .replaceAll("\"", r'\"') |
| + .replaceAll("\n", r"\n") |
| + .replaceAll("\r", r"\r") |
| + .replaceAll("\t", r"\t"); |
| + } |
| +} |
| + |
| +/// 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.
|
| +/// provided to barback so that it isn't coupled directly to pub. |
| +abstract class AssetProvider { |
| + /// 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.
|
| + /// transitive dependency graph of the entrypoint package. |
| + Iterable<String> get packages; |
| + |
| + // TODO(rnystrom): Make this async. |
| + /// The paths of all available asset files in [package], relative to the |
| + /// package's root directory. |
| + /// |
| + /// You can pass [within], which should be the relative path to a directory |
| + /// 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
|
| + 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.
|
| + |
| + 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
|
| +} |
| + |
| +/// Error thrown when an asset with [id] cannot be found. |
| +class AssetNotFoundException implements Exception { |
| + final AssetId id; |
| + |
| + AssetNotFoundException(this.id); |
| + |
| + String toString() => "Could not find asset $id."; |
| +} |
| + |
| +/// Error thrown when two transformers both output an asset with [id]. |
| +class AssetCollisionException implements Exception { |
| + final AssetId id; |
| + |
| + AssetCollisionException(this.id); |
| + |
| + String toString() => "Got collision on asset $id."; |
| +} |
| + |
| +/// Error thrown when a transformer requests an input [id] which cannot be |
| +/// found. |
| +class MissingInputException implements Exception { |
| + final AssetId id; |
| + |
| + MissingInputException(this.id); |
| + |
| + String toString() => "Missing input $id."; |
| +} |