Chromium Code Reviews| 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 locatikons of packages, and can create a `.packages` file. | |
|
nweiz
2015/06/12 23:48:08
"locatikons" -> "locations"
Lasse Reichstein Nielsen
2015/06/23 14:54:33
Done.
| |
| 6 // TODO(lrn): Also move packages/ directory management to this library. | |
| 7 library pub.package_locations; | |
| 8 import"dart:async"; | |
| 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 'log.dart' as log; | |
| 15 import 'utils.dart' show ordered; | |
| 16 | |
| 17 /// Creates a `.packages` file with the locations of the packages in [graph]. | |
| 18 /// | |
| 19 /// The file is written in the root directory of the entrypoint of [graph]. | |
| 20 /// | |
| 21 /// If the file already exists, it is deleted before the new content is written. | |
| 22 void writePackagesMap(PackageGraph graph) { | |
| 23 var packagesFilePath = graph.entrypoint.root.path(".packages"); | |
| 24 deleteEntry(packagesFilePath); | |
|
nweiz
2015/06/12 23:48:08
Is this explicit deletion necessary? I'm pretty su
Lasse Reichstein Nielsen
2015/06/23 14:54:33
No idea. I was copying the packages/ directory beh
| |
| 25 var content = _createPackagesMap(graph); | |
| 26 try { | |
| 27 writeTextFile(packagesFilePath, content); | |
| 28 } catch (error, stackTrace) { | |
| 29 log.error("Unable to create .packages file", error, stackTrace); | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 /// Template for header text put into `.packages` file. | |
| 34 /// | |
| 35 /// Contains the literal string `$now` which should be replaced by a timestamp. | |
| 36 const _headerText = r""" | |
| 37 Generate by pub on $now. | |
| 38 This file contains a map from Dart package names to Dart package locations. | |
| 39 Dart tools, including the Dart VM and Dart analyzer, rely on the content. | |
| 40 AUTO GENERATED - DO NOT EDIT | |
| 41 """; | |
| 42 | |
| 43 /// Returns the contents of the `.packages` file created from a package graph. | |
| 44 /// | |
| 45 /// The paths in the generated `.packages` file are always absolute URIs. | |
| 46 String _createPackagesMap(PackageGraph packageGraph) { | |
| 47 var header = _headerText.replaceFirst(r"$now", new DateTime.now().toString()); | |
| 48 | |
| 49 var packages = packageGraph.packages; | |
| 50 var uriMap = {}; | |
| 51 for (var packageName in ordered(packages.keys)) { | |
| 52 var location = packages[packageName].path("lib"); | |
| 53 uriMap[packageName] = p.toUri(location); | |
| 54 } | |
| 55 | |
| 56 var text = new StringBuffer(); | |
| 57 packages_file.write(text, uriMap, comment: header); | |
| 58 return text.toString(); | |
| 59 } | |
| OLD | NEW |