Skip to content

Commit

Permalink
Add source and output directory support with wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
qetza committed Mar 19, 2019
1 parent 16facef commit 9d139e2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 33 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ Parameters include:

> **Syntax**: {xdt path} => {xml path}[ => {output path}]
>
> - `web.release.config => web.config` will apply web.release.config to web.config and update the file.
> - `xdt\web.release.config => config\web.config => web.config` will apply xdt\web.release.config to config\web.config and save the result in web.config.
> - `*.release.config => .config` enabled wildcard search, the search should start with `*`. This will apply all {filename}.release.config files to the {filename}.config of the same name. The search is recursive from the working directory.
> - `*.release.config => .config => .xml` enabled wildcard search, the search should start with `*`. This will apply all {filename}.release.config files to the {filename}.config and save it in the {filename}.xml. The search is recursive from the working directory.
> - `web.release.config => web.config` will apply _web.release.config_ to _web.config_ and update the file.
> - `xdt\web.release.config => config\web.config => web.config` will apply _xdt\web.release.config_ to _config\web.config_ and save the result in _web.config_.
>
> **Wildcard support**
> - `*.release.config => *.config` will apply all _{filename}.release.config_ files to _{filename}.config_ and update the file.
> - `*.release.config => config\*.config => c:\tmp\*.config` will apply all _{filename}.release.config_ files to _config\\{filename}.config_ and save the result in _c:\tmp\\{filename}.config_.
>
> Transform pattern must start with _*_
> Transform file search is recursive
> Relative paths for source pattern and output pattern are relative to the transform file path.
## Tips
You can use the [XDT tranform task](https://marketplace.visualstudio.com/items?itemName=qetza.xdttransform) to inject tokens in your XML based configuration files configured for local development and then use the [Replace Tokens task](https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens) to replace those tokens with variable values:
Expand Down
78 changes: 49 additions & 29 deletions task/transform.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Function _ApplyTransform
}

# apply transformations
Write-Host "Applying transformations '${TransformFile}' on file '${SourceFile}'..."
Write-Host "Applying transformations '${TransformFile}' on '${SourceFile}' to '${OutputFile}'..."

$source = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument
$source.PreserveWhitespace = $true
Expand Down Expand Up @@ -72,7 +72,6 @@ try
# apply transforms
$transforms -split "(?:`n`r?)|," | % {
$rule = $_.Trim()
$wildcardSearch = $false

if (!$rule)
{
Expand All @@ -89,54 +88,75 @@ try
return
}

$transformFile = $ruleParts[0].Trim()

if($transformFile.startswith("*"))
{
$wildcardSearch = $true
$transformFileExtension = $transformFile.Substring(1, $transformFile.length - 1)
Write-Verbose "Wildcard mode enabled"
$defs = [PSCustomObject]@{
IsTransformWildcard = $false
IsTransformRelative = $false
TransformPattern = $ruleParts[0].Trim()
IsSourceRelative = $false
SourcePattern = $ruleParts[1].Trim()
IsOutputRelative = $false
OutputPattern = $null
}

if (![System.IO.Path]::IsPathRooted($transformFile))
if ($defs.TransformPattern.StartsWith('*'))
{
$transformFile = Join-Path $workingFolder $transformFile
$defs.IsTransformWildcard = $true
$defs.IsTransformRelative = $true
$defs.TransformPattern = $defs.TransformPattern.Substring(1)
}
else
{
$defs.IsTransformRelative = ![System.IO.Path]::IsPathRooted($defs.TransformPattern)
if ($defs.IsTransformRelative)
{
$defs.TransformPattern = Join-Path $workingFolder $defs.TransformPattern
}
}

$sourceFile = $ruleParts[1].Trim()
if (!$wildcardSearch -and ![System.IO.Path]::IsPathRooted($sourceFile))
$defs.IsSourceRelative = ![System.IO.Path]::IsPathRooted($defs.SourcePattern)
if ($defs.IsSourceRelative -and !$defs.IsTransformWildcard)
{
$sourceFile = Join-Path $workingFolder $sourceFile
$defs.SourcePattern = Join-Path $workingFolder $defs.SourcePattern
}

$outputFile = $sourceFile
$defs.IsOutputRelative = $defs.IsSourceRelative
$defs.OutputPattern = $defs.SourcePattern

if ($ruleParts.Length -eq 3)
{
$outputFile = $ruleParts[2].Trim()
if (!$wildcardSearch -and ![System.IO.Path]::IsPathRooted($outputFile))
$defs.OutputPattern = $ruleParts[2].Trim()
$defs.IsOutputRelative = ![System.IO.Path]::IsPathRooted($defs.OutputPattern)

if ($defs.IsOutputRelative -and !$defs.IsTransformWildcard)
{
$outputFile = Join-Path $workingFolder $outputFile
$defs.OutputPattern = Join-Path $workingFolder $defs.OutputPattern
}
}

if($wildcardSearch)
if ($defs.IsTransformWildcard)
{
$files = ls $transformFile -recurse | % FullName
foreach ($file in $files)
{
if($file.endswith($transformFileExtension, "CurrentCultureIgnoreCase"))
Get-ChildItem -Path $workingFolder -Filter "*$($defs.TransformPattern)" -Recurse | % {
$transformFile = $_
$token = $transformFile.Name.Substring(0, $transformFile.Name.Length - $defs.TransformPattern.Length)

$sourceFile = $defs.SourcePattern.Replace('*', $token)
if ($defs.IsSourceRelative)
{
$fileWithoutExtension = $file.Substring(0, $file.length - $transformFileExtension.Length)
$wildcardSource = $fileWithoutExtension + $sourceFile
$wildcardOutput = $fileWithoutExtension + $outputFile
$sourceFile = Join-Path $transformFile.DirectoryName $sourceFile
}

_ApplyTransform -SourceFile $wildcardSource -TransformFile $file -OutputFile $wildcardOutput
$outputFile = $defs.OutputPattern.Replace('*', $token)
if ($defs.IsOutputRelative)
{
$outputFile = Join-Path $transformFile.DirectoryName $outputFile
}

_ApplyTransform -TransformFile $transformFile.FullName -SourceFile $sourceFile -OutputFile $outputFile
}
}
else
else
{
_ApplyTransform -SourceFile $sourceFile -TransformFile $transformFile -OutputFile $outputFile
_ApplyTransform -SourceFile $defs.SourcePattern -TransformFile $defs.TransformPattern -OutputFile $defs.OutputPattern
}
}
}
Expand Down

0 comments on commit 9d139e2

Please sign in to comment.