Skip to content

Commit

Permalink
feat: Enable DotnetToolMode option when creating nupkg (#9115)
Browse files Browse the repository at this point in the history
feat: enable DotnetToolMode option when creating nupkg.
  • Loading branch information
filzrev authored Aug 23, 2023
1 parent 190ca6a commit 9bbaa5d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/docfx/Models/TemplateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ListCommand : Command
{
public override int Execute(CommandContext context)
{
Directory.GetDirectories(Path.Combine(AppContext.BaseDirectory, "templates"))
Directory.GetDirectories(GetTemplateBaseDirectory())
.Select(Path.GetFileName)
.ToArray()
.WriteLinesToConsole(ConsoleColor.White);
Expand Down Expand Up @@ -49,7 +49,7 @@ public override int Execute(CommandContext context, Options options)
Directory.CreateDirectory(outputFolder);

var templates = options.All || options.Templates is null || options.Templates.Length == 0 ?
Directory.GetDirectories(Path.Combine(AppContext.BaseDirectory, "templates"))
Directory.GetDirectories(GetTemplateBaseDirectory())
.Select(Path.GetFileName)
.ToArray()
: options.Templates;
Expand All @@ -69,4 +69,12 @@ public override int Execute(CommandContext context, Options options)
});
}
}

private static string GetTemplateBaseDirectory()
{
if (DataContracts.Common.Constants.Switches.IsDotnetToolsMode)
return Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../templates"));

return Path.Combine(AppContext.BaseDirectory, "templates");
}
}
28 changes: 28 additions & 0 deletions src/docfx/docfx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@
<Description>The docfx command line tool published as .NET tool</Description>
</PropertyGroup>

<!-- Custom target to merge docfx templates to TargetFramework independent folder.
By default, docfx templates is packed to `tools/$(TargetFramework)/any/templates/*`.
This target rewrite PackagePath and template files are placed at `templates/*` directory. -->
<Target Name="RewriteDocfxTemplateFiles" AfterTargets="PackTool">
<PropertyGroup>
<!-- Select first element of TargetFrameworks as template source (e.g. `net6.0` is selected when TargetFrameworks `net6.0;net7.0;net8.0`) -->
<DocfxTemplateSourceTargetFramework>$(TargetFrameworks.Split(";")[0])</DocfxTemplateSourceTargetFramework>
</PropertyGroup>
<!-- If TargetFramework is selected version. Rewrite template files PackagePath.-->
<ItemGroup Condition="'$(TargetFramework)' == '$(DocfxTemplateSourceTargetFramework)'">
<TfmSpecificPackageFile Update="@(TfmSpecificPackageFile)"
Condition="$([System.String]::new('%(TfmSpecificPackageFile.PackagePath)').StartsWith('tools/$(TargetFramework)/any/templates/'))"
PackagePath="$([System.String]::new('%(TfmSpecificPackageFile.PackagePath)').Replace('tools/$(TargetFramework)/any/',''))"/>
</ItemGroup>
<!-- If TargetFramework is not selected version. Remove template files from package. -->
<ItemGroup Condition="'$(TargetFramework)' != '$(DocfxTemplateSourceTargetFramework)'">
<TfmSpecificPackageFile Remove="@(TfmSpecificPackageFile)"
Condition="$([System.String]::new('%(TfmSpecificPackageFile.PackagePath)').StartsWith('tools/$(TargetFramework)/any/templates/'))"/>
</ItemGroup>
</Target>

<!-- Target to add custom configProperties to `docfx.runsettings.json`. See [.NET Runtime configuration settings](https://learn.microsoft.com/en-us/dotnet/core/runtime-config) -->
<Target Name="SetDocfxDotNetToolMode" BeforeTargets="PrepareForBuild" Condition="'$(_IsPacking)'=='true' AND '$(_IsPublishing)'!='true'">
<ItemGroup>
<RuntimeHostConfigurationOption Include="Docfx.DotnetToolMode" Value="true" />
</ItemGroup>
</Target>

<ItemGroup>
<InternalsVisibleTo Include="docfx.Snapshot.Tests" />
</ItemGroup>
Expand Down

0 comments on commit 9bbaa5d

Please sign in to comment.