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 'dart:async'; |
| 10 |
| 11 import 'package:package_config/packages_file.dart' as packages_file; |
| 12 import 'package:path/path.dart' as p; |
| 13 |
| 14 import 'package_graph.dart'; |
| 15 import 'io.dart'; |
| 16 import 'log.dart' as log; |
| 17 import 'utils.dart' show ordered; |
| 18 |
| 19 /// Creates a `.packages` file with the locations of the packages in [graph]. |
| 20 /// |
| 21 /// The file is written in the root directory of the entrypoint of [graph]. |
| 22 /// |
| 23 /// If the file already exists, it is deleted before the new content is written. |
| 24 void writePackagesMap(PackageGraph graph) { |
| 25 var packagesFilePath = graph.entrypoint.root.path(".packages"); |
| 26 var content = _createPackagesMap(graph); |
| 27 writeTextFile(packagesFilePath, content); |
| 28 } |
| 29 |
| 30 /// Template for header text put into `.packages` file. |
| 31 /// |
| 32 /// Contains the literal string `$now` which should be replaced by a timestamp. |
| 33 const _headerText = r""" |
| 34 Generate by pub on $now. |
| 35 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. |
| 37 AUTO GENERATED - DO NOT EDIT |
| 38 """; |
| 39 |
| 40 /// Returns the contents of the `.packages` file created from a package graph. |
| 41 /// |
| 42 /// The paths in the generated `.packages` file are always absolute URIs. |
| 43 String _createPackagesMap(PackageGraph packageGraph) { |
| 44 var header = _headerText.replaceFirst(r"$now", new DateTime.now().toString()); |
| 45 |
| 46 var packages = packageGraph.packages; |
| 47 var uriMap = {}; |
| 48 for (var packageName in ordered(packages.keys)) { |
| 49 var location = packages[packageName].path("lib"); |
| 50 uriMap[packageName] = p.toUri(location); |
| 51 } |
| 52 |
| 53 var text = new StringBuffer(); |
| 54 packages_file.write(text, uriMap, comment: header); |
| 55 return text.toString(); |
| 56 } |
OLD | NEW |