OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /// Keeps track of locations of packages, and can create a `.packages` file. | |
6 // TODO(lrn): Also move packages/ directory management to this library. | |
7 library pub.package_locations; | |
8 | |
9 import 'package:package_config/packages_file.dart' as packages_file; | |
10 import 'package:path/path.dart' as p; | |
11 | |
12 import 'package_graph.dart'; | |
13 import 'io.dart'; | |
14 import 'utils.dart' show ordered; | |
15 | |
16 /// Creates a `.packages` file with the locations of the packages in [graph]. | |
17 /// | |
18 /// The file is written to [path], which defaults to the root directory of the | |
19 /// entrypoint of [graph]. | |
20 /// | |
21 /// If the file already exists, it is deleted before the new content is written. | |
22 void writePackagesMap(PackageGraph graph, [String path]) { | |
23 path ??= graph.entrypoint.root.path(".packages"); | |
24 var content = _createPackagesMap(graph); | |
25 writeTextFile(path, content); | |
26 } | |
27 | |
28 /// Template for header text put into `.packages` file. | |
29 /// | |
30 /// Contains the literal string `$now` which should be replaced by a timestamp. | |
31 const _headerText = r""" | |
32 Generate by pub on $now. | |
33 This file contains a map from Dart package names to Dart package locations. | |
34 Dart tools, including the Dart VM and Dart analyzer, rely on the content. | |
35 AUTO GENERATED - DO NOT EDIT | |
36 """; | |
37 | |
38 /// Returns the contents of the `.packages` file created from a package graph. | |
39 /// | |
40 /// The paths in the generated `.packages` file are always absolute URIs. | |
41 String _createPackagesMap(PackageGraph packageGraph) { | |
42 var header = _headerText.replaceFirst(r"$now", new DateTime.now().toString()); | |
43 | |
44 var packages = packageGraph.packages; | |
45 var uriMap = {}; | |
46 for (var packageName in ordered(packages.keys)) { | |
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"); | |
55 uriMap[packageName] = p.toUri(location); | |
56 } | |
57 | |
58 var text = new StringBuffer(); | |
59 packages_file.write(text, uriMap, comment: header); | |
60 return text.toString(); | |
61 } | |
OLD | NEW |