|
25 | 25 |
|
26 | 26 | # Folder where the module is outputted.
|
27 | 27 | [Parameter(Mandatory)]
|
28 |
| - [System.IO.DirectoryInfo] $ModuleOutputFolder |
| 28 | + [System.IO.DirectoryInfo] $ModuleSourceFolder |
29 | 29 | )
|
30 | 30 | LogGroup 'Updating aliases to export in module manifest' {
|
31 | 31 | Write-Host "Module name: [$ModuleName]"
|
32 |
| - Write-Host "Module output folder: [$ModuleOutputFolder]" |
33 |
| - $aliases = Get-Command -Module $ModuleName -CommandType Alias |
34 |
| - Write-Host "Found aliases: [$($aliases.Count)]" |
35 |
| - foreach ($alias in $aliases) { |
36 |
| - Write-Host "Alias: [$($alias.Name)]" |
| 32 | + Write-Host "Module output folder: [$ModuleSourceFolder]" |
| 33 | + |
| 34 | + $publicFunctionsPath = Join-Path -Path $ModuleSourceFolder -ChildPath 'public/functions' |
| 35 | + Write-Host "Public functions path: [$publicFunctionsPath]" |
| 36 | + if (-not (Test-Path -Path $publicFunctionsPath)) { |
| 37 | + Write-Host "Public functions path does not exist: [$publicFunctionsPath]" |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + # Get all child items in the module source folder of a powershell file type |
| 42 | + $files = Get-ChildItem -Path $publicFunctionsPath -Recurse -File -Include '*.ps1', '*.psm1' | Select-Object -ExpandProperty Path |
| 43 | + |
| 44 | + # Initialize an array to store all found aliases |
| 45 | + $allAliases = @() |
| 46 | + |
| 47 | + foreach ($file in $files) { |
| 48 | + Write-Host "Parsing file: [$file]" |
| 49 | + |
| 50 | + # Parse the file using AST |
| 51 | + $ast = [System.Management.Automation.Language.Parser]::ParseFile( |
| 52 | + $file, |
| 53 | + [ref]$null, |
| 54 | + [ref]$null |
| 55 | + ) |
| 56 | + |
| 57 | + # Get all function definitions |
| 58 | + $functionAsts = $ast.FindAll( |
| 59 | + { |
| 60 | + param($node) |
| 61 | + $node -is [System.Management.Automation.Language.FunctionDefinitionAst] |
| 62 | + }, $false) |
| 63 | + |
| 64 | + Write-Host " Found functions: [$($functionAsts.Count)]" |
| 65 | + foreach ($functionAst in $functionAsts) { |
| 66 | + # Get the function name |
| 67 | + $functionName = $functionAst.Name |
| 68 | + Write-Host " Processing: [$functionName]" |
| 69 | + |
| 70 | + $functionAst | ForEach-Object { |
| 71 | + $funcName = $_.Name |
| 72 | + $funcAttributes = $_.Body.FindAll({ $args[0] -is [System.Management.Automation.Language.AttributeAst] }, $true) | Where-Object { |
| 73 | + $_.Parent -is [System.Management.Automation.Language.ParamBlockAst] |
| 74 | + } |
| 75 | + $aliasAttr = $funcAttributes | Where-Object { $_.TypeName.Name -eq 'Alias' } |
| 76 | + |
| 77 | + if ($aliasAttr) { |
| 78 | + $allAliases = $aliasAttr.PositionalArguments | ForEach-Object { $_.ToString().Trim('"', "'") } |
| 79 | + Write-Host " Found alias [$allAliases] for function [$funcName]" |
| 80 | + $allAliases += $aliases |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + Write-Host "Found aliases: [$($allAliases.Count)]" |
| 87 | + foreach ($alias in $allAliases) { |
| 88 | + Write-Host " - [$($alias.Name)]" |
37 | 89 | }
|
38 |
| - $outputManifestPath = Join-Path -Path $ModuleOutputFolder -ChildPath "$ModuleName.psd1" |
| 90 | + $outputManifestPath = Join-Path -Path $ModuleSourceFolder -ChildPath "$ModuleName.psd1" |
39 | 91 | Write-Host "Output manifest path: [$outputManifestPath]"
|
40 | 92 | Write-Host 'Setting module manifest with AliasesToExport'
|
41 |
| - Set-ModuleManifest -Path $outputManifestPath -AliasesToExport $aliases.Name -Verbose |
| 93 | + Set-ModuleManifest -Path $outputManifestPath -AliasesToExport $allAliases.Name -Verbose |
42 | 94 | }
|
43 | 95 | }
|
0 commit comments