Skip to content

Commit 24fef7e

Browse files
feat: Get-Turtle -AsJob ( Fixes #268, re #255 )
Also no longer complaining if a command starts with a bracket, and prepping for non-turtle types.
1 parent 8453fe6 commit 24fef7e

File tree

1 file changed

+84
-15
lines changed

1 file changed

+84
-15
lines changed

Commands/Get-Turtle.ps1

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ function Get-Turtle {
427427
.EXAMPLE
428428
# We can draw a 'Sierpinski Snowflake' with multiple Sierpinski Triangles.
429429
turtle @('rotate', 30, 'SierpinskiTriangle',42,4 * 12)
430-
.EXAMPLE
430+
.EXAMPLE
431431
turtle @('rotate', 45, 'SierpinskiTriangle',42,4 * 24)
432432
#>
433433
[CmdletBinding(PositionalBinding=$false)]
@@ -456,14 +456,33 @@ function Get-Turtle {
456456
# If the input object is not a turtle object, it will be ignored and a new turtle object will be created.
457457
[Parameter(ValueFromPipeline)]
458458
[PSObject]
459-
$InputObject
459+
$InputObject,
460+
461+
[switch]
462+
$AsJob
460463
)
461464

462465
begin {
463466
# Get information about our turtle pseudo-type.
464-
$turtleType = Get-TypeData -TypeName Turtle
467+
$turtleType = Get-TypeData -TypeName Turtle
468+
$turtleTypes = @(
469+
$turtleType
470+
# Real types would work to, and we may support them in the future
471+
# [Math]
472+
)
473+
465474
# any member name is a potential command
466-
$memberNames = $turtleType.Members.Keys
475+
$memberNames = @(
476+
foreach ($typeInfo in $turtleTypes) {
477+
if ($typeInfo.Members -is [Collections.IDictionary]) {
478+
$typeInfo.Members.Keys
479+
}
480+
481+
<#elseif ($typeInfo -is [Type]) {
482+
$typeInfo | Get-Member -Static | Select-Object -ExpandProperty Name
483+
}#>
484+
}
485+
)
467486

468487
# We want to sort the member names by length, in case we need them in a pattern or want to sort quickly.
469488
$memberNames = $memberNames | Sort-Object @{Expression={ $_.Length };Descending=$true}, name
@@ -495,15 +514,42 @@ function Get-Turtle {
495514
}
496515
}
497516

498-
process {
517+
process {
518+
# If we were piped in a Turtle,
499519
if ($PSBoundParameters.InputObject -and
500520
$PSBoundParameters.InputObject.pstypenames -eq 'Turtle') {
521+
# make it the current turtle
501522
$currentTurtle = $PSBoundParameters.InputObject
502523
} elseif ($PSBoundParameters.InputObject) {
503524
# If input was passed, and it was not a turtle, pass it through.
504525
return $PSBoundParameters.InputObject
505526
}
506527

528+
#region -AsJob
529+
# If we wanted to run a background job
530+
if ($PSBoundParameters.AsJob) {
531+
# remove the -AsJob variable from our parameters
532+
$null = $PSBoundParameters.Remove('AsJob')
533+
534+
# and then start a thread job that will import the module and run the command.
535+
return Start-ThreadJob -ScriptBlock {
536+
param([Collections.IDictionary]$IO)
537+
Import-Module -Name $io.ModulePath
538+
$argList = @($IO.ArgumentList)
539+
if ($IO.InputObject) {
540+
$io.InputObject | & $io.CommandName @argList
541+
} else {
542+
& $io.CommandName @argList
543+
}
544+
} -ArgumentList (
545+
[Ordered]@{
546+
ModulePath = $MyInvocation.MyCommand.ScriptBlock.Module.Path -replace '\.psm1$', '.psd1'
547+
CommandName = $MyInvocation.MyCommand.Name
548+
} + $PSBoundParameters
549+
)
550+
}
551+
#endregion -AsJob
552+
507553
if (-not $currentTurtle.Invocations) {
508554
$currentTurtle | Add-Member NoteProperty Invocations -Force @(,$invocationInfo)
509555
} elseif ($currentTurtle.Invocations -is [object[]]) {
@@ -531,16 +577,14 @@ function Get-Turtle {
531577
$arg -split '\s{1,}'
532578
} else {
533579
$arg
534-
}
580+
}
535581
} else {
536582
# otherwise, leave the argument alone.
537583
$arg
538584
}
539585
})
540586

541-
# If any brackets are used, we want to balance them all now, and error if they appear unbalanced.
542-
$bracketsOnly = $wordsAndArguments -replace '^[\[\]]' -join ''
543-
587+
# If any brackets are used, we want to balance them all now, and error if they appear unbalanced.
544588
# Since we want to know the exact index, we walk thru matches
545589
$depth = 0
546590
# and keep track of when it became unbalanced.
@@ -600,7 +644,11 @@ $(
600644
$arg = $wordsAndArguments[$argIndex]
601645
# If the argument is not in the member names list, we can complain about it.
602646
if ($arg -notin $memberNames) {
603-
if (-not $currentMember -and $arg -is [string] -and "$arg".Trim()) {
647+
if (
648+
# (we might not want to, if it starts with a bracket)
649+
-not $currentMember -and $arg -is [string] -and
650+
"$arg".Trim() -and $arg -notmatch '^\['
651+
) {
604652
Write-Warning "Unknown command '$arg'."
605653
}
606654
continue
@@ -611,6 +659,18 @@ $(
611659
$currentMember = $arg
612660
$memberInfo = $turtleType.Members[$currentMember]
613661

662+
if (-not $memberInfo) {
663+
$memberInfo = foreach ($typeInfo in $turtleTypes) {
664+
if ($typeInfo.Members -is [Collections.IDictionary] -and $typeInfo.Members[$currentMember]) {
665+
$typeInfo; break
666+
}
667+
if ($typeInfo::$currentMember) {
668+
$typeInfo::$currentMember
669+
break
670+
}
671+
}
672+
}
673+
614674
# If it's an alias
615675
if ($memberInfo.ReferencedMemberName) {
616676
# try to resolve it.
@@ -684,11 +744,14 @@ $(
684744
# and call the script, splatting positional parameters
685745
# (this allows more complex binding, like ValueFromRemainingArguments)
686746
. $currentTurtle.$currentMember.Script @argList
687-
} else {
688-
# Otherwise, we pass the parameters directly to the method
747+
}
748+
elseif ($currentTurtle.$currentMember.Invoke) {
689749
$currentTurtle.$currentMember.Invoke($argList)
690-
}
691-
750+
} elseif ($memberInfo.Invoke) {
751+
$memberInfo.Invoke($argList)
752+
} elseif ($memberInfo -is [ValueType]) {
753+
$memberInfo
754+
}
692755
} else {
693756
# otherwise, just invoke the method with no arguments.
694757
$currentTurtle.$currentMember.Invoke()
@@ -716,7 +779,13 @@ $(
716779
}
717780
} else {
718781
# otherwise, lets get the property
719-
$currentTurtle.$currentMember
782+
783+
if ($null -ne $currentTurtle.$currentMember) {
784+
$currentTurtle.$currentMember
785+
} elseif ($memberInfo -is [ValueType]) {
786+
$memberInfo
787+
}
788+
720789
}
721790
}
722791

0 commit comments

Comments
 (0)