Skip to content

Commit

Permalink
Enables removing per version, removing on system repo and removing la…
Browse files Browse the repository at this point in the history
…st version. Closes OpenWrap#45.
  • Loading branch information
serialseb committed Oct 20, 2010
1 parent fe82f4a commit e978c29
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 16 deletions.
12 changes: 9 additions & 3 deletions src/OpenWrap.Commands/CommandDocumentation.resx
Expand Up @@ -256,6 +256,9 @@ Note that the NuPack support is only provided for backward compatibility with le
<data name="remove-wrap" xml:space="preserve">
<value>Removes a package from local repositories.</value>
</data>
<data name="remove-wrap-last" xml:space="preserve">
<value>Specifies that the last version of the package is to be removed.</value>
</data>
<data name="remove-wrap-name" xml:space="preserve">
<value>Specifies the name of the package to remove from the local repository.</value>
</data>
Expand All @@ -265,17 +268,20 @@ Note that the NuPack support is only provided for backward compatibility with le
<data name="remove-wrap-system" xml:space="preserve">
<value>Specifies that the package is to be removed from the system repository. If specified, the project repository will not be affected.</value>
</data>
<data name="remove-wrap-version" xml:space="preserve">
<value>Specifies the exact version of the package to remove.</value>
</data>
<data name="set-wrap" xml:space="preserve">
<value>Updates a wrap dependency in the project repository.</value>
</data>
<data name="set-wrap-anchored" xml:space="preserve">
<value>Either true or false, sets the anchored flag</value>
<value>Either true or false, sets the anchored flag.</value>
</data>
<data name="set-wrap-anyversion" xml:space="preserve">
<value>Removes any existing version constraints</value>
<value>Removes any existing version constraints.</value>
</data>
<data name="set-wrap-content" xml:space="preserve">
<value>Either true or false, sets the content flag</value>
<value>Either true or false, sets the content flag.</value>
</data>
<data name="set-wrap-maxversion" xml:space="preserve">
<value>The exclusive, upper-bound of the dependency version</value>
Expand Down
70 changes: 57 additions & 13 deletions src/OpenWrap.Commands/Wrap/RemoveWrapCommand.cs
Expand Up @@ -9,9 +9,8 @@
namespace OpenWrap.Commands.Wrap
{
[Command(Verb = "remove", Noun = "wrap")]
public class RemoveWrapCommand : ICommand
public class RemoveWrapCommand : WrapCommand
{
// TODO: Need to be able to remove packages from the system repository
[CommandInput(IsRequired = true, Position = 0)]
public string Name { get; set; }

Expand All @@ -27,22 +26,54 @@ public bool Project
[CommandInput]
public bool System { get; set; }

public IEnumerable<ICommandOutput> Execute()
[CommandInput]
public Version Version { get; set; }

[CommandInput]
public bool Last { get; set; }

public override IEnumerable<ICommandOutput> Execute()
{
return Either(VerifyInputs()).Or(ExecuteCore());
}

IEnumerable<ICommandOutput> VerifyInputs()
{
if (Version != null && Last)
yield return new Error("Cannot use '-Last' and '-Version' together.");
if (System && !Environment.SystemRepository.PackagesByName[Name].Any())
yield return new Error("Cannot find package named '{0}' in system repository.", Name);
if (Project && Environment.ProjectRepository == null)
yield return new Error("Not in a pacakge directory.");
}

IEnumerable<ICommandOutput> ExecuteCore()
{
if (Project)
foreach(var m in RemoveFromProjectRepository()) yield return m;
if (System)
foreach(var m in RemoveFromSystemRepository()) yield return m;
foreach(var m in PackageManager.VerifyPackageCache(Environment, Environment.Descriptor))
yield return m;
}

IEnumerable<ICommandOutput> RemoveFromSystemRepository()
{
var systemRepository = (ISupportCleaning)Environment.SystemRepository;

return systemRepository.Clean(systemRepository.PackagesByName
.SelectMany(x => x)
.Where(PackageShouldBeKept))
.Select(x=>PackageRemovedMessage(x));

return RemoveFromRepository(systemRepository);
}

IEnumerable<ICommandOutput> RemoveFromRepository(ISupportCleaning repository)
{
if (Last)
Version = repository.PackagesByName[Name].Select(x=>x.Version)
.OrderByDescending(x => x)
.FirstOrDefault();
return repository.Clean(repository.PackagesByName
.SelectMany(x => x)
.Where(PackageShouldBeKept))
.Select(PackageRemovedMessage);
}

ICommandOutput PackageRemovedMessage(PackageCleanResult packageCleanResult)
Expand All @@ -54,10 +85,27 @@ ICommandOutput PackageRemovedMessage(PackageCleanResult packageCleanResult)

bool PackageShouldBeKept(IPackageInfo packageInfo)
{
return packageInfo.Name.EqualsNoCase(Name) == false;
bool matchesName = packageInfo.Name.EqualsNoCase(Name);
bool matchesVersion = Version == null ? true : packageInfo.Version == Version;
return !(matchesName && matchesVersion);
}

IEnumerable<ICommandOutput> RemoveFromProjectRepository()
{
return Version == null ? RemoveFromDescriptor() : RemovePackageFilesFromProjectRepo();
}

IEnumerable<ICommandOutput> RemovePackageFilesFromProjectRepo()
{
yield return
new Warning(
"You specified a version to remove from your project. Your descriptor will not be updated, and the package files will be removed. If you want to change what version of a pacakge you depend on, use the 'set-wrap' command.")
;
foreach (var m in RemoveFromRepository((ISupportCleaning)Environment.ProjectRepository))
yield return m;
}

IEnumerable<ICommandOutput> RemoveFromDescriptor()
{
var dependency = FindProjectDependencyByName();
if (dependency == null)
Expand All @@ -78,9 +126,5 @@ PackageDependency FindProjectDependencyByName()
: null;
}

static IEnvironment Environment
{
get { return Services.Services.GetService<IEnvironment>(); }
}
}
}
122 changes: 122 additions & 0 deletions src/OpenWrap.Tests/Commands/Wrap/removeWrapCommand.cs
Expand Up @@ -3,12 +3,134 @@
using System.Linq;
using NUnit.Framework;
using OpenFileSystem.IO;
using OpenWrap.Commands;
using OpenWrap.Commands.Wrap;
using OpenWrap.Dependencies;
using OpenWrap.Testing;

