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

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

问题描述

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

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文件(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不存在,则应使其忽略目标.

Also, it would be nice that when my package is removed, the custom target disappears too, although I have added a condition that should make it ignore the target if my custom task DLL is not present.

我可以编写将添加自定义目标的最简单的Powershell nuget安装脚本是什么?我感觉PowerShell脚本此处可能构成解决方案的一部分,但我还不够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()

应该的.

致谢

Jesper Hauge

Jesper Hauge

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

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