Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: lib/src/package_locations.dart

Issue 1282083002: Generate the package spec from the lockfile. (Closed) Base URL: git@github.com:dart-lang/pub.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« lib/src/lock_file.dart ('K') | « lib/src/lock_file.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
OLDNEW
« lib/src/lock_file.dart ('K') | « lib/src/lock_file.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698