Skip to content

Commit

Permalink
Merge branch 'main' into feat/merge-overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeih authored May 29, 2024
2 parents d554ccb + 0790840 commit 466d704
Show file tree
Hide file tree
Showing 23 changed files with 561 additions and 362 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
branches: [ main, feature/*, hotfix/* ]
push:
branches: [ main, feature/*, hotfix/* ]
workflow_dispatch:

jobs:
test:
Expand Down
7 changes: 7 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
<None Include="$(MSBuildThisFileDirectory)\README.md" Pack="true" PackagePath="\"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="PolySharp">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub">
<PrivateAssets>all</PrivateAssets>
Expand Down
7 changes: 4 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageVersion Include="HtmlAgilityPack" Version="1.11.61" />
<PackageVersion Include="ICSharpCode.Decompiler" Version="8.2.0.7535" />
<PackageVersion Include="IgnoresAccessChecksToGenerator" Version="0.7.0" />
<PackageVersion Include="Jint" Version="3.1.0" />
<PackageVersion Include="Jint" Version="3.1.2" />
<PackageVersion Include="JsonSchema.Net" Version="7.0.2" />
<PackageVersion Include="Markdig" Version="0.37.0" />
<PackageVersion Include="Microsoft.Playwright" Version="1.44.0" />
Expand All @@ -17,6 +17,7 @@
<PackageVersion Include="OneOf.SourceGenerator" Version="3.0.271" />
<PackageVersion Include="PdfPig" Version="0.1.9-alpha-20240510-d86c2" />
<PackageVersion Include="PlantUml.Net" Version="1.4.80" />
<PackageVersion Include="PolySharp" Version="1.14.1" />
<PackageVersion Include="Spectre.Console" Version="0.49.1" />
<PackageVersion Include="Spectre.Console.Cli" Version="0.49.1" />
<PackageVersion Include="Stubble.Core" Version="1.10.8" />
Expand Down Expand Up @@ -58,7 +59,7 @@
<PackageVersion Include="PublicApiGenerator" Version="11.1.0" />
<PackageVersion Include="Verify.DiffPlex" Version="2.3.0" />
<PackageVersion Include="Verify.Xunit" Version="24.2.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.0" />
<PackageVersion Include="xunit" Version="2.8.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.1" />
<PackageVersion Include="xunit" Version="2.8.1" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Docfx.App/Docfx.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<Content Include="templates/**" CopyToOutputDirectory="PreserveNewest" PackageCopyToOutput="true" PackagePath="contentFiles/any/any/templates" />
<None Include="Build\Docfx.App.props" Pack="true" PackagePath="build\" />
<None Include="Build\Docfx.App.props" Pack="true" PackagePath="build/" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 0 additions & 4 deletions src/Docfx.Build/ApiPage/ApiPage.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if NET7_0_OR_GREATER

using System.Text.Json;
using System.Text.Json.Serialization;
using OneOf;
Expand Down Expand Up @@ -160,5 +158,3 @@ static ApiPage()
public string? languageId { get; init; }
public Dictionary<string, OneOf<string, string[]>>? metadata { get; init; }
}

#endif
4 changes: 0 additions & 4 deletions src/Docfx.Build/ApiPage/ApiPageHtmlTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if NET7_0_OR_GREATER

using System.Net;
using OneOf;
using static Docfx.Build.HtmlTemplate;
Expand Down Expand Up @@ -158,5 +156,3 @@ LinkSpan link when string.IsNullOrEmpty(link.url) => Html($"{link.text}"),
};
}
}

#endif
4 changes: 0 additions & 4 deletions src/Docfx.Build/ApiPage/ApiPageMarkdownTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if NET7_0_OR_GREATER

using System.Text;

#nullable enable
Expand Down Expand Up @@ -124,5 +122,3 @@ internal static string Escape(string text)
return sb.ToString();
}
}

#endif
4 changes: 0 additions & 4 deletions src/Docfx.Build/ApiPage/ApiPageProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if NET7_0_OR_GREATER

using System.Collections.Immutable;
using System.Text.Json;
using Docfx.Common;
Expand Down Expand Up @@ -74,5 +72,3 @@ public SaveResult Save(FileModel model)
};
}
}

#endif
2 changes: 0 additions & 2 deletions src/Docfx.Build/DocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public void Build(IList<DocumentBuildParameters> parameters, string outputDirect

var markdownService = CreateMarkdigMarkdownService(parameters[0]);

#if NET7_0_OR_GREATER
Processors = Processors.Append(new ApiPage.ApiPageDocumentProcessor(markdownService));
#endif

Logger.LogInfo($"{Processors.Count()} plug-in(s) loaded.");
foreach (var processor in Processors)
Expand Down
37 changes: 18 additions & 19 deletions src/Docfx.Build/XRefMaps/XRefMapDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Docfx.Build.Engine;

public class XRefMapDownloader
public sealed class XRefMapDownloader
{
private readonly SemaphoreSlim _semaphore;
private readonly IReadOnlyList<string> _localFileFolders;
Expand Down Expand Up @@ -40,22 +40,22 @@ public XRefMapDownloader(string baseFolder = null, IReadOnlyList<string> fallbac
/// <param name="uri">The uri of xref map file.</param>
/// <returns>An instance of <see cref="XRefMap"/>.</returns>
/// <threadsafety>This method is thread safe.</threadsafety>
public async Task<IXRefContainer> DownloadAsync(Uri uri)
public async Task<IXRefContainer> DownloadAsync(Uri uri, CancellationToken token = default)
{
ArgumentNullException.ThrowIfNull(uri);

await _semaphore.WaitAsync();
await _semaphore.WaitAsync(token);
return await Task.Run(async () =>
{
try
{
if (uri.IsAbsoluteUri)
{
return await DownloadBySchemeAsync(uri);
return await DownloadBySchemeAsync(uri, token);
}
else
{
return ReadLocalFileWithFallback(uri);
return await ReadLocalFileWithFallback(uri, token);
}
}
finally
Expand All @@ -65,14 +65,14 @@ public async Task<IXRefContainer> DownloadAsync(Uri uri)
});
}

private IXRefContainer ReadLocalFileWithFallback(Uri uri)
private ValueTask<IXRefContainer> ReadLocalFileWithFallback(Uri uri, CancellationToken token = default)
{
foreach (var localFileFolder in _localFileFolders)
{
var localFilePath = Path.Combine(localFileFolder, uri.OriginalString);
if (File.Exists(localFilePath))
{
return ReadLocalFile(localFilePath);
return ReadLocalFileAsync(localFilePath, token);
}
}
throw new FileNotFoundException($"Cannot find xref map file {uri.OriginalString} in path: {string.Join(",", _localFileFolders)}", uri.OriginalString);
Expand All @@ -81,17 +81,17 @@ private IXRefContainer ReadLocalFileWithFallback(Uri uri)
/// <remarks>
/// Support scheme: http, https, file.
/// </remarks>
protected virtual async Task<IXRefContainer> DownloadBySchemeAsync(Uri uri)
private async ValueTask<IXRefContainer> DownloadBySchemeAsync(Uri uri, CancellationToken token = default)
{
IXRefContainer result;
if (uri.IsFile)
{
result = DownloadFromLocal(uri);
result = await DownloadFromLocalAsync(uri, token);
}
else if (uri.Scheme == Uri.UriSchemeHttp ||
uri.Scheme == Uri.UriSchemeHttps)
{
result = await DownloadFromWebAsync(uri);
result = await DownloadFromWebAsync(uri, token);
}
else
{
Expand All @@ -104,13 +104,13 @@ protected virtual async Task<IXRefContainer> DownloadBySchemeAsync(Uri uri)
return result;
}

protected static IXRefContainer DownloadFromLocal(Uri uri)
private static ValueTask<IXRefContainer> DownloadFromLocalAsync(Uri uri, CancellationToken token = default)
{
var filePath = uri.LocalPath;
return ReadLocalFile(filePath);
return ReadLocalFileAsync(filePath, token);
}

private static IXRefContainer ReadLocalFile(string filePath)
private static async ValueTask<IXRefContainer> ReadLocalFileAsync(string filePath, CancellationToken token = default)
{
Logger.LogVerbose($"Reading from file: {filePath}");

Expand All @@ -121,8 +121,8 @@ private static IXRefContainer ReadLocalFile(string filePath)

case ".json":
{
using var stream = File.OpenText(filePath);
return JsonUtility.Deserialize<XRefMap>(stream);
using var stream = File.OpenRead(filePath);
return await SystemTextJsonUtility.DeserializeAsync<XRefMap>(stream, token);
}

case ".yml":
Expand All @@ -134,7 +134,7 @@ private static IXRefContainer ReadLocalFile(string filePath)
}
}

protected static async Task<XRefMap> DownloadFromWebAsync(Uri uri)
private static async Task<XRefMap> DownloadFromWebAsync(Uri uri, CancellationToken token = default)
{
Logger.LogVerbose($"Reading from web: {uri.OriginalString}");

Expand All @@ -147,14 +147,13 @@ protected static async Task<XRefMap> DownloadFromWebAsync(Uri uri)
Timeout = TimeSpan.FromMinutes(30), // Default: 100 seconds
};

using var stream = await httpClient.GetStreamAsync(uri);
using var stream = await httpClient.GetStreamAsync(uri, token);

switch (Path.GetExtension(uri.AbsolutePath).ToLowerInvariant())
{
case ".json":
{
using var sr = new StreamReader(stream, bufferSize: 81920); // Default :1024 byte
var xrefMap = JsonUtility.Deserialize<XRefMap>(sr);
var xrefMap = await SystemTextJsonUtility.DeserializeAsync<XRefMap>(stream, token);
xrefMap.BaseUrl = ResolveBaseUrl(xrefMap, uri);
return xrefMap;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Docfx.Build/XRefMaps/XRefMapRedirection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class XRefMapRedirection
public string UidPrefix { get; set; }

[YamlMember(Alias = "href")]
[JsonProperty("Href")]
[JsonProperty("href")]
[JsonPropertyName("href")]
public string Href { get; set; }
}
7 changes: 6 additions & 1 deletion src/Docfx.Common/Docfx.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Spectre.Console" />
</ItemGroup>
Expand All @@ -13,4 +13,9 @@
<InternalsVisibleTo Include="Docfx.Dotnet"/>
</ItemGroup>

<!-- TODO: Following settings will be removed after NewtonsoftJson dependencies are removed. -->
<ItemGroup>
<InternalsVisibleTo Include="docfx.Build" />
<InternalsVisibleTo Include="Docfx.Build.Tests" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using System.Text.Json.Serialization;

#nullable enable

namespace Docfx.Common;

/// <summary>
/// Custom JsonConverters for <see cref="object"/>.
/// </summary>
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/converters-how-to?pivots=dotnet-8-0#deserialize-inferred-types-to-object-properties" />
/// <seealso href="https://github.com/dotnet/runtime/issues/98038" />
internal class ObjectToInferredTypesConverter : JsonConverter<object>
{
/// <inheritdoc/>
public override object? Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.True:
return true;
case JsonTokenType.False:
return false;
case JsonTokenType.Number when reader.TryGetInt32(out int intValue):
return intValue;
case JsonTokenType.Number when reader.TryGetInt64(out long longValue):
return longValue;
case JsonTokenType.Number:
return reader.GetDouble();
case JsonTokenType.String when reader.TryGetDateTime(out DateTime datetime):
return datetime;
case JsonTokenType.String:
return reader.GetString();
case JsonTokenType.Null:
return null;
case JsonTokenType.StartArray:
{
var list = new List<object?>();
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
{
object? element = Read(ref reader, typeof(object), options);
list.Add(element);
}
return list;
}
case JsonTokenType.StartObject:
{
try
{
using var doc = JsonDocument.ParseValue(ref reader);
return JsonSerializer.Deserialize<Dictionary<string, dynamic>>(doc, options);
}
catch (Exception)
{
goto default;
}
}
default:
{
using var doc = JsonDocument.ParseValue(ref reader);
return doc.RootElement.Clone();
}
}
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, object objectToWrite, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, objectToWrite, objectToWrite.GetType(), options);
}
}
Loading

0 comments on commit 466d704

Please sign in to comment.