Skip to content

Commit

Permalink
feat: permits to specify the placement of overwrites (#9937)
Browse files Browse the repository at this point in the history
* feat: permits to specify the placement of overwrites

Related #9908
Related #7648

* Corrected a small bug after refactoring.

* IItemWithMetadata is now internal.

* PolySharp is no more active for Docfx.Common

---------

Co-authored-by: Yufei Huang <[email protected]>
  • Loading branch information
Patrick8639 and yufeih authored Jun 3, 2024
1 parent 7df2c32 commit 4439ad1
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<None Include="$(MSBuildThisFileDirectory)\README.md" Pack="true" PackagePath="\"/>
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="'$(ProjectName)' != 'Docfx.Common'">
<PackageReference Include="PolySharp">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
5 changes: 5 additions & 0 deletions src/Docfx.Common/Docfx.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
<ProjectReference Include="..\Docfx.YamlSerialization\Docfx.YamlSerialization.csproj" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Docfx.Build.ManagedReference"/>
<InternalsVisibleTo Include="Docfx.Dotnet"/>
</ItemGroup>

<!-- TODO: Following settings will be removed after NewtonsoftJson dependencies are removed. -->
<ItemGroup>
<InternalsVisibleTo Include="docfx.Build" />
Expand Down
33 changes: 33 additions & 0 deletions src/Docfx.Common/EntityMergers/MergePlacement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.\r
// The .NET Foundation licenses this file to you under the MIT license.


namespace Docfx.Common.EntityMergers;


/// <summary>
/// The placement for a merge.
/// </summary>
internal enum MergePlacement
{

/// <summary>
/// The placement is not specified.
/// </summary>
None,

/// <summary>
/// The override must be placed after the original content.
/// </summary>
After,

/// <summary>
/// The override must be placed before the original content.
/// </summary>
Before,

/// <summary>
/// The override must replace the original content.
/// </summary>
Replace
}
47 changes: 47 additions & 0 deletions src/Docfx.Common/EntityMergers/ReflectionEntityMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,55 @@ public void Merge(ref object source, object overrides, IMergeContext context)
{
return;
}


// Gets the placement of the override
var placement = MergePlacement.None;

{
if (overrides is IItemWithMetadata ovr
&& ovr.Metadata.TryGetValue("placement", out var placementValue))
{
placement =
placementValue switch
{
"after" => MergePlacement.After,
"before" => MergePlacement.Before,
"replace" => MergePlacement.Replace,
_ => MergePlacement.None
};
}
}


foreach (var prop in Props)
{

// Placement specified in the override file
if (placement != MergePlacement.None
&& prop.Prop.Name is "Remarks" or "Summary")
{
var o = prop.Prop.GetValue(overrides);

if (o is null)
continue;

var s = prop.Prop.GetValue(source);

s = placement switch
{
MergePlacement.After => $"{s}{o}",
MergePlacement.Before => $"{o}{s}",
MergePlacement.Replace => o.ToString()
};

prop.Prop.SetValue(source, s);

continue;
}


// Placement specified in the property
switch (prop.Option)
{
case MergeOption.Merge:
Expand Down
20 changes: 20 additions & 0 deletions src/Docfx.Common/IItemWithMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.\r
// The .NET Foundation licenses this file to you under the MIT license.


namespace Docfx.Common;


/// <summary>
/// An item that contains metadate
/// </summary>
internal interface IItemWithMetadata
{

/// <summary>
/// Gets the metadata.
/// </summary>
/// <value>The metadata.</value>
Dictionary<string, object> Metadata { get; }

}
2 changes: 1 addition & 1 deletion src/Docfx.Dotnet/ManagedReference/Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Docfx.DataContracts.ManagedReference;

public class ItemViewModel : IOverwriteDocumentViewModel
public class ItemViewModel : IOverwriteDocumentViewModel, IItemWithMetadata
{
[YamlMember(Alias = Constants.PropertyName.Uid)]
[JsonProperty(Constants.PropertyName.Uid)]
Expand Down

0 comments on commit 4439ad1

Please sign in to comment.