From fecf4bafedca2c7ab1d29bb267746f4a1c39048d Mon Sep 17 00:00:00 2001 From: "djs-zmtc [RAZORBACK]" Date: Sun, 18 May 2025 18:45:06 -0400 Subject: [PATCH 1/5] add check for Class member functions or functions inside functions --- src/private/GetFunctionNameFromFile.ps1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/private/GetFunctionNameFromFile.ps1 b/src/private/GetFunctionNameFromFile.ps1 index 3ea48c6..7ecba5b 100644 --- a/src/private/GetFunctionNameFromFile.ps1 +++ b/src/private/GetFunctionNameFromFile.ps1 @@ -3,7 +3,13 @@ function Get-FunctionNameFromFile { try { $moduleContent = Get-Content -Path $filePath -Raw $ast = [System.Management.Automation.Language.Parser]::ParseInput($moduleContent, [ref]$null, [ref]$null) - $functionName = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $false) | ForEach-Object { $_.Name } + $functionName = $ast.FindAll({ + $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and + # Ignore Functions defined inside other Functions (e.g., nested functions or Class methods) + ($PSVersionTable.PSVersion.Major -lt 5 -or + $args[0].Parent -isnot [System.Management.Automation.Language.FunctionMemberAst]) + }, $false) | ForEach-Object { $_.Name } + return $functionName } catch { return '' } From 6a56375ebecbbf751833f09a62e22ce320bb5a8d Mon Sep 17 00:00:00 2001 From: "djs-zmtc [RAZORBACK]" Date: Sun, 18 May 2025 18:45:27 -0400 Subject: [PATCH 2/5] create Get-AliasNamesFromFile function --- src/private/GetAliasNamesFromFile.ps1 | 54 +++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/private/GetAliasNamesFromFile.ps1 diff --git a/src/private/GetAliasNamesFromFile.ps1 b/src/private/GetAliasNamesFromFile.ps1 new file mode 100644 index 0000000..a17869f --- /dev/null +++ b/src/private/GetAliasNamesFromFile.ps1 @@ -0,0 +1,54 @@ +# Get-AliasNamesFromFile +function Get-AliasNamesFromFile { + param($filePath) + + $aliasToExport = @() + + try { + $moduleContent = Get-Content -Path $filePath -Raw + $ast = [System.Management.Automation.Language.Parser]::ParseInput($moduleContent, [ref]$null, [ref]$null) + + # Find and add aliases defined by Set-Alias + $ast.EndBlock.Statements + | Where-Object { $_ -match "^\s*Set-Alias .+" } + | ForEach-Object { $_.Extent.text } + | ForEach-Object { + $params = $_ -split '\s+' + # $content += "`n$_" + + if ($_ -imatch "-na") { + # alias is defined using -Name parameter to Set-Alias + # so get the alias name from the next parameter + $i = 0 + $parNameLoc = 0 + ForEach ($par in $params) { + if ($par -imatch "-na") { + $parNameLoc = $i + } + ++$i + } + $aliasToExport += $params[$parNameLoc + 1] + } + else { + $aliasToExport += $params[1] + } + # Write-Verbose "Content: $_" + } + + # Find and add aliases defined by [Alias("Some-Alias")] attribute + $insideFunctionAliasName = $ast.FindAll({ + $args[0] -is [System.Management.Automation.Language.AttributeAst] + }, $true) + | Where-Object { $_.Parent.Extent.text -match '^param' } + | Select-Object -ExpandProperty PositionalArguments + | Select-Object -ExpandProperty Value -ErrorAction SilentlyContinue + + if ($insideFunctionAliasName) { + $insideFunctionAliasName | ForEach-Object { + $aliasToExport += $_ + } + } + return $aliasToExport + } + catch { return '' } +} \ No newline at end of file From 9e602baa8e499f6fdd9aa1888b2bdaa2613a2f19 Mon Sep 17 00:00:00 2001 From: "djs-zmtc [RAZORBACK]" Date: Sun, 18 May 2025 18:46:19 -0400 Subject: [PATCH 3/5] add AliasesToExport functionality to Build.Manifest --- src/private/Build.Manifest.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/private/Build.Manifest.ps1 b/src/private/Build.Manifest.ps1 index 7fb8906..e391a4d 100644 --- a/src/private/Build.Manifest.ps1 +++ b/src/private/Build.Manifest.ps1 @@ -6,8 +6,10 @@ function Build-Manifest { $PubFunctionFiles = Get-ChildItem -Path $data.PublicDir -Filter *.ps1 $functionToExport = @() + $aliasesToExport = @() $PubFunctionFiles | ForEach-Object { $functionToExport += Get-FunctionNameFromFile -filePath $_.FullName + $aliasesToExport += Get-AliasNamesFromFile -filePath $_.FullName } $ManfiestAllowedParams = (Get-Command New-ModuleManifest).Parameters.Keys @@ -16,6 +18,7 @@ function Build-Manifest { Path = $data.ManifestFilePSD1 Description = $data.Description FunctionsToExport = $functionToExport + AliasesToExport = $aliasesToExport RootModule = "$($data.ProjectName).psm1" ModuleVersion = $data.Version } From bb064149b834c73816351bab4abc427f30145772 Mon Sep 17 00:00:00 2001 From: "djs-zmtc [RAZORBACK]" Date: Sun, 18 May 2025 19:53:56 -0400 Subject: [PATCH 4/5] refactor code a bit to make it a little more readable and add some comments --- src/private/GetAliasNamesFromFile.ps1 | 50 +++++++++++++++------------ 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/private/GetAliasNamesFromFile.ps1 b/src/private/GetAliasNamesFromFile.ps1 index a17869f..30e7b6a 100644 --- a/src/private/GetAliasNamesFromFile.ps1 +++ b/src/private/GetAliasNamesFromFile.ps1 @@ -2,53 +2,59 @@ function Get-AliasNamesFromFile { param($filePath) - $aliasToExport = @() + $aliasesToExport = @() try { $moduleContent = Get-Content -Path $filePath -Raw $ast = [System.Management.Automation.Language.Parser]::ParseInput($moduleContent, [ref]$null, [ref]$null) # Find and add aliases defined by Set-Alias - $ast.EndBlock.Statements - | Where-Object { $_ -match "^\s*Set-Alias .+" } - | ForEach-Object { $_.Extent.text } - | ForEach-Object { - $params = $_ -split '\s+' - # $content += "`n$_" - - if ($_ -imatch "-na") { - # alias is defined using -Name parameter to Set-Alias - # so get the alias name from the next parameter + $outsideFunctionAliasName = ( + $ast.EndBlock.Statements + | Where-Object { $_ -match "^\s*Set-Alias .+" } + | ForEach-Object { $_.Extent.text } + ) + + # Parse Set-Alias statements to extract the alias name + ForEach ($saEntry in $outsideFunctionAliasName) { + $saParams = $saEntry -split '\s+' + + # Check if alias is defined using -Name parameter to Set-Alias + # and if so get the alias name from the next parameter after -Name + if ($saEntry -imatch "-na") { $i = 0 $parNameLoc = 0 - ForEach ($par in $params) { + # Loop through all parameters for this Set-Alias command + ForEach ($par in $saParams) { if ($par -imatch "-na") { $parNameLoc = $i } ++$i } - $aliasToExport += $params[$parNameLoc + 1] + $aliasesToExport += $saParams[$parNameLoc + 1] } + # If -Name parameter is not used then assume the alias name is the + # first parameter to Set-Alias else { - $aliasToExport += $params[1] + $aliasesToExport += $saParams[1] } - # Write-Verbose "Content: $_" } # Find and add aliases defined by [Alias("Some-Alias")] attribute + # of functions $insideFunctionAliasName = $ast.FindAll({ - $args[0] -is [System.Management.Automation.Language.AttributeAst] - }, $true) - | Where-Object { $_.Parent.Extent.text -match '^param' } - | Select-Object -ExpandProperty PositionalArguments - | Select-Object -ExpandProperty Value -ErrorAction SilentlyContinue + $args[0] -is [System.Management.Automation.Language.AttributeAst]}, $true) + | Where-Object { $_.Parent.Extent.text -match '^param' } + | Select-Object -ExpandProperty PositionalArguments + | Select-Object -ExpandProperty Value -ErrorAction SilentlyContinue if ($insideFunctionAliasName) { $insideFunctionAliasName | ForEach-Object { - $aliasToExport += $_ + $aliasesToExport += $_ } } - return $aliasToExport + + return $aliasesToExport } catch { return '' } } \ No newline at end of file From a85929eb3d5a4b025185a47e6f7ea5ceec9d9ac1 Mon Sep 17 00:00:00 2001 From: "djs-zmtc [RAZORBACK]" Date: Sun, 18 May 2025 19:55:11 -0400 Subject: [PATCH 5/5] added alias handling to Build-Module.ps1 to include Export-ModuleMember to PSM1 --- src/private/Build-Module.ps1 | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/private/Build-Module.ps1 b/src/private/Build-Module.ps1 index badccf1..57674f8 100644 --- a/src/private/Build-Module.ps1 +++ b/src/private/Build-Module.ps1 @@ -7,8 +7,10 @@ function Build-Module { # Public Folder $files = Get-ChildItem -Path $data.PublicDir -Filter *.ps1 + $aliasesToExport = @() $files | ForEach-Object { $sb.AppendLine([IO.File]::ReadAllText($_.FullName)) | Out-Null + $aliasesToExport += Get-AliasNamesFromFile -filePath $_.FullName } # Private Folder @@ -23,4 +25,14 @@ function Build-Module { } catch { Write-Error 'Failed to create psm1 file' -ErrorAction Stop } + + if ($aliasesToExport) { + $aliasesToExport = $aliasesToExport | Select-Object -Unique | Sort-Object + $exportModuleMember = "Export-ModuleMember -Alias $($aliasesToExport -join ', ')" + try { + Add-Content -Path $data.ModuleFilePSM1 -Value $exportModuleMember -Encoding 'UTF8' -ErrorAction Stop + } catch { + Write-Error 'Failed to add Export-ModuleMember to psm1 file' -ErrorAction Stop + } + } } \ No newline at end of file