Last active
June 14, 2022 21:12
-
-
Save exlted/ee4c3d024e7d6a1f89c51e5fc1fec963 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using module Simple-Settings | |
using module Error-Handling | |
$ProgramListStorageLoc = Join-Path $PSScriptRoot "progList.xml" | |
$global:ProgramList = New-Object -TypeName SettingsObj -ArgumentList $ProgramListStorageLoc | |
$LocationListStorageLoc = Join-Path $PSScriptRoot "locList.xml" | |
$global:LocationList = New-Object -TypeName SettingsObj -ArgumentList $LocationListStorageLoc | |
function Set-Program([string]$path, [string]$shortName, [string]$openIn) { | |
Assert-False($shortName -contains '|') | |
Assert-False($path -contains '|') | |
if ([string]::IsNullOrEmpty($openIn)) { | |
$global:ProgramList.setSetting($shortName, $path) | |
} else { | |
$global:ProgramList.setSetting($shortName, $path + '|' + $openIn) | |
} | |
} | |
function Remove-Program($shortName) { | |
$global:ProgramList.removeSetting($shortName); | |
} | |
function Get-ProgramList { | |
$global:ProgramList.printSettings() | |
} | |
function Set-FileLink([string]$path, [string]$programShortName, [string]$shortName) { | |
Assert-False($path -contains '|') | |
Assert-True($global:ProgramList.hasSetting($programShortName)) | |
$global:LocationList.setSetting($shortName, $programShortName + '|' + $path) | |
} | |
function Set-ScriptLink([scriptblock]$script, [string]$programShortName, [string]$shortName) { | |
Assert-False($script -contains '|') | |
Assert-True($global:ProgramList.hasSetting($programShortName)) | |
$global:LocationList.setSetting($shortName, $programShortName + '|' + "scriptblock" + '|' + $script); | |
} | |
function Remove-FileLink($shortName) { | |
$global:LocationList.removeSetting($shortName); | |
} | |
function Get-FileLinks { | |
$global:LocationList.printSettings() | |
} | |
function Open-FileLink ([string]$shortName) { | |
Assert-True($global:LocationList.hasSetting($shortName)) | |
$setting = $global:LocationList.getSetting($shortName).split('|') | |
Assert-True($global:ProgramList.hasSetting($setting[0])) | |
$openIn = $global:ProgramList.getSetting($setting[0]).split('|') | |
$settings = $setting[1].split(','); | |
# Allow for 3+ arg items inside of our setup without requiring everything to be 3 args | |
if ($setting.Count -ge 2) { | |
if ($setting[1] -eq "scriptblock") { | |
$settings = $setting[2]; | |
$scriptblock = [scriptblock]::Create($settings); | |
$settings = Invoke-Command $scriptblock; | |
} | |
} | |
if ($openIn -is [array]){ | |
Push-Location $openIn[1] | |
&$openIn[0] $settings $args | |
Pop-Location | |
} else { | |
&$openIn $settings $args | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated this today with support for extra args when opening filelinks (eg. Open-FIleLink vs , opening the named solution in the current folder) as well as supporting scriptblocks to generate the args (eg Open-FileLink cwd opening the current working directory in your file explorer)