Created
June 14, 2017 17:53
-
-
Save zaripych/eea1d86ffefa5a193b7f11f3765716c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This scripts applies an XD transform to a file | |
# | |
# For diagnostic the script outputs a diff between the original file and the transformed file. | |
# FOR MORE INFO ABOUT XDT TRANSFORMS READ HERE | |
# https://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx | |
[CmdletBinding()] | |
Param( | |
<# | |
.SYNOPSIS Transform file path | |
#> | |
[Parameter(Mandatory=$true)] | |
[ValidateScript({Test-Path $_})] | |
[ValidateNotNullOrEmpty()] | |
[string]$transformFilePath, | |
<# | |
.SYNOPSIS Transform target file path | |
#> | |
[Parameter(Mandatory=$true)] | |
[ValidateScript({Test-Path $_})] | |
[ValidateNotNullOrEmpty()] | |
[string]$transformTargetFilePath, | |
<# | |
.SYNOPSIS A directory where command line tools will be downloaded if they do not exist already | |
#> | |
[Parameter(Mandatory=$false)] | |
[ValidateScript({Test-Path $_})] | |
[ValidateNotNullOrEmpty()] | |
[string]$toolsPath = ([System.IO.Path]::GetFullPath(".\tools")), | |
<# | |
.SYNOPSIS Whether to print diff between files for diagnostic purpose | |
#> | |
[Parameter(Mandatory=$false)] | |
[bool]$printDiffForDiagnostic = $true | |
) | |
$sourceFilePath = $transformTargetFilePath | |
$dstFilePath = $transformTargetFilePath | |
$intermediateFilePath = $dstFilePath + ".new" | |
# Used in case if `nuget.exe` is not available in PATH | |
$nugetDownloadUrl = "http://nuget.org/nuget.exe" | |
<# | |
.SYNOPSIS | |
If nuget is not in the toolsPath | |
folder then it will be downloaded there. | |
#> | |
function Get-Nuget(){ | |
[cmdletbinding()] | |
param( | |
$toolsPathDir = ($toolsPath), | |
$nugetDownloadUrl = "http://nuget.org/nuget.exe" | |
) | |
process{ | |
$nugetDestPath = "nuget.exe" | |
if(!(Get-Command $nugetDestPath -ErrorAction SilentlyContinue)) { | |
$nugetDestPath = Join-Path -Path $toolsPathDir -ChildPath nuget.exe | |
if(!(Test-Path $nugetDestPath)){ | |
'Downloading nuget.exe' | Write-Verbose | |
# download nuget | |
$webclient = New-Object System.Net.WebClient | |
$webclient.DownloadFile($nugetDownloadUrl, $nugetDestPath) | |
# double check that is was written to disk | |
if(!(Test-Path $nugetDestPath)){ | |
throw 'unable to download nuget' | |
} | |
} | |
} | |
# return the path of the file | |
$nugetDestPath | |
} | |
} | |
function GetTransformXmlExe(){ | |
[cmdletbinding()] | |
param( | |
$toolsPathDir = ($toolsPath) | |
) | |
process { | |
if(!(Test-Path $toolsPathDir)){ | |
New-Item -Path $toolsPathDir -ItemType Directory | Out-Null | |
} | |
$xdtExe = (Get-ChildItem -Path $toolsPathDir -Include 'SlowCheetah.Xdt.exe' -Recurse) | Select-Object -First 1 | |
if(!$xdtExe){ | |
'Downloading xdt since it was not found in the toolsPath folder' | Write-Verbose | |
# nuget install SlowCheetah.Xdt -Prerelease -OutputDirectory toolsPathDir\ | |
$cmdArgs = @('install','SlowCheetah.Xdt','-Prerelease','-OutputDirectory',(Resolve-Path $toolsPathDir).ToString()) | |
'Calling nuget.exe to download SlowCheetah.Xdt with the following args: [{0} {1}]' -f (Get-Nuget -toolsPathDir $toolsPathDir -nugetDownloadUrl $nugetDownloadUrl), ($cmdArgs -join ' ') | Write-Verbose | |
&(Get-Nuget -toolsPathDir $toolsPathDir -nugetDownloadUrl $nugetDownloadUrl) $cmdArgs | Out-Null | |
$xdtExe = (Get-ChildItem -Path $toolsPathDir -Include 'SlowCheetah.Xdt.exe' -Recurse) | Select-Object -First 1 | |
# copy the xdt assemlby if the xdt directory is missing it | |
$xdtDllExpectedPath = (Join-Path $xdtExe.Directory.FullName 'Microsoft.Web.XmlTransform.dll') | |
if(!(Test-Path $xdtDllExpectedPath)){ | |
# copy the xdt.dll next to the slowcheetah .exe | |
$xdtDll = (Get-ChildItem -Path $toolsPathDir -Include 'Microsoft.Web.XmlTransform.dll' -Recurse) | Select-Object -First 1 | |
if(!$xdtDll){ throw 'Microsoft.Web.XmlTransform.dll not found' } | |
Copy-Item -Path $xdtDll.Fullname -Destination $xdtDllExpectedPath | Out-Null | |
} | |
} | |
if(!$xdtExe){ | |
throw ('SlowCheetah.Xdt not found. Expected location: [{0}]' -f $xdtExe) | |
} | |
$xdtExe | |
} | |
} | |
function Transform-Xml{ | |
[cmdletbinding()] | |
param( | |
[Parameter( | |
Mandatory=$true, | |
Position=0)] | |
$sourceFile, | |
[Parameter( | |
Mandatory=$true, | |
Position=1)] | |
$transformFile, | |
[Parameter( | |
Mandatory=$true, | |
Position=2)] | |
$destFile, | |
$toolsPathDir = ($toolsPath) | |
) | |
process { | |
# slowcheetah.xdt.exe <source file> <transform> <dest file> | |
$cmdArgs = @((Resolve-Path $sourceFile).ToString(), | |
(Resolve-Path $transformFile).ToString(), | |
$destFile) | |
'Calling slowcheetah.xdt.exe with the args: [{0} {1}]' -f (GetTransformXmlExe), ($cmdArgs -join ' ') | Write-Verbose | |
&(GetTransformXmlExe -toolsPathDir $toolsPathDir) $cmdArgs | Out-Null | |
} | |
} | |
Transform-Xml -sourceFile $sourceFilePath -transformFile $transformFilePath -destFile $intermediateFilePath | |
$gitLocation = "git" | |
foreach ($item in @("git", "C:\Program Files\Git\bin\git")) { | |
if (Get-Command $item -ErrorAction SilentlyContinue) { | |
$gitLocation = $item | |
break | |
} | |
} | |
$output = & $gitLocation diff `"$dstFilePath`" `"$intermediateFilePath`" 2>$null | |
if ($output.Length -eq 0) { | |
Write-Host "The target file at '$dstFilePath' was not changed, apparently transformations did not have any effect" | |
Write-Host | |
} else { | |
Write-Host "Changes has been made to the target file '$dstFilePath'" | |
Write-Host | |
if ($printDiffForDiagnostic) { | |
Write-Output $output | |
Write-Host | |
} | |
Copy-Item -Path $intermediateFilePath -Destination $dstFilePath -Force | |
Remove-Item $intermediateFilePath | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment