PowerShell_Scripts/base/Get-Path.ps1
lksz 6d2a23ae04 Modify Setup, Path, added Get-MyModules
+ base/Get-MyModules added
* base/profile.d/Test-MyModules modified to use Get-MyModules
* Setup-Profile modified so that sudo witll preserve environment
* Get-Path fixed bug introduced in last commit, Get-Path would fail when
  file to be edited didn't exist
2021-02-19 19:22:46 -05:00

54 lines
1.4 KiB
PowerShell

[CmdletBinding()]param(
[switch]$Expand,
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,Position=0)]
[Alias("FullName")]
[string[]]$Path
)
process {
function Get-FullPath {
param([string]$Path)
if([System.IO.Path]::IsPathRooted($Path)){
[System.IO.Path]::GetFullPath($Path)
}else{
[System.IO.Path]::GetFullPath((Join-Path $PWD $Path))
}
}
foreach( $local:p in $Path ) {
switch( $p[0] ) {
'@' { $p = Join-Path $MyPSScriptRoot $p.Substring(1) }
'~' {
if( $p -match '^~[^/]*' ) {
$local:m = $Matches[0]
$p = $p -replace "^$m",((Get-Item -Force $m).FullName)
}
}
}
if([System.IO.Path]::IsPathRooted($p)){
$p = [System.IO.Path]::GetFullPath($p)
}else{
$p = [System.IO.Path]::GetFullPath((Join-Path $PWD $p))
}
if( $Expand ) {
$p = $p | ForEach-Object {
if( Test-Path $p ) {
$p | Get-Item -Force | Select-Object -ExpandProperty FullName
} else {
$p
}
}
}
$p
}
}
# try {
# get-item $Path -Force -ErrorAction Stop |
# Select-Object -ExpandProperty FullName
# } catch {
# $_.targetObject
# }