From 63ad514857c82f58fbc34589b0bca808958e17d8 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum <nweiz@google.com> Date: Mon, 10 Aug 2015 12:46:02 -0700 Subject: [PATCH] Generate the package spec from the lockfile. The spec doesn't actually need all the data loaded from the package graph; all it needs are the locations of packages which can be looked up using their IDs and a SourceRegistry. R=rnystrom@google.com Review URL: https://codereview.chromium.org//1282083002 . --- lib/src/entrypoint.dart | 3 +- lib/src/global_packages.dart | 3 +- lib/src/lock_file.dart | 21 ++++++++++++ lib/src/package_locations.dart | 61 ---------------------------------- 4 files changed, 23 insertions(+), 65 deletions(-) delete mode 100644 lib/src/package_locations.dart diff --git a/lib/src/entrypoint.dart b/lib/src/entrypoint.dart index 7e90814c..10c76177 100644 --- a/lib/src/entrypoint.dart +++ b/lib/src/entrypoint.dart @@ -15,7 +15,6 @@ import 'lock_file.dart'; import 'log.dart' as log; import 'package.dart'; import 'package_graph.dart'; -import 'package_locations.dart'; import 'sdk.dart' as sdk; import 'solver/version_solver.dart'; import 'source/cached.dart'; @@ -156,7 +155,7 @@ class Entrypoint { log.exception(error, stackTrace); } - writePackagesMap(_packageGraph); + writeTextFile(packagesFile, lockFile.packagesFile(root.name)); } /// Precompile any transformed dependencies of the entrypoint. diff --git a/lib/src/global_packages.dart b/lib/src/global_packages.dart index aa9885b6..ac21e95e 100644 --- a/lib/src/global_packages.dart +++ b/lib/src/global_packages.dart @@ -19,7 +19,6 @@ import 'io.dart'; import 'lock_file.dart'; import 'log.dart' as log; import 'package.dart'; -import 'package_locations.dart'; import 'pubspec.dart'; import 'sdk.dart' as sdk; import 'solver/version_solver.dart'; @@ -177,7 +176,7 @@ class GlobalPackages { .loadPackageGraph(result); var snapshots = await _precompileExecutables(graph.entrypoint, dep.name); _writeLockFile(dep.name, lockFile); - writePackagesMap(graph, _getPackagesFilePath(dep.name)); + writeTextFile(_getPackagesFilePath(dep.name), lockFile.packagesFile()); _updateBinStubs(graph.packages[dep.name], executables, overwriteBinStubs: overwriteBinStubs, snapshots: snapshots); diff --git a/lib/src/lock_file.dart b/lib/src/lock_file.dart index d79a42e2..85316c2a 100644 --- a/lib/src/lock_file.dart +++ b/lib/src/lock_file.dart @@ -7,6 +7,7 @@ library pub.lock_file; import 'dart:collection'; import 'package:path/path.dart' as p; +import 'package:package_config/packages_file.dart' as packages_file; import 'package:pub_semver/pub_semver.dart'; import 'package:source_span/source_span.dart'; import 'package:yaml/yaml.dart'; @@ -152,6 +153,26 @@ class LockFile { return new LockFile._(packages, _sources); } + /// Returns the contents of the `.packages` file generated from this lockfile. + /// + /// If [entrypoint] is passed, a relative entry is added for its "lib/" + /// directory. + String packagesFile([String entrypoint]) { + var header = "Generated by pub on ${new DateTime.now()}."; + + var map = new Map.fromIterable(ordered(packages.keys), value: (name) { + var id = packages[name]; + var source = _sources[id.source]; + return p.toUri(p.join(source.getDirectory(id), "lib")); + }); + + if (entrypoint != null) map[entrypoint] = Uri.parse("lib/"); + + var text = new StringBuffer(); + packages_file.write(text, map, comment: header); + return text.toString(); + } + /// Returns the serialized YAML text of the lock file. /// /// [packageDir] is the containing directory of the root package, used to diff --git a/lib/src/package_locations.dart b/lib/src/package_locations.dart deleted file mode 100644 index 78ebb186..00000000 --- a/lib/src/package_locations.dart +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -/// Keeps track of locations of packages, and can create a `.packages` file. -// TODO(lrn): Also move packages/ directory management to this library. -library pub.package_locations; - -import 'package:package_config/packages_file.dart' as packages_file; -import 'package:path/path.dart' as p; - -import 'package_graph.dart'; -import 'io.dart'; -import 'utils.dart' show ordered; - -/// Creates a `.packages` file with the locations of the packages in [graph]. -/// -/// The file is written to [path], which defaults to the root directory of the -/// entrypoint of [graph]. -/// -/// If the file already exists, it is deleted before the new content is written. -void writePackagesMap(PackageGraph graph, [String path]) { - path ??= graph.entrypoint.root.path(".packages"); - var content = _createPackagesMap(graph); - writeTextFile(path, content); -} - -/// Template for header text put into `.packages` file. -/// -/// Contains the literal string `$now` which should be replaced by a timestamp. -const _headerText = r""" -Generate by pub on $now. -This file contains a map from Dart package names to Dart package locations. -Dart tools, including the Dart VM and Dart analyzer, rely on the content. -AUTO GENERATED - DO NOT EDIT -"""; - -/// Returns the contents of the `.packages` file created from a package graph. -/// -/// The paths in the generated `.packages` file are always absolute URIs. -String _createPackagesMap(PackageGraph packageGraph) { - var header = _headerText.replaceFirst(r"$now", new DateTime.now().toString()); - - var packages = packageGraph.packages; - var uriMap = {}; - for (var packageName in ordered(packages.keys)) { - var package = packages[packageName]; - - // This indicates an in-memory package, which is presumably a fake - // entrypoint we created for something like "pub global activate". We don't - // need to import from it anyway, so we can just not add it to the map. - if (package.dir == null) continue; - - var location = package.path("lib"); - uriMap[packageName] = p.toUri(location); - } - - var text = new StringBuffer(); - packages_file.write(text, uriMap, comment: header); - return text.toString(); -} -- GitLab