Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor towards mono repo support #73

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ end_of_line = lf
[*.cs]
csharp_style_namespace_declarations=file_scoped:error

dotnet_diagnostic.RCS1225.severity = none

# Code files
[*.{cs,csx,vb,vbx}]
indent_size = 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Versionize.Tests;

public class ProjectTests
public class MsBuildVersionSourceTests
{
[Fact]
public void ShouldThrowInCaseOfInvalidVersion()
Expand All @@ -20,7 +20,7 @@ public void ShouldThrowInCaseOfInvalidVersion()
var projectFilePath = Path.Join(tempDir, "test.csproj");
File.WriteAllText(projectFilePath, projectFileContents);

Should.Throw<InvalidOperationException>(() => Project.Create(projectFilePath));
Should.Throw<InvalidOperationException>(() => MsBuildVersionSource.Create(projectFilePath));
}

[Fact]
Expand All @@ -36,7 +36,7 @@ public void ShouldThrowInCaseOfInvalidXml()
var projectFilePath = Path.Join(tempDir, "test.csproj");
File.WriteAllText(projectFilePath, projectFileContents);

Should.Throw<InvalidOperationException>(() => Project.Create(projectFilePath));
Should.Throw<InvalidOperationException>(() => MsBuildVersionSource.Create(projectFilePath));
}

[Fact]
Expand All @@ -51,7 +51,7 @@ public void ShouldUpdateTheVersionElementOnly()
</Project>";
var projectFilePath = WriteProjectFile(tempDir, projectFileContents);

var project = Project.Create(projectFilePath);
var project = MsBuildVersionSource.Create(projectFilePath);
project.WriteVersion(new Version(2, 0, 0));

var versionedProjectContents = File.ReadAllText(projectFilePath);
Expand All @@ -69,8 +69,7 @@ public void ShouldNotBeVersionableIfNoVersionIsContainedInProjectFile()
</PropertyGroup>
</Project>");

var isVersionable = Project.IsVersionable(projectFilePath);
isVersionable.ShouldBeFalse();
MsBuildVersionSource.Create(projectFilePath).IsVersionable.ShouldBeFalse();
}

[Fact]
Expand All @@ -84,8 +83,7 @@ public void ShouldBeDetectedAsNotVersionableIfAnEmptyVersionIsContainedInProject
</PropertyGroup>
</Project>");

var isVersionable = Project.IsVersionable(projectFilePath);
isVersionable.ShouldBeFalse();
MsBuildVersionSource.Create(projectFilePath).IsVersionable.ShouldBeFalse();
}

private static string WriteProjectFile(string dir, string projectFileContents)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

namespace Versionize.Tests;

public class ProjectsTests
public class VersionSourcesTests
{
[Fact]
public void ShouldDiscoverAllProjects()
public void ShouldDiscoverAllVersionSources()
{
var tempDir = TempDir.Create();
TempProject.CreateCsharpProject(Path.Join(tempDir, "project1"));
TempProject.CreateCsharpProject(Path.Join(tempDir, "project2"));
TempProject.CreateVBProject(Path.Join(tempDir, "project3"));

var projects = Projects.Discover(tempDir);
projects.GetProjectFiles().Count().ShouldBe(3);
var versionSources = VersionSources.Discover(tempDir);
versionSources.Versionables.Count().ShouldBe(3);
}

[Fact]
Expand All @@ -26,8 +26,8 @@ public void ShouldDetectInconsistentVersions()
TempProject.CreateCsharpProject(Path.Join(tempDir, "project1"), "2.0.0");
TempProject.CreateCsharpProject(Path.Join(tempDir, "project2"), "1.1.1");

var projects = Projects.Discover(tempDir);
projects.HasInconsistentVersioning().ShouldBeTrue();
var versionSources = VersionSources.Discover(tempDir);
versionSources.HasInconsistentVersioning().ShouldBeTrue();
}

[Fact]
Expand All @@ -37,26 +37,26 @@ public void ShouldDetectConsistentVersions()
TempProject.CreateCsharpProject(Path.Join(tempDir, "project1"));
TempProject.CreateCsharpProject(Path.Join(tempDir, "project2"));

var projects = Projects.Discover(tempDir);
projects.HasInconsistentVersioning().ShouldBeFalse();
var versionSources = VersionSources.Discover(tempDir);
versionSources.HasInconsistentVersioning().ShouldBeFalse();
}

