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.command.build; | 5 library pub.command.build; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:barback/barback.dart'; | 9 import 'package:barback/barback.dart'; |
10 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 (dir) => environment.serveDirectory(dir))).then((_) { | 85 (dir) => environment.serveDirectory(dir))).then((_) { |
86 | 86 |
87 return environment.barback.getAllAssets(); | 87 return environment.barback.getAllAssets(); |
88 }); | 88 }); |
89 }).then((assets) { | 89 }).then((assets) { |
90 // Find all of the JS entrypoints we built. | 90 // Find all of the JS entrypoints we built. |
91 var dart2JSEntrypoints = assets | 91 var dart2JSEntrypoints = assets |
92 .where((asset) => asset.id.path.endsWith(".dart.js")) | 92 .where((asset) => asset.id.path.endsWith(".dart.js")) |
93 .map((asset) => asset.id); | 93 .map((asset) => asset.id); |
94 | 94 |
95 return Future.wait(assets.map(_writeAsset)).then((_) { | 95 return Future.wait(assets.map(_writeAsset)) |
96 builtFiles += _copyBrowserJsFiles(dart2JSEntrypoints); | 96 .then((_) => _copyBrowserJsFiles(dart2JSEntrypoints, assets)) |
97 .then((_) { | |
Bob Nystrom
2015/03/12 22:36:08
In pub, we tend to leave .then() hanging on the ri
Chris Bracken
2015/03/12 22:52:42
Done.
| |
97 log.message('Built $builtFiles ${pluralize('file', builtFiles)} ' | 98 log.message('Built $builtFiles ${pluralize('file', builtFiles)} ' |
98 'to "$outputDirectory".'); | 99 'to "$outputDirectory".'); |
99 | 100 |
100 log.json.message({ | 101 log.json.message({ |
101 "buildResult": "success", | 102 "buildResult": "success", |
102 "outputDirectory": outputDirectory, | 103 "outputDirectory": outputDirectory, |
103 "numFiles": builtFiles, | 104 "numFiles": builtFiles, |
104 "log": logJson | 105 "log": logJson |
105 }); | 106 }); |
106 }); | 107 }); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 Future _writeOutputFile(Asset asset, String relativePath) { | 184 Future _writeOutputFile(Asset asset, String relativePath) { |
184 builtFiles++; | 185 builtFiles++; |
185 var destPath = path.join(outputDirectory, relativePath); | 186 var destPath = path.join(outputDirectory, relativePath); |
186 ensureDir(path.dirname(destPath)); | 187 ensureDir(path.dirname(destPath)); |
187 return createFileFromStream(asset.read(), destPath); | 188 return createFileFromStream(asset.read(), destPath); |
188 } | 189 } |
189 | 190 |
190 /// If this package depends directly on the `browser` package, this ensures | 191 /// If this package depends directly on the `browser` package, this ensures |
191 /// that the JavaScript bootstrap files are copied into `packages/browser/` | 192 /// that the JavaScript bootstrap files are copied into `packages/browser/` |
192 /// directories next to each entrypoint in [entrypoints]. | 193 /// directories next to each entrypoint in [entrypoints]. |
193 /// | 194 Future _copyBrowserJsFiles(Iterable<AssetId> entrypoints, AssetSet assets) { |
Bob Nystrom
2015/03/12 22:36:08
Feel free to use async/await here to make your lif
| |
194 /// Returns the number of files it copied. | |
Bob Nystrom
2015/03/12 22:36:08
Can you preserve this behavior? It should be fairl
Chris Bracken
2015/03/12 22:52:42
Is there a good reason to? The new code uses _writ
Bob Nystrom
2015/03/13 16:20:21
Ack, my reading comprehension was bad here. Sorry.
| |
195 int _copyBrowserJsFiles(Iterable<AssetId> entrypoints) { | |
196 // Must depend on the browser package. | 195 // Must depend on the browser package. |
197 if (!entrypoint.root.immediateDependencies.any( | 196 if (!entrypoint.root.immediateDependencies.any( |
198 (dep) => dep.name == 'browser' && dep.source == 'hosted')) { | 197 (dep) => dep.name == 'browser' && dep.source == 'hosted')) { |
199 return 0; | 198 return new Future.value(); |
200 } | 199 } |
201 | 200 |
202 // Get all of the subdirectories that contain Dart entrypoints. | 201 // Get all of the subdirectories that contain Dart entrypoints. |
203 var entrypointDirs = entrypoints | 202 var entrypointDirs = entrypoints |
204 // Convert the asset path to a native-separated one and get the | 203 // Convert the asset path to a native-separated one and get the |
205 // directory containing the entrypoint. | 204 // directory containing the entrypoint. |
206 .map((id) => path.dirname(path.fromUri(id.path))) | 205 .map((id) => path.dirname(path.fromUri(id.path))) |
207 // Don't copy files to the top levels of the build directories since | 206 // Don't copy files to the top levels of the build directories since |
208 // the normal lib asset copying will take care of that. | 207 // the normal lib asset copying will take care of that. |
209 .where((dir) => path.split(dir).length > 1) | 208 .where((dir) => path.split(dir).length > 1) |
210 .toSet(); | 209 .toSet(); |
211 | 210 |
212 for (var dir in entrypointDirs) { | 211 var jsAssets = assets.where((asset) => |
212 asset.id.package == 'browser' && asset.id.extension == '.js'); | |
213 return Future.wait(entrypointDirs.expand((dir) { | |
213 // TODO(nweiz): we should put browser JS files next to any HTML file | 214 // TODO(nweiz): we should put browser JS files next to any HTML file |
214 // rather than any entrypoint. An HTML file could import an entrypoint | 215 // rather than any entrypoint. An HTML file could import an entrypoint |
215 // that's not adjacent. | 216 // that's not adjacent. |
216 _addBrowserJs(dir, "dart"); | 217 return jsAssets.map((asset) { |
217 _addBrowserJs(dir, "interop"); | 218 var jsPath = path.join(dir, _idToPath(asset.id)); |
218 } | 219 return _writeOutputFile(asset, jsPath); |
219 | 220 }); |
220 return entrypointDirs.length * 2; | 221 })); |
221 } | |
222 | |
223 // TODO(nweiz): do something more principled when issue 6101 is fixed. | |
224 /// Ensures that the [name].js file is copied into [directory] in [target], | |
225 /// under `packages/browser/`. | |
226 void _addBrowserJs(String directory, String name) { | |
227 var jsPath = entrypoint.root.path( | |
228 outputDirectory, directory, 'packages', 'browser', '$name.js'); | |
229 ensureDir(path.dirname(jsPath)); | |
230 | |
231 // TODO(rnystrom): This won't work if we get rid of symlinks and the top | |
232 // level "packages" directory. Will need to copy from the browser | |
233 // directory. | |
234 copyFile(path.join(entrypoint.packagesDir, 'browser', '$name.js'), jsPath); | |
235 } | 222 } |
236 | 223 |
237 /// Converts [entry] to a JSON object for use with JSON-formatted output. | 224 /// Converts [entry] to a JSON object for use with JSON-formatted output. |
238 Map _logEntryToJson(LogEntry entry) { | 225 Map _logEntryToJson(LogEntry entry) { |
239 var data = { | 226 var data = { |
240 "level": entry.level.name, | 227 "level": entry.level.name, |
241 "transformer": { | 228 "transformer": { |
242 "name": entry.transform.transformer.toString(), | 229 "name": entry.transform.transformer.toString(), |
243 "primaryInput": { | 230 "primaryInput": { |
244 "package": entry.transform.primaryId.package, | 231 "package": entry.transform.primaryId.package, |
(...skipping 17 matching lines...) Expand all Loading... | |
262 "end": { | 249 "end": { |
263 "line": entry.span.end.line, | 250 "line": entry.span.end.line, |
264 "column": entry.span.end.column | 251 "column": entry.span.end.column |
265 }, | 252 }, |
266 }; | 253 }; |
267 } | 254 } |
268 | 255 |
269 return data; | 256 return data; |
270 } | 257 } |
271 } | 258 } |
OLD | NEW |