Skip to content

Commit

Permalink
Allow including content in the package readme from a URL
Browse files Browse the repository at this point in the history
Fixes #216
  • Loading branch information
kzu committed Aug 12, 2022
1 parent 526d87b commit c96f1c6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
46 changes: 40 additions & 6 deletions src/NuGetizer.Tasks/IncludesResolver.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace NuGetizer;

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)
{
var content = File.ReadAllText(filePath).Trim();
string content = null;

if (Uri.TryCreate(filePath, UriKind.Absolute, out var uri))
{
var ev = new ManualResetEventSlim();
Exception ex = null;

Task.Run(() => http.GetStringAsync(uri)).ContinueWith(t =>
{
ex = t.Exception?.InnerException;
if (ex == null)
content = t.Result.Trim();
ev.Set();
});

ev.Wait();
if (ex != null)
throw ex;
}
else
{
content = File.ReadAllText(filePath).Trim();
}

// TODO: removing this for now, since this would prevent a consumer of the
// resolve includes github action (see https://github.com/marketplace/actions/resolve-file-includes)
// from excluding the readme from CI-based resolution, while still keeping
// this (100% compatible) pack-time resolution.

// Allow self-excluding files for processing. Could be useful if the file itself
// documents the include/exclude mechanism, for example.
if (content.StartsWith("<!-- exclude -->") || content.EndsWith("<!-- exclude -->"))
return content;
//if (content.StartsWith("<!-- exclude -->") || content.EndsWith("<!-- exclude -->"))
// return content;

var replacements = new Dictionary<Regex, string>();

Expand All @@ -29,11 +62,12 @@ public static string Process(string filePath)
includedPath = includedPath.Split('#')[0];
}

var includedFullPath = Path.Combine(Path.GetDirectoryName(filePath), includedPath).Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
if (File.Exists(includedFullPath))
var isUri = Uri.IsWellFormedUriString(includedPath, UriKind.Absolute);
var includedFullPath = Path.Combine(Path.GetDirectoryName(filePath), includedPath).Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
if (isUri || File.Exists(includedFullPath))
{
// Resolve nested includes
var includedContent = Process(includedFullPath);
var includedContent = Process(isUri ? includedPath : includedFullPath);
if (fragment != null)
{
var anchor = $"<!-- {fragment} -->";
Expand Down
1 change: 1 addition & 0 deletions src/NuGetizer.Tests/Content/url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- include https://github.com/devlooped/nugetizer/raw/main/license.txt -->
8 changes: 8 additions & 0 deletions src/NuGetizer.Tests/IncludesResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ public void ResolveIncludes()
Assert.Contains("section#3", content);
Assert.Contains("@kzu", content);
}

[Fact]
public void ResolveUrlInclude()
{
var content = IncludesResolver.Process("Content/url.md");

Assert.Contains("Daniel Cazzulino", content);
}
}
6 changes: 6 additions & 0 deletions src/NuGetizer.Tests/NuGetizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
<ProjectProperty Include="OutputPath" />
</ItemGroup>

<ItemGroup>
<None Update="Content\url.md">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<Target Name="EnsureProjectInformation" BeforeTargets="InjectThisAssemblyProject;GenerateMSBuildEditorConfigFileShouldRun" DependsOnTargets="InitializeSourceControlInformation" />

<Target Name="Test" DependsOnTargets="GetTargetPath">
Expand Down

0 comments on commit c96f1c6

Please sign in to comment.