如何从Powershell将新的PropertyGroup添加到csproj [英] How to add new PropertyGroup to csproj from powershell

查看:89
本文介绍了如何从Powershell将新的PropertyGroup添加到csproj的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下ps1文件。该文件中存在三个问题。

I have the following ps1 file. There are three problems in this file.

第一季度。如何在XML中使用单引号和双引号?我搜寻了一下,发现我需要多加一个单引号。我试过了,但是没用。

Q1. How to use single quote and double-quote in XML? I googled and found that I need to put one extra single quote. I tried but didn't work.

第二季度。当我将新的PropertyGroup作为子级附加到Project节点时,出现错误类型错误。我该如何解决。

Q2. I got "wrong type" error when I append new PropertyGroup as a child to Project node. How can I fix it.

第三季度。我可以在项目节点上附加多个PropertyGroup吗?

Q3. Can I append multiple PropertyGroups to Project Node?

$dir = "C:\Work\scripttest\output\"
$ns = @{ defaultNamespace = "http://schemas.microsoft.com/developer/msbuild/2003" }

$configs = [xml]"<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Dev-1|AnyCPU'">
    <OutputPath>bin\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <Optimize>true</Optimize>
    <DebugType>pdbonly</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
  </PropertyGroup>  
  ";

Get-ChildItem $dir *.csproj -recurse | 
   % { 
   $content = [xml](gc $_.FullName); 
   $project = $content.Project;
   $project
   $project.AppendChild($configs);
   # $content.Save($_.FullName);
   }

谢谢!

推荐答案

Q1:powershell中的转义字符为`,而不是引号。请记住,您还应该转义$符号

Q1: Escape character in powershell is ` and not quote. Keep in mind you should also escape $ symbol

第二季度:您遇到了问题,因为 $ project.AppendChild(); XmlNode ,而您的 $ configs 是XmlDocument

Q2: You had problems because $project.AppendChild(); is XmlNode and your $configs is XmlDocument

Q3 :您可以,但不确定MsBuild是否会满意

Q3: You can, but not sure if MsBuild will be happy with it

这是脚本本身:

$dir = "C:\Work\scripttest\output\"
$ns = @{ defaultNamespace = "http://schemas.microsoft.com/developer/msbuild/2003" }

$configs = [xml] "<PropertyGroup Condition=`"'`$(Configuration)|`$(Platform)' == 'Dev-1|AnyCPU'`">
     <OutputPath>bin\</OutputPath>
     <DefineConstants>TRACE</DefineConstants>
     <Optimize>true</Optimize>
     <DebugType>pdbonly</DebugType>
     <PlatformTarget>AnyCPU</PlatformTarget>
     <ErrorReport>prompt</ErrorReport>
</PropertyGroup>"

Get-ChildItem $dir *.csproj -recurse | 
% { 
  $content = [xml](gc $_.FullName); 
  $importNode = $content.ImportNode($configs.DocumentElement, $true) 
  $project = $content.Project;
  $project
  $project.AppendChild($importNode);
  # $content.Save($_.FullName);
}

如您所见,我不得不导入Node fisrt,因为它来自另一个文档

As you can see I had to ImportNode fisrt as it was coming from another document

这篇关于如何从Powershell将新的PropertyGroup添加到csproj的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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