Created
October 4, 2020 11:05
-
-
Save eabase/bd442afdd335f122bd86979bc9980dd1 to your computer and use it in GitHub Desktop.
Trying to create a Powershell (pwsh.exe) equivalent to the Bash internal "typset -f"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------------------------ | |
# Trying to create a PoSh equivalent to the Bash internal "typset -f" | |
# Trying to create a Powershell (pwsh.exe) equivalent to the Bash internal "typset -f" | |
#------------------------------------------------------------------------------ | |
# From: | |
# https://stackoverflow.com/questions/40967449/get-list-of-functions-from-script | |
# https://stackoverflow.com/a/57635570/ | |
# Usage: | |
# Write-Host [System.String[]]$FX_DEFS = Get-ScriptFunctionDefinitions -Path "$($PSCommandPath | Split-Path)/Functions.ps1" | |
# Write-Host [System.String[]]$FX_DEFS = Get-ScriptFunctionDefinitions -Path "$($PSCommandPath | Split-Path)/Microsoft.PowerShell_profile.ps1" | |
#------------------------------------------------------------------------------ | |
function Get-ScriptFunctionNames { | |
param ( | |
[parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)] | |
[AllowEmptyString()] | |
[AllowNull()] | |
[System.String] | |
$Path | |
) | |
Process{ | |
[System.Collections.Generic.List[String]]$FX_NAMES = New-Object System.Collections.Generic.List[String] | |
if(!([System.String]::IsNullOrWhiteSpace($Path))) { | |
Select-String -Path "$Path" -Pattern "function" | | |
ForEach-Object { | |
[System.Text.RegularExpressions.Regex] $regexp = New-Object Regex("(function)( +)([\w-]+)") | |
[System.Text.RegularExpressions.Match] $match = $regexp.Match("$_") | |
if($match.Success) { | |
$FX_NAMES.Add("$($match.Groups[3])") | |
} | |
} | |
} | |
return ,$FX_NAMES.ToArray() | |
} | |
} | |
function Get-ScriptFunctionDefinitions { | |
param ( | |
[parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)] | |
[AllowEmptyString()] | |
[AllowNull()] | |
[System.String] | |
$Path | |
) | |
Process{ | |
[System.Collections.Generic.List[String]]$FX_DEFS = New-Object System.Collections.Generic.List[String] | |
if(!([System.String]::IsNullOrWhiteSpace($Path))) { | |
Import-Module -Name "$Path" -Force | |
} | |
# NOTE: Use of previous function here: | |
$names = Get-ScriptFunctionNames -Path $Path | |
Get-ChildItem "function:" | Where-Object { $_ -in $names } | ForEach-Object{ | |
#$FX_DEFS.Add("function $($_.Name) { $($_.Definition) };") | |
$FX_DEFS.Add("function $($_.Name) { $($_.Definition) };`n") | |
} | |
return ,$FX_DEFS.ToArray() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment