Skip to content

Commit

Permalink
Merge branch 'feature/allow-parameters' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
joonro committed Jun 8, 2022
2 parents 19142e4 + 771c3ab commit 75e0c4c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Get-ChildItemColor.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Description = 'Get-ChildItemColor provides colored versions of Get-ChildItem Cmd
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @(
'Get-ChildItemColor',
'Get-ChildItemColorFormatWide'
'Get-ChildItemColorFormatWide',
'Out-ChildItemColor'
)

Expand Down
103 changes: 92 additions & 11 deletions src/Get-ChildItemColor.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -203,23 +203,104 @@ Function Out-ChildItemColor {
}

Function Get-ChildItemColor {
[CmdletBinding()]
Param(
[string]$Path = ""
)
[CmdletBinding(DefaultParameterSetName='Items', HelpUri='https://go.microsoft.com/fwlink/?LinkID=2096492')]
param(
[Parameter(ParameterSetName='Items', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string[]]
${Path},

[Parameter(ParameterSetName='LiteralItems', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('PSPath','LP')]
[string[]]
${LiteralPath},

[Parameter(Position=1)]
[string]
${Filter},

[string[]]
${Include},

[string[]]
${Exclude},

[Alias('s')]
[switch]
${Recurse},

[uint]
${Depth},

[switch]
${Force},

[switch]
${Name})


dynamicparam
{
try {
$targetCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Get-ChildItem', [System.Management.Automation.CommandTypes]::Cmdlet, $PSBoundParameters)
$dynamicParams = @($targetCmd.Parameters.GetEnumerator() | Microsoft.PowerShell.Core\Where-Object { $_.Value.IsDynamic })
if ($dynamicParams.Length -gt 0)
{
$paramDictionary = [Management.Automation.RuntimeDefinedParameterDictionary]::new()
foreach ($param in $dynamicParams)
{
$param = $param.Value

if(-not $MyInvocation.MyCommand.Parameters.ContainsKey($param.Name))
{
$dynParam = [Management.Automation.RuntimeDefinedParameter]::new($param.Name, $param.ParameterType, $param.Attributes)
$paramDictionary.Add($param.Name, $dynParam)
}
}

Begin {
$Expression = "Get-ChildItem -Path `"$Path`" $Args"
$Items = Invoke-Expression $Expression
return $paramDictionary
}
} catch {
throw
}
}

begin
{
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}

$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Get-ChildItem', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = {& $wrappedCmd @PSBoundParameters }

} catch {
throw
}
}

process
{
try {
$items = $scriptCmd.invoke()

Process {
If ($PSCmdlet.MyInvocation.Line -Match '\|') { # pipeline is used
$Items
$items
} Else {
$Items | Out-ChildItemColor
$items | Out-ChildItemColor
}
} catch {
throw
}
}

Export-ModuleMember -Function Out-ChildItemColor, 'Get-*'
end
{
try {
} catch {
throw
}
}
}

0 comments on commit 75e0c4c

Please sign in to comment.