| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library pub.global_packages; | 5 library pub.global_packages; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 // Call this just to log what the current active package is, if any. | 133 // Call this just to log what the current active package is, if any. |
| 134 _describeActive(name); | 134 _describeActive(name); |
| 135 | 135 |
| 136 // Write a lockfile that points to the local package. | 136 // Write a lockfile that points to the local package. |
| 137 var fullPath = canonicalize(entrypoint.root.dir); | 137 var fullPath = canonicalize(entrypoint.root.dir); |
| 138 var id = new PackageId(name, "path", entrypoint.root.version, | 138 var id = new PackageId(name, "path", entrypoint.root.version, |
| 139 PathSource.describePath(fullPath)); | 139 PathSource.describePath(fullPath)); |
| 140 | 140 |
| 141 // TODO(rnystrom): Look in "bin" and display list of binaries that | 141 // TODO(rnystrom): Look in "bin" and display list of binaries that |
| 142 // user can run. | 142 // user can run. |
| 143 _writeLockFile(name, new LockFile([id])); | 143 _writeLockFile(name, new LockFile([id], cache.sources)); |
| 144 | 144 |
| 145 var binDir = p.join(_directory, name, 'bin'); | 145 var binDir = p.join(_directory, name, 'bin'); |
| 146 if (dirExists(binDir)) deleteEntry(binDir); | 146 if (dirExists(binDir)) deleteEntry(binDir); |
| 147 | 147 |
| 148 _updateBinStubs(entrypoint.root, executables, | 148 _updateBinStubs(entrypoint.root, executables, |
| 149 overwriteBinStubs: overwriteBinStubs); | 149 overwriteBinStubs: overwriteBinStubs); |
| 150 } | 150 } |
| 151 | 151 |
| 152 /// Installs the package [dep] and its dependencies into the system cache. | 152 /// Installs the package [dep] and its dependencies into the system cache. |
| 153 Future _installInCache(PackageDep dep, List<String> executables, | 153 Future _installInCache(PackageDep dep, List<String> executables, |
| 154 {bool overwriteBinStubs}) async { | 154 {bool overwriteBinStubs}) async { |
| 155 // Create a dummy package with just [dep] so we can do resolution on it. | 155 // Create a dummy package with just [dep] so we can do resolution on it. |
| 156 var root = new Package.inMemory(new Pubspec("pub global activate", | 156 var root = new Package.inMemory(new Pubspec("pub global activate", |
| 157 dependencies: [dep], sources: cache.sources)); | 157 dependencies: [dep], sources: cache.sources)); |
| 158 | 158 |
| 159 // Resolve it and download its dependencies. | 159 // Resolve it and download its dependencies. |
| 160 var result = await resolveVersions(SolveType.GET, cache.sources, root); | 160 var result = await resolveVersions(SolveType.GET, cache.sources, root); |
| 161 if (!result.succeeded) { | 161 if (!result.succeeded) { |
| 162 // If the package specified by the user doesn't exist, we want to | 162 // If the package specified by the user doesn't exist, we want to |
| 163 // surface that as a [DataError] with the associated exit code. | 163 // surface that as a [DataError] with the associated exit code. |
| 164 if (result.error.package != dep.name) throw result.error; | 164 if (result.error.package != dep.name) throw result.error; |
| 165 if (result.error is NoVersionException) dataError(result.error.message); | 165 if (result.error is NoVersionException) dataError(result.error.message); |
| 166 throw result.error; | 166 throw result.error; |
| 167 } | 167 } |
| 168 result.showReport(SolveType.GET); | 168 result.showReport(SolveType.GET); |
| 169 | 169 |
| 170 // Make sure all of the dependencies are locally installed. | 170 // Make sure all of the dependencies are locally installed. |
| 171 var ids = await Future.wait(result.packages.map(_cacheDependency)); | 171 var ids = await Future.wait(result.packages.map(_cacheDependency)); |
| 172 var lockFile = new LockFile(ids); | 172 var lockFile = new LockFile(ids, cache.sources); |
| 173 | 173 |
| 174 // Load the package graph from [result] so we don't need to re-parse all | 174 // Load the package graph from [result] so we don't need to re-parse all |
| 175 // the pubspecs. | 175 // the pubspecs. |
| 176 var graph = await new Entrypoint.inMemory(root, lockFile, cache) | 176 var graph = await new Entrypoint.inMemory(root, lockFile, cache) |
| 177 .loadPackageGraph(result); | 177 .loadPackageGraph(result); |
| 178 var snapshots = await _precompileExecutables(graph.entrypoint, dep.name); | 178 var snapshots = await _precompileExecutables(graph.entrypoint, dep.name); |
| 179 _writeLockFile(dep.name, lockFile); | 179 _writeLockFile(dep.name, lockFile); |
| 180 writePackagesMap(graph, _getPackagesFilePath(dep.name)); | 180 writePackagesMap(graph, _getPackagesFilePath(dep.name)); |
| 181 | 181 |
| 182 _updateBinStubs(graph.packages[dep.name], executables, | 182 _updateBinStubs(graph.packages[dep.name], executables, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 /// Finishes activating package [package] by saving [lockFile] in the cache. | 223 /// Finishes activating package [package] by saving [lockFile] in the cache. |
| 224 void _writeLockFile(String package, LockFile lockFile) { | 224 void _writeLockFile(String package, LockFile lockFile) { |
| 225 ensureDir(p.join(_directory, package)); | 225 ensureDir(p.join(_directory, package)); |
| 226 | 226 |
| 227 // TODO(nweiz): This cleans up Dart 1.6's old lockfile location. Remove it | 227 // TODO(nweiz): This cleans up Dart 1.6's old lockfile location. Remove it |
| 228 // when Dart 1.6 is old enough that we don't think anyone will have these | 228 // when Dart 1.6 is old enough that we don't think anyone will have these |
| 229 // lockfiles anymore (issue 20703). | 229 // lockfiles anymore (issue 20703). |
| 230 var oldPath = p.join(_directory, "$package.lock"); | 230 var oldPath = p.join(_directory, "$package.lock"); |
| 231 if (fileExists(oldPath)) deleteEntry(oldPath); | 231 if (fileExists(oldPath)) deleteEntry(oldPath); |
| 232 | 232 |
| 233 writeTextFile(_getLockFilePath(package), | 233 writeTextFile(_getLockFilePath(package), lockFile.serialize(cache.rootDir)); |
| 234 lockFile.serialize(cache.rootDir, cache.sources)); | |
| 235 | 234 |
| 236 var id = lockFile.packages[package]; | 235 var id = lockFile.packages[package]; |
| 237 log.message('Activated ${_formatPackage(id)}.'); | 236 log.message('Activated ${_formatPackage(id)}.'); |
| 238 } | 237 } |
| 239 | 238 |
| 240 /// Shows the user the currently active package with [name], if any. | 239 /// Shows the user the currently active package with [name], if any. |
| 241 void _describeActive(String name) { | 240 void _describeActive(String name) { |
| 242 try { | 241 try { |
| 243 var lockFile = new LockFile.load(_getLockFilePath(name), cache.sources); | 242 var lockFile = new LockFile.load(_getLockFilePath(name), cache.sources); |
| 244 var id = lockFile.packages[name]; | 243 var id = lockFile.packages[name]; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 } on IOException { | 296 } on IOException { |
| 298 // If we couldn't read the lock file, it's not activated. | 297 // If we couldn't read the lock file, it's not activated. |
| 299 dataError("No active package ${log.bold(name)}."); | 298 dataError("No active package ${log.bold(name)}."); |
| 300 } | 299 } |
| 301 | 300 |
| 302 // Move the old lockfile to its new location. | 301 // Move the old lockfile to its new location. |
| 303 ensureDir(p.dirname(lockFilePath)); | 302 ensureDir(p.dirname(lockFilePath)); |
| 304 new File(oldLockFilePath).renameSync(lockFilePath); | 303 new File(oldLockFilePath).renameSync(lockFilePath); |
| 305 } | 304 } |
| 306 | 305 |
| 307 // Load the package from the cache. | 306 // Remove the package itself from the lockfile. We put it in there so we |
| 307 // could find and load the [Package] object, but normally an entrypoint |
| 308 // doesn't expect to be in its own lockfile. |
| 308 var id = lockFile.packages[name]; | 309 var id = lockFile.packages[name]; |
| 309 lockFile.packages.remove(name); | 310 lockFile = lockFile.removePackage(name); |
| 310 | 311 |
| 311 var source = cache.sources[id.source]; | 312 var source = cache.sources[id.source]; |
| 312 if (source is CachedSource) { | 313 if (source is CachedSource) { |
| 313 // For cached sources, the package itself is in the cache and the | 314 // For cached sources, the package itself is in the cache and the |
| 314 // lockfile is the one we just loaded. | 315 // lockfile is the one we just loaded. |
| 315 var dir = cache.sources[id.source].getDirectory(id); | 316 var dir = cache.sources[id.source].getDirectory(id); |
| 316 var package = new Package.load(name, dir, cache.sources); | 317 var package = new Package.load(name, dir, cache.sources); |
| 317 return new Entrypoint.inMemory(package, lockFile, cache); | 318 return new Entrypoint.inMemory(package, lockFile, cache); |
| 318 } | 319 } |
| 319 | 320 |
| (...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 784 } | 785 } |
| 785 | 786 |
| 786 /// Returns the value of the property named [name] in the bin stub script | 787 /// Returns the value of the property named [name] in the bin stub script |
| 787 /// [source]. | 788 /// [source]. |
| 788 String _binStubProperty(String source, String name) { | 789 String _binStubProperty(String source, String name) { |
| 789 var pattern = new RegExp(quoteRegExp(name) + r": ([a-zA-Z0-9_-]+)"); | 790 var pattern = new RegExp(quoteRegExp(name) + r": ([a-zA-Z0-9_-]+)"); |
| 790 var match = pattern.firstMatch(source); | 791 var match = pattern.firstMatch(source); |
| 791 return match == null ? null : match[1]; | 792 return match == null ? null : match[1]; |
| 792 } | 793 } |
| 793 } | 794 } |
| OLD | NEW |