Skip to content

Commit 4a880c9

Browse files
authored
Improve processing of version token in GetNuSpecReader (#92)
1 parent 7054193 commit 4a880c9

File tree

1 file changed

+17
-40
lines changed

1 file changed

+17
-40
lines changed

tools/VersionCop/Program.cs

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
//
2-
// Copyright (c) .NET Foundation and Contributors
3-
// See LICENSE file in the project root for full license information.
4-
//
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
53

6-
using NuGet.Common;
7-
using NuGet.Configuration;
8-
using NuGet.Packaging;
9-
using NuGet.Packaging.Core;
10-
using NuGet.Protocol.Core.Types;
114
using System;
125
using System.Collections.Generic;
136
using System.IO;
147
using System.Linq;
158
using System.Text.RegularExpressions;
169
using System.Threading;
1710
using System.Xml.Linq;
11+
using NuGet.Common;
12+
using NuGet.Configuration;
13+
using NuGet.Packaging;
14+
using NuGet.Packaging.Core;
15+
using NuGet.Protocol.Core.Types;
1816

1917
class Program
2018
{
@@ -101,7 +99,7 @@ static int Main(
10199
// grab working directory from solution, if it's empty
102100
if (workingDirectory is null)
103101
{
104-
workingDirectory = Path.GetFullPath(solutionToCheck);
102+
workingDirectory = Path.GetDirectoryName(Path.GetFullPath(solutionToCheck));
105103
}
106104

107105
// handle mscorlib library
@@ -136,7 +134,7 @@ static int Main(
136134

137135
if (nuspecFile is not null)
138136
{
139-
nuspecReader = new NuspecReader(XDocument.Load(nuspecFile));
137+
nuspecReader = GetNuspecReader(nuspecFile);
140138
}
141139

142140

@@ -218,7 +216,6 @@ static int Main(
218216
Console.WriteLine($"INFO: found matching nuspec file '{Path.GetFileName(nuspecFileName)}'");
219217

220218
// load nuspec file
221-
// NOTE: this is replacing as well $version$ by 9.99.99.999
222219
nuspecReader = GetNuspecReader(nuspecFileName);
223220
}
224221
else
@@ -232,7 +229,6 @@ static int Main(
232229
Console.WriteLine($"INFO: found matching nuspec file '{Path.GetFileName(nuspecFileName)}'");
233230

234231
// load nuspec file
235-
// NOTE: this is replacing as well $version$ by 9.99.99.999
236232
nuspecReader = GetNuspecReader(nuspecFileName);
237233
}
238234
else
@@ -779,34 +775,15 @@ private static bool FindDependency(string packageName,
779775

780776
private static NuspecReader GetNuspecReader(string nuspecFileName)
781777
{
782-
string tokenizedVersion = "version=\"$version$\"";
783-
string replacementVersion = "version=\"9.99.999.9999\"";
784-
bool nuspecReplaced = false;
785-
786-
// handle nuspec files that include token replacement for version
787-
// need to replace it with a valid version string, read and then replace it back
788-
string nuspecContent = File.ReadAllText(nuspecFileName);
778+
string nuspecAsText = File.ReadAllText(nuspecFileName);
789779

790-
if (nuspecContent.Contains(tokenizedVersion))
791-
{
792-
nuspecReplaced = true;
793-
794-
nuspecContent = nuspecContent.Replace(tokenizedVersion, replacementVersion);
795-
796-
File.WriteAllText(nuspecFileName, nuspecContent);
797-
}
798-
799-
800-
var nuspecReader = new NuspecReader(XDocument.Load(nuspecFileName));
801-
802-
// replace back the original version string
803-
if (nuspecReplaced)
804-
{
805-
nuspecContent = nuspecContent.Replace(replacementVersion, tokenizedVersion);
806-
807-
File.WriteAllText(nuspecFileName, nuspecContent);
808-
}
780+
// It is valid to use the variable $version$ in nuspec
781+
// as it will be replaced with correct value when the package is created.
782+
// The replacement value is unique, so it will not match any literal version value.
783+
// The assumption is that all packages share the same value of $version$
784+
// at the time of packaging.
785+
nuspecAsText = nuspecAsText.Replace("$version$", "9.99.999.9999");
809786

810-
return nuspecReader;
787+
return new NuspecReader(XDocument.Parse(nuspecAsText));
811788
}
812789
}

0 commit comments

Comments
 (0)