如何从 powershell 枚举 csproj 文件中的 PropertyGroup 元素

How do I get enumerate the PropertyGroup elements in a csproj file from powershell

我正在 powershell 中编写 nuget 安装脚本,我想访问项目文件 (*.csproj) 的所有 PropertyGroup 元素?

我可以访问一个名为 $project 的变量,它表示一个对象,我相信它可以实现来自 EnvDTE 的 Project 接口 - 项目文件 (*.csproj) 的抽象。

如何从 $project 对象中获取 PropertyGroup 实例?

我希望 nuget install powershell 脚本能够更改 包含此子元素的所有 PropertyGroup 元素的输出路径。

PropertyGroup是.csproj文件的内部实现(MSBuild技术),而EnvDTE.Project是接口,所以用EnvDTE.Project不能直接获取或修改 MSBuild 元素,因为实际上在 VS 2005 之前,.csproj 不是基于 MSBuild 的,并且 EnvDTE.Project 接口已经存在并且可以使用以前的技术。

但是作为一个界面,你当然可以完成你的任务:

OutputPath 是每个项目配置的属性。您可以获得 EnvDTE.Project 的所有配置,如以下所述:

HOWTO: Get the projects configurations / platforms from a Visual Studio add-in

(加载项使用 EnvDTE)

一旦您拥有 EnvDTE.Configuration,您就可以访问其 EnvDTE.Configuration.Properties 集合,特别是 "OutputPath"。参见:

HOWTO: Get the output build folder from a Visual Studio add-in or macro

所有项目配置更改后,您可以调用EnvDTE.Project.Save or EnvDTE.Project.SaveAs passing the EnvDTE.Project.FullName值作为参数。

我找到了这个让我找到解决方案的答案:

这是我在 NuGet 包中使用的脚本:

$msbuild = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName) | Select-Object -First 1
($msbuild.Xml.PropertyGroups | Select-Object -First 1).AddProperty("TypeScriptCompileBlocked", "true")
$project.Save()