2012-01-24 29 views
41

在MSBuild之外是否可以使用Microsoft的XML文檔轉換來準備web.configs?我想使用PowerShell來完成這些轉換,而不必通過MSBuild引擎運行。如果微軟使用標準XSLT,在PowerShell中很容易做到。從我能告訴我必須使用他們的C:\ Program Files文件(x86)\ MSBuild \ Microsoft \ VisualStudio \ v10.0 \ Web \ Microsoft.Web.Publishing.Tasks.dll需要一個構建引擎。 謝謝Web.Config在Microsoft MSBuild之外轉換嗎?

回答

77

我創建了一個小函數來處理Microsoft的PowerShell中的XML文檔轉換。

我將Visual Studio生成文件夾中的Microsoft.Web.XmlTransform.dll文件複製到我的腳本路徑中,但是如果您願意,可以從源文件夾引用它。

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

要使用web.release.config變換的web.config:

XmlDocTransform("Web.config", "Web.Release.config") 
+0

神奇的是,這正是我需要編寫一個C#版本 –

+10

我有一個自引導PowerShell腳本,你可以使用在https://gist.github.com/sayedihashimi/f1fdc4bfba74d398ec5b –

+1

非常好!我必須在$ xml和$ xdt上調用Resolve-Path,但除此之外,它非常有用!,thx – PierrOz

10

轉換的邏輯包含在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> 
+2

感謝賽義德,其實我去你的解決方案。我做了一些修改以包含3個項目的屬性組,所以我可以在PowerShell的命令行中覆蓋這些項目。據悉,從你的書:) '<的PropertyGroup> 的Web.config Web.Debug.config Web.Prod.config \t \t \t \t <目標名稱= 「TransConfig」> <的TransformXML 源= 「$(TransformSource)」 變換= 「$(變壓器)」 目的地= 「$(TransformDest)」/ > ' –

5

根據米歇爾的答案我寫了一個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); 
} 

你會只需要包含到以下參考:

C:\ Program Files文件 (86)\的MSBuild \微軟\ VisualStudio的\ 11.0 \網絡\ Microsoft.Web.XmlTransform.dll

+0

很好的答案,這需要更多的投票! – Matt

5

微軟已將XDT發佈到codeplex http://xdt.codeplex.com並作爲NuGet包https://www.nuget.org/packages/Microsoft.Web.Xdt/。我也用MSBuild任務創建了一個NuGet豬,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

1

我更新了腳本,使它能夠與最新版本的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 
    } 

並調用該函數使用

XmlDocTransform "App.config" "App.acc.config"