Last active
October 20, 2019 18:28
-
-
Save akuryan/80b3496237604a5e22c938d8eab97b28 to your computer and use it in GitHub Desktop.
Gets files in folder to be used as transformation files
This file contains 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
param( | |
[string]$folderWithTransforms, | |
[string]$webRoot, | |
[string]$roleName, | |
[string]$transformationAssemblyPath | |
) | |
Write-Output "Starting collecting files for transformations" | |
if ([string]::IsNullOrWhiteSpace($folderWithTransforms)) { | |
Write-Output "folderWithTransforms is not defined, so we are assuming it is same folder as script" | |
$folderWithTransforms = $PSScriptRoot | |
Write-Output "folderWithTransforms is $folderWithTransforms" | |
} | |
if ([string]::IsNullOrWhiteSpace($webRoot)) { | |
Write-Output "Webroot is not defined - so, we'll try to define it by convention" | |
$webRoot = Join-Path -Path $folderWithTransforms -ChildPath ..\.. -Resolve | |
Write-Output "webRoot is $webRoot" | |
} | |
function TransformFile { | |
param( | |
[string]$fileToTransform, | |
[string]$transformationFile, | |
[string]$transformationAssemblyPath | |
) | |
if ([string]::IsNullOrWhiteSpace($transformationAssemblyPath)) { | |
Write-Output "Loading Microsoft.Web.XmlTransform.dll from script directory" | |
Add-Type -Path $PSScriptRoot\Microsoft.Web.XmlTransform.dll | |
} else { | |
Write-Output "Loading Microsoft.Web.XmlTransform.dll from $transformationAssemblyPath" | |
Add-Type -Path $transformationAssemblyPath | |
} | |
$target = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument; | |
$target.PreserveWhitespace = $true | |
Write-Output "Loading $fileToTransform" | |
$target.Load($fileToTransform); | |
Write-Output "Transforming $fileToTransform with $transformationFile" | |
$transformation = New-Object Microsoft.Web.XmlTransform.XmlTransformation($transformationFile); | |
if ($transformation.Apply($target) -eq $false) | |
{ | |
throw "XDT transformation failed." | |
} | |
$target.Save($fileToTransform); | |
Write-Output "Finished transformation" | |
} | |
if ([string]::IsNullOrWhiteSpace($roleName)) { | |
$filter = ".xdt" | |
} else { | |
$filter = ".xdt.$roleName" | |
} | |
$extensionFilter = "*$filter" | |
Write-Output "Filtering for $extensionFilter" | |
$helixFolders = "Hotfix", "Website", "Project", "Foundation", "Feature" | |
$transformationFiles = @() | |
foreach($folder in $helixFolders) { | |
#to get ordered transformation - I wish to traverse folders based on theirs helix naming convention | |
$helixTransformPath = Join-Path $folderWithTransforms $folder | |
$transformationFiles += Get-ChildItem -Path $helixTransformPath -Filter $extensionFilter -r | |
} | |
foreach($transformFile in $transformationFiles) { | |
$configToTransform = $transformFile.FullName.ToLowerInvariant().Replace($folderWithTransforms.ToLowerInvariant(), [string]::Empty) | |
if ($configToTransform.ToLowerInvariant().Contains("bin") -or $configToTransform.ToLowerInvariant().Contains("obj")) { | |
continue; | |
} | |
$regex = [regex]::new(".+code\\(.+)\$filter") | |
$configToTransform = Join-Path $webRoot $regex.Replace($configToTransform, '$1') | |
TransformFile -fileToTransform $configToTransform -transformationFile $transformFile.FullName -transformationAssemblyPath $transformationAssemblyPath | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment