public void UpdatePackageReferenceFromTransitiveDependency(RequiredNugetUpdate update, string solutionFolder) { ColorConsole.WriteEmbeddedColorLine($"Update {update.Library.Name}\t\t from [red]{update.Library.Version}[/red]" + $" to [green]{update.TargetVersion}[/green] [yellow](Transitive)[/yellow] in\t\t{Path.GetFileName(update.ProjectPath)}"); var projectName = Path.GetFileName(update.ProjectPath); var projectFolder = Path.GetDirectoryName(update.ProjectPath); if (projectFolder == null) { ColorConsole.WriteWarning("Nothing patched - project was null"); return; } var packageReferenceFile = Path.Combine(projectFolder, PROJECT_FILE_PROPS_FILE_NAME); var document = CreateOrOpenBuildPropsFile(packageReferenceFile); var itemGroup = document.Root?.Descendants("ItemGroup").FirstOrDefault(); if (itemGroup != null) { itemGroup.Add(new XComment($"Required to update transitive reference of {update.RootReferenceName}\r\n" + $"\t\t\tin {projectName} from {update.Library.Version}\r\n" + $"\t\t\tbecause {update.TargetVersion} is used in {update.UpateCausedBy}\r\n" + "\t\t"), new XElement("PackageReference", new XAttribute("Include", update.Library.Name), new XAttribute("Version", update.TargetVersion.ToString()))); using XmlWriter w = XmlWriter.Create(packageReferenceFile, new XmlWriterSettings() { OmitXmlDeclaration = true, Indent = true }); document.Save(w); } else { ColorConsole.WriteError($"File {packageReferenceFile} not patched - invalid. Run this tool again with clean flag"); } }
public void UpdateDirectPackageReference(RequiredNugetUpdate update) { ColorConsole.WriteEmbeddedColorLine($"Update {update.Library.Name}\t\t from [red]{update.Library.Version}[/red] to" + $" [green]{update.TargetVersion}[/green] [green](Direct)[/green] in\t\t{Path.GetFileName(update.ProjectPath)}"); var doc = XDocument.Load(update.ProjectPath); var element = doc.Root? .Descendants("PackageReference") .SingleOrDefault(e => e.Attribute("Include")?.Value.Equals(update.Library.Name, StringComparison.OrdinalIgnoreCase) == true); if (element != null) { var versionAttribute = element.Attribute("Version"); if (versionAttribute != null) { if (versionAttribute.Value != update.Library.Version.ToString()) { ColorConsole.WriteWarning( $"Expected version {update.Library.Version} but {versionAttribute.Value} found"); } } element.SetAttributeValue("Version", update.TargetVersion); doc.Save(update.ProjectPath); } else { ColorConsole.WriteError("No package reference node found"); } }