Created
January 2, 2020 04:33
-
-
Save Cologler/2d3c152a9ef829c16174a161206732e4 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
param( | |
[Parameter(Position=0, mandatory=$true)] | |
[string] $Command, | |
[Parameter(Position=1, Mandatory=$false, ValueFromRemainingArguments=$true)] | |
[string[]] $CommandArgs | |
) | |
$PM_BASE_DIR = "$env:UserProfile\.pm" | |
function _GetProjectAbsPath { | |
param( | |
[Parameter(Position=0, mandatory=$true)] | |
[string] $Name | |
) | |
$dest = "$PM_BASE_DIR\$name" | |
if (!(Test-Path $dest)) { | |
Write-Host "No project named $name" | |
exit 1 | |
} | |
return $dest | |
} | |
function Help { | |
} | |
function Add { | |
param( | |
[Parameter(Position=0, mandatory=$true)] | |
[string] $Name, | |
[string] $Path = '.' | |
) | |
if (!(Test-Path $path)) { | |
Write-Host "Path ($path) does not exists" | |
exit 1 | |
} | |
$target = Resolve-Path -Path $path | |
New-Item -ItemType Directory -Path $PM_BASE_DIR -Force | Out-Null | |
New-Item -ItemType SymbolicLink -Path "$PM_BASE_DIR\$Name" -Target $target -Force | Out-Null | |
} | |
function Remove { | |
param( | |
[Parameter(Position=0, mandatory=$true)] | |
[string] $Name | |
) | |
Remove-Item -Path (_GetProjectAbsPath $name) | |
} | |
function List { | |
Get-ChildItem $PM_BASE_DIR | Select-Object Name, Target | |
} | |
function Go { | |
param( | |
[Parameter(Position=0, mandatory=$true)] | |
[string] $Name | |
) | |
$path = _GetProjectAbsPath $name | |
$symbolic = Get-Item $path | |
if ($symbolic.Target.Length -gt 0) { | |
Push-Location $symbolic.Target[0] | |
} else { | |
Push-Location $path | |
} | |
} | |
function GoBack { | |
Pop-Location | |
} | |
if ($Command -eq 'add') { | |
Add @CommandArgs | |
} elseif (($Command -eq 'remove') -or ($Command -eq 'rm')) { | |
Remove @CommandArgs | |
} elseif (($Command -eq 'list') -or ($Command -eq 'ls')) { | |
List | |
} elseif ($Command -eq 'Go') { | |
Go @CommandArgs | |
} elseif ($Command -eq 'GoBack') { | |
GoBack | |
} elseif ($command -eq '--help') { | |
help | |
} else { | |
Write-Host "Unknown commands: $Command" | |
help | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment