Skip to content

Commit

Permalink
Added support for Directory.Packages.props
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyril Mulder authored and SteveDesmond-ca committed Feb 28, 2024
1 parent 61eb824 commit a9aca5f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/LibYear.Core/FileTypes/DirectoryPackagesPropsFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace LibYear.Core.FileTypes;

public class DirectoryPackagesPropsFile : XmlProjectFile
{
public DirectoryPackagesPropsFile(string filename, string contents) : base(filename, contents, "PackageVersion", new[] { "Include", "Update" }, "Version")
{
}
}
7 changes: 5 additions & 2 deletions src/LibYear.Core/ProjectFileManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.IO.Abstractions;
using System.Text;
using LibYear.Core.FileTypes;
using System.IO.Abstractions;

namespace LibYear.Core;

Expand Down Expand Up @@ -49,13 +48,15 @@ private IReadOnlyCollection<Task<IProjectFile>> FindProjects(IDirectoryInfo dir,
.Union(dir.EnumerateFiles("Directory.build.props", searchMode))
.Union(dir.EnumerateFiles("Directory.build.targets", searchMode))
.Union(dir.EnumerateFiles("packages.config", searchMode))
.Union(dir.EnumerateFiles("Directory.packages.props", searchMode))
.Select(ReadFile)
.ToArray();

private static bool IsCsProjFile(IFileSystemInfo fileInfo) => fileInfo.Extension == ".csproj";
private static bool IsDirectoryBuildPropsFile(IFileSystemInfo fileInfo) => fileInfo.Name == "Directory.Build.props";
private static bool IsDirectoryBuildTargetsFile(IFileSystemInfo fileInfo) => fileInfo.Name == "Directory.Build.targets";
private static bool IsNuGetFile(IFileSystemInfo fileInfo) => fileInfo.Name == "packages.config";
private static bool IsDirectoryPackagesPropsFile(IFileSystemInfo fileInfo) => fileInfo.Name == "Directory.Packages.props";

private async Task<IProjectFile> ReadFile(IFileSystemInfo fileInfo)
{
Expand All @@ -72,6 +73,8 @@ private async Task<IProjectFile> ReadFile(IFileSystemInfo fileInfo)
return new DirectoryBuildTargetsFile(path, contents);
if (IsNuGetFile(fileInfo))
return new PackagesConfigFile(path, contents);
if (IsDirectoryPackagesPropsFile(fileInfo))
return new DirectoryPackagesPropsFile(path, contents);

throw new NotImplementedException("Unknown file type");
}
Expand Down
13 changes: 13 additions & 0 deletions test/LibYear.Core.Tests/FileTypes/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="test1" Version="0.1.0" />
<PackageVersion Update="test2" Version="0.2.0" />
<PackageVersion>
<Update>test3</Update>
<Version>0.3.0</Version>
</PackageVersion>
</ItemGroup>
</Project>
68 changes: 68 additions & 0 deletions test/LibYear.Core.Tests/FileTypes/DirectoryPackagesPropsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using LibYear.Core.FileTypes;
using Xunit;

namespace LibYear.Core.Tests.FileTypes;

public class DirectoryPackagesPropsTests
{
[Fact]
public async Task CanLoadDirectoryPackagesPropsFile()
{
//arrange
var filename = Path.Combine("FileTypes", "Directory.Packages.props");

//act
var file = new DirectoryPackagesPropsFile(filename, await File.ReadAllTextAsync(filename));

//assert
Assert.Equal("test1", file.Packages.First().Key);
Assert.Equal("test2", file.Packages.Skip(1).First().Key);
Assert.Equal("test3", file.Packages.Skip(2).First().Key);
}

[Fact]
public async Task CanUpdateDirectoryPackagesPropsFile()
{
//arrange
var filename = Path.Combine("FileTypes", "Directory.Packages.props");
var file = new DirectoryPackagesPropsFile(filename, await File.ReadAllTextAsync(filename));
var results = new[]
{
new Result("test1", new Release(new PackageVersion(0, 1, 0), DateTime.Today), new Release(new PackageVersion(1, 2, 3), DateTime.Today)),
new Result("test2", new Release(new PackageVersion(0, 2, 0), DateTime.Today), new Release(new PackageVersion(2, 3, 4), DateTime.Today)),
new Result("test3", new Release(new PackageVersion(0, 3, 0), DateTime.Today), new Release(new PackageVersion(3, 4, 5), DateTime.Today))
};

//act
var updated = file.Update(results);

//assert
var newFile = new DirectoryPackagesPropsFile(filename, updated);
Assert.Equal("1.2.3", newFile.Packages.First().Value!.ToString());
Assert.Equal("2.3.4", newFile.Packages.Skip(1).First().Value!.ToString());
Assert.Equal("3.4.5", newFile.Packages.Skip(2).First().Value!.ToString());
}

[Fact]
public async Task InvalidVersionShowsInfo()
{
//arrange
var filename = Path.Combine("FileTypes", "Directory.Packages.props");
var contents = await File.ReadAllTextAsync(filename);
var newContents = contents.Replace("0.2.0", "0.2.x");

try
{
//act
_ = new DirectoryPackagesPropsFile(filename, newContents);
Assert.False(true);
}
catch (Exception e)
{
//assert
Assert.Contains("0.2.x", e.Message);
Assert.Contains("test2", e.Message);
Assert.Contains("Directory.Packages.props", e.Message);
}
}
}
5 changes: 5 additions & 0 deletions test/LibYear.Core.Tests/LibYear.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@
<None Include="FileTypes/Directory.Build.props" CopyToOutputDirectory="Always" />
<None Include="FileTypes/Directory.Build.targets" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<None Update="FileTypes\Directory.Packages.props">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

0 comments on commit a9aca5f

Please sign in to comment.