添加解决方案级项目中的NuGet包 [英] Adding solution-level items in a NuGet package
问题描述
我想通过的NuGet一揽子解决方案的文件夹和解决方案的项目(不是项目)添加到解决方案文件。我想这将通过PowerShell的实现。我已经通过了的NuGet,PowerShell中,并EnvDTE文件看了看,想不通:
- 在哪些命令/方法,我会用?
- 在哪个标准的脚本我想在这样做,Init.ps1,Install.ps1,或别的地方?
下面是一个PowerShell脚本,将创建一个名为家长一个解决方案文件夹,并呼吁里面,一个孩子另一种解决方案文件夹。它还增加了一个项目文件(MyProject.csproj)儿童解决方案文件夹里面。
#获取的开放式解决方案。
$液=获取接口$ dte.Solution([EnvDTE80.Solution2])
#创建父解决方案文件夹。
$ parentProject = $ solution.AddSolutionFolder(母公司)
#创建子解决方案文件夹。
$ parentSolutionFolder =获取接口$ parentProject.Object([EnvDTE80.SolutionFolder])
$ childProject = $ parentSolutionFolder.AddSolutionFolder(儿童)
#将文件添加到子文件夹的解决方案。
$ childSolutionFolder =获取接口$ childProject.Object([EnvDTE80.SolutionFolder])
$文件名=D:\项目\ MyProject的\ MyProject.csproj
$ projectFile = $ childSolutionFolder.AddFromFile($文件名)
这里使用的两种主要的Visual Studio接口溶液2 并<一href="http://msdn.microsoft.com/en-us/library/envdte80.solutionfolder%28v=VS.100%29.aspx">SolutionFolder.它还使用其通过的NuGet提供的Get接口功能
有关解决方案,只包你应该把在init.ps1你的脚本,因为<一href="http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package#Automatically_Running_PowerShell_Scripts_During_Package_Installation_and_Removal">install.ps1只有基于项目的包调用。第一次安装软件包时Init.ps1运行一次为一个解决方案,每次解决方案重新打开在Visual Studio。
要任意文件(非项目文件)添加到解决方案文件夹,你需要做类似下面的内容:
$ vsSolution =获取接口$ dte.Solution([EnvDTE80.Solution2])
$ vsProject = $ vsSolution.AddSolutionFolder(newFolder)
$ projectItems =获取接口$ vsProject.ProjectItems([EnvDTE.ProjectItems])
$ projectItems.AddFromFile(pathToFileToAdd.txt)
什么是从这个PowerShell脚本缺少的是标准参数的声明在文件的顶部。
参数($ INSTALLPATH,$ toolsPath,$包,$项目)
什么也不翼而飞正在检查解决方案文件夹和文件夹的项目是否已经存在。我将离开这个作为一个练习你做。
I want to add solution folders and solution items (not projects) to a solution file via a NuGet package. I imagine this would be accomplished through Powershell. I've looked through the documentation for NuGet, Powershell, and EnvDTE and can't figure out:
- Which commands/methods I would use?
- Which standard script I would do this in, Init.ps1, Install.ps1, or somewhere else?
Here is a PowerShell script that will create a solution folder called Parent and another solution folder called Child inside that one. It also adds a project file (MyProject.csproj) inside the Child solution folder.
# Get the open solution.
$solution = Get-Interface $dte.Solution ([EnvDTE80.Solution2])
# Create the parent solution folder.
$parentProject = $solution.AddSolutionFolder("Parent")
# Create a child solution folder.
$parentSolutionFolder = Get-Interface $parentProject.Object ([EnvDTE80.SolutionFolder])
$childProject = $parentSolutionFolder.AddSolutionFolder("Child")
# Add a file to the child solution folder.
$childSolutionFolder = Get-Interface $childProject.Object ([EnvDTE80.SolutionFolder])
$fileName = "D:\projects\MyProject\MyProject.csproj"
$projectFile = $childSolutionFolder.AddFromFile($fileName)
The two main Visual Studio interfaces being used here are Solution2 and SolutionFolder. It also uses the Get-Interface function which is provided by NuGet.
For a solution-only package you should place your script in init.ps1 because install.ps1 is only invoked for project-based packages. Init.ps1 runs once for a solution when the package is first installed and every time the solution is re-opened in Visual Studio.
To add arbitrary files (non-project files) to a solution folder you will need to do something similar to the following:
$vsSolution = Get-Interface $dte.Solution ([EnvDTE80.Solution2])
$vsProject = $vsSolution.AddSolutionFolder("newFolder")
$projectItems = Get-Interface $vsProject.ProjectItems ([EnvDTE.ProjectItems])
$projectItems.AddFromFile("pathToFileToAdd.txt")
What is missing from this PowerShell script is the standard parameter declarations at the top of file.
param($installPath, $toolsPath, $package, $project)
What is also missing is checking whether the solution folder and folder item already exist. I shall leave that as an exercise for you to do.
这篇关于添加解决方案级项目中的NuGet包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!