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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 import 'package:async/async.dart'; | 8 import 'package:async/async.dart'; |
9 import 'package:barback/barback.dart'; | 9 import 'package:barback/barback.dart'; |
10 import 'package:collection/collection.dart'; | 10 import 'package:collection/collection.dart'; |
11 import 'package:path/path.dart' as path; | 11 import 'package:path/path.dart' as p; |
| 12 import 'package:package_resolver/package_resolver.dart'; |
12 import 'package:pub_semver/pub_semver.dart'; | 13 import 'package:pub_semver/pub_semver.dart'; |
13 | 14 |
14 import '../io.dart'; | 15 import '../io.dart'; |
15 import '../package.dart'; | 16 import '../package.dart'; |
16 import '../package_graph.dart'; | 17 import '../package_graph.dart'; |
17 import '../preprocess.dart'; | 18 import '../preprocess.dart'; |
18 import '../sdk.dart' as sdk; | 19 import '../sdk.dart' as sdk; |
19 | 20 |
20 /// The path to the lib directory of the compiler_unsupported package used by | 21 /// The path to the lib directory of the compiler_unsupported package used by |
21 /// pub. | 22 /// pub. |
22 /// | 23 /// |
23 /// This is used to make sure dart2js is running against its own version of its | 24 /// This is used to make sure dart2js is running against its own version of its |
24 /// internal libraries when running from the pub repo. It's `null` if we're | 25 /// internal libraries when running from the pub repo. It's `null` if we're |
25 /// running from the Dart repo or from the built SDK. | 26 /// running from the Dart repo or from the built SDK. |
26 final _compilerUnsupportedLib = (() { | 27 final Future<String> _compilerUnsupportedLib = (() async { |
27 if (runningFromSdk) return null; | 28 if (runningFromSdk) return null; |
28 if (runningFromDartRepo) return null; | 29 if (runningFromDartRepo) return null; |
29 | 30 |
30 // TODO(nweiz): When we switch over to ".packages", read the path from there | 31 return p.fromUri( |
31 // instead, or from the resource API if it's usable by that point. | 32 await PackageResolver.current.urlFor('compiler_unsupported')); |
32 return path.join(pubRoot, 'packages', 'compiler_unsupported'); | |
33 })(); | 33 })(); |
34 | 34 |
35 final _zlib = new ZLibCodec(); | 35 final _zlib = new ZLibCodec(); |
36 | 36 |
37 /// An implementation of barback's [PackageProvider] interface so that barback | 37 /// An implementation of barback's [PackageProvider] interface so that barback |
38 /// can find assets within pub packages. | 38 /// can find assets within pub packages. |
39 class PubPackageProvider implements StaticPackageProvider { | 39 class PubPackageProvider implements StaticPackageProvider { |
40 final PackageGraph _graph; | 40 final PackageGraph _graph; |
41 final List<String> staticPackages; | 41 final List<String> staticPackages; |
42 | 42 |
43 Iterable<String> get packages => | 43 Iterable<String> get packages => |
44 _graph.packages.keys.toSet().difference(staticPackages.toSet()); | 44 _graph.packages.keys.toSet().difference(staticPackages.toSet()); |
45 | 45 |
46 PubPackageProvider(PackageGraph graph) | 46 PubPackageProvider(PackageGraph graph) |
47 : _graph = graph, | 47 : _graph = graph, |
48 staticPackages = [r"$pub", r"$sdk"]..addAll( | 48 staticPackages = [r"$pub", r"$sdk"]..addAll( |
49 graph.packages.keys.where(graph.isPackageStatic)); | 49 graph.packages.keys.where(graph.isPackageStatic)); |
50 | 50 |
51 Future<Asset> getAsset(AssetId id) async { | 51 Future<Asset> getAsset(AssetId id) async { |
52 // "$pub" is a psuedo-package that allows pub's transformer-loading | 52 // "$pub" is a psuedo-package that allows pub's transformer-loading |
53 // infrastructure to share code with pub proper. | 53 // infrastructure to share code with pub proper. |
54 if (id.package == r'$pub') { | 54 if (id.package == r'$pub') { |
55 var components = path.url.split(id.path); | 55 var components = p.url.split(id.path); |
56 assert(components.isNotEmpty); | 56 assert(components.isNotEmpty); |
57 assert(components.first == 'lib'); | 57 assert(components.first == 'lib'); |
58 components[0] = 'dart'; | 58 components[0] = 'dart'; |
59 var file = assetPath(path.joinAll(components)); | 59 var file = assetPath(p.joinAll(components)); |
60 _assertExists(file, id); | 60 _assertExists(file, id); |
61 | 61 |
62 // Barback may not be in the package graph if there are no user-defined | 62 // Barback may not be in the package graph if there are no user-defined |
63 // transformers being used at all. The "$pub" sources are still provided, | 63 // transformers being used at all. The "$pub" sources are still provided, |
64 // but will never be loaded. | 64 // but will never be loaded. |
65 if (!_graph.packages.containsKey("barback")) { | 65 if (!_graph.packages.containsKey("barback")) { |
66 return new Asset.fromPath(id, file); | 66 return new Asset.fromPath(id, file); |
67 } | 67 } |
68 | 68 |
69 var versions = mapMap/*<String, Package, String, Version>*/( | 69 var versions = mapMap/*<String, Package, String, Version>*/( |
70 _graph.packages, | 70 _graph.packages, |
71 value: (_, package) => package.version); | 71 value: (_, package) => package.version); |
72 var contents = readTextFile(file); | 72 var contents = readTextFile(file); |
73 contents = preprocess(contents, versions, path.toUri(file)); | 73 contents = preprocess(contents, versions, p.toUri(file)); |
74 return new Asset.fromString(id, contents); | 74 return new Asset.fromString(id, contents); |
75 } | 75 } |
76 | 76 |
77 // "$sdk" is a pseudo-package that provides access to the Dart library | 77 // "$sdk" is a pseudo-package that provides access to the Dart library |
78 // sources in the SDK. The dart2js transformer uses this to locate the Dart | 78 // sources in the SDK. The dart2js transformer uses this to locate the Dart |
79 // sources for "dart:" libraries. | 79 // sources for "dart:" libraries. |
80 if (id.package == r'$sdk') { | 80 if (id.package == r'$sdk') { |
81 // The asset path contains two "lib" entries. The first represents pub's | 81 // The asset path contains two "lib" entries. The first represents pub's |
82 // concept that all public assets are in "lib". The second comes from the | 82 // concept that all public assets are in "lib". The second comes from the |
83 // organization of the SDK itself. Strip off the first. Leave the second | 83 // organization of the SDK itself. Strip off the first. Leave the second |
84 // since dart2js adds it and expects it to be there. | 84 // since dart2js adds it and expects it to be there. |
85 var parts = path.split(path.fromUri(id.path)); | 85 var parts = p.split(p.fromUri(id.path)); |
86 assert(parts.isNotEmpty && parts[0] == 'lib'); | 86 assert(parts.isNotEmpty && parts[0] == 'lib'); |
87 parts = parts.skip(1).toList(); | 87 parts = parts.skip(1).toList(); |
88 | 88 |
89 if (_compilerUnsupportedLib == null) { | 89 var compilerUnsupportedLib = await _compilerUnsupportedLib; |
90 var file = path.join(sdk.rootDirectory, path.joinAll(parts)); | 90 if (compilerUnsupportedLib == null) { |
| 91 var file = p.join(sdk.rootDirectory, p.joinAll(parts)); |
91 _assertExists(file, id); | 92 _assertExists(file, id); |
92 return new Asset.fromPath(id, file); | 93 return new Asset.fromPath(id, file); |
93 } | 94 } |
94 | 95 |
95 // If we're running from pub's repo, our version of dart2js comes from | 96 // If we're running from pub's repo, our version of dart2js comes from |
96 // compiler_unsupported and may expect different SDK sources than the | 97 // compiler_unsupported and may expect different SDK sources than the |
97 // actual SDK we're using. Handily, compiler_unsupported contains a full | 98 // actual SDK we're using. Handily, compiler_unsupported contains a full |
98 // (ZLib-encoded) copy of the SDK, so we load sources from that instead. | 99 // (ZLib-encoded) copy of the SDK, so we load sources from that instead. |
99 var file = path.join(_compilerUnsupportedLib, 'sdk', | 100 var file = p.join(compilerUnsupportedLib, 'sdk', |
100 path.joinAll(parts.skip(1))) + "_"; | 101 p.joinAll(parts.skip(1))) + "_"; |
101 _assertExists(file, id); | 102 _assertExists(file, id); |
102 return new Asset.fromStream(id, new LazyStream(() => | 103 return new Asset.fromStream(id, new LazyStream(() => |
103 _zlib.decoder.bind(new File(file).openRead()))); | 104 _zlib.decoder.bind(new File(file).openRead()))); |
104 } | 105 } |
105 | 106 |
106 var nativePath = path.fromUri(id.path); | 107 var nativePath = p.fromUri(id.path); |
107 var file = _graph.packages[id.package].path(nativePath); | 108 var file = _graph.packages[id.package].path(nativePath); |
108 _assertExists(file, id); | 109 _assertExists(file, id); |
109 return new Asset.fromPath(id, file); | 110 return new Asset.fromPath(id, file); |
110 } | 111 } |
111 | 112 |
112 /// Throw an [AssetNotFoundException] for [id] if [path] doesn't exist. | 113 /// Throw an [AssetNotFoundException] for [id] if [path] doesn't exist. |
113 void _assertExists(String path, AssetId id) { | 114 void _assertExists(String path, AssetId id) { |
114 if (!fileExists(path)) throw new AssetNotFoundException(id); | 115 if (!fileExists(path)) throw new AssetNotFoundException(id); |
115 } | 116 } |
116 | 117 |
117 Stream<AssetId> getAllAssetIds(String packageName) { | 118 Stream<AssetId> getAllAssetIds(String packageName) { |
118 if (packageName == r'$pub') { | 119 if (packageName == r'$pub') { |
119 // "$pub" is a pseudo-package that allows pub's transformer-loading | 120 // "$pub" is a pseudo-package that allows pub's transformer-loading |
120 // infrastructure to share code with pub proper. We provide it only during | 121 // infrastructure to share code with pub proper. We provide it only during |
121 // the initial transformer loading process. | 122 // the initial transformer loading process. |
122 var dartPath = assetPath('dart'); | 123 var dartPath = assetPath('dart'); |
123 return new Stream.fromIterable(listDir(dartPath, recursive: true) | 124 return new Stream.fromIterable(listDir(dartPath, recursive: true) |
124 // Don't include directories. | 125 // Don't include directories. |
125 .where((file) => path.extension(file) == ".dart") | 126 .where((file) => p.extension(file) == ".dart") |
126 .map((library) { | 127 .map((library) { |
127 var idPath = path.join('lib', path.relative(library, from: dartPath)); | 128 var idPath = p.join('lib', p.relative(library, from: dartPath)); |
128 return new AssetId('\$pub', path.toUri(idPath).toString()); | 129 return new AssetId('\$pub', p.toUri(idPath).toString()); |
129 })); | 130 })); |
130 } else if (packageName == r'$sdk') { | 131 } else if (packageName == r'$sdk') { |
131 // "$sdk" is a pseudo-package that allows the dart2js transformer to find | 132 return StreamCompleter.fromFuture(() async { |
132 // the Dart core libraries without hitting the file system directly. This | 133 var compilerUnsupportedLib = await _compilerUnsupportedLib; |
133 // ensures they work with source maps. | 134 // "$sdk" is a pseudo-package that allows the dart2js transformer to |
134 var libPath = _compilerUnsupportedLib == null | 135 // find the Dart core libraries without hitting the file system |
135 ? path.join(sdk.rootDirectory, "lib") | 136 // directly. This ensures they work with source maps. |
136 : path.join(_compilerUnsupportedLib, "sdk"); | 137 var libPath = compilerUnsupportedLib == null |
137 var files = listDir(libPath, recursive: true); | 138 ? p.join(sdk.rootDirectory, "lib") |
| 139 : p.join(compilerUnsupportedLib, "sdk"); |
| 140 var files = listDir(libPath, recursive: true); |
138 | 141 |
139 if (_compilerUnsupportedLib != null) { | 142 if (compilerUnsupportedLib != null) { |
140 // compiler_unsupported's SDK sources are ZLib-encoded; to indicate | 143 // compiler_unsupported's SDK sources are ZLib-encoded; to indicate |
141 // this, they end in "_". We serve them decoded, though, so we strip the | 144 // this, they end in "_". We serve them decoded, though, so we strip |
142 // underscore to get the asset paths. | 145 // the underscore to get the asset paths. |
143 var trailingUnderscore = new RegExp(r"_$"); | 146 var trailingUnderscore = new RegExp(r"_$"); |
144 files = files.map((file) => file.replaceAll(trailingUnderscore, "")); | 147 files = files.map((file) => file.replaceAll(trailingUnderscore, "")); |
145 } | 148 } |
146 | 149 |
147 return new Stream.fromIterable(files | 150 return new Stream.fromIterable(files |
148 .where((file) => path.extension(file) == ".dart") | 151 .where((file) => p.extension(file) == ".dart") |
149 .map((file) { | 152 .map((file) { |
150 var idPath = path.join("lib", "lib", | 153 var idPath = p.join("lib", "lib", |
151 path.relative(file, from: libPath)); | 154 p.relative(file, from: libPath)); |
152 return new AssetId('\$sdk', path.toUri(idPath).toString()); | 155 return new AssetId('\$sdk', p.toUri(idPath).toString()); |
153 })); | 156 })); |
| 157 }()); |
154 } else { | 158 } else { |
155 var package = _graph.packages[packageName]; | 159 var package = _graph.packages[packageName]; |
156 return new Stream.fromIterable( | 160 return new Stream.fromIterable( |
157 package.listFiles(beneath: 'lib').map((file) { | 161 package.listFiles(beneath: 'lib').map((file) { |
158 return new AssetId(packageName, | 162 return new AssetId(packageName, |
159 path.toUri(package.relative(file)).toString()); | 163 p.toUri(package.relative(file)).toString()); |
160 })); | 164 })); |
161 } | 165 } |
162 } | 166 } |
163 } | 167 } |
OLD | NEW |