简体   繁体   中英

Deliver command-line tool via NuGet for use in post-build events in Visual Studio

I've developed a command-line utility that takes .NET assemblies as input and generates XML as output for use by another product.

Ideally I'd have it:

  • Delivered via NuGet as a solution-level package
  • Accessible in post-build events at the project level
  • Bit where I'm stuck: Accessible in that post-build event just by specifying the command name without path
    • ie MyTool.exe /i $(TargetPath) as opposed to ..\\packages\\ToolName-1.0.0.0\\tools\\MyTool.exe /i $(TargetPath)

The first two things I can already do, and a post-build event command as above works just fine but only if I first open the Package Manager Console (even if I don't type any commands, just have to open it) - seemingly it's doing something cool with paths when it fires up so that I don't have to specify the ..\\packages\\ToolName-1.0.0.0\\tools\\MyTool.exe.

Is it possible for my NuGet package to use an Init.ps1 script (or some other mechanism) to adjust paths so that I needn't specify the full relative path to the tool when writing a post-build event, or to otherwise include run tool as a post-build step automatically?

Note - I accept NuGet mightn't be the ideal vector for delivering this sort of tool, but it would be convenient.

Chocolatey (which technically qualifies as using NuGet) has a very cool magical "shimming" facility.

In brief, any executable in your package will be available on the PATH automatically. It's very cool.

Just for the folks that come across this;

I've seen people use a nuget package for this, eg see nswag.msbuild: https://github.com/RicoSuter/NSwag/wiki/NSwag.MSBuild

In short the nuget package contains the tool as a command line utility executable. It also defines some buildprops that define the path to this tool (something along the lines of /packages/myToolNugetPkg/mytool.exe ) Then you can use this property in an msbuild task.

Also .NET core supports 'tools', which is exactly what you needed:

https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools

This might help you -> http://lostechies.com/joshuaflanagan/2011/06/24/how-to-use-a-tool-installed-by-nuget-in-your-build-scripts/

I used the solution with the wildcard in the 'CD' command, so my postbuild looks like this:

CD "$(SolutionDir)packages\ToolName*\tools"
MyTool.exe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM