Skip to content

Commit

Permalink
fix: detect versions in xml namespaced projects (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
saintedlama committed Feb 26, 2022
1 parent 1819c1c commit 7102a27
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 48 deletions.
30 changes: 26 additions & 4 deletions Versionize.Tests/ProjectsTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Shouldly;
using NuGet.Versioning;
using Shouldly;
using Versionize.Tests.TestSupport;
using Xunit;
using Version = NuGet.Versioning.SemanticVersion;

namespace Versionize.Tests;

Expand Down Expand Up @@ -48,9 +48,31 @@ public void ShouldWriteAllVersionsToProjectFiles()
TempProject.CreateCsharpProject(Path.Join(tempDir, "project2"), "1.1.1");

var projects = Projects.Discover(tempDir);
projects.WriteVersion(new Version(2, 0, 0));
projects.WriteVersion(new SemanticVersion(2, 0, 0));

var updated = Projects.Discover(tempDir);
updated.Version.ShouldBe(Version.Parse("2.0.0"));
updated.Version.ShouldBe(SemanticVersion.Parse("2.0.0"));
}

[Fact]
public void ShouldDetectVersionInNamespacedXmlProjects()
{
var tempDir = TempDir.Create();
var version = SemanticVersion.Parse("1.0.0");

// Create .net project
var projectFileContents =
$@"<Project ToolsVersion=""12.0"" DefaultTargets=""Build"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
<PropertyGroup>
...
<Version>{version}</Version>
...
</PropertyGroup>
</Project>";

TempProject.CreateFromProjectContents(tempDir, "csproj", projectFileContents);

var projects = Projects.Discover(tempDir);
projects.Version.ShouldBe(version);
}
}
30 changes: 12 additions & 18 deletions Versionize.Tests/TestSupport/TempProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,32 @@ public static string CreateFsharpProject(string tempDir, string version = "1.0.0
{
return Create(tempDir, "fsproj", version);
}

public static string CreateCsharpProject(string tempDir, string version = "1.0.0")
{
return Create(tempDir, "csproj", version);
}

private static string Create(string tempDir, string extension, string version = "1.0.0")
public static string Create(string tempDir, string extension, string version = "1.0.0")
{
Directory.CreateDirectory(tempDir);

var projectDirName = new DirectoryInfo(tempDir).Name;
var csProjFile = $"{tempDir}/{projectDirName}.{extension}";

// Create .net project
var projectFileContents =
$@"<Project Sdk=""Microsoft.NET.Sdk"">
$@"<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<Version>{version}</Version>
</PropertyGroup>
</Project>";
File.WriteAllText(csProjFile, projectFileContents);

// Add version string to csproj
var doc = new XmlDocument { PreserveWhitespace = true };
return CreateFromProjectContents(tempDir, extension, projectFileContents);
}

doc.Load(csProjFile);
public static string CreateFromProjectContents(string tempDir, string extension, string projectFileContents)
{
Directory.CreateDirectory(tempDir);

var projectNode = doc.SelectSingleNode("/Project/PropertyGroup");
var versionNode = doc.CreateNode("element", "Version", "");
versionNode.InnerText = version;
projectNode.AppendChild(versionNode);
using var tw = new XmlTextWriter(csProjFile, null);
doc.Save(tw);
var projectDirName = new DirectoryInfo(tempDir).Name;
var csProjFile = $"{tempDir}/{projectDirName}.{extension}";

File.WriteAllText(csProjFile, projectFileContents);

return csProjFile;
}
Expand Down
49 changes: 26 additions & 23 deletions Versionize/Project.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System.Xml;
using Version = NuGet.Versioning.SemanticVersion;
using NuGet.Versioning;

namespace Versionize;

public class Project
{
public string ProjectFile { get; }
public Version Version { get; }
public SemanticVersion Version { get; }

private Project(string projectFile, Version version)
private Project(string projectFile, SemanticVersion version)
{
ProjectFile = projectFile;
Version = version;
Expand All @@ -34,20 +34,11 @@ public static bool IsVersionable(string projectFile)
}
}

private static Version ReadVersion(string projectFile)
private static SemanticVersion ReadVersion(string projectFile)
{
XmlDocument doc = new XmlDocument { PreserveWhitespace = true };
var doc = ReadProject(projectFile);

try
{
doc.Load(projectFile);
}
catch (Exception)
{
throw new InvalidOperationException($"Project {projectFile} is not a valid csproj file. Please make sure that you have a valid csproj file in place!");
}

var versionString = doc.SelectSingleNode("/Project/PropertyGroup/Version")?.InnerText;
var versionString = SelectVersionNode(doc)?.InnerText;

if (string.IsNullOrWhiteSpace(versionString))
{
Expand All @@ -56,30 +47,42 @@ private static Version ReadVersion(string projectFile)

try
{
return Version.Parse(versionString);
return SemanticVersion.Parse(versionString);
}
catch (Exception)
{
throw new InvalidOperationException($"Project {projectFile} contains an invalid version {versionString}. Please fix the currently contained version - for example use <Version>1.0.0</Version>");
}
}

public void WriteVersion(Version nextVersion)
public void WriteVersion(SemanticVersion nextVersion)
{
var doc = ReadProject(ProjectFile);

var versionElement = SelectVersionNode(doc);
versionElement.InnerText = nextVersion.ToString();

doc.Save(ProjectFile);
}

private static XmlNode SelectVersionNode(XmlDocument doc)
{
return doc.SelectSingleNode("/*[local-name()='Project']/*[local-name()='PropertyGroup']/*[local-name()='Version']");
}

private static XmlDocument ReadProject(string projectFile)
{
var doc = new XmlDocument { PreserveWhitespace = true };

try
{
doc.Load(ProjectFile);
doc.Load(projectFile);
}
catch (Exception)
{
throw new InvalidOperationException($"Project {ProjectFile} is not a valid csproj file. Please make sure that you have a valid csproj file in place!");
throw new InvalidOperationException($"Project {projectFile} is not a valid xml project file. Please make sure that you have a valid project file in place!");
}

var versionElement = doc.SelectSingleNode("/Project/PropertyGroup/Version");
versionElement.InnerText = nextVersion.ToString();

doc.Save(ProjectFile);
return doc;
}
}
6 changes: 3 additions & 3 deletions Versionize/Projects.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Version = NuGet.Versioning.SemanticVersion;
using NuGet.Versioning;

namespace Versionize;

Expand Down Expand Up @@ -28,7 +28,7 @@ public bool HasInconsistentVersioning()
return _projects.Any(p => !p.Version.Equals(firstProjectVersion));
}

public Version Version { get => _projects.First().Version; }
public SemanticVersion Version { get => _projects.First().Version; }

public static Projects Discover(string workingDirectory)
{
Expand All @@ -44,7 +44,7 @@ public static Projects Discover(string workingDirectory)
return new Projects(projects);
}

public void WriteVersion(Version nextVersion)
public void WriteVersion(SemanticVersion nextVersion)
{
foreach (var project in _projects)
{
Expand Down

0 comments on commit 7102a27

Please sign in to comment.