Skip to content

Improve processing of version token in GetNuSpecReader #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 17 additions & 40 deletions tools/VersionCop/Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
//
// 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;
using System.Linq;
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
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -136,7 +134,7 @@ static int Main(

if (nuspecFile is not null)
{
nuspecReader = new NuspecReader(XDocument.Load(nuspecFile));
nuspecReader = GetNuspecReader(nuspecFile);
}


Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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));
}
}