2
votes

Is it possible to create a nuget package containing a custom build step (analyzing the generated assembly and automatically generating some metadata)? The target environment should be .NET Core with the new .csproj format (Currently using Visual Studio 2017 RC). Best case would be if this only adds a msbuild step and no dependency to the projects resulting assembly.

If seen that it should be possible with full .NET, but the documentation for msbuild and the new .csproj for .NET Core is a bit sparse at the moment.

2
I would think that the way to do this would be via the built-in target imports. Not sure if it works for Core projects, but I don't see why it wouldn't. Much easier than mucking around with install scripts.Mike Zboray

2 Answers

2
votes

Nuget may execute "init.ps1" powershell script during installation if find it in your nuget package. But keep attention, that script support was modified in Nuget 3.x:

In Nuget 3.xx Powershell script support was modified to no longer execute install and uninstall scripts (install.ps1, uninstall.ps1), but init scripts are still executed.

Additionally, running NuGet outside of Visual Studio at the command-line or on other operating systems cannot execute these Powershell scripts as they relied on Visual Studio automation. Powershell is in NuGet to provide a shim to allow for missing features to run inside of Visual Studio Powershell script support was modified to no longer execute install and uninstall scripts, but init scripts are still executed.

You can find more here in "Powershell Install and Uninstall Scripts".

8
votes

You can add MSBuild targets to a NuGet package in the "build" folder. The targets or props file name must match the package ID. When installed via "PackageReference" or packages.config, NuGet will automatically import the targets/props file into the csproj, so these files must be written as valid MSBuild code.

i.e. if your package was named "MyClassLibrary", you could have the following layout:

(package root)
    lib/
        netstandard1.3/
            MyClassLibrary.dll
    build/
        netstandard1.3/
            MyClassLibrary.props
            MyClassLibrary.targets

For example, see Microsoft.Extensions.Configuration.UserSecrets on NuGet.org or and its source code on GitHub. This package uses an MSBuild target to automatically generate an assembly attribute before the compilation step