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 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 } diff --git a/src/private/GetAliasNamesFromFile.ps1 b/src/private/GetAliasNamesFromFile.ps1 new file mode 100644 index 0000000..30e7b6a --- /dev/null +++ b/src/private/GetAliasNamesFromFile.ps1 @@ -0,0 +1,60 @@ +# Get-AliasNamesFromFile +function Get-AliasNamesFromFile { + param($filePath) + + $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 + $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 + # Loop through all parameters for this Set-Alias command + ForEach ($par in $saParams) { + if ($par -imatch "-na") { + $parNameLoc = $i + } + ++$i + } + $aliasesToExport += $saParams[$parNameLoc + 1] + } + # If -Name parameter is not used then assume the alias name is the + # first parameter to Set-Alias + else { + $aliasesToExport += $saParams[1] + } + } + + # 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 + + if ($insideFunctionAliasName) { + $insideFunctionAliasName | ForEach-Object { + $aliasesToExport += $_ + } + } + + return $aliasesToExport + } + catch { return '' } +} \ No newline at end of file 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 '' }