【问题标题】:How to add new PropertyGroup to csproj from powershell如何从 powershell 将新的 PropertyGroup 添加到 csproj
【发布时间】:2012-05-04 02:49:24
【问题描述】:

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

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

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

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

$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);
   }

提前致谢!

【问题讨论】:

    标签: powershell csproj


    【解决方案1】:

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

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

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

    这是脚本本身:

    $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);
    }
    

    如您所见,我必须先导入节点,因为它来自另一个文档

    【讨论】:

    • 如果 csproj 项目位于 TFS 服务器中,则任何源代码示例。需要先Check out再修改,然后修改文件,再check in。
    • @Kiquenet 网上有很多样本。获取最新版本的方法如下: TF.exe get "$/Performance Testing/Project" /force /recursive
    • 这会将:&lt;PropertyGroup xmlns=""&gt; 添加到我的无效文件中;如何让“xmlns”部分消失?
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2018-12-25
    • 2017-11-11
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    相关资源
    最近更新 更多