Alberto Chiesa
Alberto Chiesa

Reputation: 7350

Creating a nuget package with a post build action

I have a custom made command line tool, which performs some code generation operations. Basically, it takes one assembly as input and, through reflection, searches for certaing custom attributes, used to trigger generation of external (JavaScript) files.

Everything works (almost) fine, but the distribution and execution of the tool is somewhat disorganized. I would like to pack it as a Nuget package (hosted in a private repository), which would essentially contain the tool and a build target that should trigger the execution of the tool.

How should I package the tool? I read about the special Nuget tool, content, and build folders, and I don't know where to put what, and how to setup a custom target.

I don't know if the question is "too broad", but even if I know pretty exactly what I need, I'm in kind of a blank page syndrome.

Upvotes: 3

Views: 2872

Answers (2)

Matt Ward
Matt Ward

Reputation: 47937

I would put the tool into the build directory inside the NuGet package and then have a custom MSBuild .targets file in the same build directory. This MSBuild .targets file would then be written in such a way so it is called at some point during the build process.

\build
    \MyPackage.targets

Then your build targets file would insert itself into the build process:

<PropertyGroup>
    <BuildDependsOn>
        $(BuildDependsOn);
        MyCustomTarget
    </BuildDependsOn>
</PropertyGroup>

<Target Name=“MyCustomTarget“>
    <!-- Execute tool -->    
</Target>

The above should run the MyCustomTarget as the last item during the build.

Upvotes: 4

Сергей
Сергей

Reputation: 156

In post build action specify $(SolutionDir).nuget\nuget.exe pack $(ProjectPath) -IncludeReferencedProjects. Also you can specify nuspec file. As described here https://docs.nuget.org/create/nuspec-reference. Example:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>SDK</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <releaseNotes>Initial release</releaseNotes>
    <copyright>Copyright 2016</copyright>
    <tags>SDK</tags>
  </metadata>
  <files>
    <file src="tools\install.ps1" target="tools\install.ps1" />
    </files>
</package>

Upvotes: 1

Related Questions