Skip to content

Commit 96389a0

Browse files
authored
Merge pull request #74 from PoshCode/feature/ExportAlias
Implement exporting Aliases from public functions by default
2 parents a800be5 + bdef9c7 commit 96389a0

19 files changed

Lines changed: 349 additions & 285 deletions
-5.33 KB
Binary file not shown.

PotentialContribution/ModuleBuilder.psm1

Lines changed: 0 additions & 239 deletions
This file was deleted.

Source/ModuleBuilder.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
# Always define FunctionsToExport as an empty @() which will be replaced on build
3939
FunctionsToExport = @()
40+
AliasesToExport = @()
4041

4142
# ID used to uniquely identify this module
4243
GUID = '4775ad56-8f64-432f-8da7-87ddf7a34653'

Source/Private/ConvertToAst.ps1

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function ConvertToAst {
2+
<#
3+
.SYNOPSIS
4+
Parses the given code and returns an object with the AST, Tokens and ParseErrors
5+
#>
6+
param(
7+
# The script content, or script or module file path to parse
8+
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
9+
[Alias("Path", "PSPath", "Definition", "ScriptBlock", "Module")]
10+
$Code
11+
)
12+
process {
13+
Write-Debug " ENTER: ConvertToAst $Code"
14+
$ParseErrors = $null
15+
$Tokens = $null
16+
if ($Code | Test-Path -ErrorAction SilentlyContinue) {
17+
Write-Debug " Parse Code as Path"
18+
$AST = [System.Management.Automation.Language.Parser]::ParseFile(($Code | Convert-Path), [ref]$Tokens, [ref]$ParseErrors)
19+
} elseif ($Code -is [System.Management.Automation.FunctionInfo]) {
20+
Write-Debug " Parse Code as Function"
21+
$String = "function $($Code.Name) { $($Code.Definition) }"
22+
$AST = [System.Management.Automation.Language.Parser]::ParseInput($String, [ref]$Tokens, [ref]$ParseErrors)
23+
} else {
24+
Write-Debug " Parse Code as String"
25+
$AST = [System.Management.Automation.Language.Parser]::ParseInput([String]$Code, [ref]$Tokens, [ref]$ParseErrors)
26+
}
27+
28+
Write-Debug " EXIT: ConvertToAst"
29+
[PSCustomObject]@{
30+
PSTypeName = "PoshCode.ModuleBuilder.ParseResults"
31+
ParseErrors = $ParseErrors
32+
Tokens = $Tokens
33+
AST = $AST
34+
}
35+
}
36+
}

Source/Private/GetCommandAlias.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function GetCommandAlias {
2+
[CmdletBinding()]
3+
param(
4+
# Path to the PSM1 file to amend
5+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
6+
[System.Management.Automation.Language.Ast]$AST
7+
)
8+
begin {
9+
$Result = [Ordered]@{}
10+
}
11+
process {
12+
foreach($function in $AST.FindAll(
13+
{ $Args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] },
14+
$false )
15+
) {
16+
$Result[$function.Name] = $function.Body.ParamBlock.Attributes.Where{
17+
$_.TypeName.Name -eq "Alias" }.PositionalArguments.Value
18+
}
19+
}
20+
end {
21+
$Result
22+
}
23+
}

Source/Private/MoveUsingStatements.ps1

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,24 @@ function MoveUsingStatements {
1919
Param(
2020
# Path to the PSM1 file to amend
2121
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
22-
$RootModule,
22+
[System.Management.Automation.Language.Ast]$AST,
23+
24+
[Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)]
25+
[AllowNull()]
26+
[System.Management.Automation.Language.ParseError[]]$ParseErrors,
2327

2428
# The encoding defaults to UTF8 (or UTF8NoBom on Core)
2529
[Parameter(DontShow)]
2630
[string]$Encoding = $(if ($IsCoreCLR) { "UTF8NoBom" } else { "UTF8" })
2731
)
2832

29-
$ParseError = $null
30-
$AST = [System.Management.Automation.Language.Parser]::ParseFile(
31-
$RootModule,
32-
[ref]$null,
33-
[ref]$ParseError
34-
)
35-
3633
# Avoid modifying the file if there's no Parsing Error caused by Using Statements or other errors
37-
if (!$ParseError.Where{$_.ErrorId -eq 'UsingMustBeAtStartOfScript'}) {
34+
if (!$ParseErrors.Where{$_.ErrorId -eq 'UsingMustBeAtStartOfScript'}) {
3835
Write-Debug "No Using Statement Error found."
3936
return
4037
}
4138
# Avoid modifying the file if there's other parsing errors than Using Statements misplaced
42-
if ($ParseError.Where{$_.ErrorId -ne 'UsingMustBeAtStartOfScript'}) {
39+
if ($ParseErrors.Where{$_.ErrorId -ne 'UsingMustBeAtStartOfScript'}) {
4340
Write-Warning "Parsing errors found. Skipping Moving Using statements."
4441
return
4542
}
@@ -70,13 +67,13 @@ function MoveUsingStatements {
7067
$null = [System.Management.Automation.Language.Parser]::ParseInput(
7168
$ScriptText,
7269
[ref]$null,
73-
[ref]$ParseError
70+
[ref]$ParseErrors
7471
)
7572

76-
if ($ParseError) {
73+
if ($ParseErrors) {
7774
Write-Warning "Oops, it seems that we introduced parsing error(s) while moving the Using Statements. Cancelling changes."
7875
}
7976
else {
8077
$null = Set-Content -Value $ScriptText -Path $RootModule -Encoding $Encoding
8178
}
82-
}
79+
}

0 commit comments

Comments
 (0)