How can I automatically run a dotnet tool during compilation

87 views Asked by At

I want to write a NuGet package that automatically executes a dotnet tool prior to compilation. The dotnet tool will process a file, and generate (non-code) output.

This is easily done by adding a .targets file to my NuGet package:

\build
  \My.Package.targets

where we Exec the tool:

<!-- My.Package.targets -->
<Project>
  <ItemGroup>
    <FilesToProcess Include="**/*.my_extension" />
  </ItemGroup>

  <Target Name="Run Tool" BeforeTargets="CoreCompile">      
    <Exec Command="dotnet my-tool -input %(FilesToProcess.Identity)" />
  </Target>
</Project>

However, this still requires the user to create a tool manifest, and install the tool before building the project.

dotnet new tool-manifest
dotnet tool install My.Tool

I want to make this automatic - the process for the developer should be as simple as "install a NuGet package, which will install/reference the tool automatically".

I've considered:

  • Using DotNetCliToolReference to reference the tool, but that hasn't been supported since .NET Core 2.2.

  • Using Source Generators, but they have 2 key limitations: they can only generate code files, not any other kind of file; and they don't permit IO.

  • Adding instructions to the Readme about how to install the tool, e.g.

    Please remember to run dotnet new tool-manifest and dotnet tool install My.Tool before building this project

    but I'm a strong believer in reducing friction when starting on a new project. I try and automate anything past "clone the repository, and open the solution in your editor of choice".

  • Rewriting the tool as an MSBuild task, but I don't own the tool - it's a 3rd party tool.

Related questions:

0

There are 0 answers