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

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

Powered by Google App Engine
This is Rietveld 408576698