Skip to content

Commit de064b9

Browse files
authored
Merge pull request #544 from atifaziz/fix-issue-407
Recognize pinned pre-release package references
2 parents a2f1013 + c3993d0 commit de064b9

File tree

7 files changed

+82
-18
lines changed

7 files changed

+82
-18
lines changed

src/Dotnet.Script.DependencyModel.Nuget/Dotnet.Script.DependencyModel.NuGet.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
<Description>A MetadataReferenceResolver that allows inline nuget references to be specified in script(csx) files.</Description>
1313
<Authors>dotnet-script</Authors>
1414
<Company>dotnet-script</Company>
15+
<LangVersion>latest</LangVersion>
1516
</PropertyGroup>
17+
18+
<ItemGroup>
19+
<Compile Include="..\Dotnet.Script.DependencyModel\ProjectSystem\ScriptParserInternal.cs" Link="ScriptParserInternal.cs" />
20+
</ItemGroup>
1621
<ItemGroup>
1722
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.6.0" />
1823
</ItemGroup>

src/Dotnet.Script.DependencyModel.Nuget/NuGetSourceReferenceResolver.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using System.IO;
5-
using System.Text.RegularExpressions;
64
using Microsoft.CodeAnalysis;
5+
using Dotnet.Script.DependencyModel.ProjectSystem;
76

