Initial commit.
This commit is contained in:
54
Modules/posh-venv/posh-venv.psm1
Normal file
54
Modules/posh-venv/posh-venv.psm1
Normal file
@@ -0,0 +1,54 @@
|
||||
if (Get-Module posh-venv) { return }
|
||||
|
||||
function CompleteVenvName {
|
||||
param($commandName, $parameterName, $wordToComplete,
|
||||
$commandAst, $fakeBoundParameters)
|
||||
|
||||
Get-ChildItem -Directory $env:WORKON_HOME
|
||||
| ForEach-Object { $_.Name }
|
||||
| Where-Object { $_ -Like "$wordToComplete*" }
|
||||
| Sort-Object
|
||||
}
|
||||
|
||||
|
||||
function venv {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateSet("list", "activate", "deactivate")]
|
||||
$command,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[ArgumentCompleter({ CompleteVenvName @args })]
|
||||
$venv)
|
||||
switch ($command) {
|
||||
"list" {
|
||||
Get-ChildItem -Directory $env:WORKON_HOME
|
||||
| ForEach-Object {
|
||||
$name = $_.Name
|
||||
$version = (& "$_/Scripts/python.exe" --version)
|
||||
Write-Output "${name}: $version"
|
||||
}
|
||||
}
|
||||
|
||||
"activate" {
|
||||
if (Test-Path "$env:WORKON_HOME/$venv/Scripts/activate.ps1") {
|
||||
& "$env:WORKON_HOME/$venv/Scripts/activate.ps1"
|
||||
}
|
||||
elseif ((!$venv -Or !$venv.Trim()) -And (Test-Path "venv/Scripts/activate.ps1")) {
|
||||
& "venv/Scripts/activate.ps1"
|
||||
}
|
||||
else {
|
||||
Write-Error "Environment '${venv}' does not exists."
|
||||
}
|
||||
}
|
||||
|
||||
"deactivate" {
|
||||
deactivate
|
||||
}
|
||||
|
||||
default {
|
||||
Write-Error "Unknown venv command ${args[0]}."
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user