Skip to content

Commit

Permalink
When include in readme fails, log a warning
Browse files Browse the repository at this point in the history
This makes it easier to troubleshoot include authoring mistakes.

Fixes #232
  • Loading branch information
kzu committed Sep 4, 2022
1 parent 24cbb7b commit e57c388
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
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);
}
}

0 comments on commit e57c388

Please sign in to comment.