namespace OpenWrap.Tests.Commands
{
public class remove_from_project_when_not_in_project : context.remove_wrap
{
public remove_from_project_when_not_in_project()
{
given_project_repository(null);
when_executing_command("saruman");
}
[Test]
public void an_error_is_triggered()
{
Results.ShouldHaveError();
}
}
public class remove_unknown_project_package : context.remove_wrap
{
public remove_unknown_project_package()
{
when_executing_command("saruman");
}
[Test]
public void an_error_is_triggered()
{
Results.ShouldHaveError();
}
}
public class remove_unknown_system_package : context.remove_wrap
{
public remove_unknown_system_package()
{
when_executing_command("saruman", "-system");
}
[Test]
public void an_error_is_triggered()
{
Results.ShouldHaveError();
}
}
public class removing_last_version : context.remove_wrap
{
public removing_last_version()
{
given_system_package("saruman", "1.0.0.0".ToVersion());
given_system_package("saruman", "1.0.0.1".ToVersion());
when_executing_command("saruman","-system", "-last");
}
[Test]
public void packge_is_removed()
{
Environment.SystemRepository.ShouldNotHavePackage("saruman", "1.0.0.1");
}
[Test]
public void earlier_versions_are_preserved()
{
Environment.SystemRepository.ShouldHavePackage("saruman", "1.0.0.0");
}
}
public class removing_wrap_by_version_and_last : context.remove_wrap
{
public removing_wrap_by_version_and_last()
{
given_system_package("saruman", "1.0.0.0".ToVersion());
when_executing_command("saruman", "-version", "1.0.0.0", "-last");
}
[Test]
public void error_is_displayed()
{
Results.ShouldHaveError();
}
}
public class removing_wrap_by_version_in_system : context.remove_wrap
{
public removing_wrap_by_version_in_system()
{
given_system_package("saruman", "1.0.0.0".ToVersion());
given_system_package("saruman", "1.0.0.1".ToVersion());
when_executing_command("saruman", "-system", "-version", "1.0.0.1");
}
[Test]
public void version_is_removed()
{
Environment.SystemRepository
.ShouldNotHavePackage("saruman", "1.0.0.1");
}
[Test]
public void other_versions_not_removed()
{
Environment.SystemRepository
.ShouldHavePackage("saruman", "1.0.0.0");
}
}
public class removing_wrap_by_version_in_project : context.remove_wrap
{
public removing_wrap_by_version_in_project()
{
given_dependency("depends: saruman");
given_project_package("saruman", "1.0.0.0".ToVersion());
given_project_package("saruman", "1.0.0.1".ToVersion());
when_executing_command("saruman", "-project", "-version", "1.0.0.0");
}
[Test]
public void version_is_removed()
{
Environment.ProjectRepository.ShouldNotHavePackage("saruman", "1.0.0.0");
}
[Test]
public void other_versions_are_not_removed()
{
Environment.ProjectRepository.ShouldHavePackage("saruman", "1.0.0.1");
}
[Test]
public void descriptor_is_not_updated()
{
Environment.Descriptor.Dependencies.ShouldHaveCountOf(1);
}
[Test]
public void warning_is_issued_about_descriptor_not_updated()
{
Results.OfType<Warning>()
.ShouldHaveCountOf(1);
}
}
public class removing_wrap_by_name_in_both_system_and_proejct : context.remove_wrap
{
public removing_wrap_by_name_in_both_system_and_proejct()
Expand Down

0 comments on commit e978c29

Please sign in to comment.