Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable DotnetToolMode option when creating nupkg #9115

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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