Skip to content

Instantly share code, notes, and snippets.

@Cologler
Created January 2, 2020 04:33
Show Gist options
  • Save Cologler/2d3c152a9ef829c16174a161206732e4 to your computer and use it in GitHub Desktop.
Save Cologler/2d3c152a9ef829c16174a161206732e4 to your computer and use it in GitHub Desktop.
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