Skip to content

Commit

Permalink
Supporting Posh.Profiles
Browse files Browse the repository at this point in the history
Fixes #40, Fixes #57, Fixes #58, Fixes #59, Fixes #60
  • Loading branch information
James Brundage committed Jul 20, 2023
2 parents 9200a2d + b3bf100 commit 4725869
Show file tree
Hide file tree
Showing 2 changed files with 267 additions and 8 deletions.
259 changes: 259 additions & 0 deletions Posh.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,208 @@ if ($Request -or $Host.UI.SupportsHTML) {
</CustomEntries>
</CustomControl>
</Control>
<Control>
<Name>${Posh_Format-Yaml}</Name>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>
&lt;#
.SYNOPSIS
Formats objects as YAML
.DESCRIPTION
Formats an object as YAML.
.EXAMPLE
Format-Yaml -InputObject @("a", "b", "c")
.EXAMPLE
@{a="b";c="d";e=@{f=@('g')}} | Format-Yaml
#&gt;
[Management.Automation.Cmdlet("Format","Object")]
[ValidateScript({return $true})]
param(
# The InputObject.
[Parameter(ValueFromPipeline)]
[PSObject]
$InputObject,

# If set, will make a YAML header by adding a YAML Document tag above and below output.
[Alias('YAMLDocument')]
[switch]
$YamlHeader,

[int]
$Indent = 0,

# The maximum depth of objects to include.
# Beyond this depth, an empty string will be returned.
[int]
$Depth
)

begin {
if (-not $Depth) { $depth = $FormatEnumerationLimit }
$toYaml = {
param(
[Parameter(ValueFromPipeline,Position=0)]$Object,
[Object]$Parent,
[Object]$GrandParent,
[int]$Indent = 0)

begin { $n = 0; $mySelf = $myInvocation.MyCommand.ScriptBlock }
process {
$n++
if ($Object -eq $null) { return }

if ($depth) {
$myDepth = $indent / 2
if ($myDepth -gt $depth) {
return ''
}
}

if ($Parent -and $Parent -is [Collections.IList]) {
if ($Parent.IndexOf($Object) -gt 0) { ' ' * $Indent }
'- '
}

#region Primitives
if ( $Object -is [string] ) { # If it's a string
if ($object -match '\n') { # see if it's a multline string.
"|" # If it is, emit the multiline indicator
$indent +=2
foreach ($l in $object -split '(?&gt;\r\n|\n)') { # and emit each line indented
[Environment]::NewLine
' ' * $indent
$l
}
$indent -=2
} elseif ("$object".Contains('*')) {
"'$($Object -replace "'","''")'"
} else {
$object
}

if ($Parent -is [Collections.IList]) { # If the parent object was a list
[Environment]::NewLine # emit a newline.
}
return # Once the string has been emitted, return.
}
if ( $Object.GetType().IsPrimitive ) { # If it is a primitive type
"$Object".ToLower() # Emit it in lowercase.
if ($Parent -is [Collections.IList]) {
[Environment]::NewLine
}
return
}
#endregion Primitives

#region KVP
if ( $Object -is [Collections.DictionaryEntry] -or $object -is [Management.Automation.PSPropertyInfo]) {
if ($Parent -isnot [Collections.IList] -and
($GrandParent -isnot [Collections.IList] -or $n -gt 1)) {
[Environment]::NewLine + (" " * $Indent)
}
if ($object.Key -and $Object.Key -is [string]) {
$Object.Key +": "
} elseif ($object.Name -and $object.Name -is [string]) {
$Object.Name +": "
}
}

if ( $Object -is [Collections.DictionaryEntry] -or $Object -is [Management.Automation.PSPropertyInfo]) {
&amp; $mySelf -Object $Object.Value -Parent $Object -GrandParent $parent -Indent $Indent
return
}
#endregion KVP


#region Nested
if ($parent -and ($Object -is [Collections.IDictionary] -or $Object -is [PSObject])) {
$Indent += 2
}
elseif ($object -is [Collections.IList]) {
$allPrimitive = 1
foreach ($Obj in $Object) {
$allPrimitive = $allPrimitive -band (
$Obj -is [string] -or
$obj.GetType().IsPrimitive
)
}
if ($parent -and -not $allPrimitive) {
$Indent += 2
}
}


if ( $Object -is [Collections.IDictionary] ) {
$Object.GetEnumerator() |
&amp; $mySelf -Parent $Object -GrandParent $Parent -Indent $Indent
} elseif ($Object -is [Collections.IList]) {

[Environment]::NewLine + (' ' * $Indent)

$Object |
&amp; $mySelf -Parent $Object -GrandParent $Parent -Indent $Indent

}
elseif ($object -is [enum]) {
$object.ToString()
}
elseif ($Object.PSObject.Properties) {
$Object.psobject.properties |
&amp; $mySelf -Parent $Object -GrandParent $Parent -Indent $Indent
}

if ($Object -is [Collections.IDictionary] -or $Object -is [PSCustomObject] -or $Object -is [Collections.IList]) {
if ($Parent -is [Collections.IList]) { [Environment]::NewLine }
$Indent -= 2;
}
#endregion Nested
}
}
function IndentString([string]$String,[int]$Indent) {
@(foreach ($line in @($String -split '(?&gt;\r\n|\n)')) {
(' ' * $indent) + $line
}) -join [Environment]::NewLine
}
$inputWasNotPiped = $PSBoundParameters.InputObject -as [bool]
$allInputObjects = @()
}

process {
if ($inputWasNotPiped) {
IndentString ('' + $(if ($YamlHeader) { '---' + [Environment]::NewLine }) + (
(&amp; $toYaml -object $inputObject) -join '' -replace
"$([Environment]::NewLine * 2)", [Environment]::NewLine
) + $(if ($YamlHeader) { [Environment]::NewLine + '---'})) -Indent $Indent
} else {
$allInputObjects += $inputObject
}
}

end {
if (-not $allInputObjects) { return }
if ($allInputObjects.Length -eq 1) {
IndentString ('' + $(if ($YamlHeader) { '---' + [Environment]::NewLine}) + (
(&amp; $toYaml -object $inputObject) -join '' -replace
"$([Environment]::NewLine * 2)", [Environment]::NewLine
) + $(if ($YamlHeader) { [Environment]::NewLine + '---'})) -Indent $Indent
} else {
IndentString ('' + $(if ($YamlHeader) { '---' + [Environment]::NewLine}) + (
(&amp; $toYaml -object $allInputObjects) -join '' -replace
"$([Environment]::NewLine * 2)", [Environment]::NewLine
) + $(if ($YamlHeader) { [Environment]::NewLine + '---'})) -Indent $Indent
}
}
</ScriptBlock>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</Control>
<Control>
<Name>${Posh_Format-RichText}</Name>
<CustomControl>
Expand Down Expand Up @@ -2511,6 +2713,63 @@ if ($Request -or $Host.UI.SupportsHTML) {
</CustomEntries>
</CustomControl>
</View>
<View>
<Name>Posh.Resources</Name>
<ViewSelectedBy>
<TypeName>Posh.Resources</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ItemSelectionCondition>
<ScriptBlock> $_.README </ScriptBlock>
</ItemSelectionCondition>
<ScriptBlock>
Show-Markdown -InputObject $_.README
</ScriptBlock>
</ExpressionBinding>
<NewLine />
<ExpressionBinding>
<ScriptBlock>$moduleName = 'Posh'

do {
$lm = Get-Module -Name $moduleName -ErrorAction Ignore
if (-not $lm) { continue }
if ($lm.FormatPartsLoaded) { break }
$wholeScript = @(foreach ($formatFilePath in $lm.exportedFormatFiles) {
foreach ($partNodeName in Select-Xml -LiteralPath $formatFilePath -XPath "/Configuration/Controls/Control/Name[starts-with(., '$')]") {
$ParentNode = $partNodeName.Node.ParentNode
"$($ParentNode.Name)={
$($ParentNode.CustomControl.CustomEntries.CustomEntry.CustomItem.ExpressionBinding.ScriptBlock)}"
}
}) -join [Environment]::NewLine
New-Module -Name "${ModuleName}.format.ps1xml" -ScriptBlock ([ScriptBlock]::Create(($wholeScript + ';Export-ModuleMember -Variable *'))) |
Import-Module -Global
$onRemove = [ScriptBlock]::Create("Remove-Module '${ModuleName}.format.ps1xml'")

if (-not $lm.OnRemove) {
$lm.OnRemove = $onRemove
} else {
$lm.OnRemove = [ScriptBlock]::Create($onRemove.ToString() + '' + [Environment]::NewLine + $lm.OnRemove)
}
$lm | Add-Member NoteProperty FormatPartsLoaded $true -Force

} while ($false)



$_ |
Select-Object -ExcludeProperty README -Property * |
&amp; ${Posh_Format-Yaml}
</ScriptBlock>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
<View>
<Name>System.Reflection.MemberInfo</Name>
<ViewSelectedBy>
Expand Down
16 changes: 8 additions & 8 deletions Posh.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ foreach ($stackableFunctionKeyValue in $stackableFunctions.GetEnumerator()) {
$Posh |
Add-Member NoteProperty "Posh.Parameters" (
[PSCustomObject]@{
PSTypeName = "Posh.$functionStackType"
PSTypeName = "Posh.Parameters"
DefaultValues = $global:PSDefaultParameterValues
}
) -Force
Expand All @@ -77,14 +77,14 @@ $posh | Add-Member NoteProperty Commands $poshCommands -Force

$PoshResources = [Ordered]@{
PSTypeName = 'Posh.Resources'
PowerShellGuide = 'https://PowerShellGuide.com/'
PowerShellDotOrg = 'https://powershell.org/'
'PowerShell Guide' = 'https://PowerShellGuide.com/'
'PowerShell.Org' = 'https://powershell.org/'
'PowerShell Discord' = 'https://discord.com/invite/powershell'
PowerShellProject = 'https://github.com/PowerShell/PowerShell'
PowerShellGitHub = 'https://github.com/topics/powershell'
PowerShellTwitter = 'https://twitter.com/search?q=%23PowerShell'
PowerShellFacebook = 'https://www.facebook.com/groups/powershell/'
PoshProject = $posh.PrivateData.PSData.ProjectURI
'PowerShell Project' = 'https://github.com/PowerShell/PowerShell'
'PowerShell GitHub' = 'https://github.com/topics/powershell'
'PowerShell Twitter' = 'https://twitter.com/search?q=%23PowerShell'
'PowerShell Facebook' = 'https://www.facebook.com/groups/powershell/'
'PoshProject' = $posh.PrivateData.PSData.ProjectURI
}

$posh | Add-Member NoteProperty Resources ([PSCustomObject]$PoshResources)
Expand Down

0 comments on commit 4725869

Please sign in to comment.