Skip to content

Commit 3c894b9

Browse files
Refactor path normalization in ConvertTo-NormalizedModulePath function for improved consistency and clarity
1 parent c1faae5 commit 3c894b9

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

scripts/Helpers.psm1

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,23 @@ function ConvertTo-NormalizedModulePath {
8888
)
8989

9090
process {
91-
# Split PSModulePath into individual paths and normalize them
92-
$modulePaths = $env:PSModulePath -split [IO.Path]::PathSeparator |
93-
Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
94-
ForEach-Object { $_.TrimEnd('\', '/') }
95-
96-
# Try to match the start of the path with any module path
97-
foreach ($modulePath in $modulePaths) {
98-
if ($Path -match [regex]::Escape($modulePath)) {
99-
$normalizedPath = $Path -replace [regex]::Escape($modulePath), ''
100-
# Remove any leading path separators
101-
$normalizedPath = $normalizedPath.TrimStart('\', '/')
102-
103-
# If path was successfully normalized, return it
104-
if ($normalizedPath -ne $Path) {
105-
return "Modules/$normalizedPath"
106-
}
107-
} elseif ($Path -match '(runner|runneradmin)[/\\].*[/\\]Modules[/\\]') {
108-
# Handle common GitHub runner paths that might not be in PSModulePath
109-
$normalizedPath = $Path -replace '.*?(Modules[/\\])', 'Modules/'
110-
return $normalizedPath
111-
}
91+
# Normalize backslashes to forward slashes for consistency
92+
$Path = $Path.Replace('\', '/')
93+
94+
# Get only the first module path and normalize it
95+
$modulePath = ($env:PSModulePath -split [IO.Path]::PathSeparator | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | Select-Object -First 1).TrimEnd('\', '/').Replace('\', '/')
96+
97+
if ($modulePath -and $Path -match [regex]::Escape($modulePath)) {
98+
# Remove the module path prefix
99+
$normalizedPath = $Path -replace [regex]::Escape($modulePath), ''
100+
# Remove any leading path separators
101+
$normalizedPath = $normalizedPath.TrimStart('/')
102+
103+
# Return with the standard "Modules/" prefix
104+
return "Modules/$normalizedPath"
112105
}
113106

114-
# If no module path matched, use the existing relative path function
115-
return ConvertTo-RelativePath $Path
107+
# If no match found, just normalize slashes and return the original path
108+
return $Path
116109
}
117110
}

scripts/main.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ LogGroup 'List files' {
2424
}
2525

2626
LogGroup 'Module paths' {
27-
Write-Output "PSModulePath entries:"
27+
Write-Output 'PSModulePath entries:'
2828
$env:PSModulePath -split [IO.Path]::PathSeparator | ForEach-Object { " $_" }
2929
}
3030

0 commit comments

Comments
 (0)