From 4b2c6b23dea78b90672937a8faefddf87641d37e Mon Sep 17 00:00:00 2001 From: Xeevis Date: Mon, 1 Jul 2019 10:22:59 +0200 Subject: [PATCH 1/2] #1408 Refactor NugetReferenceReplaceStep --- .../Steps/NugetReferenceReplaceStep.cs | 37 +++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/NugetReferenceReplaceStep.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/NugetReferenceReplaceStep.cs index e3fb2e8b29..da33ab10c0 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/NugetReferenceReplaceStep.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/NugetReferenceReplaceStep.cs @@ -1,8 +1,7 @@ using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text.RegularExpressions; -using HtmlAgilityPack; +using System.Xml; using Volo.Abp.Cli.ProjectBuilding.Files; namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps @@ -49,28 +48,22 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps { Check.NotNull(content, nameof(content)); - var doc = new HtmlDocument(); + var doc = new XmlDocument() { PreserveWhitespace = true }; doc.Load(GenerateStreamFromString(content)); - var nodes = doc.DocumentNode.SelectNodes("//projectreference[@include]"); - - if (nodes == null) - { - return content; - } - - return ProcessReferenceNodes(nodes, content); + return ProcessReferenceNodes(doc, content); } - private string ProcessReferenceNodes(HtmlNodeCollection nodes, string content) + private string ProcessReferenceNodes(XmlDocument doc, string content) { - Check.NotNull(nodes, nameof(nodes)); Check.NotNull(content, nameof(content)); - foreach (var node in nodes) + var nodes = doc.SelectNodes("/Project/ItemGroup/ProjectReference[@Include]"); + + foreach (XmlNode node in nodes) { - var valueAttr = node.Attributes.FirstOrDefault(a => a.Name.ToLower() == "include"); + var valueAttr = node.Attributes["Include"]; // ReSharper disable once PossibleNullReferenceException : Can not be null because nodes are selected with include attribute filter in previous method if (valueAttr.Value.Contains($"{_companyNamePlaceHolder}.{_projectNamePlaceHolder}")) @@ -78,22 +71,20 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps continue; } - var newValue = ConvertToNugetReference(valueAttr.Value); + valueAttr.Value = ConvertToNugetReference(valueAttr.Value); - var oldLine = $" Date: Mon, 1 Jul 2019 13:59:08 +0200 Subject: [PATCH 2/2] Fix ProjectReference -> PackageReference --- .../Building/Steps/NugetReferenceReplaceStep.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/NugetReferenceReplaceStep.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/NugetReferenceReplaceStep.cs index da33ab10c0..d129bbde55 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/NugetReferenceReplaceStep.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/NugetReferenceReplaceStep.cs @@ -61,22 +61,27 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps var nodes = doc.SelectNodes("/Project/ItemGroup/ProjectReference[@Include]"); - foreach (XmlNode node in nodes) + foreach (XmlNode oldNode in nodes) { - var valueAttr = node.Attributes["Include"]; + var oldNodeIncludeValue = oldNode.Attributes["Include"].Value; // ReSharper disable once PossibleNullReferenceException : Can not be null because nodes are selected with include attribute filter in previous method - if (valueAttr.Value.Contains($"{_companyNamePlaceHolder}.{_projectNamePlaceHolder}")) + if (oldNodeIncludeValue.Contains($"{_companyNamePlaceHolder}.{_projectNamePlaceHolder}")) { continue; } - valueAttr.Value = ConvertToNugetReference(valueAttr.Value); + var newNode = doc.CreateElement("PackageReference"); + + var includeAttr = doc.CreateAttribute("Include"); + includeAttr.Value = ConvertToNugetReference(oldNodeIncludeValue); + newNode.Attributes.Append(includeAttr); var versionAttr = doc.CreateAttribute("Version"); versionAttr.Value = _latestNugetPackageVersion; + newNode.Attributes.Append(versionAttr); - node.Attributes.Append(versionAttr); + oldNode.ParentNode.ReplaceChild(newNode, oldNode); } return doc.OuterXml;