Skip to content

Commit

Permalink
abpframework#1408 Refactor NugetReferenceReplaceStep
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeevis committed Jul 1, 2019
1 parent 309c5f3 commit 4b2c6b2
Showing 1 changed file with 14 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -49,51 +48,43 @@ private string ProcessFileContent(string content)
{
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}"))
{
continue;
}

var newValue = ConvertToNugetReference(valueAttr.Value);
valueAttr.Value = ConvertToNugetReference(valueAttr.Value);

var oldLine = $"<ProjectReference Include=\"{valueAttr.Value}\"";
var oldLineAlt = $"<ProjectReference Include=\"{valueAttr.Value}\"";
var newLine = $"<PackageReference Include=\"{newValue}\" Version=\"{_latestNugetPackageVersion}\"";
var versionAttr = doc.CreateAttribute("Version");
versionAttr.Value = _latestNugetPackageVersion;

content = content.Replace(oldLine, newLine);
content = content.Replace(oldLineAlt, newLine);
node.Attributes.Append(versionAttr);
}

return content;
return doc.OuterXml;
}

private string ConvertToNugetReference(string oldValue)
{
var newValue = Regex.Match(oldValue, @"\\((?!.+?\\).+?)\.csproj");
var newValue = Regex.Match(oldValue, @"\\((?!.+?\\).+?)\.csproj", RegexOptions.CultureInvariant | RegexOptions.Compiled);
if (newValue.Success && newValue.Groups.Count == 2)
{
return newValue.Groups[1].Value;
Expand Down

0 comments on commit 4b2c6b2

Please sign in to comment.