From 0eb404af47a950c0078d55c671c587faea951b0d Mon Sep 17 00:00:00 2001 From: Frank Robijn Date: Tue, 18 Feb 2025 13:01:16 +0100 Subject: [PATCH 1/2] Version check bug fix + .editorconfig # Conflicts: # .editorconfig # tools/VersionCop/Program.cs --- tools/VersionCop/Program.cs | 58 ++++++++++++------------------------- 1 file changed, 18 insertions(+), 40 deletions(-) diff --git a/tools/VersionCop/Program.cs b/tools/VersionCop/Program.cs index bdf159c..a54ad57 100644 --- a/tools/VersionCop/Program.cs +++ b/tools/VersionCop/Program.cs @@ -1,13 +1,6 @@ -// -// Copyright (c) .NET Foundation and Contributors -// See LICENSE file in the project root for full license information. -// +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. -using NuGet.Common; -using NuGet.Configuration; -using NuGet.Packaging; -using NuGet.Packaging.Core; -using NuGet.Protocol.Core.Types; using System; using System.Collections.Generic; using System.IO; @@ -15,11 +8,17 @@ using System.Text.RegularExpressions; using System.Threading; using System.Xml.Linq; +using NuGet.Common; +using NuGet.Configuration; +using NuGet.Packaging; +using NuGet.Packaging.Core; +using NuGet.Protocol.Core.Types; class Program { private static IEnumerable _nugetRepositories; private static DependencyInfoResource _dependencyInfoResourceNuGet; + private static readonly string _versionReplacementValue = $"9.99.999.9999+{Guid.NewGuid():N}"; /// Path to the solution to check. /// Path of the working directory where solutions will be searched. @@ -101,7 +100,7 @@ static int Main( // grab working directory from solution, if it's empty if (workingDirectory is null) { - workingDirectory = Path.GetFullPath(solutionToCheck); + workingDirectory = Path.GetDirectoryName(Path.GetFullPath(solutionToCheck)); } // handle mscorlib library @@ -136,7 +135,7 @@ static int Main( if (nuspecFile is not null) { - nuspecReader = new NuspecReader(XDocument.Load(nuspecFile)); + nuspecReader = GetNuspecReader(nuspecFile); } @@ -218,7 +217,6 @@ static int Main( Console.WriteLine($"INFO: found matching nuspec file '{Path.GetFileName(nuspecFileName)}'"); // load nuspec file - // NOTE: this is replacing as well $version$ by 9.99.99.999 nuspecReader = GetNuspecReader(nuspecFileName); } else @@ -232,7 +230,6 @@ static int Main( Console.WriteLine($"INFO: found matching nuspec file '{Path.GetFileName(nuspecFileName)}'"); // load nuspec file - // NOTE: this is replacing as well $version$ by 9.99.99.999 nuspecReader = GetNuspecReader(nuspecFileName); } else @@ -779,34 +776,15 @@ private static bool FindDependency(string packageName, private static NuspecReader GetNuspecReader(string nuspecFileName) { - string tokenizedVersion = "version=\"$version$\""; - string replacementVersion = "version=\"9.99.999.9999\""; - bool nuspecReplaced = false; - - // handle nuspec files that include token replacement for version - // need to replace it with a valid version string, read and then replace it back - string nuspecContent = File.ReadAllText(nuspecFileName); + string nuspecAsText = File.ReadAllText(nuspecFileName); - if (nuspecContent.Contains(tokenizedVersion)) - { - nuspecReplaced = true; - - nuspecContent = nuspecContent.Replace(tokenizedVersion, replacementVersion); - - File.WriteAllText(nuspecFileName, nuspecContent); - } - - - var nuspecReader = new NuspecReader(XDocument.Load(nuspecFileName)); - - // replace back the original version string - if (nuspecReplaced) - { - nuspecContent = nuspecContent.Replace(replacementVersion, tokenizedVersion); - - File.WriteAllText(nuspecFileName, nuspecContent); - } + // It is valid to use the variable $version$ in nuspec + // as it will be replaced with correct value when the package is created. + // The replacement value is unique, so it will not match any literal version value. + // The assumption is that all packages share the same value of $version$ + // at the time of packaging. + nuspecAsText = nuspecAsText.Replace("$version$", _versionReplacementValue); - return nuspecReader; + return new NuspecReader(XDocument.Parse(nuspecAsText)); } } From 24bb1d29c951415bae57fdb4c004321f03304f46 Mon Sep 17 00:00:00 2001 From: Frank Robijn Date: Tue, 18 Feb 2025 15:47:08 +0100 Subject: [PATCH 2/2] 9.99.999.9999 --- tools/VersionCop/Program.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/VersionCop/Program.cs b/tools/VersionCop/Program.cs index a54ad57..b36e3db 100644 --- a/tools/VersionCop/Program.cs +++ b/tools/VersionCop/Program.cs @@ -18,7 +18,6 @@ class Program { private static IEnumerable _nugetRepositories; private static DependencyInfoResource _dependencyInfoResourceNuGet; - private static readonly string _versionReplacementValue = $"9.99.999.9999+{Guid.NewGuid():N}"; /// Path to the solution to check. /// Path of the working directory where solutions will be searched. @@ -783,7 +782,7 @@ private static NuspecReader GetNuspecReader(string nuspecFileName) // The replacement value is unique, so it will not match any literal version value. // The assumption is that all packages share the same value of $version$ // at the time of packaging. - nuspecAsText = nuspecAsText.Replace("$version$", _versionReplacementValue); + nuspecAsText = nuspecAsText.Replace("$version$", "9.99.999.9999"); return new NuspecReader(XDocument.Parse(nuspecAsText)); }