Skip to content

Commit 0e0aab1

Browse files
authored
Merge pull request #15 from seesharper/use-improved-regex
Match CRLF in update script package
2 parents 1f0ead5 + 235eb67 commit 0e0aab1

File tree

5 files changed

+30
-23
lines changed

5 files changed

+30
-23
lines changed

src/Dotnet.Deps.Core/Dotnet.Deps.Core.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
<RepositoryUrl>https://github.com/seesharper/dotnet-deps.git</RepositoryUrl>
1111
<Authors>Bernhard Richter</Authors>
1212
<Description>A simple library that can be used to analyze and update NuGet dependencies.</Description>
13-
<Version>2.1.1</Version>
13+
<Version>2.1.2</Version>
14+
<LangVersion>latest</LangVersion>
1415
</PropertyGroup>
1516
<ItemGroup>
1617
<PackageReference Include="NuGet.Configuration" Version="6.4.0" />

src/Dotnet.Deps.Core/ProjectSystem/ScriptPackageReference.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ public class ScriptPackageReference : NuGetPackageReference
1212

1313
public ScriptPackageReference(string name, string version, FloatRange floatRange, ScriptFileContent content) : base(name, version, floatRange)
1414
{
15-
referenceDirectivePattern = $@"^(\s*#r\s*""nuget:\s*)({name})(,\s*)(.*)(\s*""$)";
16-
loadDirectivePattern = $@"^(\s*#load\s*""nuget:\s*)({name})(,\s*)(.*)(\s*""$)";
15+
// https://stackoverflow.com/questions/8618557/why-doesnt-in-net-multiline-regular-expressions-match-crlf
16+
referenceDirectivePattern = $@"^(\s*#r\s*""nuget:\s*)({name})(,\s*)(.*)(\s*""\r?$)";
17+
loadDirectivePattern = $@"^(\s*#load\s*""nuget:\s*)({name})(,\s*)(.*)(\s*""\r?$)";
1718
this.content = content;
1819
}
1920

2021
public override void Update(string newVersion)
2122
{
23+
var match = Regex.Match(content.SourceCode, ScriptRegularExpressions.ReferenceDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
24+
var oldMatch = Regex.Match(content.SourceCode, referenceDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
2225
content.SourceCode = Regex.Replace(content.SourceCode, referenceDirectivePattern, "${1}${2}${3}" + newVersion + "${5}", RegexOptions.IgnoreCase | RegexOptions.Multiline);
2326
content.SourceCode = Regex.Replace(content.SourceCode, loadDirectivePattern, "${1}${2}${3}" + newVersion + "${5}", RegexOptions.IgnoreCase | RegexOptions.Multiline);
2427
}

src/Dotnet.Deps.Core/ProjectSystem/ScriptProjectLoader.cs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,6 @@ namespace Dotnet.Deps.Core.ProjectSystem
88
{
99
public class ScriptProjectLoader : IProjectLoader
1010
{
11-
const string Hws = @"[\x20\t]*"; // hws = horizontal whitespace
12-
13-
const string NuGetPattern = @"nuget:"
14-
// https://github.com/NuGet/docs.microsoft.com-nuget/issues/543#issue-270039223
15-
+ Hws + @"(\w+(?:[_.-]\w+)*)"
16-
+ @"(?:" + Hws + "," + Hws + @"(.+?))?";
17-
18-
const string WholeNuGetPattern = @"^" + NuGetPattern + @"$";
19-
20-
const string NuGetDirectivePatternSuffix = Hws + @"""" + NuGetPattern + @"""";
21-
22-
const string DirectivePatternPrefix = @"^" + Hws + @"#";
23-
24-
const string ReferenceDirectivePattern = DirectivePatternPrefix + "r" + NuGetDirectivePatternSuffix;
25-
const string LoadDirectivePattern = DirectivePatternPrefix + "load" + NuGetDirectivePatternSuffix;
26-
27-
2811
private readonly AppConsole console;
2912

3013
public ScriptProjectLoader(AppConsole console)
@@ -38,8 +21,8 @@ public IProjectFile<NuGetPackageReference> Load(string path)
3821
{
3922
var content = File.ReadAllText(path);
4023
var scriptFileContent = new ScriptFileContent() { SourceCode = content };
41-
var matches = Regex.Matches(content, ReferenceDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Cast<Match>()
42-
.Union(Regex.Matches(content, LoadDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Cast<Match>()).ToArray();
24+
var matches = Regex.Matches(content, ScriptRegularExpressions.ReferenceDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Cast<Match>()
25+
.Union(Regex.Matches(content, ScriptRegularExpressions.LoadDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Cast<Match>()).ToArray();
4326
var packageReferences = new List<ScriptPackageReference>();
4427
foreach (var match in matches)
4528
{
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Dotnet.Deps.Core.ProjectSystem;
2+
3+
public static class ScriptRegularExpressions
4+
{
5+
const string Hws = @"[\x20\t]*"; // hws = horizontal whitespace
6+
7+
const string NuGetPattern = @"nuget:"
8+
// https://github.com/NuGet/docs.microsoft.com-nuget/issues/543#issue-270039223
9+
+ Hws + @"(\w+(?:[_.-]\w+)*)"
10+
+ @"(?:" + Hws + "," + Hws + @"(.+?))?";
11+
12+
const string WholeNuGetPattern = @"^" + NuGetPattern + @"$";
13+
14+
const string NuGetDirectivePatternSuffix = Hws + @"""" + NuGetPattern + @"""";
15+
16+
const string DirectivePatternPrefix = @"^" + Hws + @"#";
17+
18+
public const string ReferenceDirectivePattern = DirectivePatternPrefix + "r" + NuGetDirectivePatternSuffix;
19+
public const string LoadDirectivePattern = DirectivePatternPrefix + "load" + NuGetDirectivePatternSuffix;
20+
}

src/Dotnet.Deps/Dotnet.Deps.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1616
<RepositoryType>git</RepositoryType>
1717
<RepositoryUrl>https://github.com/seesharper/dotnet-deps.git</RepositoryUrl>
18-
<Version>3.0.1</Version>
18+
<Version>3.0.2</Version>
1919
</PropertyGroup>
2020
<ItemGroup>
2121
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.2" />

0 commit comments

Comments
 (0)