87
namespace Dotnet.Script.DependencyModel.NuGet
98
{
@@ -15,7 +14,6 @@ public class NuGetSourceReferenceResolver : SourceReferenceResolver
1514
{
1615
private readonly SourceReferenceResolver _sourceReferenceResolver;
1716
private readonly IDictionary<string, IReadOnlyList<string>> _scriptMap;
18-
private static readonly Regex PackageNameMatcher = new Regex(@"\s*nuget\s*:\s*(.*)\s*,", RegexOptions.Compiled | RegexOptions.IgnoreCase);
1917

2018
public NuGetSourceReferenceResolver(SourceReferenceResolver sourceReferenceResolver, IDictionary<string, IReadOnlyList<string>> scriptMap)
2119
{
@@ -48,9 +46,8 @@ public override string NormalizePath(string path, string baseFilePath)
4846

4947
public override string ResolveReference(string path, string baseFilePath)
5048
{
51-
if (path.StartsWith("nuget:", StringComparison.OrdinalIgnoreCase))
49+
if (ScriptParser.TryParseNuGetPackageReference(path, out var packageName, out _))
5250
{
53-
var packageName = PackageNameMatcher.Match(path).Groups[1].Value;
5451
if (_scriptMap.TryGetValue(packageName, out var scripts))
5552
{
5653
if (scripts.Count == 1)
@@ -66,9 +63,8 @@ public override string ResolveReference(string path, string baseFilePath)
6663

6764
public override Stream OpenRead(string resolvedPath)
6865
{
69-
if (resolvedPath.StartsWith("nuget:", StringComparison.OrdinalIgnoreCase))
66+
if (ScriptParser.TryParseNuGetPackageReference(resolvedPath, out var packageName, out _))
7067
{
71-
var packageName = PackageNameMatcher.Match(resolvedPath).Groups[1].Value;
7268
var scripts = _scriptMap[packageName];
7369
if (scripts.Count == 1)
7470
{

src/Dotnet.Script.DependencyModel/Dotnet.Script.DependencyModel.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
<None Remove="NuGet\NuGet430.exe" />
2020
</ItemGroup>
2121

22+
<ItemGroup>
23+
<Compile Update="ProjectSystem\ScriptParserInternal.cs">
24+
<DependentUpon>ScriptParser.cs</DependentUpon>
25+
</Compile>
26+
</ItemGroup>
27+
2228
<ItemGroup>
2329
<EmbeddedResource Include="ProjectSystem\csproj.template" />
2430
</ItemGroup>

src/Dotnet.Script.DependencyModel/ProjectSystem/PackageVersion.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,38 @@ namespace Dotnet.Script.DependencyModel.ProjectSystem
88
/// </summary>
99
public class PackageVersion : IEquatable<PackageVersion>
1010
{
11-
private static readonly Regex IsPinnedRegex = new Regex(@"^(?>\[\d+[^,\]]+(?<!\.)\]|\d+(\.\d+){2,})$", RegexOptions.Compiled);
11+
// Following patterns are "inspired" from SemVer 2.0 grammar:
12+
//
13+
// Source: https://semver.org/spec/v2.0.0.html#backusnaur-form-grammar-for-valid-semver-versions
14+
15+
// <numeric identifier> ::= "0"
16+
// | <positive digit>
17+
// | <positive digit> <digits>
18+
//
19+
// <digits> ::= <digit>
20+
// | <digit> <digits>
21+
//
22+
// <digit> ::= "0"
23+
// | <positive digit>
24+
//
25+
// <positive digit> ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
26+
27+
const string NumericPattern = @"(?:0|[1-9][0-9]*)";
28+
29+
// <valid semver> ::= <version core>
30+
// | <version core> "-" <pre-release>
31+
// | <version core> "+" <build>
32+
// | <version core> "-" <pre-release> "+" <build>
33+
//
34+
// <version core> ::= <major> "." <minor> "." <patch>
35+
36+
const string MajorPlusVersionPattern = NumericPattern + @"(?:\." + NumericPattern + @")";
37+
const string VersionSuffixPattern = @"(?:[+-][\w][\w+.-]*)?";
38+
39+
private static readonly Regex IsPinnedRegex =
40+
new Regex(@"^(?>\[" + MajorPlusVersionPattern + @"{1,4}" + VersionSuffixPattern + @"\]"
41+
+ @"|" + MajorPlusVersionPattern + @"{2,3}" + VersionSuffixPattern + @")$",
42+
RegexOptions.Compiled);
1243

1344
/// <summary>
1445
/// Initializes a new instance of the <see cref="PackageVersion"/> class.

src/Dotnet.Script.DependencyModel/ProjectSystem/ScriptParser.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Dotnet.Script.DependencyModel.ProjectSystem
88
{
9-
public class ScriptParser
9+
public partial class ScriptParser
1010
{
1111
private readonly Logger _logger;
1212

@@ -37,15 +37,6 @@ public ParseResult ParseFromFiles(IEnumerable<string> csxFiles)
3737
return new ParseResult(allPackageReferences);
3838
}
3939

40-
const string Hws = @"[\x20\t]*"; // hws = horizontal whitespace
41-
42-
const string DirectivePatternPrefix = @"^"
43-
+ Hws + @"#";
44-
const string DirectivePatternSuffix = Hws + @"""nuget:"
45-
// https://github.com/NuGet/docs.microsoft.com-nuget/issues/543#issue-270039223
46-
+ Hws + @"(\w+(?:[_.-]\w+)*)"
47-
+ @"(?:" + Hws + "," + Hws + @"(.+?))?""";
48-
4940
private static IEnumerable<PackageReference> ReadPackageReferencesFromReferenceDirective(string fileContent)
5041
{
5142
const string pattern = DirectivePatternPrefix + "r" + DirectivePatternSuffix;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace Dotnet.Script.DependencyModel.ProjectSystem
4+
{
5+
partial class ScriptParser
6+
{
7+
const string Hws = @"[\x20\t]*"; // hws = horizontal whitespace
8+
9+
const string NuGetPattern = @"nuget:"
10+
// https://github.com/NuGet/docs.microsoft.com-nuget/issues/543#issue-270039223
11+
+ Hws + @"(\w+(?:[_.-]\w+)*)"
12+
+ @"(?:" + Hws + "," + Hws + @"(.+?))?";
13+
14+
const string WholeNuGetPattern = @"^" + NuGetPattern + @"$";
15+
16+
const string DirectivePatternPrefix = @"^" + Hws + @"#";
17+
const string DirectivePatternSuffix = Hws + @"""" + NuGetPattern + @"""";
18+
19+
internal static bool TryParseNuGetPackageReference(string input,
20+
out string id, out string version)
21+
{
22+
bool success;
23+
(success, id, version) =
24+
Regex.Match(input, WholeNuGetPattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)
25+
is {} match && match.Success
26+
? (true, match.Groups[1].Value, match.Groups[2].Value)
27+
: default;
28+
return success;
29+
}
30+
}
31+
}

src/Dotnet.Script.Tests/PackageVersionTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public class PackageVersionTests
1212
[Theory]
1313
[InlineData("1.2.3")]
1414
[InlineData("1.2.3.4")]
15+
[InlineData("1.2.3-beta1")]
16+
[InlineData("0.1.4-beta")] // See: https://github.com/filipw/dotnet-script/issues/407#issuecomment-563363947
17+
[InlineData("2.0.0-preview3.20122.2")] // See: https://github.com/filipw/dotnet-script/issues/407#issuecomment-631122591
18+
[InlineData("1.0.0-ci-20180920T1656")]
1519
[InlineData("[1.2]")]
1620
[InlineData("[1.2.3]")]
1721
[InlineData("[1.2.3-beta1]")]

0 commit comments

Comments
 (0)