Skip to content

Commit c42830e

Browse files
author
James Brundage
committed
Updating until transpiler (supporting #153)
1 parent 97312e2 commit c42830e

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

Transpilers/Keywords/Until.psx.ps1

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,33 @@
1616
}
1717
} |.>PipeScript
1818
.EXAMPLE
19-
{
19+
Invoke-PipeScript {
2020
until "00:00:05" {
2121
[DateTime]::Now
2222
Start-Sleep -Milliseconds 500
2323
}
24+
}
25+
.EXAMPLE
26+
Invoke-PipeScript {
27+
until "12:17 pm" {
28+
[DateTime]::Now
29+
Start-Sleep -Milliseconds 500
30+
}
31+
}
32+
.EXAMPLE
33+
{
34+
$eventCounter = 0
35+
until "MyEvent" {
36+
$eventCounter++
37+
$eventCounter
38+
until "00:00:03" {
39+
"sleeping a few seconds"
40+
Start-Sleep -Milliseconds 500
41+
}
42+
if (-not ($eventCounter % 5)) {
43+
$null = New-Event -SourceIdentifier MyEvent
44+
}
45+
}
2446
} | .>PipeScript
2547
.EXAMPLE
2648
Invoke-PipeScript {
@@ -48,6 +70,10 @@ param(
4870
$CommandAst
4971
)
5072

73+
begin {
74+
$myCmdName = $MyInvocation.MyCommand.Name
75+
}
76+
5177
process {
5278
$CommandName, $CommandArgs = $commandAst.CommandElements
5379
if ($commandName -like ':*') {
@@ -60,7 +86,9 @@ process {
6086
# If the first arg is a command expression, it becomes do {} while ($firstArg)
6187
if (-not $firstArg -or $firstArg.GetType().Name -notin
6288
'ParenExpressionAst', 'ScriptBlockExpressionAst',
63-
'VariableExpressionAst','MemberExpressionAst','ExpandableStringExpressionAst') {
89+
'VariableExpressionAst','MemberExpressionAst',
90+
'string',
91+
'ExpandableStringExpressionAst') {
6492
Write-Error "Until must be followed by a Variable, Member, ExpandableString, or Parenthesis Expression"
6593
return
6694
}
@@ -75,13 +103,37 @@ process {
75103
$firstArg.Pipeline.PipelineElements.Count -eq 1 -and
76104
$firstArg.Pipeline.PipelineElements[0].Expression -and
77105
$firstArg.Pipeline.PipelineElements[0].Expression.GetType().Name -in
78-
'VariableExpressionAst','MemberExpressionAst','ExpandableStringExpressionAst') {
106+
'VariableExpressionAst','MemberExpressionAst','ExpandableStringExpressionAst',
107+
'StringConstantExpressionAst') {
79108
$condition = $firstArg.Pipeline.PipelineElements[0].Expression
80109
}
81110
elseif ($firstArg.GetType().Name -eq 'ScriptBlockExpressionAst') {
82111
$condition = $firstArg -replace '^\{' -replace '\}$'
83112
}
84113

114+
$BeforeLoop = ''
115+
116+
$callstack = Get-PSCallStack
117+
$callCount = @($callstack |
118+
Where-Object { $_.InvocationInfo.MyCommand.Name -eq $myCmdName}).count - 1
119+
$untilVar = '$' + ('_' * $callCount) + 'untilStartTime'
120+
121+
if ($condition -is [string]) {
122+
123+
124+
if ($condition -as [Timespan]) {
125+
$beforeLoop = "$untilVar = [DateTime]::Now"
126+
$condition = "(([DateTime]::Now - $untilVar) -ge ([Timespan]'$Condition'))"
127+
}
128+
elseif ($condition -as [DateTime]) {
129+
$condition = "[DateTime]::Now -ge ([DateTime]'$Condition')"
130+
}
131+
else {
132+
$beforeLoop = "$untilVar = [DateTime]::Now"
133+
$condition = 'Get-Event -SourceIdentifier ' + "'$condition'" + " -ErrorAction Ignore | Where-Object TimeGenerated -ge $untilVar"
134+
}
135+
}
136+
85137
$conditionScript = [ScriptBlock]::Create($condition)
86138
$LoopScript = $secondArg
87139

@@ -98,6 +150,7 @@ process {
98150

99151

100152
$newScript = @"
153+
$(if ($BeforeLoop) { $BeforeLoop + [Environment]::NewLine})
101154
$(if ($CommandName -like ':*') { "$CommandName "})do {
102155
$untilTranspiled
103156
} while $conditionScript

0 commit comments

Comments
 (0)