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