Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2442)

Unified Diff: pkg/barback/lib/src/asset.dart

Issue 16854005: First pass at build dependency graph for barback. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/barback/lib/src/asset.dart
diff --git a/pkg/barback/lib/src/asset.dart b/pkg/barback/lib/src/asset.dart
new file mode 100644
index 0000000000000000000000000000000000000000..2de733fcf1f1b268714b8f3e229fe9824c571cc9
--- /dev/null
+++ b/pkg/barback/lib/src/asset.dart
@@ -0,0 +1,95 @@
+// 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.asset;
+
+import 'dart:async';
+import 'dart:io';
+
+/// A blob of content.
+///
+/// Assets may come from the file system, or as the output of a [Transformer].
+/// They are identified by [AssetId].
+abstract class Asset {
+ 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));
+ }
+
+ // TODO(rnystrom): This prevents users from defining their own
+ // implementations of Asset. Use serialization package instead.
+ factory Asset.deserialize(data) {
+ // 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;
+ }
+ }
+
+ /// Returns the contents of the asset as a string.
+ // TODO(rnystrom): Figure out how binary assets should be handled.
+ String readAsString();
+
+ /// Streams the contents of the asset.
+ Stream<List<int>> read();
+
+ /// Serializes this [Asset] to an object that can be sent across isolates
+ /// and passed to [deserialize].
+ Object serialize();
+}
+
+/// An asset backed by a file on the local file system.
+class _FileAsset implements Asset {
+ final File _file;
+ _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.
+// TODO(rnystrom): Have something similar for in-memory binary assets.
+class _StringAsset implements Asset {
+ final String _contents;
+
+ _StringAsset(this._contents);
+
+ String readAsString() => _contents;
+
+ // TODO(rnystrom): Implement this and handle encoding.
+ Stream<List<int>> read() => throw new UnimplementedError();
+
+ 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");
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698