diff --git a/tools/VersionCop/Program.cs b/tools/VersionCop/Program.cs index bdf159c..b36e3db 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,6 +8,11 @@ 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 { @@ -101,7 +99,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 +134,7 @@ static int Main( if (nuspecFile is not null) { - nuspecReader = new NuspecReader(XDocument.Load(nuspecFile)); + nuspecReader = GetNuspecReader(nuspecFile); } @@ -218,7 +216,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 +229,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 +775,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$", "9.99.999.9999"); - return nuspecReader; + return new NuspecReader(XDocument.Parse(nuspecAsText)); } }