Skip to content
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

When include in readme fails, log a warning #233

Merged
merged 1 commit into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/NuGetizer.Tasks/CreatePackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ void GeneratePackage(Stream output = null)
File.Exists(readmeFile.Source))
{
// replace readme with includes replaced.
var replaced = IncludesResolver.Process(readmeFile.Source, message => Log.LogWarningCode("NG001", message));
var temp = Path.GetTempFileName();
File.WriteAllText(temp, IncludesResolver.Process(readmeFile.Source));
File.WriteAllText(temp, replaced);
readmeFile.Source = temp;
}

Expand Down
10 changes: 8 additions & 2 deletions src/NuGetizer.Tasks/IncludesResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class IncludesResolver
static readonly Regex IncludeRegex = new Regex(@"<!--\s?include\s(.*?)\s?-->", RegexOptions.Compiled);
static readonly HttpClient http = new();

public static string Process(string filePath)
public static string Process(string filePath, Action<string> logWarning = default)
{
string content = null;

Expand Down Expand Up @@ -74,8 +74,10 @@ public static string Process(string filePath)
var anchor = $"<!-- {fragment} -->";
var start = includedContent.IndexOf(anchor);
if (start == -1)
// Warn/error?
{
logWarning?.Invoke($"Failed to resolve anchor {fragment} in {includedPath}.");
continue;
}

includedContent = includedContent.Substring(start);
var end = includedContent.IndexOf(anchor, anchor.Length);
Expand All @@ -91,6 +93,10 @@ public static string Process(string filePath)
else
replacements[new Regex(@$"<!--\s?include {includedPath}{fragment}\s?-->")] = replacement;
}
else
{
logWarning?.Invoke($"Failed to resolve include: {includedPath}{fragment}. File not found at expected location {includedFullPath}.");
}
}

if (replacements.Count > 0)
Expand Down
17 changes: 16 additions & 1 deletion src/NuGetizer.Tests/IncludesResolverTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xunit;
using System.IO;
using Xunit;

namespace NuGetizer;

Expand All @@ -25,4 +26,18 @@ public void ResolveUrlInclude()

Assert.Contains("Daniel Cazzulino", content);
}

[Fact]
public void ResolveNonExistingInclude()
{
var path = Path.GetTempFileName();
var include = "<!-- include foo.md#bar -->";
File.WriteAllText(path, include);

string? failed = default;
var content = IncludesResolver.Process(path, s => failed = s);

Assert.NotNull(failed);
Assert.Contains("foo.md#bar", failed);
}
}