Skip to content

Commit bba0bc4

Browse files
author
James Brundage
committed
feat: Save-Turtle ( Fixes PoshWeb#52 )
1 parent bd43773 commit bba0bc4

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

Commands/Save-Turtle.ps1

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
function Save-Turtle {
2+
<#
3+
.SYNOPSIS
4+
Saves a turtle.
5+
.DESCRIPTION
6+
Saves a turtle graphics pattern to a file.
7+
.EXAMPLE
8+
New-Turtle |
9+
Move-Turtle Si
10+
#>
11+
param(
12+
# The file path to save the turtle graphics pattern.
13+
[Parameter(ValueFromPipelineByPropertyName)]
14+
[Alias('Path')]
15+
[string]
16+
$FilePath,
17+
18+
# The property of the turtle to save.
19+
[ArgumentCompleter({
20+
param ( $commandName,
21+
$parameterName,
22+
$wordToComplete,
23+
$commandAst,
24+
$fakeBoundParameters )
25+
$myInv = $myInvocation
26+
$turtleType = Get-TypeData -TypeName Turtle
27+
$propertyNames = @(foreach ($memberName in $turtleType.Members.Keys) {
28+
if ($turtleType.Members[$memberName] -is [System.Management.Automation.Runspaces.ScriptPropertyData]) {
29+
$memberName
30+
}
31+
})
32+
33+
if ($wordToComplete) {
34+
return $propertyNames -like "$wordToComplete*"
35+
} else {
36+
return $propertyNames
37+
}
38+
})]
39+
[string]
40+
$Property = 'Symbol',
41+
42+
# The turtle input object.
43+
[Parameter(ValueFromPipeline)]
44+
[Alias('Turtle')]
45+
[PSObject]
46+
$InputObject
47+
)
48+
49+
process {
50+
if (-not $inputObject) { return }
51+
$toExport = $inputObject.$Property
52+
if (-not $toExport) { return }
53+
$unresolvedPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($FilePath)
54+
$null = New-Item -ItemType File -Force -Path $unresolvedPath
55+
if ($toExport -is [xml]) {
56+
$toExport.Save("$unresolvedPath")
57+
}
58+
elseif ($toExport -is [byte[]]) {
59+
Set-Content -Path $unresolvedPath -Value $toExport -AsByteStream
60+
} else {
61+
$toExport > $unresolvedPath
62+
}
63+
if ($?) {
64+
Get-Item -Path $unresolvedPath
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)