-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake.ps1
More file actions
136 lines (119 loc) · 4.43 KB
/
make.ps1
File metadata and controls
136 lines (119 loc) · 4.43 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<#
.SYNOPSIS
Admin script for setting up, activating, and cleaning your project venv with Poetry.
.PARAMETER task
What to do: 'active', 'make', 'update', or 'clean'.
Defaults to 'active'.
#>
param (
[ValidateSet("active","make","update","clean")]
[string]$task = "active"
)
$ErrorActionPreference = 'Stop'
# Pin your Poetry version here:
[string]$poetryVersion = '2.2.1'
# Ensure we run from repo root
Push-Location (Split-Path -Parent $MyInvocation.MyCommand.Definition)
# --- read exact Python version from .python-version ---
if (Test-Path ".\.python-version") {
$pythonVersion = (Get-Content ".\.python-version" -ErrorAction Stop).Trim()
if (-not $pythonVersion) {
Throw ".python-version is empty. Write '3.13' in it."
}
} else {
Throw "Required file .python-version not found. Create it with '3.13' as it's only content."
}
function Ensure-PythonPath {
if ($env:PYTHONPATH) {
if (-not ($env:PYTHONPATH -match [regex]::Escape($PWD))) {
$env:PYTHONPATH = "$PWD;$env:PYTHONPATH"
Write-Output "`nPYTHONPATH updated to: $env:PYTHONPATH"
} else {
Write-Output "`nPYTHONPATH already includes project root."
}
} else {
$env:PYTHONPATH = $PWD
Write-Output "`nPYTHONPATH set to: $env:PYTHONPATH"
}
}
switch ($task) {
"active" {
. .\venv\Scripts\Activate.ps1
Ensure-PythonPath
Write-Output "`nUsing Python in venv '$(Split-Path $env:VIRTUAL_ENV -Leaf)':"
python --version
}
"update" {
. .\venv\Scripts\Activate.ps1
Ensure-PythonPath
Write-Output "`nUsing Python in venv '$(Split-Path $env:VIRTUAL_ENV -Leaf)':"
python --version
poetry update --with dev
poetry export --output requirements.txt --without-hashes --all-groups
}
"make" {
# remove any existing venv
if (Test-Path .\venv) { Remove-Item .\venv -Recurse -Force }
# if pyenv exists, pin it; otherwise verify system Python
if (Test-Path "$env:USERPROFILE\.pyenv") {
pyenv global $pythonVersion
pyenv local $pythonVersion
Write-Output "Set pyenv global & local to Python $pythonVersion."
} else {
$sysVer = (& python --version 2>&1) -replace 'Python ', ''
if ($sysVer -eq $pythonVersion) {
Write-Output "System Python $sysVer matches required $pythonVersion."
} else {
Write-Warning "System Python is $sysVer; expected $pythonVersion. Please install or use pyenv-win."
}
}
# create & activate venv
python -m venv .\venv
if ($LASTEXITCODE -eq 0) {
Write-Host "✅ Virtual environment created." -ForegroundColor Green
} else {
Write-Host "❌ Failed to create virtual environment." -ForegroundColor Red
exit 1
}
. .\venv\Scripts\Activate.ps1
Ensure-PythonPath
Write-Output "`nUsing Python in venv '$(Split-Path $env:VIRTUAL_ENV -Leaf)':"
python --version
# install tooling & deps
python -m pip install --upgrade pip
python -m pip install "poetry==$poetryVersion"
if (Test-Path 'poetry.lock') {
Remove-Item 'poetry.lock' -Force -ErrorAction SilentlyContinue
}
if (Test-Path 'requirements.txt') {
Remove-Item 'requirements.txt' -Force -ErrorAction SilentlyContinue
}
poetry config warnings.export false
poetry install --no-root --with dev
poetry run pre-commit install
poetry export --output requirements.txt --without-hashes --all-groups
}
"clean" {
# deactivate any active virtual environment (silently if none)
if ($env:VIRTUAL_ENV) {
& "$env:VIRTUAL_ENV\Scripts\deactivate.bat" 2>$null
}
# uninstall hooks, tear down venv & lockfile
poetry run pre-commit uninstall
if (Test-Path .\venv) {
Remove-Item .\venv -Recurse -Force
}
if (Test-Path 'poetry.lock') {
Remove-Item 'poetry.lock' -Force -ErrorAction SilentlyContinue
}
if (Test-Path 'requirements.txt') {
Remove-Item 'requirements.txt' -Force -ErrorAction SilentlyContinue
}
Write-Output "🧹 Clean complete."
}
default {
Write-Error "Invalid task '$task'. Use active, make, update, or clean."
exit 1
}
}
Pop-Location