OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// Keeps track of locations of packages, and can create a `.packages` file. | 5 /// Keeps track of locations of packages, and can create a `.packages` file. |
6 // TODO(lrn): Also move packages/ directory management to this library. | 6 // TODO(lrn): Also move packages/ directory management to this library. |
7 library pub.package_locations; | 7 library pub.package_locations; |
8 | 8 |
9 import 'dart:async'; | |
10 | |
11 import 'package:package_config/packages_file.dart' as packages_file; | 9 import 'package:package_config/packages_file.dart' as packages_file; |
12 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
13 | 11 |
14 import 'package_graph.dart'; | 12 import 'package_graph.dart'; |
15 import 'io.dart'; | 13 import 'io.dart'; |
16 import 'log.dart' as log; | |
17 import 'utils.dart' show ordered; | 14 import 'utils.dart' show ordered; |
18 | 15 |
19 /// Creates a `.packages` file with the locations of the packages in [graph]. | 16 /// Creates a `.packages` file with the locations of the packages in [graph]. |
20 /// | 17 /// |
21 /// The file is written in the root directory of the entrypoint of [graph]. | 18 /// The file is written to [path], which defaults to the root directory of the |
| 19 /// entrypoint of [graph]. |
22 /// | 20 /// |
23 /// If the file already exists, it is deleted before the new content is written. | 21 /// If the file already exists, it is deleted before the new content is written. |
24 void writePackagesMap(PackageGraph graph) { | 22 void writePackagesMap(PackageGraph graph, [String path]) { |
25 var packagesFilePath = graph.entrypoint.root.path(".packages"); | 23 path ??= graph.entrypoint.root.path(".packages"); |
26 var content = _createPackagesMap(graph); | 24 var content = _createPackagesMap(graph); |
27 writeTextFile(packagesFilePath, content); | 25 writeTextFile(path, content); |
28 } | 26 } |
29 | 27 |
30 /// Template for header text put into `.packages` file. | 28 /// Template for header text put into `.packages` file. |
31 /// | 29 /// |
32 /// Contains the literal string `$now` which should be replaced by a timestamp. | 30 /// Contains the literal string `$now` which should be replaced by a timestamp. |
33 const _headerText = r""" | 31 const _headerText = r""" |
34 Generate by pub on $now. | 32 Generate by pub on $now. |
35 This file contains a map from Dart package names to Dart package locations. | 33 This file contains a map from Dart package names to Dart package locations. |
36 Dart tools, including the Dart VM and Dart analyzer, rely on the content. | 34 Dart tools, including the Dart VM and Dart analyzer, rely on the content. |
37 AUTO GENERATED - DO NOT EDIT | 35 AUTO GENERATED - DO NOT EDIT |
38 """; | 36 """; |
39 | 37 |
40 /// Returns the contents of the `.packages` file created from a package graph. | 38 /// Returns the contents of the `.packages` file created from a package graph. |
41 /// | 39 /// |
42 /// The paths in the generated `.packages` file are always absolute URIs. | 40 /// The paths in the generated `.packages` file are always absolute URIs. |
43 String _createPackagesMap(PackageGraph packageGraph) { | 41 String _createPackagesMap(PackageGraph packageGraph) { |
44 var header = _headerText.replaceFirst(r"$now", new DateTime.now().toString()); | 42 var header = _headerText.replaceFirst(r"$now", new DateTime.now().toString()); |
45 | 43 |
46 var packages = packageGraph.packages; | 44 var packages = packageGraph.packages; |
47 var uriMap = {}; | 45 var uriMap = {}; |
48 for (var packageName in ordered(packages.keys)) { | 46 for (var packageName in ordered(packages.keys)) { |
49 var location = packages[packageName].path("lib"); | 47 var package = packages[packageName]; |
| 48 |
| 49 // This indicates an in-memory package, which is presumably a fake |
| 50 // entrypoint we created for something like "pub global activate". We don't |
| 51 // need to import from it anyway, so we can just not add it to the map. |
| 52 if (package.dir == null) continue; |
| 53 |
| 54 var location = package.path("lib"); |
50 uriMap[packageName] = p.toUri(location); | 55 uriMap[packageName] = p.toUri(location); |
51 } | 56 } |
52 | 57 |
53 var text = new StringBuffer(); | 58 var text = new StringBuffer(); |
54 packages_file.write(text, uriMap, comment: header); | 59 packages_file.write(text, uriMap, comment: header); |
55 return text.toString(); | 60 return text.toString(); |
56 } | 61 } |
OLD | NEW |