Skip to content

Commit 9530967

Browse files
committed
Fixed bug with nested filters interpolation
1 parent 44d2130 commit 9530967

15 files changed

+53
-52
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[![GitHub workflows (all tests)](https://github.com/wild-devops/pwshake/workflows/all%20tests/badge.svg)](/actions/workflows/tests.yml)
2-
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/wild-devops/pwshake)](/releases/tag/v1.5.0)
2+
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/wild-devops/pwshake)](/releases/tag/v1.5.1)
33
[![PowerShell Gallery](https://img.shields.io/powershellgallery/v/pwshake)](https://www.powershellgallery.com/packages/pwshake)
44
[![PowerShell Gallery](https://img.shields.io/powershellgallery/dt/pwshake)](https://www.powershellgallery.com/packages/pwshake)
55

@@ -123,7 +123,7 @@ invoke_tasks:
123123
attributes:
124124
pwshake_module_path: /path/to/pwshake/module/source
125125
pwshake_path: /absolute/path/to/your/working/directory
126-
pwshake_version: 1.6.0
126+
pwshake_version: 1.5.1
127127
work_dir: /absolute/path/to/process/working/directory
128128
pwshake_log_path: /absolute/path/to/your/working/directory/my_pwshake.log
129129
some_attribute: this is an attribute value

examples/4.complex/v1.0/includes/module1.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ attributes_overrides:
66
tasks:
77
role1: role3
88
apply_roles:
9-
- role1
10-
- role2
9+
- role1
10+
- role2

examples/4.complex/v1.0/includes/module3.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ roles:
22
role2: role2
33
role3: role3
44
apply_roles:
5-
- role1
6-
- role2
7-
- role5
8-
- role6
5+
- role1
6+
- role2
7+
- role5
8+
- role6

pwshake/pwshake.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'pwshake.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.6.0'
15+
ModuleVersion = '1.5.1'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()

pwshake/scripts/Arrange-Tasks.ps1

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
function Arrange-Tasks {
22
[CmdletBinding()]
33
param (
4-
[Parameter(Position = 0, Mandatory = $true)]
5-
[hashtable]$config,
4+
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
5+
[object[]]$depends_on,
66

7-
[Parameter(Position = 1, Mandatory = $false)]
8-
[object[]]$depends_on = $config.invoke_tasks,
7+
[Parameter(Mandatory = $false)]
8+
[hashtable]$config = (Peek-Config),
99

10-
[Parameter(Position = 2, Mandatory = $false)]
10+
[Parameter(Mandatory = $false)]
1111
[int]$depth = 0
1212
)
1313
process {
@@ -18,19 +18,21 @@ function Arrange-Tasks {
1818
$tasks = @()
1919

2020
foreach ($name in $depends_on) {
21+
"Arrange-Tasks:foreach:`$name:`n$(cty $name)" | f-log-dbg
2122
if (-not ($config.tasks.Keys -contains $name)) {
2223
throw "Task '$name' is undefined in the PWSHAKE config."
2324
}
2425

2526
$task = Build-Task $config.tasks[$name] $name
2627

2728
if ($task.depends_on) {
28-
$tasks += (Arrange-Tasks $config $task.depends_on ($depth + 1))
29+
$tasks += ($task.depends_on | Arrange-Tasks -depth ($depth + 1))
2930
}
3031

3132
$tasks += $task
3233
}
3334

35+
"Arrange-Tasks:Out:`$tasks:`n$(cty $tasks)" | f-log-dbg
3436
return $tasks
3537
}
3638
}

pwshake/scripts/Interpolate-Attributes.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ function Interpolate-Attributes {
1515
"Interpolate-Attributes:$($counter):`$substitute:$substitute" | f-log-dbg
1616
if ($substitute -match '^\$\((?<eval>.*)\)$') {
1717
"Interpolate-Attributes:$($counter):`$eval:{$($matches.eval)}" | f-log-dbg
18-
$value = Invoke-Expression (ConvertFrom-Json "`"$($matches.eval)`"")
18+
$value = "`"$($matches.eval)`"" | ConvertFrom-Json | Invoke-Expression
1919
} elseif ($substitute -match '^(?<filter>\$\S+):(?<input>.*)') {
2020
"Interpolate-Attributes:$($counter):`$filter:{$($matches.filter)}:`$input:{$($matches.input)}" | f-log-dbg
21-
$value = $matches.input | & "f-$($matches.filter)"
21+
$value = "`"$($matches.input)`"" | ConvertFrom-Json | & f-$($matches.filter)
2222
"Interpolate-Attributes:$($counter):`$value:{$value}" | f-log-dbg
2323
} else {
2424
$value = Invoke-Expression "`$config.attributes.$substitute" -ErrorAction Stop

pwshake/scripts/Invoke-Step.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function Invoke-Step {
1515

1616
try {
1717
"Invoke-Step:In:$(@{'$_'=$_} | ConvertTo-Yaml)" | f-log-dbg
18-
$step = $step | Build-Step
19-
"Invoke-Step:Build-Step:$(@{'$step'=$step} | ConvertTo-Yaml)" | f-log-dbg
18+
$step = $_ = $_ | Build-Step
19+
"Invoke-Step:Build-Step:$(@{'$_'=$_} | ConvertTo-Yaml)" | f-log-dbg
2020

2121
if ($step['$context'].template_key) {
2222
# add alias to simplify templates definition
@@ -59,7 +59,7 @@ function Invoke-Step {
5959
} catch {
6060
$_ | f-log-err
6161
if ($step.on_error -eq 'throw') {
62-
(Peek-Context).thrown = $true
62+
(Peek-Context).caught = $true
6363
throw $_
6464
}
6565
} finally {

pwshake/scripts/Invoke-Task.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function Invoke-Task {
44
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
55
[hashtable]$task,
66

7-
[Parameter(Position = 1, Mandatory = $false)]
7+
[Parameter(Mandatory = $false)]
88
[hashtable]$config = (Coalesce (Peek-Config), @{})
99
)
1010
process {
@@ -21,11 +21,11 @@ function Invoke-Task {
2121
Push-Location (Build-Path "$($task.work_dir)" $config)
2222

2323
foreach ($step in $task.steps) {
24-
(Peek-Context).thrown = $false
24+
(Peek-Context).caught = $false
2525
$step | Invoke-Step -work_dir $task.work_dir
2626
}
2727
} catch {
28-
if (-not (Peek-Context).thrown) {
28+
if (-not (Peek-Context).caught) {
2929
# if it was not thrown in execution context, it should be logged
3030
$_ | f-log-err
3131
}

pwshake/scripts/Invoke-pwshake.ps1

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,14 @@ function Invoke-pwshake {
6161
}
6262
}
6363

64-
$arranged_tasks = Arrange-Tasks (Peek-Config)
64+
$arranged_tasks = (Peek-Config).invoke_tasks | Arrange-Tasks
6565
"Invoke-pwshake:`$arranged_tasks:`n$(ConvertTo-Yaml $arranged_tasks)" | f-log-dbg
6666

6767
Push-Location (Peek-Config).attributes.work_dir
68-
foreach ($task in $arranged_tasks) {
69-
Invoke-Task $task
70-
}
68+
$arranged_tasks | Invoke-Task
69+
7170
} catch {
72-
if (-not (Peek-Context).thrown) {
71+
if (-not (Peek-Context).caught) {
7372
# if it was not thrown in execution context, it should be logged
7473
$_ | f-log-err
7574
}

pwshake/templates/invoke_steps.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
pwshake-context:
22
templates:
33
invoke_steps:
4-
each:
5-
items: $[[$_.invoke_steps]]
6-
action: $_ | Invoke-Step
4+
powershell: $_.invoke_steps | Invoke-Step

pwshake/templates/invoke_tasks.yaml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,10 @@ pwshake-context:
22
templates:
33
invoke_tasks:
44
powershell: |
5-
$tasks = Arrange-Tasks $config $_.invoke_tasks
6-
foreach ($task in $tasks) {
7-
Invoke-Task $task $config
8-
}
5+
$_.invoke_tasks | Arrange-Tasks | Invoke-Task
96
apply_roles:
107
powershell: |
11-
$tasks = Arrange-Tasks $config $_.apply_roles
12-
foreach ($task in $tasks) {
13-
Invoke-Task $task $config
14-
}
8+
$_.apply_roles | Arrange-Tasks | Invoke-Task
159
invoke_run_lists:
1610
powershell: |
17-
$tasks = Arrange-Tasks $config $_.invoke_run_lists
18-
foreach ($task in $tasks) {
19-
Invoke-Task $task $config
20-
}
11+
$_.invoke_run_lists | Arrange-Tasks | Invoke-Task

pwshake/templates/script.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ pwshake-context:
44
powershell: |
55
$paths = $config.scripts_directories | ForEach-Object { Join-Path $config.attributes.pwshake_path -ChildPath $_ }
66
$script_path = Get-ChildItem $paths -File `
7-
| Where-Object BaseName -eq $step.script `
7+
| Where-Object BaseName -eq $_.script `
88
| Select-Object -ExpandProperty FullName
99
"Script file: $script_path" | f-log-info
10-
if (-not $script_path) { throw "Script file: $($step.script).* not found." }
10+
if (-not $script_path) { throw "Script file: $($_.script).* not found." }
1111
& $script_path -attributes $config.attributes

tests/Arrange-Tasks.Context.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Context "Arrange-Tasks" {
88
$config.invoke_tasks = @('role1')
99

1010
It "Should return an Object[]" {
11-
(Arrange-Tasks $config) -is [Object[]] | Should -BeTrue
11+
($config.invoke_tasks | Arrange-Tasks) -is [Object[]] | Should -BeTrue
1212
}
1313

1414
It "Should throw on circular reference in depends_on" {
@@ -18,7 +18,7 @@ Context "Arrange-Tasks" {
1818
depends_on = @('role1')
1919
}
2020

21-
{ Arrange-Tasks $config } `
21+
{ $config.invoke_tasks | Arrange-Tasks } `
2222
| Should -Throw "Circular reference detected for dependant tasks in:"
2323
}
2424
}

tests/Build-Template.Context.ps1

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,10 @@ Context "Build-Template" {
138138
'@ | ConvertFrom-Yaml | ForEach-Object steps | Select-Object -First 1 | `
139139
Build-Template | Ensure-Template -sb { param($actual)
140140
$actual.on_error | Should -Be 'continue'
141-
$actual.powershell | Should -BeLike 'if (-not $each.items) { throw*'
142-
$actual.items[0].echo | Should -Be 'say'
143-
$actual.items[1].invoke_steps[0].echo | Should -Be 'hello'
144-
$actual.action | Should -Be '$_ | Invoke-Step'
145-
$actual['$context'].template_key | Should -Be 'each'
141+
$actual.powershell | Should -BeLike '$_.invoke_steps | Invoke-Step'
142+
$actual.invoke_steps[0].echo | Should -Be 'say'
143+
$actual.invoke_steps[1].invoke_steps[0].echo | Should -Be 'hello'
144+
$actual['$context'].template_key | Should -Be 'invoke_steps'
146145
}
147146
}
148147

tests/Interpolate-Attributes.Context.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,16 @@ Context "Interpolate-Attributes" {
121121
}
122122
} | Should -Throw "Circular reference detected for substitutions: {{a}} {{b}} {{c}}"
123123
}
124+
125+
It "Should substitute special characters" {
126+
$chars = '`~!@#$qwest%^&*()_-=+\|][}{";?.,></№' + "'"
127+
128+
Interpolate-Attributes @{
129+
attributes = @{a="{{b}}";b='{{$secured:{{c}}}}';c=$chars};
130+
} | Interpolate-Attributes | New-Variable -Name actual
131+
132+
$actual.attributes.a | Should -Be $chars
133+
$actual.attributes.b | Should -Be $chars
134+
$actual.attributes.c | Should -Be $chars
135+
}
124136
}

0 commit comments

Comments
 (0)