[Fact]
public void ShouldWriteAllVersionsToProjectFiles()
public void ShouldWriteAllVersionsToMsBuildProjects()
{
var tempDir = TempDir.Create();
TempProject.CreateCsharpProject(Path.Join(tempDir, "project1"), "1.1.1");
TempProject.CreateCsharpProject(Path.Join(tempDir, "project2"), "1.1.1");

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

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

[Fact]
public void ShouldDetectVersionInNamespacedXmlProjects()
public void ShouldDetectVersionInNamespacedMsBuildProject()
{
var tempDir = TempDir.Create();
var version = SemanticVersion.Parse("1.0.0");
Expand All @@ -73,7 +73,7 @@ public void ShouldDetectVersionInNamespacedXmlProjects()

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

var projects = Projects.Discover(tempDir);
projects.Version.ShouldBe(version);
var versionSources = VersionSources.Discover(tempDir);
versionSources.Version.ShouldBe(version);
}
}
11 changes: 11 additions & 0 deletions Versionize/IVersionSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using NuGet.Versioning;

namespace Versionize;

public interface IVersionSource
{
bool IsVersionable { get; }
SemanticVersion Version { get; }
string FilePath {get;}
void WriteVersion(SemanticVersion nextVersion);
}
48 changes: 24 additions & 24 deletions Versionize/Project.cs → Versionize/MsBuildVersionSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,35 @@

namespace Versionize;

public class Project
public class MsBuildVersionSource : IVersionSource
{
public string ProjectFile { get; }
public string FilePath { get; }
public SemanticVersion Version { get; }

private Project(string projectFile, SemanticVersion version)
public bool IsVersionable { get { return Version != null; } }

private MsBuildVersionSource(string projectFile, SemanticVersion version)
{
ProjectFile = projectFile;
FilePath = projectFile;
Version = version;
}

public static Project Create(string projectFile)
public static MsBuildVersionSource Create(string projectFile)
{
var version = ReadVersion(projectFile);

return new Project(projectFile, version);
}
SemanticVersion version = ReadVersion(projectFile);

public static bool IsVersionable(string projectFile)
{
try
{
ReadVersion(projectFile);
return true;
}
catch (Exception)
{
return false;
}
return new MsBuildVersionSource(projectFile, version);
}

private static SemanticVersion ReadVersion(string projectFile)
{
var doc = ReadProject(projectFile);
var doc = ReadProject(projectFile);

var versionString = SelectVersionNode(doc)?.InnerText;

if (string.IsNullOrWhiteSpace(versionString))
{
throw new InvalidOperationException($"Project {projectFile} contains no or an empty <Version> XML Element. Please add one if you want to version this project - for example use <Version>1.0.0</Version>");
return null;
}

try
Expand All @@ -55,14 +44,25 @@ private static SemanticVersion ReadVersion(string projectFile)
}
}

public static IEnumerable<IVersionSource> Discover(string workingDirectory)
{
var filters = new[] { "*.vbproj", "*.csproj", "*.fsproj" };

return filters.SelectMany(filter => Directory
.GetFiles(workingDirectory, filter, SearchOption.AllDirectories)
.Select(Create)
.ToList()
);
}

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

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

doc.Save(ProjectFile);
doc.Save(FilePath);
}

private static XmlNode SelectVersionNode(XmlDocument doc)
Expand Down
59 changes: 0 additions & 59 deletions Versionize/Projects.cs

This file was deleted.

48 changes: 48 additions & 0 deletions Versionize/VersionSources.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using NuGet.Versioning;

namespace Versionize;

public class VersionSources
{
private readonly IEnumerable<IVersionSource> _projects;

private VersionSources(IEnumerable<IVersionSource> projects)
{
_projects = projects;
}

public IEnumerable<IVersionSource> Versionables
{
get { return _projects.Where(p => p.IsVersionable); }
}

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

public bool HasInconsistentVersioning()
{
var firstProjectVersion = Versionables.FirstOrDefault()?.Version;

if (firstProjectVersion == null)
{
return true;
}

return Versionables.Any(p => !p.Version.Equals(firstProjectVersion));
}

public void WriteVersion(SemanticVersion nextVersion)
{
foreach (var versionSource in Versionables)
{
versionSource.WriteVersion(nextVersion);
}
}

public static VersionSources Discover(string workingDirectory)
{
var versionSources = new List<IVersionSource>();
versionSources.AddRange(MsBuildVersionSource.Discover(workingDirectory));

return new VersionSources(versionSources);
}
}