diff --git a/lib/src/global_packages.dart b/lib/src/global_packages.dart index c47e9799c90ce05f1feb349f52d60bea77296848..0baa6502ab66a71c47fa033d0cfefd07c42500eb 100644 --- a/lib/src/global_packages.dart +++ b/lib/src/global_packages.dart @@ -63,27 +63,24 @@ class GlobalPackages { /// Finds the latest version of the hosted package with [name] that matches /// [constraint] and makes it the active global version. Future activateHosted(String name, VersionConstraint constraint) { - // See if we already have it activated. - var lockFile = _describeActive(name); - var currentVersion; - if (lockFile != null) { - var id = lockFile.packages[name]; + _describeActive(name); - // Try to preserve the current version if we've already activated the - // hosted package. - if (id.source == "hosted") currentVersion = id.version; + var source = cache.sources["hosted"]; + return source.getVersions(name, name).then((versions) { + versions = versions.where(constraint.allows).toList(); - // Pull the root package out of the lock file so the solver doesn't see - // it. - lockFile.packages.remove(name); - } else { - lockFile = new LockFile.empty(); - } + if (versions.isEmpty) { + // TODO(rnystrom): Show most recent unmatching version? + dataError("Package ${log.bold(name)} has no versions that match " + "$constraint."); + } + + // Pick the best matching version. + versions.sort(Version.prioritize); - return _selectVersion(name, currentVersion, constraint).then((version) { // Make sure it's in the cache. - var id = new PackageId(name, "hosted", version, name); - return _installInCache(id, lockFile); + var id = new PackageId(name, "hosted", versions.last, name); + return _installInCache(id); }); } @@ -106,9 +103,8 @@ class GlobalPackages { }); } - /// Installs the package [id] with [lockFile] into the system cache along - /// with its dependencies. - Future _installInCache(PackageId id, LockFile lockFile) { + /// Installs the package [id] and its dependencies into the system cache. + Future _installInCache(PackageId id) { var source = cache.sources[id.source]; // Put the main package in the cache. @@ -123,8 +119,7 @@ class GlobalPackages { id = id_; // Resolve it and download its dependencies. - return resolveVersions(SolveType.GET, cache.sources, package, - lockFile: lockFile); + return resolveVersions(SolveType.GET, cache.sources, package); }); }).then((result) { if (!result.succeeded) throw result.error; @@ -172,11 +167,8 @@ class GlobalPackages { // user can run. } - /// Gets the lock file for the currently active package with [name]. - /// - /// Displays a message to the user about the current package, if any. Returns - /// the [LockFile] for the active package or `null` otherwise. - LockFile _describeActive(String package) { + /// Shows the user the currently active package with [name], if any. + void _describeActive(String package) { try { var lockFile = new LockFile.load(_getLockFilePath(package), cache.sources); @@ -190,8 +182,6 @@ class GlobalPackages { log.message("Package ${log.bold(package)} is currently active at " "version ${log.bold(id.version)}."); } - - return lockFile; } on IOException catch (error) { // If we couldn't read the lock file, it's not activated. return null; @@ -261,35 +251,6 @@ class GlobalPackages { }); } - /// Picks the best hosted version of [package] to activate that meets - /// [constraint]. - /// - /// If [version] is not `null`, this tries to maintain that version if - /// possible. - Future<Version> _selectVersion(String package, Version version, - VersionConstraint constraint) { - // If we already have a valid active version, just use it. - if (version != null && constraint.allows(version)) { - return new Future.value(version); - } - - // Otherwise, select the best version the matches the constraint. - var source = cache.sources["hosted"]; - return source.getVersions(package, package).then((versions) { - versions = versions.where(constraint.allows).toList(); - - if (versions.isEmpty) { - // TODO(rnystrom): Show most recent unmatching version? - dataError("Package ${log.bold(package)} has no versions that match " - "$constraint."); - } - - // Pick the best matching version. - versions.sort(Version.prioritize); - return versions.last; - }); - } - /// Gets the path to the lock file for an activated cached package with /// [name]. String _getLockFilePath(name) => p.join(_directory, name + ".lock"); diff --git a/test/global/activate/same_version_test.dart b/test/global/activate/ignores_active_version_test.dart similarity index 78% rename from test/global/activate/same_version_test.dart rename to test/global/activate/ignores_active_version_test.dart index 3d0209837b147f0c1e520b5f720b9f791d752287..36508558add05dc1db99f9299355c5ef088e432c 100644 --- a/test/global/activate/same_version_test.dart +++ b/test/global/activate/ignores_active_version_test.dart @@ -6,7 +6,7 @@ import '../../test_pub.dart'; main() { initConfig(); - integration('keeps previously activated version if it meets the constraint', + integration('ignores previously activated version', () { servePackages([ packageMap("foo", "1.2.3"), @@ -16,10 +16,11 @@ main() { // Activate 1.2.3. schedulePub(args: ["global", "activate", "foo", "1.2.3"]); - // Activating it again re-resolves but maintains the version. + // Activating it again resolves to the new best version. schedulePub(args: ["global", "activate", "foo", ">1.0.0"], output: """ Package foo is currently active at version 1.2.3. +Downloading foo 1.3.0... Resolving dependencies... -Activated foo 1.2.3."""); +Activated foo 1.3.0."""); }); }