MSBuild の外部で web.configs を準備するために、Microsoft の XML ドキュメント変換を使用することは可能ですか? MSBuild エンジンを介してこれを実行することなく、PowerShell を使用してこれらの変換を実行したいと考えています。Microsoft が標準の XSLT を使用していた場合、PowerShell で簡単に実行できます。私が言えることから、ビルドエンジンを必要とする C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll を使用する必要があります。ありがとう
8 に答える
PowerShell で Microsoft の XML Document Transform を処理する小さな関数を作成しました。
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);
}
web.release.config を使用して web.config を変換するには:
XmlDocTransform -xml "Web.config" -xdt "Web.Release.config"
または、Sayed の自己ブートストラップ Xml Transform スクリプトを使用して、Microsoft.Xml.Xdt.dll を取得することもできます。
変換のロジックは、TransformXmlタスク自体の内部に含まれています。コードから呼び出す場合は、MSBuildAPIをモックエンジンで使用して実行する必要があります。必要に応じて、このためのコードをいくつか用意しています。
あなたの場合、PowerShellについて言及したので、最善の方法は、ラッパーMSBuildファイルを作成してTransformXmlタスクを呼び出すことです。これは、PowerShellが.NET 2.0で実行するように構成されているためですが、TransformXmlタスクには.NET4.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>
の場合mono
、これは機能するはずです(mono 6.4、macos、2019でテスト済み):
<Project DefaultTargets="TransformConfig" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="TransformXml"
AssemblyFile="$(MSBuildSDKsPath)/Microsoft.NET.Sdk.Publish/tools/net46/Microsoft.NET.Sdk.Publish.Tasks.dll"/>
<PropertyGroup>
<TransformSource>Web.config</TransformSource>
<Transformer>Web.Live.config</Transformer>
<Destination>Output.Web.config</Destination>
</PropertyGroup>
<Target Name="TransformConfig">
<Message Text="From TransformSource : $(TransformSource)" />
<Message Text="Using Transform : $(Transformer)" />
<Message Text="Output : $(Destination)" />
<Message Text="MSBuildSDKsPath=$(MSBuildSDKsPath)" Condition="'$(MSBuildSDKsPath)' != ''" />
<TransformXml Source="$(TransformSource)" Transform="$(Transformer)" Destination="$(Destination)"/>
</Target>
</Project>
msbuild
これを実行するか、パラメータを指定して実行できます
msbuild /p:TransformSource=... /p:Transformer=...
スクリプトを少し更新して、最新バージョンの 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"
パッケージを変換してデプロイできる PowerShell スクリプト API があるため、MSDeploy の使用を検討してください。
XML-Document-Transformを見ることもできます。必要に応じて、独自のコードを記述して変換を実行できます。
これは、似たようなことをした codeplex プロジェクトです。XDT 変換ツール