Skip to content

Commit b4cdb58

Browse files
Merge pull request #161 from StartAutomating/EZ-Updates
EZOut 2.0.1
2 parents 4ffc2a8 + 2b3e380 commit b4cdb58

23 files changed

+456
-97
lines changed

.github/ISSUE_TEMPLATE/Bug.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Bug Report
22
description: Report a Problem
3-
title: ""
3+
title: "Bug"
44
labels: ["bug"]
55
body:
66
- type: markdown

.github/ISSUE_TEMPLATE/FeatureRequest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Feature Request
22
description: Request a Feature
3-
title: ""
3+
title: "Request:"
44
labels: ["enhancement"]
55
body:
66
- type: markdown

.github/ISSUE_TEMPLATE/NewCommand.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: New Command
22
description: Request a new command
3-
title: ""
3+
title: "New Command - "
44
labels: ["enhancement"]
55
assignees:
66
- startautomating

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
## 2.0.1
2+
3+
* Formatting can now live in the Types directory!
4+
* Import-TypeView - Skipping format files (#151)
5+
* Import-FormatView - Skipping non-format files (#157)
6+
* Import-FormatView - Setting $PSTypeName (if file is present) (#159)
7+
* Write-EZFormatFile - Adding 'Types' to Import-FormatView (#158)
8+
* Moving Hello.EZOut to 'Types' (#150)
9+
* -Style Parameter improvements
10+
* Infinite dotting (#148)
11+
* Arbitrary variable support (#155)
12+
* Write-FormatView - Defaulting -ViewName to first -TypeName (Fixes #160)
13+
* New Command: Out-Alternate (Fixes #156)
14+
15+
---
16+
117
## 2.0:
218

319
* EZOut Turns 2.0!

Commands/Format.PS1XML/Import-FormatView.ps1

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,21 @@
9898
# (this can simplify development of complex formatting)
9999
}
100100

101+
102+
101103
if ($fi.Extension -eq '.xml') {
102104
$innerFormat[$fi.Name] = [xml][IO.File]::ReadAllText($fi.FullName)
103105
} else {
104-
$innerFormat[$fi.Name] = [ScriptBlock]::Create([IO.File]::ReadAllText($fi.FullName))
106+
$scriptBlock = $ExecutionContext.SessionState.InvokeCommand.GetCommand($fi.FullName, 'ExternalScript').ScriptBlock
107+
108+
if ($fi.Name -notmatch '\.(?>format|view)\.ps1') {
109+
if (@($ScriptBlock.Attributes.PSTypeName)) {
110+
$innerFormat[$fi.FullName] = $scriptBlock
111+
}
112+
} else {
113+
$innerFormat[$fi.FullName] = $scriptBlock
114+
}
115+
105116
}
106117
}
107118

@@ -123,23 +134,20 @@
123134
}
124135
}) | Select-Object -Unique
125136

126-
# Infer the type name from the file name.
127-
$inferredTypeName = $if.Key -replace '\.(format|type|view)\.ps1'
137+
128138

129139

130140
if (-not $typeName) { # If no typename has been determined by now,
131-
$typeName = $inferredTypeName # use the inferred type name.
132-
}
133-
134-
if ($typeName -ne $inferredTypeName) { # If the typename is not the inferred type name,
135-
# the last item before . will denote a known condition.
136-
$pluginName = @($inferredTypeName -split '\.')[-1]
141+
# Infer the type name from the file name.
142+
$formatFileName = $if.Key | Split-Path -Leaf
143+
if ($formatFileName -notmatch '\.(format|type|view)') {
144+
continue
145+
}
146+
$inferredTypeName = $formatFileName -replace '\.(format|type|view)\.ps1'
147+
148+
$typeName = $inferredTypeName # then use the inferred type name.
137149
}
138-
139-
140-
141-
142-
150+
143151
if (-not $formatterByTypeName[$typeName]) {
144152
$formatterByTypeName[$typeName] = @()
145153
}
@@ -151,7 +159,7 @@
151159

152160
foreach ($formatterGroup in $formatterByTypeName.GetEnumerator()) {
153161
foreach ($fileNameAndScriptBlock in $formatterGroup.Value) {
154-
$fileName, $scriptBlock = $fileNameAndScriptBlock.Key, $fileNameAndScriptBlock.Value
162+
$fileName, $scriptBlock = $fileNameAndScriptBlock.Key, $fileNameAndScriptBlock.Value
155163
if (-not $scriptBlock) {continue }
156164
$usesEzOutCommands = $scriptBlock.Ast.FindAll({param($ast)
157165
$ast -is [Management.Automation.Language.CommandAst] -and $ezOutCommands -contains $ast.CommandElements[0].Value
@@ -183,10 +191,27 @@
183191
$defaultValueName
184192
}
185193
})
194+
195+
$psTypeNameFile = $null
196+
if ($scriptBlock.File) {
197+
$psTypeNameFile =
198+
$scriptBlock.File |
199+
Split-Path |
200+
Get-ChildItem |
201+
Where-Object Name -Match '^(?:PS)?TypeNames{0,1}\.txt$' |
202+
Select-Object -First 1
203+
if ($psTypeNameFile) {
204+
$global:ExecutionContext.SessionState.PSVariable.Set('PSTypeName', @(Get-Content $psTypeNameFile))
205+
}
206+
}
207+
186208
& $scriptBlock
187209
foreach ($unset in $toUnset) {
188210
$Global:PSDefaultParameterValues.Remove($unset)
189211
}
212+
if ($psTypeNameFile) {
213+
$global:ExecutionContext.SessionState.PSVariable.Remove('PSTypeName')
214+
}
190215
}
191216
} else {
192217
$formatParams = @{

Commands/Format.PS1XML/Write-FormatListView.ps1

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,24 @@
152152
{
153153
$CellColorValue = if ($psStyle) {
154154
@(foreach ($styleProp in $CellColorValue) {
155-
if ($styleProp -match '\.') {
156-
$styleGroup, $styleProp = $styleProp -split '\.'
157-
$psStyle.$styleGroup.$styleProp
158-
} else {
155+
if ($styleProp -match '^\$') {
156+
$ExecutionContext.SessionState.InvokeCommand.InvokeScript($styleProp)
157+
}
158+
elseif ($styleProp -match '\.') {
159+
$targetObject = $psStyle
160+
foreach ($dotProperty in $styleProp -split '(?<!\.)\.') {
161+
if ($targetObject.Item -is [Management.Automation.PSMethodInfo] -or
162+
$targetObject -is [Collections.IDictionary]) {
163+
$targetObject = $targetObject[$dotProperty]
164+
} else {
165+
$targetObject = $targetObject.$dotProperty
166+
}
167+
}
168+
if ($targetObject) {
169+
$targetObject
170+
}
171+
}
172+
else {
159173
$psStyle.$styleProp
160174
}
161175
}) -join ''

Commands/Format.PS1XML/Write-FormatTableView.ps1

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,24 @@ function Write-FormatTableView
212212
{
213213
$CellColorValue = if ($psStyle) {
214214
@(foreach ($styleProp in $CellColorValue) {
215-
if ($styleProp -match '\.') {
216-
$styleGroup, $styleProp = $styleProp -split '\.'
217-
$psStyle.$styleGroup.$styleProp
218-
} else {
215+
if ($styleProp -match '^\$') {
216+
$ExecutionContext.SessionState.InvokeCommand.InvokeScript($styleProp)
217+
}
218+
elseif ($styleProp -match '\.') {
219+
$targetObject = $psStyle
220+
foreach ($dotProperty in $styleProp -split '(?<!\.)\.') {
221+
if ($targetObject.Item -is [Management.Automation.PSMethodInfo] -or
222+
$targetObject -is [Collections.IDictionary]) {
223+
$targetObject = $targetObject[$dotProperty]
224+
} else {
225+
$targetObject = $targetObject.$dotProperty
226+
}
227+
}
228+
if ($targetObject) {
229+
$targetObject
230+
}
231+
}
232+
else {
219233
$psStyle.$styleProp
220234
}
221235
}) -join ''

Commands/Format.PS1XML/Write-FormatView.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,13 @@
289289
process {
290290
#region Generate Format Content
291291
[string]$FormatContent = ""
292+
$parameterCopy = [Ordered]@{} + $psBoundParameters
292293
if ($psCmdlet.ParameterSetName -eq "Action") {
293-
$FormatContent = $psBoundParameters | & ${.@} Write-FormatCustomView
294+
$FormatContent = $parameterCopy | & ${.@} Write-FormatCustomView
294295
} elseif ($psCmdlet.ParameterSetName -eq 'TableView') {
295-
$formatContent = $PSBoundParameters | & ${.@} Write-FormatTableView
296+
$formatContent = $parameterCopy | & ${.@} Write-FormatTableView
296297
} elseif ($psCmdlet.ParameterSetName -eq "ListView") {
297-
$formatContent = $psBoundParameters | & ${.@} Write-FormatListView
298+
$formatContent = $parameterCopy | & ${.@} Write-FormatListView
298299
}
299300
#endregion Generate Format Content
300301

@@ -344,7 +345,7 @@
344345
}
345346
$viewName = $Name
346347
if (-not $viewName) {
347-
$viewName = $typeName
348+
$viewName = $typeName[0]
348349
}
349350
$xml = [xml]"
350351
<View>

Commands/Format.PS1XML/Write-FormatViewExpression.ps1

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,34 @@
175175

176176
foreach ($n in 1..$count) {
177177
if ($Style) {
178-
$scriptLines =
179-
@('if ($psStyle) {'
180-
" @(foreach (`$styleProp in '$($style -join "','")') {"
181-
' if ($styleProp -match ''\.'') {'
182-
' $styleGroup, $styleProp = $styleProp -split ''\.'''
183-
' $psStyle.$styleGroup.$styleProp'
184-
' } else {'
185-
' $psStyle.$styleProp'
186-
' }'
187-
" }) -ne '' -join ''"
188-
'}')
178+
$scriptLines = @(
179+
'if ($psStyle) {'
180+
" @(foreach (`$styleProp in '$($style -join "','")') {"
181+
{
182+
if ($styleProp -match '^\$') {
183+
$ExecutionContext.SessionState.InvokeCommand.InvokeScript($styleProp)
184+
}
185+
elseif ($styleProp -match '\.') {
186+
$targetObject = $psStyle
187+
foreach ($dotProperty in $styleProp -split '(?<!\.)\.') {
188+
if ($targetObject.Item -is [Management.Automation.PSMethodInfo] -or
189+
$targetObject -is [Collections.IDictionary]) {
190+
$targetObject = $targetObject[$dotProperty]
191+
} else {
192+
$targetObject = $targetObject.$dotProperty
193+
}
194+
}
195+
if ($targetObject) {
196+
$targetObject
197+
}
198+
}
199+
else {
200+
$psStyle.$styleProp
201+
}
202+
}
203+
" }) -ne '' -join ''"
204+
'}'
205+
)
189206
$styleScript = [ScriptBlock]::Create($scriptLines -join [Environment]::NewLine)
190207
Write-FormatViewExpression -ScriptBlock $styleScript -If $if
191208
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
function Out-Alternate
2+
{
3+
<#
4+
.SYNOPSIS
5+
Outputs alternate views
6+
.DESCRIPTION
7+
Outputs alternate views of an object or typename.
8+
9+
While it is possible to display multiple views of an object in PowerShell, it's not as straightforward to see what those views are.
10+
11+
Out-Alternate solves this problem, and is embeddable within a format file.
12+
.EXAMPLE
13+
Out-Alternate -TypeName "System.Diagnostics.Process"
14+
#>
15+
[Management.Automation.Cmdlet("Format", "Object")]
16+
param(
17+
# An input object. If this is provided, it will infer the typenames
18+
[Parameter(ValueFromPipeline)]
19+
[PSObject]
20+
$InputObject,
21+
22+
# The typename of the alternate.
23+
# If this is not provided, it can be inferred from the `-InputObject`.
24+
[Alias('TypeName')]
25+
[string[]]
26+
$PSTypeName,
27+
28+
# The name of the current view.
29+
# If this is provided, it will not be displayed as an alternate.
30+
[string]
31+
$CurrentView,
32+
33+
# A prefix to each view.
34+
[string]
35+
$Prefix = "",
36+
37+
# A suffix to each view.
38+
[string]
39+
$Suffix,
40+
41+
# If set, will not padd the space between the name of the format control and the -View parameter
42+
[switch]
43+
$NoPadding
44+
)
45+
46+
process {
47+
# If no typename was provided
48+
if (-not $PSTypeName) {
49+
# and the input object has one
50+
if ($InputObject.pstypenames) {
51+
# use the input's PSTypeNames
52+
$PSTypeName = $InputObject.pstypenames
53+
}
54+
}
55+
56+
# If we have no PSTypename, return.
57+
if (-not $PSTypeName) { return }
58+
59+
$views = # Get the views and force them into an array
60+
@(foreach ($typeName in $PSTypeName) {
61+
foreach ($view in (Get-FormatData -TypeName $TypeName).FormatViewDefinition) {
62+
$view
63+
}
64+
})
65+
66+
# Now we walk over each view
67+
@(foreach ($view in $views) {
68+
# If we provided a -CurrentView, and this is it, skip.
69+
if ($CurrentView -and $view.Name -eq $CurrentView) { continue }
70+
# Determine the format type by getting the control and removing "Control" from the typename.
71+
$formatType = $view.Control.GetType().Name -replace 'Control$'
72+
73+
# By default, we pad space (for aesthetic reasons).
74+
if (-not $NoPadding) { # If `-NoPadding` was passed, we won't pad space.
75+
# Otherwise, always pad to 6.
76+
# (the length of 'Custom', the longest of the potential format types)
77+
$formatType = $formatType.PadRight(6, ' ')
78+
}
79+
# Now output the name of the format command and it's view.
80+
"Format-$formatType -View '$($view.Name -replace "'", "''")'"
81+
}) -join [Environment]::NewLine
82+
}
83+
}

0 commit comments

Comments
 (0)