-
-
Save DerekZiemba/5ea510e905f6f363e909ec6cc7681e4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Resolve Paths that have environment variables that works for cmd, bash, and powershell | |
# Resolve Paths with wildcards and splits on delimiters | |
class PathEnvVarsShellCompat { | |
static [hashtable]$EnvVars = ({ | |
$hs = @{}; | |
Get-ChildItem -Path 'env:' | ForEach-Object { $hs.Add($_.Key, $_.Value) } | |
$hs | Sort-Object; | |
}.InvokeReturnAsIs()); | |
static [regex[]]$RGX_PathVars = @( | |
[regex]::new('\%([A-Z_]+?)\%', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase), # Match Cmd Variables | |
[regex]::new('\$env:([A-Z_]+?)\b', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase), # Match PowerShell Variables | |
[regex]::new('\$([A-Z_]+?)\b', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase) # Match Bash Variables | |
); | |
# Sub environment variables that works for cmd, bash, and powershell | |
static [string] SubEnvVars([string]$aPath) { | |
[hashtable]$Vars = [PathEnvVarsShellCompat]::EnvVars; | |
foreach($rgx in [PathEnvVarsShellCompat]::RGX_PathVars) { | |
foreach ($rMatch in $rgx.Matches($aPath)) { | |
[string]$sKey = $rMatch.Groups[1].Value; | |
[string]$sVal = $Vars.$sKey; | |
if (!([string]::IsNullOrWhiteSpace($sVal))) { | |
$aPath = $aPath.Replace($rMatch.Value, $sVal); | |
} else { | |
$sVal = [string](Get-Variable -Scope Script -Name $sKey -ErrorAction Ignore -ValueOnly) | |
if (![string]::IsNullOrWhiteSpace($sVal)) { | |
$aPath = $aPath.Replace($rMatch.Value, $sVal); | |
} | |
} | |
} | |
} | |
return $aPath; | |
} | |
# Resolve Paths that have environment variables that works for cmd, bash, and powershell | |
# Resolve Paths with wildcards and splits on delimiters | |
static [System.Collections.Generic.List[string]] ResolveCompatPaths([string[]]$aPaths) { | |
$result = [System.Collections.Generic.List[string]]::new(); | |
foreach ($path in $aPaths.Split([char[]]@(',', ';'), [System.StringSplitOptions]::RemoveEmptyEntries).Trim()) { | |
[string]$subbed = [PathEnvVarsShellCompat]::SubEnvVars($path); | |
foreach ($resolved in (Resolve-Path $subbed -)) { $result.Add($resolved.Path); } | |
} | |
return $result; | |
} | |
} | |
# Resolve Paths that have environment variables that works for cmd, bash, and powershell | |
function Resolve-CPaths([parameter(Mandatory, ValueFromPipeline)][string[]]$paths) { | |
[PathEnvVarsShellCompat]::ResolveCompatPaths($paths); | |
} | |
######################################## | |
# Tests | |
######################################## | |
$RoamPart = 'AppData\Roaming' | |
$AppCache = 'LocalLow\*\Win*\*Cache\..\..\**' | |
'%UserProfile%\$RoamPart' | Resolve-CPaths | |
# C:\Users\Derek\AppData\Roaming | |
Resolve-CPaths('$UserProfile\$RoamPart\Code\**.json', | |
'$ENV:APPDATA\..\Roaming\Notepad++\stylers.xml', | |
'%LocalAPPDATA%\**\Crashpad\reports\*.dmp', | |
'%APPDATA%\..\$AppCache\**\*.json' | |
) | |
# C:\Users\Derek\AppData\Roaming\Code\languagepacks.json | |
# C:\Users\Derek\AppData\Roaming\Code\rapid_render.json | |
# C:\Users\Derek\AppData\Roaming\Code\storage.json | |
# C:\Users\Derek\AppData\Roaming\Notepad++\stylers.xml | |
# C:\Users\Derek\AppData\Local\Dropbox\Crashpad\reports\229cd2b4-7a08-4839-bae3-292e8ad4eb95.dmp | |
# C:\Users\Derek\AppData\Local\Dropbox\Crashpad\reports\22ab84a6-d133-4af5-86cf-c48f20c31543.dmp | |
# C:\Users\Derek\AppData\Local\Dropbox\Crashpad\reports\4c0fd893-8917-4353-adae-a77d66f7012b.dmp | |
# C:\Users\Derek\AppData\Local\Dropbox\Crashpad\reports\b5506a55-a06a-4b9f-810e-73f5dc79102c.dmp | |
# C:\Users\Derek\AppData\Local\Dropbox\Crashpad\reports\f60f26ca-6cac-4e9c-984f-42460c0db881.dmp | |
# C:\Users\Derek\AppData\LocalLow\Microsoft\F12\debugger\settings.json | |
# C:\Users\Derek\AppData\LocalLow\Microsoft\F12\emulation\settings.json | |
# C:\Users\Derek\AppData\LocalLow\Microsoft\F12\header\MyCode.json | |
# C:\Users\Derek\AppData\LocalLow\Microsoft\F12\network\settings.json | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment