Web.Config在Microsoft MSBuild之外转换?

Wha*_*ool 52 msbuild powershell powershell-2.0 xml-document-transform

是否可以使用Microsoft的XML文档转换,在MSBuild之外准备web.configs?我想使用PowerShell进行这些转换,而无需通过MSBuild引擎运行它.如果Microsoft使用标准XSLT,那么在PowerShell中很容易做到.据我所知,我必须使用他们需要构建引擎的C:\ Program Files(x86)\ MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll.谢谢

Mik*_*sta 98

我创建了一个小函数来处理PowerShell中的Microsoft XML文档转换.

我将Microsoft.Web.XmlTransform.dll文件从Visual Studio构建文件夹复制到我的脚本路径,但如果您愿意,可以从源文件夹中引用它.

function XmlDocTransform($xml, $xdt)
{
    if (!$xml -or !(Test-Path -path $xml -PathType Leaf)) {
        throw "File not found. $xml";
    }
    if (!$xdt -or !(Test-Path -path $xdt -PathType Leaf)) {
        throw "File not found. $xdt";
    }

    $scriptPath = (Get-Variable MyInvocation -Scope 1).Value.InvocationName | split-path -parent
    Add-Type -LiteralPath "$scriptPath\Microsoft.Web.XmlTransform.dll"

    $xmldoc = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument;
    $xmldoc.PreserveWhitespace = $true
    $xmldoc.Load($xml);

    $transf = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdt);
    if ($transf.Apply($xmldoc) -eq $false)
    {
        throw "Transformation failed."
    }
    $xmldoc.Save($xml);
}
Run Code Online (Sandbox Code Playgroud)

使用web.release.config转换web.config:

XmlDocTransform -xml "Web.config" -xdt "Web.Release.config"
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用Sayed的自引导Xml转换脚本,它将负责为您获取Microsoft.Xml.Xdt.dll:

https://gist.github.com/sayedihashimi/f1fdc4bfba74d398ec5b

  • 我有一个自启动的PowerShell脚本,您可以在https://gist.github.com/sayedihashimi/f1fdc4bfba74d398ec5b上使用 (12认同)
  • 调整函数的语法的小修正:`XmlDocTransform"Web.config""Web.Release.config"` (6认同)

Say*_*imi 12

转换的逻辑包含在TransformXml任务本身内部.如果要从代码中调用它,则必须使用带有模拟引擎的MSBuild API并执行它.如果你愿意,我有一些代码.

在你提到PowerShell的情况下,你要做的最好的事情就是创建一个包装器MSBuild文件来调用TransformXml任务.我这样说是因为PowerShell配置为在.NET 2.0下运行,但TransformXml任务需要.NET 4.0.为了从虚拟MSBuild文件中调用它,您可以在http://sedodream.com/2010/04/26/ConfigTransformationsOutsideOfWebAppBuilds.aspx上查看我的博客,但我还粘贴了以下链接中的示例.

<Project ToolsVersion="4.0" DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="TransformXml"
             AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>

    <Target Name="Demo">
        <TransformXml Source="app.config"
                      Transform="Transform.xml"
                      Destination="app.prod.config"/>
    </Target>
</Project>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢Sayed,我实际上选择了你的解决方案.我稍微修改了它以包含3个项目的属性组,因此我可以在PowerShell的命令行上覆盖它们.从你的书中了解到:)`<PropertyGroup> <TransformSource> Web.config </ TransformSource> <Transformer> Web.Debug.config </ Transformer> <TransformDest> Web.Prod.config </ TransformDest> </ PropertyGroup> < Target Name ="TransConfig"> <TransformXml Source ="$(TransformSource)"Transform ="$(Transformer)"Destination ="$(TransformDest)"/> </ Target>` (2认同)

Say*_*imi 10

Microsoft已将XDT发布到codeplex http://xdt.codeplex.com和NuGet包https://www.nuget.org/packages/Microsoft.Web.Xdt/.我还创建了一个带有MSBuild任务的NuGet pig,TransformXml和一个.exe来调用它们https://www.nuget.org/packages/SlowCheetah.Xdt/1.1.6-beta.

对于PowerShell,我创建了一个自引导脚本,您可以使用https://gist.github.com/sayedihashimi/f1fdc4bfba74d398ec5b.

有关自引导脚本的更多信息,请访问http://sedodream.com/2014/07/22/StopCheckinginBinariesInsteadCreateSelfbootstrappingScripts.aspx.


Seb*_*n K 9

基于Michel的回答,我写了一个C#函数,它将完成相同的工作.

当然你可以使用PowerShell调用生成的DLL,但我实际上是在寻找一个完全编程的版本,所以在这里,以防其他人正在寻找类似的解决方案:

using Microsoft.Web.XmlTransform;

...

public static void TransformConfig(string configFileName, string transformFileName)
{
     var document = new XmlTransformableDocument();
     document.PreserveWhitespace = true;
     document.Load(configFileName);

     var transformation = new XmlTransformation(transformFileName);
     if (!transformation.Apply(document))
     {
         throw new Exception("Transformation Failed");
     }
     document.Save(configFileName);
}
Run Code Online (Sandbox Code Playgroud)

您只需要包含以下参考:

C:\ Program Files(x86)\ MSBuild\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.XmlTransform.dll


小智 6

我稍微更新了脚本以使其与最新版本的PowerShell一起使用并使其更容易一些.

function XmlDocTransform($xml, $xdt)
{
      $scriptpath = $PSScriptRoot + "\"
      $xmlpath = $scriptpath + $xml
      $xdtpath = $scriptpath + $xdt

      if (!($xmlpath) -or !(Test-Path -path ($xmlpath) -PathType Leaf)) {
         throw "Base file not found. $xmlpath";
      }

      if (!($xdtpath) -or !(Test-Path -path ($xdtpath) -PathType Leaf)) {
         throw "Transform file not found. $xdtpath";
      }

      Add-Type -LiteralPath "$PSScriptRoot\Microsoft.Web.XmlTransform.dll"

      $xmldoc = New-Object   Microsoft.Web.XmlTransform.XmlTransformableDocument;
      $xmldoc.PreserveWhitespace = $true
      $xmldoc.Load($xmlpath);

      $transf = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdtpath);
      if ($transf.Apply($xmldoc) -eq $false)
      {
          throw "Transformation failed."
      }
      $xmldoc.Save($xmlpath);

      Write-Host "Transformation succeeded" -ForegroundColor Green
  }
Run Code Online (Sandbox Code Playgroud)

并调用函数使用

 XmlDocTransform "App.config" "App.acc.config"
Run Code Online (Sandbox Code Playgroud)


Sof*_*ter 5

由于MSDeploy具有PowerShell脚本API,使您可以转换和部署程序包,因此请使用MSDeploy。

您还可以查看XML-Document-Transform,如果需要,可以编写自己的代码来执行Transform。

这是一个执行类似操作的Codeplex项目。XDT转换工具