Skip to content

Commit

Permalink
Merge pull request #15 from seesharper/use-improved-regex
Browse files Browse the repository at this point in the history
Match CRLF in update script package
  • Loading branch information
seesharper committed Dec 21, 2022
2 parents 1f0ead5 + 235eb67 commit 0e0aab1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/Dotnet.Deps.Core/Dotnet.Deps.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<RepositoryUrl>https://github.com/seesharper/dotnet-deps.git</RepositoryUrl>
<Authors>Bernhard Richter</Authors>
<Description>A simple library that can be used to analyze and update NuGet dependencies.</Description>
<Version>2.1.1</Version>
<Version>2.1.2</Version>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NuGet.Configuration" Version="6.4.0" />
Expand Down
7 changes: 5 additions & 2 deletions src/Dotnet.Deps.Core/ProjectSystem/ScriptPackageReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ public class ScriptPackageReference : NuGetPackageReference

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

public override void Update(string newVersion)
{
var match = Regex.Match(content.SourceCode, ScriptRegularExpressions.ReferenceDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
var oldMatch = Regex.Match(content.SourceCode, referenceDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
content.SourceCode = Regex.Replace(content.SourceCode, referenceDirectivePattern, "${1}${2}${3}" + newVersion + "${5}", RegexOptions.IgnoreCase | RegexOptions.Multiline);
content.SourceCode = Regex.Replace(content.SourceCode, loadDirectivePattern, "${1}${2}${3}" + newVersion + "${5}", RegexOptions.IgnoreCase | RegexOptions.Multiline);
}
Expand Down
21 changes: 2 additions & 19 deletions src/Dotnet.Deps.Core/ProjectSystem/ScriptProjectLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@ namespace Dotnet.Deps.Core.ProjectSystem
{
public class ScriptProjectLoader : IProjectLoader
{
const string Hws = @"[\x20\t]*"; // hws = horizontal whitespace

const string NuGetPattern = @"nuget:"
// https://github.com/NuGet/docs.microsoft.com-nuget/issues/543#issue-270039223
+ Hws + @"(\w+(?:[_.-]\w+)*)"
+ @"(?:" + Hws + "," + Hws + @"(.+?))?";

const string WholeNuGetPattern = @"^" + NuGetPattern + @"$";

const string NuGetDirectivePatternSuffix = Hws + @"""" + NuGetPattern + @"""";

const string DirectivePatternPrefix = @"^" + Hws + @"#";

const string ReferenceDirectivePattern = DirectivePatternPrefix + "r" + NuGetDirectivePatternSuffix;
const string LoadDirectivePattern = DirectivePatternPrefix + "load" + NuGetDirectivePatternSuffix;


private readonly AppConsole console;

public ScriptProjectLoader(AppConsole console)
Expand All @@ -38,8 +21,8 @@ public IProjectFile<NuGetPackageReference> Load(string path)
{
var content = File.ReadAllText(path);
var scriptFileContent = new ScriptFileContent() { SourceCode = content };
var matches = Regex.Matches(content, ReferenceDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Cast<Match>()
.Union(Regex.Matches(content, LoadDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Cast<Match>()).ToArray();
var matches = Regex.Matches(content, ScriptRegularExpressions.ReferenceDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Cast<Match>()
.Union(Regex.Matches(content, ScriptRegularExpressions.LoadDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Cast<Match>()).ToArray();
var packageReferences = new List<ScriptPackageReference>();
foreach (var match in matches)
{
Expand Down
20 changes: 20 additions & 0 deletions src/Dotnet.Deps.Core/ProjectSystem/ScriptRegularExpressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Dotnet.Deps.Core.ProjectSystem;

public static class ScriptRegularExpressions
{
const string Hws = @"[\x20\t]*"; // hws = horizontal whitespace

const string NuGetPattern = @"nuget:"
// https://github.com/NuGet/docs.microsoft.com-nuget/issues/543#issue-270039223
+ Hws + @"(\w+(?:[_.-]\w+)*)"
+ @"(?:" + Hws + "," + Hws + @"(.+?))?";

const string WholeNuGetPattern = @"^" + NuGetPattern + @"$";

const string NuGetDirectivePatternSuffix = Hws + @"""" + NuGetPattern + @"""";

const string DirectivePatternPrefix = @"^" + Hws + @"#";

public const string ReferenceDirectivePattern = DirectivePatternPrefix + "r" + NuGetDirectivePatternSuffix;
public const string LoadDirectivePattern = DirectivePatternPrefix + "load" + NuGetDirectivePatternSuffix;
}
2 changes: 1 addition & 1 deletion src/Dotnet.Deps/Dotnet.Deps.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/seesharper/dotnet-deps.git</RepositoryUrl>
<Version>3.0.1</Version>
<Version>3.0.2</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.2" />
Expand Down

0 comments on commit 0e0aab1

Please sign in to comment.