diff --git a/CentralisedPackageConverter.Tests/VersioningTests.cs b/CentralisedPackageConverter.Tests/VersioningTests.cs index 19d1034..727102d 100644 --- a/CentralisedPackageConverter.Tests/VersioningTests.cs +++ b/CentralisedPackageConverter.Tests/VersioningTests.cs @@ -134,6 +134,21 @@ public void VersioningIgnorePrerelease(bool ignorePrerelease, string expectedVer } + [Test] + public void TestVariableAsVersion() + { + var initialProjectContent = + @"" + LineWrap + + @" " + LineWrap + + @" " + LineWrap + + @" " + LineWrap + + @"" + 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 diff --git a/CentralisedPackageConverter/PackageConverter.cs b/CentralisedPackageConverter/PackageConverter.cs index fe1dd4f..ee35f12 100644 --- a/CentralisedPackageConverter/PackageConverter.cs +++ b/CentralisedPackageConverter/PackageConverter.cs @@ -8,7 +8,7 @@ namespace CentralisedPackageConverter; public class PackageConverter { - private readonly Dictionary> referencesByConditionThenName = new(StringComparer.OrdinalIgnoreCase); + private readonly Dictionary> referencesByConditionThenName = new(StringComparer.OrdinalIgnoreCase); private const string s_DirPackageProps = "Directory.Packages.props"; internal static readonly Regex FileExtensionsRegex = @@ -168,12 +168,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(StringComparer.OrdinalIgnoreCase); + packagesByName = new Dictionary(StringComparer.OrdinalIgnoreCase); this.referencesByConditionThenName[condition] = packagesByName; } @@ -319,8 +323,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)) { version = parsedVersion; } @@ -338,7 +342,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; } @@ -369,7 +373,7 @@ private void ConvertProject(FileInfo csprojFile, bool dryRun, Encoding encoding, if (!this.referencesByConditionThenName.TryGetValue(condition, out var referencesForCondition)) { - referencesForCondition = new Dictionary(StringComparer.OrdinalIgnoreCase); + referencesForCondition = new Dictionary(StringComparer.OrdinalIgnoreCase); this.referencesByConditionThenName[condition] = referencesForCondition; } diff --git a/CentralisedPackageConverter/PackageVersion.cs b/CentralisedPackageConverter/PackageVersion.cs new file mode 100644 index 0000000..40c4196 --- /dev/null +++ b/CentralisedPackageConverter/PackageVersion.cs @@ -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; + } +} diff --git a/CentralisedPackageConverter/Versioning.cs b/CentralisedPackageConverter/Versioning.cs index 7973518..a6da2a1 100644 --- a/CentralisedPackageConverter/Versioning.cs +++ b/CentralisedPackageConverter/Versioning.cs @@ -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 . /// - 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; } /// @@ -40,15 +40,18 @@ public bool IgnorePackageVersion([NotNullWhen(false)]NuGetVersion? version) /// /// /// - 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; } } \ No newline at end of file