-
Notifications
You must be signed in to change notification settings - Fork 0
/
va.ps1
executable file
·76 lines (62 loc) · 1.76 KB
/
va.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<#
.SYNOPSIS
Activate venv
#>
param (
[string] $Name,
[switch] $NoInstall
)
$script:ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
try {
deactivate
}
catch {
}
if ($Name) {
$envDir = $Name
} else {
for ($dir = gi (Get-Location); ; $dir = $dir.Parent) {
if (!$dir) {
$newEnvParent = Get-Location
try {
$gitRoot = git rev-parse --show-toplevel
if ($gitRoot) { $newEnvParent = $gitRoot }
} catch { }
$newEnvParent = Convert-Path $newEnvParent # Convert i.e. Temp:/ path to a real filesystem path
$gitIgnore = Join-Path $newEnvParent ".gitignore"
if (Test-Path $gitIgnore) {
Write-Host "Searched /env/ in $gitIgnore" -ForegroundColor Blue
Select-String '^\w*env' $gitIgnore
}
$envDir = Join-Path $newEnvParent "env"
$userChoice = (Read-Host "Create venv directory [$envDir]")
$envDir = $userChoice ? $userChoice : $envDir
break
}
# walk up the path until we find a directory that contains an env directory
$envDir = gci $dir -Directory *env*
if (@($envDir).Count -gt 1) { throw "Found multiple env: $($envDir.Name)" }
if (@($envDir).Count -eq 1) { break }
}
}
if (-not (Test-Path $envDir)) {
py -m venv $envDir
Write-Host "Created venv directory $envDir" -ForegroundColor Green
}
$activate = @(
(Join-Path $envDir Scripts Activate.ps1),
(Join-Path $envDir bin Activate.ps1)
) | ? { Test-Path $_ }
if (@($activate).Length -ne 1) { throw $activate.Name }
. $activate -prompt 'v'
if (-not $NoInstall) {
py -m pip install -q --upgrade pip
$requirementsTXT = Join-Path $envDir .. "requirements.txt"
if (Test-Path $requirementsTXT) {
py -m pip install -q -r $requirementsTXT
}
}
if (Test-Path .env) {
Source-Anything .env
}