Last active
August 29, 2015 14:04
-
-
Save kenhia/8b56aaacd470bc70d024 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
Set-StrictMode -Version latest | |
# fod : Feet on Desk, just a personal thing I use in place of a corporation for personal stuff | |
$hkfod = 'HKCU:\Software\fod' | |
$hkcdalias = Join-Path -Path $hkfod -ChildPath cdalias | |
$Script:xlocation = @{} | |
function Set-XLocation | |
{ | |
<# | |
.Synopsis | |
Sets location based on the alias name used to call the function | |
.DESCRIPTION | |
Uses the name of the alias used to call the function to look up | |
a path in the script-scoped dictionary 'xlocation' then sets | |
the location to that value. | |
.PARAMETER ChildPath | |
Optional child path, if passed it will be joined with the base. | |
.NOTES | |
Not sure why the filter trick works, but it does. Since src is my | |
most commonly used alias location, I can use it to auto-load. What | |
really confuses me is that it still sets the location to src the | |
first time I type it. | |
#> Param | |
( | |
[string]$ChildPath | |
) | |
$k = $MyInvocation.InvocationName | |
if ($Script:xlocation.ContainsKey($k)) | |
{ | |
Set-Location -Path (Join-Path -Path $Script:xlocation[$k] -ChildPath $ChildPath) | |
} | |
else | |
{ | |
Write-Warning "`$xlocation does not contain the key '$k'." | |
} | |
} | |
function Save-XLocationData | |
{ | |
if (($Script:xlocation -eq $null) -and ($Script:xlocation.Count -gt 0)) | |
{ | |
Write-Warning 'No XLocation data to save. Use Add-XLocationItem to add data.' | |
} | |
New-HKCdAlias | Write-Verbose | |
$tmp = Get-Item $hkcdalias | |
foreach ($k in $Script:xlocation.Keys) | |
{ | |
if ($tmp.Property.Contains($k)) | |
{ | |
Set-ItemProperty -Path $hkcdalias -Name $k -Value $Script:xlocation[$k] | |
} | |
else | |
{ | |
New-ItemProperty -Path $hkcdalias -Name $k -Value $Script:xlocation[$k] -PropertyType "String" | |
} | |
} | |
} | |
function New-HKCdAlias | |
{ | |
if (-not (Test-Path $hkfod)) | |
{ | |
New-Item $hkfod | |
} | |
if (-not (Test-Path $hkcdalias)) | |
{ | |
New-Item $hkcdalias | |
} | |
} | |
function Get-XLocationData | |
{ | |
New-HKCdAlias | |
$data = Get-Item -Path $hkcdalias | |
foreach ($prop in $data.Property) | |
{ | |
if ($Script:xlocation.ContainsKey($prop)) | |
{ | |
$Script:xlocation[$prop] = $data.GetValue($prop) | |
} | |
else | |
{ | |
$Script:xlocation.Add($prop, $data.GetValue($prop)) | |
} | |
} | |
} | |
function Set-XLocationAlias | |
{ | |
<# | |
.Synopsis | |
Sets or adds an alias/location pair | |
.DESCRIPTION | |
Sets the alias/location pair, creates the new alias, and | |
saves the current set of alias/locations in the Script:xlocation | |
hash to the registry. | |
The path passed in will be tested, if it does not resolve a warning | |
will be issued and the pair will not be set. This behavior can be | |
overriden with the -force switch. | |
.PARAMETER alias | |
The alias to add | |
.PARAMETER path | |
The location associated with the alias | |
.PARAMETER force | |
Switch that will cause a new pair to be saved even if the path | |
does not resolve. | |
#> | |
Param( | |
[Parameter(Mandatory=$true)] | |
[ValidatePattern('[^\s]+')] | |
[string]$alias, | |
[Parameter(Mandatory=$true)][string]$path, | |
[switch]$force | |
) | |
if (-not $force) | |
{ | |
if (-not (Test-Path -Path $path)) | |
{ | |
Write-Warning "$path not found; XLocation not added (use -force to insist)." | |
return | |
} | |
} | |
if ($Script:xlocation.ContainsKey($alias)) | |
{ | |
$Script:xlocation[$alias] = $path | |
} | |
else | |
{ | |
$Script:xlocation.Add($alias, $path) | |
} | |
Set-Alias -Name $alias -Value Set-XLocation -Scope Global | |
Save-XLocationData | |
} | |
Get-XLocationData | |
foreach ($k in $Script:xlocation.Keys) { Set-Alias -Name $k -Value Set-XLocation } | |
# This enables autoloading when 'src' is typed at the prompt | |
filter src | |
{ | |
} | |
Export-ModuleMember -Function Set-XLocation | |
Export-ModuleMember -Function Set-XLocationAlias | |
Export-ModuleMember -Function Save-XLocationData | |
Export-ModuleMember -Alias * |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment