public void PackagesConfigWriter_Basic() { MemoryStream stream = new MemoryStream(); using (PackagesConfigWriter writer = new PackagesConfigWriter(stream)) { writer.WriteMinClientVersion(NuGetVersion.Parse("3.0.1")); writer.WritePackageEntry("packageB", NuGetVersion.Parse("2.0.0"), NuGetFramework.Parse("portable-net45+win8")); writer.WritePackageEntry("packageA", NuGetVersion.Parse("1.0.1"), NuGetFramework.Parse("net45")); } stream.Seek(0, SeekOrigin.Begin); var xml = XDocument.Load(stream); Assert.Equal("utf-8", xml.Declaration.Encoding); PackagesConfigReader reader = new PackagesConfigReader(xml); Assert.Equal("3.0.1", reader.GetMinClientVersion().ToNormalizedString()); var packages = reader.GetPackages().ToArray(); Assert.Equal("packageA", packages[0].PackageIdentity.Id); Assert.Equal("packageB", packages[1].PackageIdentity.Id); Assert.Equal("1.0.1", packages[0].PackageIdentity.Version.ToNormalizedString()); Assert.Equal("2.0.0", packages[1].PackageIdentity.Version.ToNormalizedString()); Assert.Equal("net45", packages[0].TargetFramework.GetShortFolderName()); Assert.Equal("portable-net45+win8", packages[1].TargetFramework.GetShortFolderName()); }
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_Basic() { var stream = new MemoryStream(); using (PackagesConfigWriter writer = new PackagesConfigWriter(stream)) { writer.WriteMinClientVersion(NuGetVersion.Parse("3.0.1")); writer.WritePackageEntry("packageB", NuGetVersion.Parse("2.0.0"), NuGetFramework.Parse("portable-net45+win8")); writer.WritePackageEntry("packageA", NuGetVersion.Parse("1.0.1"), NuGetFramework.Parse("net45")); } stream.Seek(0, SeekOrigin.Begin); var xml = XDocument.Load(stream); Assert.Equal("utf-8", xml.Declaration.Encoding); PackagesConfigReader reader = new PackagesConfigReader(xml); Assert.Equal("3.0.1", reader.GetMinClientVersion().ToNormalizedString()); var packages = reader.GetPackages().ToArray(); Assert.Equal("packageA", packages[0].PackageIdentity.Id); Assert.Equal("packageB", packages[1].PackageIdentity.Id); Assert.Equal("1.0.1", packages[0].PackageIdentity.Version.ToNormalizedString()); Assert.Equal("2.0.0", packages[1].PackageIdentity.Version.ToNormalizedString()); Assert.Equal("net45", packages[0].TargetFramework.GetShortFolderName()); Assert.Equal("portable-net45+win8", packages[1].TargetFramework.GetShortFolderName()); }
public void PackagesConfigWriter_ThrowOnMissingPackagesNode() { // Arrange using (var folderPath = TestDirectory.Create()) { var filePath = Path.Combine(folderPath, "packages.config"); using (var fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write)) { using (var fileWriter = new StreamWriter(fileStream)) { var content = @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> </configuration> "; fileWriter.Write(content); } } using (var writer = new PackagesConfigWriter(filePath, false)) { // Assert Assert.Throws <PackagesConfigWriterException>(() => writer.AddPackageEntry("packageA", NuGetVersion.Parse("2.0.1"), NuGetFramework.Parse("net4"))); } } }
public void PackagesConfigWriter_NoOldPackagesConfigFileLeftOnDisk() { // Arrange using (var folderPath = TestDirectory.Create()) { var directoryInfo = new DirectoryInfo(folderPath); var filePath = Path.Combine(folderPath, "packages.config"); using (var fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write)) { using (var fileWriter = new StreamWriter(fileStream)) { var content = @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id = ""packageA"" version = ""1.0.0"" targetFramework = ""win81"" userInstalled = ""true"" protocolVersion = ""V2"" /> </packages>"; fileWriter.Write(content); } } using (var writer = new PackagesConfigWriter(filePath, false)) { // Act writer.AddPackageEntry("packageB", NuGetVersion.Parse("2.0.1"), NuGetFramework.Parse("net4")); } // Assert var packagesConfigFiles = directoryInfo.GetFiles(). Where(p => p.Name.ToLowerInvariant().Contains("packages.config")); Assert.Equal(1, packagesConfigFiles.Count()); } }
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()); } }
private void Writer(Action <PackagesConfigWriter> handler) { using (PackagesConfigWriter writer = new PackagesConfigWriter(ExtractFilePath, !File.Exists(ExtractFilePath))) { handler(writer); } }
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 void PackagesConfigWriter_BasicWithDifferentCulture() { var currentCulture = CultureInfo.CurrentCulture; var currentUICulture = CultureInfo.CurrentUICulture; try { var calendar = new CultureInfo("he-IL"); calendar.DateTimeFormat.Calendar = new HebrewCalendar(); CultureInfo.CurrentCulture = calendar; CultureInfo.CurrentUICulture = calendar; // Arrange using (var testFolder = TestDirectory.Create()) { var path = Path.Combine(testFolder + "packages.config"); // Act using (var writer = new PackagesConfigWriter(path, true)) { writer.WriteMinClientVersion(NuGetVersion.Parse("3.0.1")); writer.AddPackageEntry("packageB", NuGetVersion.Parse("2.0.0"), NuGetFramework.Parse("portable-net45+win8")); writer.AddPackageEntry("packageA", NuGetVersion.Parse("1.0.1"), NuGetFramework.Parse("net45")); } // Assert var xml = XDocument.Load(path); // Assert Assert.Equal("utf-8", xml.Declaration.Encoding); var reader = new PackagesConfigReader(xml); Assert.Equal("3.0.1", reader.GetMinClientVersion().ToNormalizedString()); var packages = reader.GetPackages().ToArray(); Assert.Equal("packageA", packages[0].PackageIdentity.Id); Assert.Equal("packageB", packages[1].PackageIdentity.Id); Assert.Equal("1.0.1", packages[0].PackageIdentity.Version.ToNormalizedString()); Assert.Equal("2.0.0", packages[1].PackageIdentity.Version.ToNormalizedString()); Assert.Equal("net45", packages[0].TargetFramework.GetShortFolderName()); Assert.Equal("portable-net45+win8", packages[1].TargetFramework.GetShortFolderName()); } } finally { CultureInfo.CurrentCulture = currentCulture; CultureInfo.CurrentUICulture = currentUICulture; } }
public void PackagesConfigWriter_Duplicate() { MemoryStream stream = new MemoryStream(); using (PackagesConfigWriter writer = new PackagesConfigWriter(stream)) { writer.WritePackageEntry("packageA", NuGetVersion.Parse("1.0.1"), NuGetFramework.Parse("net45")); Assert.Throws <PackagingException>(() => writer.WritePackageEntry("packageA", NuGetVersion.Parse("2.0.1"), NuGetFramework.Parse("net4"))); } }
protected virtual void UpdatePackageReferences( string packageReferenceFileName, Dictionary <PackageReference, PackageReference> packageReferencesToUpdate) { using (var writer = new PackagesConfigWriter(packageReferenceFileName, createNew: false)) { foreach (var entry in packageReferencesToUpdate) { writer.UpdatePackageEntry(entry.Key, entry.Value); } } }
public void PackagesConfigWriter_Duplicate() { var stream = new MemoryStream(); using (PackagesConfigWriter writer = new PackagesConfigWriter(stream)) { writer.WritePackageEntry("packageA", NuGetVersion.Parse("1.0.1"), NuGetFramework.Parse("net45")); Assert.Throws<PackagingException>(() => writer.WritePackageEntry("packageA", NuGetVersion.Parse("2.0.1"), NuGetFramework.Parse("net4"))); } }
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_OpenExistingFile() { // Arrange using (var folderPath = TestDirectory.Create()) { var filePath = Path.Combine(folderPath, "packages.config"); using (var fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write)) { using (var fileWriter = new StreamWriter(fileStream)) { var content = @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id = ""packageA"" version = ""1.0.0"" targetFramework = ""win81"" userInstalled = ""true"" protocolVersion = ""V2"" /> <package id = ""Microsoft.ApplicationInsights.PersistenceChannel"" version = ""0.14.3-build00177"" targetFramework = ""win81"" /> <package id = ""Microsoft.ApplicationInsights.WindowsApps"" version = ""0.14.3-build00177"" targetFramework = ""win81"" /> <package id = ""Microsoft.Diagnostics.Tracing.EventSource.Redist"" version = ""1.1.16-beta"" targetFramework = ""win81"" /> <package id = ""System.Numerics.Vectors"" version = ""4.0.0"" targetFramework = ""win81"" /> </packages>"; fileWriter.Write(content); } } using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite)) { using (var writer = new PackagesConfigWriter(stream, false)) { // Act var packageIdentityA1 = new PackageIdentity("packageA", NuGetVersion.Parse("1.0.0")); var packageReferenceA1 = new PackageReference(packageIdentityA1, NuGetFramework.Parse("win81"), userInstalled: true, developmentDependency: false, requireReinstallation: false); var packageIdentityA2 = new PackageIdentity("packageA", NuGetVersion.Parse("1.0.1")); var packageReferenceA2 = new PackageReference(packageIdentityA2, NuGetFramework.Parse("net45")); writer.UpdatePackageEntry(packageReferenceA1, packageReferenceA2); } // Assert stream.Seek(0, SeekOrigin.Begin); var xml = XDocument.Load(stream); // Assert Assert.Equal("utf-8", xml.Declaration.Encoding); var packageNode = xml.Descendants(PackagesConfig.PackageNodeName).FirstOrDefault(); Assert.Equal(packageNode.ToString(), "<package id=\"packageA\" version=\"1.0.1\" targetFramework=\"net45\" userInstalled=\"true\" protocolVersion=\"V2\" />"); } } }
public void PackagesConfigWriter_Duplicate() { // Arrange var stream = new MemoryStream(); // Act using (var writer = new PackagesConfigWriter(stream, true)) { writer.AddPackageEntry("packageA", NuGetVersion.Parse("1.0.1"), NuGetFramework.Parse("net45")); // Assert Assert.Throws <PackagesConfigWriterException>(() => writer.AddPackageEntry("packageA", NuGetVersion.Parse("2.0.1"), NuGetFramework.Parse("net4"))); } }
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"))); } }
public override Task <bool> InstallPackageAsync(PackageIdentity packageIdentity, Stream packageStream, INuGetProjectContext nuGetProjectContext, CancellationToken token) { if (packageIdentity == null) { throw new ArgumentNullException("packageIdentity"); } if (nuGetProjectContext == null) { throw new ArgumentNullException("nuGetProjectContext"); } var newPackageReference = new PackageReference(packageIdentity, TargetFramework); List <PackageReference> installedPackagesList = GetInstalledPackagesList(); var packageReferenceWithSameId = installedPackagesList.Where(p => p.PackageIdentity.Id.Equals(packageIdentity.Id, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (packageReferenceWithSameId != null) { if (packageReferenceWithSameId.PackageIdentity.Equals(packageIdentity)) { nuGetProjectContext.Log(MessageLevel.Warning, Strings.PackageAlreadyExistsInPackagesConfig, packageIdentity, Path.GetFileName(FullPath)); return(Task.FromResult(false)); } else { // Higher version of an installed package is being installed. Remove old and add new installedPackagesList.Remove(packageReferenceWithSameId); installedPackagesList.Add(newPackageReference); } } else { installedPackagesList.Add(newPackageReference); } // Create new file or overwrite existing file using (var stream = FileSystemUtility.CreateFile(FullPath, nuGetProjectContext)) { var writer = new PackagesConfigWriter(stream); foreach (var pr in installedPackagesList) { writer.WritePackageEntry(pr); } writer.Close(); } nuGetProjectContext.Log(MessageLevel.Info, Strings.AddedPackageToPackagesConfig, packageIdentity, Path.GetFileName(FullPath)); return(Task.FromResult(true)); }
public void PackagesConfigWriter_UpdateAttributesFromOriginalConfig() { // Arrange using (var stream = new MemoryStream()) using (var stream2 = new MemoryStream()) { // Act using (var writer = new PackagesConfigWriter(stream, true)) { var vensionRange = new VersionRange(NuGetVersion.Parse("0.5.0")); var packageIdentityA = new PackageIdentity("packageA", NuGetVersion.Parse("1.0.1")); var packageReferenceA = new PackageReference(packageIdentityA, NuGetFramework.Parse("net45"), userInstalled: false, developmentDependency: false, requireReinstallation: true, allowedVersions: vensionRange); writer.AddPackageEntry(packageReferenceA); } stream.Seek(0, SeekOrigin.Begin); var xml = XDocument.Load(stream); var packageIdentityB = new PackageIdentity("packageA", NuGetVersion.Parse("3.0.1")); var packageReferenceB = new PackageReference(packageIdentityB, NuGetFramework.Parse("dnxcore50"), userInstalled: false, developmentDependency: false, requireReinstallation: false); using (var writer = new PackagesConfigWriter(stream2, true)) { writer.UpdateOrAddPackageEntry(xml, packageReferenceB); } stream2.Seek(0, SeekOrigin.Begin); var xml2 = XDocument.Load(stream2); var reader = new PackagesConfigReader(xml2); // Assert var packages = reader.GetPackages().ToArray(); Assert.Equal("1", packages.Count().ToString()); Assert.Equal("packageA", packages[0].PackageIdentity.Id); Assert.Equal("3.0.1", packages[0].PackageIdentity.Version.ToNormalizedString()); Assert.Equal("dnxcore50", packages[0].TargetFramework.GetShortFolderName()); // Verify allowedVersions attribute is kept after package update. Assert.Equal("[0.5.0, )", packages[0].AllowedVersions.ToNormalizedString()); // Verify that RequireReinstallation attribute is removed after package upate. Assert.Equal("False", packages[0].RequireReinstallation.ToString()); } }
/// <summary> /// Marks the packages to be reinstalled on the projects' packages.config /// </summary> public static async Task MarkPackagesForReinstallation(NuGetProject project, IList <PackageIdentity> packagesToBeReinstalled) { Debug.Assert(project != null); Debug.Assert(packagesToBeReinstalled != null); var installedPackageReferences = (await project.GetInstalledPackagesAsync(CancellationToken.None)).ToList(); var packageReferencesToUpdateReinstall = new Dictionary <Packaging.PackageReference, Packaging.PackageReference>(); if (installedPackageReferences != null && installedPackageReferences.Any()) { foreach (var packageReference in installedPackageReferences) { bool markForReinstall = packagesToBeReinstalled.Any(p => p.Equals(packageReference.PackageIdentity)); // Determine if requireReinstallation attribute needs to be updated. if (packageReference.RequireReinstallation ^ markForReinstall) { var newPackageReference = new Packaging.PackageReference(packageReference.PackageIdentity, packageReference.TargetFramework, packageReference.IsUserInstalled, packageReference.IsDevelopmentDependency, markForReinstall); packageReferencesToUpdateReinstall.Add(packageReference, newPackageReference); } } var projectFullPath = project.GetMetadata <string>(NuGetProjectMetadataKeys.FullPath); var packagesConfigFullPath = Path.Combine(projectFullPath ?? string.Empty, ProjectManagement.Constants.PackageReferenceFile); // Create new file or overwrite existing file if (File.Exists(packagesConfigFullPath)) { try { using (var writer = new PackagesConfigWriter(packagesConfigFullPath, createNew: false)) { foreach (var entry in packageReferencesToUpdateReinstall) { writer.UpdatePackageEntry(entry.Key, entry.Value); } } } catch (Exception ex) { ExceptionHelper.WriteErrorToActivityLog(ex); } } } }
public void PackagesConfigWriter_CreateEmptyFile() { // Arrange using (var testFolder = TestDirectory.Create()) { var path = Path.Combine(testFolder + "packages.config"); // Act using (var stream = File.Create(path)) using (var writer = new PackagesConfigWriter(stream, true)) { } // Assert var xml = XDocument.Load(path); Assert.NotNull(xml); } }
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"); } List <PackageReference> 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)); } installedPackagesList.Remove(packageReference); if (installedPackagesList.Count > 0) { // Create new file or overwrite existing file using (var stream = FileSystemUtility.CreateFile(FullPath, nuGetProjectContext)) { var writer = new PackagesConfigWriter(stream); foreach (var pr in installedPackagesList) { writer.WritePackageEntry(pr); } writer.Close(); } } else { FileSystemUtility.DeleteFile(FullPath, nuGetProjectContext); } nuGetProjectContext.Log(MessageLevel.Info, Strings.RemovedPackageFromPackagesConfig, packageIdentity, Path.GetFileName(FullPath)); return(Task.FromResult(true)); }
private void UpdatePackagesConfig(Project project, string packageName, string packageVersion) { var packagesFilePath = Path.Combine(project.DirectoryPath, "packages.config"); IEnumerable <PackageReference> entries = new List <PackageReference>(); if (File.Exists(packagesFilePath)) { using (var stream = new FileStream(packagesFilePath, FileMode.OpenOrCreate, FileAccess.Read)) { var reader = new PackagesConfigReader(stream, true); entries = reader.GetPackages(); stream.Close(); } } File.Delete(packagesFilePath); using (var stream = new FileStream(packagesFilePath, FileMode.Create, FileAccess.Write)) { using (var writer = new PackagesConfigWriter(stream)) { entries.ToList().ForEach(writer.WritePackageEntry); try { writer.WritePackageEntry(packageName, NuGetVersion.Parse(packageVersion), NuGetFramework.AnyFramework); } catch (PackagingException) { _traceWriter.Output("packages.config already contains references"); } writer.Close(); } stream.Close(); } }
public void PackagesConfigWriter_UpdateError() { // Arrange var stream = new MemoryStream(); // Act using (var writer = new PackagesConfigWriter(stream, true)) { var packageIdentityA = new PackageIdentity("packageA", NuGetVersion.Parse("1.0.1")); var packageReferenceA = new PackageReference(packageIdentityA, NuGetFramework.Parse("net45")); writer.AddPackageEntry(packageReferenceA); var packageIdentityB = new PackageIdentity("packageB", NuGetVersion.Parse("2.0.0")); var packageReferenceB = new PackageReference(packageIdentityB, NuGetFramework.Parse("portable-net45+win8")); var packageIdentityC = new PackageIdentity("packageC", NuGetVersion.Parse("1.0.1")); var packageReferenceC = new PackageReference(packageIdentityC, NuGetFramework.Parse("net45")); // Assert Assert.Throws <PackagesConfigWriterException>(() => writer.UpdatePackageEntry(packageReferenceB, packageReferenceC)); } }
public override async Task <bool> InstallPackageAsync( PackageIdentity packageIdentity, DownloadResourceResult downloadResourceResult, INuGetProjectContext nuGetProjectContext, CancellationToken token) { if (packageIdentity == null) { throw new ArgumentNullException("packageIdentity"); } if (nuGetProjectContext == null) { throw new ArgumentNullException("nuGetProjectContext"); } var isDevelopmentDependency = await CheckDevelopmentDependencyAsync(downloadResourceResult, token); var newPackageReference = new PackageReference( packageIdentity, TargetFramework, userInstalled: true, developmentDependency: isDevelopmentDependency, requireReinstallation: false); var installedPackagesList = GetInstalledPackagesList(); try { // Avoid modifying the packages.config file while it is being read // This can happen when VS API calls are made to get the list of // all installed packages. lock (_configLock) { // Packages.config exist at full path if (installedPackagesList.Any()) { var packageReferenceWithSameId = installedPackagesList.FirstOrDefault( p => p.PackageIdentity.Id.Equals(packageIdentity.Id, StringComparison.OrdinalIgnoreCase)); if (packageReferenceWithSameId != null) { if (packageReferenceWithSameId.PackageIdentity.Equals(packageIdentity)) { nuGetProjectContext.Log(MessageLevel.Warning, Strings.PackageAlreadyExistsInPackagesConfig, packageIdentity, Path.GetFileName(FullPath)); return(false); } // Higher version of an installed package is being installed. Remove old and add new using (var writer = new PackagesConfigWriter(FullPath, createNew: false)) { writer.UpdatePackageEntry(packageReferenceWithSameId, newPackageReference); } } else { using (var writer = new PackagesConfigWriter(FullPath, createNew: false)) { if (nuGetProjectContext.OriginalPackagesConfig == null) { // Write a new entry writer.AddPackageEntry(newPackageReference); } else { // Update the entry based on the original entry if it exists writer.UpdateOrAddPackageEntry(nuGetProjectContext.OriginalPackagesConfig, newPackageReference); } } } } // Create new packages.config file and add the package entry else { using (var writer = new PackagesConfigWriter(FullPath, createNew: true)) { if (nuGetProjectContext.OriginalPackagesConfig == null) { // Write a new entry writer.AddPackageEntry(newPackageReference); } else { // Update the entry based on the original entry if it exists writer.UpdateOrAddPackageEntry(nuGetProjectContext.OriginalPackagesConfig, newPackageReference); } } } } } catch (PackagesConfigWriterException ex) { throw new InvalidOperationException(string.Format( CultureInfo.CurrentCulture, Strings.ErrorWritingPackagesConfig, FullPath, ex.Message)); } nuGetProjectContext.Log(MessageLevel.Info, Strings.AddedPackageToPackagesConfig, packageIdentity, Path.GetFileName(FullPath)); return(true); }