创建包含 .NET Core 自定义构建步骤的 nuget 包

Creating a nuget package containing a custom build step for .NET Core

是否可以创建包含自定义构建步骤的 nuget 包(分析生成的程序集并自动生成一些元数据)?目标环境应该是具有新 .csproj 格式的 .NET Core(当前使用 Visual Studio 2017 RC)。最好的情况是,如果这仅添加一个 msbuild 步骤并且不依赖于项目生成的程序集。

如果看到完整的 .NET 应该是可能的,但 msbuild 的文档和 .NET Core 的新 .csproj 目前有点稀疏。

如果在您的 nuget 包中找到它,Nuget 可能会在安装过程中执行“init.ps1”powershell 脚本。但请注意,脚本支持已在 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.

您可以在“Powershell 安装和卸载脚本”中找到更多 here

您可以将 MSBuild 目标添加到 "build" 文件夹中的 NuGet 包。目标或道具文件名必须与包 ID 匹配。通过 "PackageReference" 或 packages.config 安装时,NuGet 会自动将 targets/props 文件导入 csproj,因此这些文件必须作为有效的 MSBuild 代码编写。

即如果你的包被命名为 "MyClassLibrary",你可以有以下布局:

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

例如,参见 GitHub 上的 Microsoft.Extensions.Configuration.UserSecrets on NuGet.org or and its source code。此包使用 MSBuild 目标在编译步骤之前自动生成程序集属性