Skip to content

Commit

Permalink
Merge pull request #43 from SenDevGmbH/support-variables-in-versions
Browse files Browse the repository at this point in the history
Added support for variables in versions
  • Loading branch information
Webreaper authored Aug 3, 2024
2 parents 581f01d + 43f71df commit 854e350
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 12 deletions.
15 changes: 15 additions & 0 deletions CentralisedPackageConverter.Tests/VersioningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ public void VersioningIgnorePrerelease(bool ignorePrerelease, string expectedVer
}


[Test]
public void TestVariableAsVersion()
{
var initialProjectContent =
@"<Project>" + LineWrap +
@" <ItemGroup>" + LineWrap +
@" <PackageReference Include=""TestPackage"" Version=""$(VersionVariable)"" />" + LineWrap +
@" </ItemGroup>" + LineWrap +
@"</Project>" + LineWrap;

TestResultVersion([initialProjectContent], "$(VersionVariable)");
CheckProjectFilesHaveVersionsRemoved();
}



// something like this generator https://github.com/belav/csharpier/tree/master/Src/CSharpier.Tests.Generators
// to create the tests that copying files to a temp directory out of the folder structure in here
Expand Down
18 changes: 11 additions & 7 deletions CentralisedPackageConverter/PackageConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace CentralisedPackageConverter;

public class PackageConverter
{
private readonly Dictionary<string, Dictionary<string, NuGetVersion>> referencesByConditionThenName = new(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, Dictionary<string, PackageVersion>> referencesByConditionThenName = new(StringComparer.OrdinalIgnoreCase);
private const string s_DirPackageProps = "Directory.Packages.props";

internal static readonly Regex FileExtensionsRegex =
Expand Down Expand Up @@ -175,12 +175,16 @@ private void ReadDirectoryPackagePropsFile(string packageConfigPath)
{
var package = GetAttributeValue(packageVersion, "Include") ??
throw new InvalidOperationException("PackageVersion element has no Include");
var version = NuGetVersion.Parse(GetAttributeValue(packageVersion, "Version"));
string? versionAttributeValue = GetAttributeValue(packageVersion, "Version");
if (versionAttributeValue == null)
throw new InvalidOperationException("PackageVersion element has no Version attribute");

var version = PackageVersion.Parse(versionAttributeValue);
var condition = GetAttributeValue(packageVersion.Parent, "Condition") ?? string.Empty;

if (!this.referencesByConditionThenName.TryGetValue(condition, out var packagesByName))
{
packagesByName = new Dictionary<string, NuGetVersion>(StringComparer.OrdinalIgnoreCase);
packagesByName = new Dictionary<string, PackageVersion>(StringComparer.OrdinalIgnoreCase);
this.referencesByConditionThenName[condition] = packagesByName;
}

Expand Down Expand Up @@ -326,8 +330,8 @@ private void ConvertProject(FileInfo csprojFile, bool dryRun, Encoding encoding,
continue;

var versionString = GetAttributeValue(packageReference, "Version");
var version = default(NuGetVersion?);
if (NuGetVersion.TryParse(versionString, out var parsedVersion))
var version = default(PackageVersion?);
if (PackageVersion.TryParse(versionString, out var parsedVersion))

Check warning on line 334 in CentralisedPackageConverter/PackageConverter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'bool PackageVersion.TryParse(string value, out PackageVersion? version)'.

Check warning on line 334 in CentralisedPackageConverter/PackageConverter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'bool PackageVersion.TryParse(string value, out PackageVersion? version)'.

Check warning on line 334 in CentralisedPackageConverter/PackageConverter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'bool PackageVersion.TryParse(string value, out PackageVersion? version)'.

Check warning on line 334 in CentralisedPackageConverter/PackageConverter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'bool PackageVersion.TryParse(string value, out PackageVersion? version)'.
{
version = parsedVersion;
}
Expand All @@ -346,7 +350,7 @@ private void ConvertProject(FileInfo csprojFile, bool dryRun, Encoding encoding,
continue;
}

if (NuGetVersion.TryParse(versionElement.Value, out parsedVersion))
if (PackageVersion.TryParse(versionElement.Value, out parsedVersion))
{
version = parsedVersion;
}
Expand Down Expand Up @@ -378,7 +382,7 @@ private void ConvertProject(FileInfo csprojFile, bool dryRun, Encoding encoding,

if (!this.referencesByConditionThenName.TryGetValue(condition, out var referencesForCondition))
{
referencesForCondition = new Dictionary<string, NuGetVersion>(StringComparer.OrdinalIgnoreCase);
referencesForCondition = new Dictionary<string, PackageVersion>(StringComparer.OrdinalIgnoreCase);
this.referencesByConditionThenName[condition] = referencesForCondition;
}

Expand Down
57 changes: 57 additions & 0 deletions CentralisedPackageConverter/PackageVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using NuGet.Versioning;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CentralisedPackageConverter
{
public class PackageVersion
{

public string? VariableName { get; private set; }

public NuGetVersion? NuGetVersion { get; private set; }


public static PackageVersion Parse(string value)
{
if (!TryParse(value, out var version))
throw new ArgumentException("Invalid package version", nameof(value));

return version!;
}
public static bool TryParse(string value, out PackageVersion? version)
{

if (value != null)
{
if (value.Contains("$("))
{
version = new PackageVersion
{
VariableName = value
};
return true;
}

if (NuGetVersion.TryParse(value, out var nugetVersion))
{
version = new PackageVersion
{
NuGetVersion = nugetVersion
};
return true;
}
}
version = null;
return false;
}

public bool IsVariable => !string.IsNullOrWhiteSpace(VariableName);
public string ToFullString() => NuGetVersion?.ToFullString() ?? VariableName ?? string.Empty;

public override string ToString() => NuGetVersion?.ToString() ?? VariableName ?? string.Empty;
}
}
13 changes: 8 additions & 5 deletions CentralisedPackageConverter/Versioning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public Versioning(bool pickMinVersion, bool ignorePrerelease, VersionComparison
/// Don't consider the version for PackageVersion setting.
/// True if null or has parts not supported by <see cref="Comparer"/>.
/// </summary>
public bool IgnorePackageVersion([NotNullWhen(false)]NuGetVersion? version)
public bool IgnorePackageVersion([NotNullWhen(false)]PackageVersion? version)
{
if (version == null)
{
return true;
}

return IgnorePrerelease && version.IsPrerelease;
return IgnorePrerelease && version.NuGetVersion?.IsPrerelease == true;
}

/// <summary>
Expand All @@ -40,15 +40,18 @@ public bool IgnorePackageVersion([NotNullWhen(false)]NuGetVersion? version)
/// <param name="existingVersion"></param>
/// <param name="nextVersion"></param>
/// <returns></returns>
public bool PreferExisting(NuGetVersion existingVersion, NuGetVersion? nextVersion)
public bool PreferExisting(PackageVersion existingVersion, PackageVersion? nextVersion)
{
if (existingVersion.IsVariable) return true;
if (nextVersion == null)
{
return true;
}

if (nextVersion.IsVariable) return false;

return PickMinVersion
? Comparer.Compare(existingVersion, nextVersion) <= 0
: Comparer.Compare(existingVersion, nextVersion) >= 0;
? Comparer.Compare(existingVersion.NuGetVersion, nextVersion.NuGetVersion) <= 0
: Comparer.Compare(existingVersion.NuGetVersion, nextVersion.NuGetVersion) >= 0;
}
}

0 comments on commit 854e350

Please sign in to comment.