Skip to content

Commit

Permalink
Echo properties, close #221
Browse files Browse the repository at this point in the history
  • Loading branch information
nightroman committed Jul 5, 2024
1 parent c12e922 commit 1eabff9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Invoke-Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ function *Echo {
Write-Build 3 "exec {$(if (${*t} -match '((?:\r\n|[\r\n]) *)\S') {"$(${*t}.TrimEnd().Replace($matches[1], "`n "))`n"} else {${*t}})}"
Write-Build 8 "cd $global:pwd"
foreach(${*v} in ${*c}.Ast.FindAll({$args[0] -is [System.Management.Automation.Language.VariableExpressionAst]}, $true)) {
${*p} = ${*v}.Parent
if (${*p} -is [System.Management.Automation.Language.MemberExpressionAst]) {
if (${*p} -is [System.Management.Automation.Language.InvokeMemberExpressionAst]) {continue}
${*v} = ${*p}
}
if (${*v}.Parent -isnot [System.Management.Automation.Language.AssignmentStatementAst]) {
${*t} = "${*v}" -replace '^@', '$'
Write-Build 8 "${*t}: $(& ([scriptblock]::Create(${*t})))"
Expand Down
4 changes: 4 additions & 0 deletions Release-Notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Invoke-Build Release Notes

## v5.11.3

`exec -Echo`: echo properties, #221

## v5.11.2

Like `ib.cmd`, the dotnet tool `ib` uses the environment variable `pwsh`.
Expand Down
33 changes: 31 additions & 2 deletions Tests/Exec.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

Import-Module .\Tools

$PSv3 = $PSVersionTable.PSVersion.Major -ge 3

task ExecWorksCode0 {
$r = exec {
if (Test-Unix) {
Expand Down Expand Up @@ -81,7 +83,7 @@ task ExecShouldUseGlobalLastExitCode {
}

# New switch Echo, #176 #179
task Echo1 -If ($PSVersionTable.PSVersion.Major -ge 3) {
task Echo1 -If $PSv3 {
# different kind variables
$env:SOME_VAR = 'SOME_VAR'
$script:foo = 'foo'
Expand Down Expand Up @@ -140,7 +142,7 @@ task ErrorMessage {
}

# #192
task Echo2 -If ($PSVersionTable.PSVersion.Major -ge 3) {
task Echo2 -If $PSv3 {
function *Write { $args[1] }

#! 1 line, make 1 leading and trailing space
Expand Down Expand Up @@ -211,3 +213,30 @@ task StdErrBadCommand {
assert ("$_" -like "*The term 'BadCommand'*")
}
}

# Echo properties, #221
task EchoProperties -If $PSv3 {
Set-Alias Write-Build Write-Build-Fake
function Write-Build-Fake($Color, $Text) {$Text}

$v1 = '_v1'
$v2 = [pscustomobject]@{p1 = '_v2'; p2 = 'bug'}
$v2 | Add-Member -Name m1 -MemberType ScriptMethod -Value {'bug'}

$r = exec -Echo {
# printed
$null = $v1
$null = $v2.p1

# not printed
$assign1 = 42
$v2.p2 = 'bug'
$null = $v2.m1()
}

equals $r.Count 4
assert $r[0].StartsWith('exec {')
assert $r[1].StartsWith('cd ')
equals $r[2] '$v1: _v1'
equals $r[3] '$v2.p1: _v2'
}

0 comments on commit 1eabff9

Please sign in to comment.