Skip to content

Commit 3ff03de

Browse files
authored
Merge pull request #14 from belibug/alias
Include Aliases to export in Module Manifest
2 parents 1cf201b + 99a1648 commit 3ff03de

5 files changed

Lines changed: 41 additions & 4 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
temp/
22
run.ps1
3-
dist/
3+
dist/
4+
justfile

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [1.1.0] - 2025-08-28
6+
7+
## Added
8+
9+
- Now Module manifest includes `AliasesToExport`. This helps loading aliases without explicitly importing modules to session.
10+
- thanks to @djs-zmtc for suggesting the feature
11+
512
## [1.0.0] - 2025-03-11
613

714
### Added

project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"ProjectName": "ModuleTools",
33
"Description": "ModuleTools is a versatile, standalone PowerShell module builder. Create anything from simple to robust modules with ease. Built for CICD and Automation.",
4-
"Version": "1.0.0",
4+
"Version": "1.1.0",
55
"copyResourcesToModuleRoot": false,
66
"Manifest": {
77
"Author": "Manjunath Beli",

src/private/Build.Manifest.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ function Build-Manifest {
66

77
$PubFunctionFiles = Get-ChildItem -Path $data.PublicDir -Filter *.ps1
88
$functionToExport = @()
9-
$PubFunctionFiles | ForEach-Object {
10-
$functionToExport += Get-FunctionNameFromFile -filePath $_.FullName
9+
$aliasToExport = @()
10+
$PubFunctionFiles.FullName | ForEach-Object {
11+
$functionToExport += Get-FunctionNameFromFile -filePath $_
12+
$aliasToExport += Get-AliasInFunctionFromFile -filePath $_
1113
}
1214

1315
$ManfiestAllowedParams = (Get-Command New-ModuleManifest).Parameters.Keys
@@ -16,6 +18,7 @@ function Build-Manifest {
1618
Path = $data.ManifestFilePSD1
1719
Description = $data.Description
1820
FunctionsToExport = $functionToExport
21+
AliasesToExport = $aliasToExport
1922
RootModule = "$($data.ProjectName).psm1"
2023
ModuleVersion = $data.Version
2124
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<#
2+
.SYNOPSIS
3+
Retrieves information about alias in a given function/file so it can be added to module manifest
4+
5+
.DESCRIPTION
6+
Adding alias to module manifest and exporting it will ensure that functions can be called using alias without importing explicitly
7+
#>
8+
function Get-AliasInFunctionFromFile {
9+
param($filePath)
10+
try {
11+
$ast = [System.Management.Automation.Language.Parser]::ParseFile($filePath, [ref]$null, [ref]$null)
12+
13+
$functionNodes = $ast.FindAll({
14+
param($node)
15+
$node -is [System.Management.Automation.Language.FunctionDefinitionAst]
16+
}, $true)
17+
18+
$function = $functionNodes[0]
19+
$paramsAttributes = $function.Body.ParamBlock.Attributes
20+
21+
$aliases = ($paramsAttributes | Where-Object { $_.TypeName -like 'Alias' } | ForEach-Object PositionalArguments).Value
22+
$aliases
23+
} catch {
24+
return
25+
}
26+
}

0 commit comments

Comments
 (0)