notes/snippet/powershell/venv.ps1

45 lines
1.1 KiB
PowerShell

$UserShell = $(Get-ChildItem "HKCU:Software\Microsoft\Windows\CurrentVersion\Explorer")
$Folders = $UserShell.Get(34)
$DocPlace = $Folders.GetValue("Personal")
$VenvPath = Join-Path -Path $DocPlace -ChildPath "Pyvenv"
function CheckOrCreate($PathName) {
$PathExists = $(Test-Path $PathName)
if ($PathExists) {
return
}
New-Item $PathName -ItemType Directory
}
CheckOrCreate($VenvPath)
function lsvenv() {
Get-ChildItem $VenvPath
}
function mkvenv($name) {
$VenvNamePath = Join-Path -Path $VenvPath -ChildPath $name
CheckOrCreate($VenvNamePath)
python -m venv $VenvNamePath
}
function rmvenv($name) {
$VenvNamePath = Join-Path -Path $VenvPath -ChildPath $name
$PathExists = $(Test-Path $VenvNamePath)
if (-not $PathExists) {
return
}
Remove-Item $VenvNamePath -Recurse
}
function workon($name) {
$VenvNamePath = Join-Path -Path $VenvPath -ChildPath $name
$VenvScripts = Join-Path -Path $VenvNamePath -ChildPath "Scripts"
$VenvActive = Join-Path -Path $VenvScripts -ChildPath "Activate.ps1"
$PathExists = $(Test-Path $VenvNamePath)
if (-not $PathExists) {
return
}
& $VenvActive
}