Web.Config transforms outside of Microsoft MSBuild?

后端 未结 8 2115
情书的邮戳
情书的邮戳 2020-11-29 18:13

Is it possible to use Microsoft\'s XML document transform, for preparing web.configs, outside of MSBuild? I would like to use PowerShell to do these transform without having

8条回答
  •  被撕碎了的回忆
    2020-11-29 18:48

    I came to this post well after it original posting but it helped resolve my issue so I thought I'd put my solution (based on the solution above) here for anyone having the same problem with a current visual studio / msbuild install.

    Currently in VS build time transforms are done using the SlowCheetah nuget pkg. If you can rely on this package in your project you can use the script I have placed below and it will automatically find the required assembly based on the installed cheetah version and perform your transform as needed.

    Hope this helps someone.

    param(  
      [ValidateScript({$(Test-Path $_) -eq $true})]
      [string] $NuGetRootPath,
    
      [ValidateScript({$(Test-Path $_) -eq $true})]
      [string] $BaseFile,
    
      [ValidateScript({$(Test-Path $_) -eq $true})]
      [string] $TransformFile,
    
      [string] $TargetPath
    )
    
    "[INFO] Creating Custom XML Transform..." | Out-Default   
    "[INFO] ==> Source:    $BaseFile" | Out-Default   
    "[INFO] ==> Transform: $TransformFile" | Out-Default   
    "[INFO] ==> Target:    $TargetPath" | Out-Default   
    
    $cheetahDir = Join-Path $NuGetRootPath *SlowCheetah* | Resolve-Path | Select-Object -Last 1 -ExpandProperty Path
    $xformDll = Join-Path $cheetahDir "tools\Microsoft.Web.XmlTransform.dll" | Resolve-Path | Select-Object -ExpandProperty Path
    Add-Type -LiteralPath $xformDll
    
    $xmldoc = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument;
    $xmldoc.PreserveWhitespace = $true
    $xmldoc.Load($BaseFile);
    
    "[INFO] Running Transform..." | Out-Default
    $transf = New-Object Microsoft.Web.XmlTransform.XmlTransformation($TransformFile);
    if ($transf.Apply($xmldoc) -eq $false) {
        throw "[ERROR] Transformation failed."
    }
    
    $xmldoc.Save($TargetPath);
    "[INFO] Transformation Complete..." | Out-Default
    

提交回复
热议问题