为 nuget 创建自定义 powershell 脚本,将自定义目标添加到 csproj BeforeBuild 步骤 [英] Create a custom powershell script for nuget that adds a custom target to the csproj BeforeBuild step

查看:15
本文介绍了为 nuget 创建自定义 powershell 脚本,将自定义目标添加到 csproj BeforeBuild 步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 nuget 包,使用我创建的自定义 MSBuild 任务将 BeforeBuild 步骤添加到我的 csproj.理想情况下,我想:

I want to create a nuget package that adds a BeforeBuild step to my csproj using a custom MSBuild task I have created. Ideally, I want to:

  1. 在 csproj 文件中添加一个新的 Target (MyCustomBeforeBuildTarget)
  2. 如果没有 BeforeBuild 目标,请添加它
  3. 编辑 BeforeBuild DependsOnTargets 属性以包含我的自定义目标

所以安装后我的 csproj 应该有以下内容:

So after install my csproj should have the following in:

<Target Name="MyCustomBeforeBuildTarget" Condition="SomeCondition">
     <MyCustomTask />
</Target>
<Target Name="BeforeBuild" DependsOnTargets="MyCustomBeforeBuildTarget">

</Target>

另外,如果我的包被删除,自定义目标也消失了,那就太好了,虽然我添加了一个条件,如果我的自定义任务 DLL 不存在,它应该忽略目标.

我可以编写的最简单的 Powershell nuget 安装脚本是什么,它将添加我的自定义目标?我觉得 PowerShell 脚本 here 可能是解决方案的一部分,但我没有足够的PowerShell 经验知道如何实际使用它们来编辑 csproj.

What is the simplest Powershell nuget install script I can write that will add my custom target? I have a feeling that the PowerShell scripts here might form part of the solution, but I don't have enough PowerShell experience to know how to actually use them to edit the csproj.

推荐答案

NuGetPowerTools 是一个包添加了一些 PowerShell 函数,可以更轻松地处理要添加包的项目的项目设置.要使用可用的功能,您只需使您的包依赖于 NuGetPowerTools,使用包 nuspec 文件中的依赖项标记,如下所示:

The NuGetPowerTools is a package that adds some PowerShell functions that makes it easier to work with the project setup of the project you're adding a package to. To use the functions available you only need to make your package depend on the NuGetPowerTools, using the dependencies tag in your packages nuspec file like this:

<dependencies>
  <dependency id="NuGetPowerTools" version="0.26" />
</dependencies>

这将使获取对您项目的构建项目表示的引用成为可能.

This will make it possible to grab a reference to a build project representation of your project.

然后你需要在你的 NuGet 包的 tools 文件夹中放置一个 install.ps1 文件,这个 PowerShell 文件将在你安装包时运行,之后不再运行安装.

Then you need to put an install.ps1 file in the tools folder of your NuGet package, this PowerShell file will run when you install the package and never again after installation.

文件应如下所示:

#First some common params, delivered by the nuget package installer
param($installPath, $toolsPath, $package, $project)

# Grab a reference to the buildproject using a function from NuGetPowerTools
$buildProject = Get-MSBuildProject

# Add a target to your build project
$target = $buildProject.Xml.AddTarget("PublishWebsite")

# Make this target run before build
# You don't need to call your target from the beforebuild target,
# just state it using the BeforeTargets attribute
$target.BeforeTargets = "BeforeBuild"

# Add your task to the newly created target
$target.AddTask("MyCustomTask")

# Save the buildproject
$buildProject.Save()

# Save the project from the params
$project.Save()

应该是这样的.

问候

杰斯珀·豪格

这篇关于为 nuget 创建自定义 powershell 脚本,将自定义目标添加到 csproj BeforeBuild 步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