使用Init.ps1在csproj文件中添加项目

时间:2022-02-07 11:22:47

i am using Nuget to install a package due to some restrictions i have to generate a reference and the path of the assembly in my csproj file.

我正在使用Nuget来安装包,因为我必须在csproj文件中生成引用和程序集的路径。

i am think of doing it with Init.ps1 file which runs when you first install the package. the element that i want to add is the Reference element some thing like.

我想到使用Init.ps1文件来执行它,该文件在您第一次安装软件包时运行。我要添加的元素是Reference元素。

and should be like

应该是这样的

<Reference Include="Library.MyLib.Imaging, Version=255.255.255.255, Culture=neutral, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\packages\SDK.0.9.2\lib\wp8\X86\Library.mylib.Imaging.winmd</HintPath>
</Reference>

Can some body guide me that how i can add these to the csproj file. i belive that AddItem method can be used from the Project Class

有些人可以指导我如何将这些添加到csproj文件中。我相信可以从Project Class中使用AddItem方法

project Class Reference

项目类参考

thanks

谢谢

1 个解决方案

#1


1  

Be careful with the false assumption you made for init.ps1: that PowerShell script is run everytime the package is initialized (or every time the solution is loaded) which is likely not what you want in this scenario. You'll have to use install.ps1 instead, which is run once during installation of the package only.

请注意您对init.ps1所做的错误假设:每次初始化包时(或每次加载解决方案时)都会运行PowerShell脚本,这可能不是您在此方案中所需的。您将不得不使用install.ps1,它仅在安装程序包期间运行一次。

The main issue with making changes to a consuming project file is that you'll have to know the file name somehow. The parameters that get injected into the PowerShell scripts are the following ones:

更改耗材项目文件的主要问题是您必须以某种方式知道文件名。注入PowerShell脚本的参数如下:

param($installPath, $toolsPath, $package, $project)

param($ installPath,$ toolsPath,$ package,$ project)

I've had a similar requirement and what I did instead of automating it during package installation: I expose a cmdlet in the Package Manager Console for the consumer to use. You can find an example of my implementation on GitHub for the NuSpec package: https://github.com/myget/NuGetPackages/blob/master/NuSpec/tools/NuSpec.psm1

我有类似的要求和我所做的,而不是在软件包安装过程中自动执行:我在软件包管理器控制台中公开了一个cmdlet供消费者使用。您可以在GitHub上找到我在NuSpec程序包中实现的示例:https://github.com/myget/NuGetPackages/blob/master/NuSpec/tools/NuSpec.psm1

Maybe that will help you to get started. If you do have a proper solution to fully automate this during package installation, please feel free to share it here as well (or send me a Pull Request :-)).

也许这会帮助你开始。如果你有一个适当的解决方案,在包安装过程中完全自动化,请随时在这里分享(或给我一个拉请求:-))。

#1


1  

Be careful with the false assumption you made for init.ps1: that PowerShell script is run everytime the package is initialized (or every time the solution is loaded) which is likely not what you want in this scenario. You'll have to use install.ps1 instead, which is run once during installation of the package only.

请注意您对init.ps1所做的错误假设:每次初始化包时(或每次加载解决方案时)都会运行PowerShell脚本,这可能不是您在此方案中所需的。您将不得不使用install.ps1,它仅在安装程序包期间运行一次。

The main issue with making changes to a consuming project file is that you'll have to know the file name somehow. The parameters that get injected into the PowerShell scripts are the following ones:

更改耗材项目文件的主要问题是您必须以某种方式知道文件名。注入PowerShell脚本的参数如下:

param($installPath, $toolsPath, $package, $project)

param($ installPath,$ toolsPath,$ package,$ project)

I've had a similar requirement and what I did instead of automating it during package installation: I expose a cmdlet in the Package Manager Console for the consumer to use. You can find an example of my implementation on GitHub for the NuSpec package: https://github.com/myget/NuGetPackages/blob/master/NuSpec/tools/NuSpec.psm1

我有类似的要求和我所做的,而不是在软件包安装过程中自动执行:我在软件包管理器控制台中公开了一个cmdlet供消费者使用。您可以在GitHub上找到我在NuSpec程序包中实现的示例:https://github.com/myget/NuGetPackages/blob/master/NuSpec/tools/NuSpec.psm1

Maybe that will help you to get started. If you do have a proper solution to fully automate this during package installation, please feel free to share it here as well (or send me a Pull Request :-)).

也许这会帮助你开始。如果你有一个适当的解决方案,在包安装过程中完全自动化,请随时在这里分享(或给我一个拉请求:-))。