From 96c5b686a7706af0401754bcfd171e4cee818c1b Mon Sep 17 00:00:00 2001 From: Sergey Zwezdin Date: Sun, 6 Nov 2016 07:59:45 +0500 Subject: [PATCH] Recurse search support for VSTS extension #6 --- src/MagicChunks/VSTS/MagicChunks/task.json | 9 ++++ .../VSTS/MagicChunks/transform.ps1 | 52 ++++++++++++++----- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/MagicChunks/VSTS/MagicChunks/task.json b/src/MagicChunks/VSTS/MagicChunks/task.json index 223d4ed..f5f211e 100644 --- a/src/MagicChunks/VSTS/MagicChunks/task.json +++ b/src/MagicChunks/VSTS/MagicChunks/task.json @@ -46,6 +46,15 @@ "helpMarkDown": "Configuration file path which should be transformed.", "groupName": "sourcePath" }, + { + "name": "sourcePathRecurse", + "type": "boolean", + "label": "Recursive search", + "defaultValue": "false", + "required": false, + "groupName": "sourcePath", + "helpMarkDown": "Trying to serarch specified file in folder and sub-folders" + }, { "name": "fileType", "type": "pickList", diff --git a/src/MagicChunks/VSTS/MagicChunks/transform.ps1 b/src/MagicChunks/VSTS/MagicChunks/transform.ps1 index a7c536a..b6d0982 100644 --- a/src/MagicChunks/VSTS/MagicChunks/transform.ps1 +++ b/src/MagicChunks/VSTS/MagicChunks/transform.ps1 @@ -3,6 +3,9 @@ param( [String] [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] $sourcePath, + [bool] + $sourcePathRecurse, + [String] [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] $fileType, @@ -19,29 +22,54 @@ param( $transformations ) +# Init Magic Chunks + Add-Type -Path "$PSScriptRoot\MagicChunks.dll" -Write-Host "Transforming file $($sourcePath)" + +# Parse transforms try { $transforms = New-Object -TypeName MagicChunks.Core.TransformationCollection ` - foreach($t in ($transformations.Replace("\", "\\") | ConvertFrom-Json).psobject.properties) { $transforms.Add($t.name, $t.value) } +} +catch { + Write-Error -Message "Transforms parsing error: $($_.Exception.Message)" -Exception $_.Exception + throw; +} - if ($targetPathType -eq "source") { - $target = $sourcePath; - } - elseif ($targetPathType -eq "specific") { - $target = $targetPath; - } - [MagicChunks.TransformTask]::Transform(($fileType, $null)[[string]::IsNullOrWhitespace($fileType) -or ($fileType -eq "Auto")], $sourcePath, $target, $transforms) +# Find files to transform - Write-Host "File transformed to $($target)" +if ($sourcePathRecurse) { + $files = Get-ChildItem $sourcePath -Recurse } -catch { - Write-Error -Message "File transformation error: $($_.Exception.Message)" -Exception $_.Exception +else { + $files = Get-ChildItem $sourcePath } + +foreach ($file in $files) { + + # Transform file + + Write-Host "Transforming file $($file)" + + try { + if ($targetPathType -eq "source") { + $target = $file; + } + elseif ($targetPathType -eq "specific") { + $target = $targetPath; + } + + [MagicChunks.TransformTask]::Transform(($fileType, $null)[[string]::IsNullOrWhitespace($fileType) -or ($fileType -eq "Auto")], $sourcePath, $target, $transforms) + + Write-Host "File transformed to $($target)" + } + catch { + Write-Error -Message "File $($file) transformation error: $($_.Exception.Message)" -Exception $_.Exception + } +} \ No newline at end of file