1
- function Convert-LineNumber {
1
+ function ConvertTo-SourceLineNumber {
2
2
<#
3
3
. SYNOPSIS
4
4
Convert the line number in a built module to a file and line number in source
@@ -7,43 +7,53 @@ function Convert-LineNumber {
7
7
. EXAMPLE
8
8
Convert-LineNumber -PositionMessage "At C:\Users\Joel\OneDrive\Documents\PowerShell\Modules\ErrorMaker\ErrorMaker.psm1:27 char:4"
9
9
#>
10
+ [Alias (" Convert-LineNumber" )]
10
11
[CmdletBinding (DefaultParameterSetName = " FromString" )]
11
12
param (
12
13
# A position message as found in PowerShell's error messages, ScriptStackTrace, or InvocationInfo
13
14
[Parameter (Mandatory , ValueFromPipeline , ValueFromPipelineByPropertyName , ParameterSetName = " FromString" )]
14
15
[string ]$PositionMessage ,
15
16
17
+ # The SourceFile (from an InvocationInfo) is the module psm1 path
16
18
[Parameter (Mandatory , ValueFromPipelineByPropertyName , Position = 0 , ParameterSetName = " FromInvocationInfo" )]
17
19
[Alias (" PSCommandPath" , " File" , " ScriptName" , " Script" )]
18
20
[string ]$SourceFile ,
19
21
22
+ # The SourceLineNumber (from an InvocationInfo) is the module line number
20
23
[Parameter (Mandatory , ValueFromPipelineByPropertyName , Position = 1 , ParameterSetName = " FromInvocationInfo" )]
21
24
[Alias (" LineNumber" , " Line" , " ScriptLineNumber" )]
22
25
[int ]$SourceLineNumber ,
23
26
27
+ # The actual InvocationInfo
24
28
[Parameter (ValueFromPipeline , DontShow, ParameterSetName = " FromInvocationInfo" )]
25
29
[psobject ]$InputObject ,
26
30
31
+ # If set, passes through the InputObject, overwriting the SourceFile and SourceLineNumber.
32
+ # Otherwise, creates a new SourceLocation object with just those properties.
27
33
[Parameter (ParameterSetName = " FromInvocationInfo" )]
28
34
[switch ]$Passthru
29
35
)
30
36
begin {
31
37
$filemap = @ {}
32
38
}
33
39
process {
34
- if ($PSCmdlet.ParameterSetName -eq " FromString" ) {
40
+ if ($PSCmdlet.ParameterSetName -eq " FromString" ) {
35
41
$Invocation = ParseLineNumber $PositionMessage
36
42
$SourceFile = $Invocation.SourceFile
37
43
$SourceLineNumber = $Invocation.SourceLineNumber
38
44
}
39
- if (! (Test-Path $SourceFile )) {
45
+ if (! (Test-Path $SourceFile )) {
40
46
throw " '$SourceFile ' does not exist"
41
47
}
42
48
Push-Location (Split-Path $SourceFile )
43
49
try {
44
50
if (! $filemap.ContainsKey ($SourceFile )) {
45
51
# Note: the new pattern is #Region but the old one was # BEGIN
46
52
$regions = Select-String ' ^(?:#Region|# BEGIN) (?<SourceFile>.*) (?<LineNumber>\d+)?$' - Path $SourceFile
53
+ if ($regions.Count -eq 0 ) {
54
+ Write-Warning " No SourceMap for $SourceFile "
55
+ return
56
+ }
47
57
$filemap [$SourceFile ] = @ ($regions.ForEach {
48
58
[PSCustomObject ]@ {
49
59
PSTypeName = " BuildSourceMapping"
0 commit comments