Esempio n. 1
0
        public void Uninstall(string packageId)
        {
            Ensure.NotNullOrEmpty(packageId, "packageId");

            NuGetVersion   version   = null;
            NuGetFramework framework = null;

            ReadPackageConfig(
                (p, cache) =>
            {
                if (string.Equals(p.PackageIdentity.Id, packageId, StringComparison.CurrentCultureIgnoreCase))
                {
                    version   = p.PackageIdentity.Version;
                    framework = p.TargetFramework;
                    return(Task.FromResult(true));
                }

                return(Task.FromResult(false));
            },
                default
                ).Wait();

            if (version == null)
            {
                return;
            }

            using (PackagesConfigWriter writer = new PackagesConfigWriter(ConfigFilePath, !File.Exists(ConfigFilePath)))
            {
                log.Debug($"Removing entry '{packageId}' from packages.config.");
                writer.RemovePackageEntry(packageId, version, framework);
            }
        }
        public void PackagesConfigWriter_Remove()
        {
            // Arrange
            using (var stream = new MemoryStream())
            {
                // Act
                using (var writer = new PackagesConfigWriter(stream, true))
                {
                    writer.AddPackageEntry("packageB", NuGetVersion.Parse("2.0.0"), NuGetFramework.Parse("portable-net45+win8"));

                    writer.AddPackageEntry("packageA", NuGetVersion.Parse("1.0.1"), NuGetFramework.Parse("net45"));

                    writer.RemovePackageEntry("packageB", NuGetVersion.Parse("2.0.0"), NuGetFramework.Parse("portable-net45+win8"));
                }

                stream.Seek(0, SeekOrigin.Begin);

                var xml = XDocument.Load(stream);

                // Assert
                Assert.Equal("utf-8", xml.Declaration.Encoding);

                var reader = new PackagesConfigReader(xml);

                var packages = reader.GetPackages().ToArray();
                Assert.Equal("1", packages.Count().ToString());
                Assert.Equal("packageA", packages[0].PackageIdentity.Id);
                Assert.Equal("1.0.1", packages[0].PackageIdentity.Version.ToNormalizedString());
                Assert.Equal("net45", packages[0].TargetFramework.GetShortFolderName());
            }
        }
Esempio n. 3
0
        public void Uninstall(IPackageIdentity package)
        {
            Ensure.NotNull(package, "package");

            using (PackagesConfigWriter writer = new PackagesConfigWriter(ConfigFilePath, !File.Exists(ConfigFilePath)))
            {
                log.Debug($"Removing entry '{package.ToIdentityString()}' from packages.config.");
                writer.RemovePackageEntry(package.Id, new NuGetVersion(package.Version), NuGetFramework.AnyFramework);
            }
        }
        public override Task <bool> UninstallPackageAsync(PackageIdentity packageIdentity, INuGetProjectContext nuGetProjectContext, CancellationToken token)
        {
            if (packageIdentity == null)
            {
                throw new ArgumentNullException("packageIdentity");
            }

            if (nuGetProjectContext == null)
            {
                throw new ArgumentNullException("nuGetProjectContext");
            }

            var installedPackagesList = GetInstalledPackagesList();
            var packageReference      = installedPackagesList.Where(p => p.PackageIdentity.Equals(packageIdentity)).FirstOrDefault();

            if (packageReference == null)
            {
                nuGetProjectContext.Log(MessageLevel.Warning, Strings.PackageDoesNotExisttInPackagesConfig, packageIdentity, Path.GetFileName(FullPath));
                return(Task.FromResult(false));
            }

            try
            {
                if (installedPackagesList.Any())
                {
                    // Matching packageReference is found and is the only entry
                    // Then just delete the packages.config file
                    if (installedPackagesList.Count == 1 && nuGetProjectContext.ActionType == NuGetActionType.Uninstall)
                    {
                        FileSystemUtility.DeleteFile(FullPath, nuGetProjectContext);
                    }
                    else
                    {
                        // Remove the package reference from packages.config file
                        using (var writer = new PackagesConfigWriter(FullPath, createNew: false))
                        {
                            writer.RemovePackageEntry(packageReference);
                        }
                    }
                }
            }
            catch (PackagesConfigWriterException ex)
            {
                throw new InvalidOperationException(string.Format(
                                                        CultureInfo.CurrentCulture,
                                                        Strings.ErrorWritingPackagesConfig,
                                                        FullPath,
                                                        ex.Message));
            }

            nuGetProjectContext.Log(MessageLevel.Info, Strings.RemovedPackageFromPackagesConfig, packageIdentity, Path.GetFileName(FullPath));
            return(Task.FromResult(true));
        }
        public void PackagesConfigWriter_RemoveError()
        {
            // Arrange
            var stream = new MemoryStream();

            // Act
            using (var writer = new PackagesConfigWriter(stream, true))
            {
                writer.AddPackageEntry("packageB", NuGetVersion.Parse("2.0.0"), NuGetFramework.Parse("portable-net45+win8"));

                // Assert
                Assert.Throws <PackagesConfigWriterException>(() => writer.RemovePackageEntry("packageA", NuGetVersion.Parse("2.0.1"), NuGetFramework.Parse("net4")));
            }
        }