-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile.windows
More file actions
248 lines (222 loc) · 6.93 KB
/
makefile.windows
File metadata and controls
248 lines (222 loc) · 6.93 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<# Makefile.ps1 — Windows task runner for Python projects
Mirrors the GNU Makefile targets with colors, start/done logs, and guards.
Examples:
.\Makefile.ps1
.\Makefile.ps1 install
.\Makefile.ps1 build
.\Makefile.ps1 twine-check
#>
[CmdletBinding()]
param(
[Parameter(Position=0)]
[ValidateNotNullOrEmpty()]
[string]$Task = "help"
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# -------- Configurable variables (env overrides supported) --------
function Get-OrDefault($envName, $default) {
if ($env:$envName) { return $env:$envName } else { return $default }
}
$Python = Get-OrDefault 'PYTHON' 'py -3'
$Package = Get-OrDefault 'PACKAGE' 'PythonRuns'
$IndexUrl = Get-OrDefault 'INDEX_URL' 'https://nexus.myrepo.net/repository/pypi-releases/simple'
$ExtraIndexUrl = Get-OrDefault 'EXTRA_INDEX_URL' 'https://pypi.org/simple'
$RepositoryUpload = Get-OrDefault 'REPOSITORY_UPLOAD' 'https://nexus.myrepo.net/repository/pypi-releases/'
$Uv = Get-OrDefault 'UV' 'uv'
# UV flags (DRY)
$UvFlags = @('--index-url', $IndexUrl, '--extra-index-url', $ExtraIndexUrl)
# -------- Colors --------
$Cyan = "`e[1;36m"
$Green = "`e[1;32m"
$Reset = "`e[0m"
# -------- Logging --------
function Start-Log([string]$name) {
$script:sw = [System.Diagnostics.Stopwatch]::StartNew()
$ts = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
Write-Host ""
Write-Host ("{0}[{1}] ▶ Starting {2}{3}" -f $Cyan, $ts, $name, $Reset)
}
function Done-Log([string]$name) {
if ($script:sw) { $script:sw.Stop() }
$elapsed = if ($script:sw) { [math]::Round($script:sw.Elapsed.TotalSeconds,2) } else { 0 }
Write-Host ("{0}✓ Done {1} in {2}s{3}" -f $Green, $name, $elapsed, $Reset)
}
# -------- Helpers / Guards --------
function Ensure-File([string]$path) {
if (-not (Test-Path -LiteralPath $path -PathType Leaf)) {
throw "Missing file: $path"
}
}
function Ensure-Dist {
if (-not (Test-Path -LiteralPath 'dist' -PathType Container)) {
throw "No 'dist/' directory. Run 'build' first."
}
$files = Get-ChildItem -LiteralPath 'dist' -File -ErrorAction SilentlyContinue
if (-not $files -or $files.Count -eq 0) {
throw "'dist/' is empty. Run 'build' first."
}
return $files
}
function Ensure-Command([string]$cmd, [string]$hint) {
if (-not (Get-Command $cmd -ErrorAction SilentlyContinue)) {
throw "$cmd not found. $hint"
}
}
# ---- Target implementations ----
$Tasks = [ordered]@{
'config' = @{
Description = 'Configure uv pip indexes (Nexus + PyPI)';
Action = {
Start-Log 'config'
& $UvPipCmd config --user set global.index-url $IndexUrl
& $UvPipCmd config --user set global.extra-index-url $ExtraIndexUrl
& $UvPipCmd config --user list
Done-Log 'config'
}
}
'clean' = @{
Description = 'Remove build/test artifacts';
Action = {
Start-Log 'clean'
foreach ($p in 'dist','build','.pytest_cache','.mypy_cache','htmlcov') {
if (Test-Path -LiteralPath $p) { Remove-Item -LiteralPath $p -Recurse -Force -ErrorAction SilentlyContinue }
}
foreach ($f in '.coverage','report.html') {
if (Test-Path -LiteralPath $f -PathType Leaf) { Remove-Item -LiteralPath $f -Force -ErrorAction SilentlyContinue }
}
Get-ChildItem -Recurse -Directory -Filter '__pycache__' -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
Get-ChildItem -Recurse -Directory -Filter '*.egg-info' -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
Done-Log 'clean'
}
}
'sync' = @{
Description = 'Sync dependencies from uv.lock (recommended)';
Action = {
Start-Log 'sync'
Ensure-Command $Uv 'Install uv: https://docs.astral.sh/uv/'
& $Uv sync
Done-Log 'sync'
}
}
'install' = @{
Description = 'Install project with uv (creates venv and syncs dependencies)';
Action = {
Start-Log 'install'
Ensure-Command $Uv 'Install uv: https://docs.astral.sh/uv/'
& $Uv venv
& $Uv sync
Done-Log 'install'
}
}
'update' = @{
Description = 'Update dependencies and regenerate lockfile';
Action = {
Start-Log 'update'
Ensure-Command $Uv 'Install uv: https://docs.astral.sh/uv/'
& $Uv lock --upgrade
& $Uv sync
Done-Log 'update'
}
}
'test' = @{
Description = 'Run tests (verbose)';
Action = {
Start-Log 'test'
& $Python -m pytest --verbose
Done-Log 'test'
}
}
'test-coverage' = @{
Description = 'Run tests with coverage; HTML at ./report.html';
Action = {
Start-Log 'test-coverage'
& $Python -m pytest --cov=$Package --verbose --html=report.html --self-contained-html
Done-Log 'test-coverage'
}
}
'run' = @{
Description = 'Run the package as a module';
Action = {
Start-Log 'run'
& $Python -m $Package
Done-Log 'run'
}
}
'pre-commit' = @{
Description = 'Run all pre-commit hooks';
Action = {
Start-Log 'pre-commit'
if (Get-Command 'pre-commit' -ErrorAction SilentlyContinue) {
& pre-commit run --all-files
} else {
# Fallback to module invocation if CLI shim not in PATH
& $Python -m pre_commit run --all-files
}
Done-Log 'pre-commit'
}
}
'build' = @{
Description = 'Build sdist and wheel into ./dist';
Action = {
Start-Log 'build'
# Ensure 'build' module available
& $Python -c 'import build' 2>$null
& $Python -m build
Done-Log 'build'
}
}
'twine-check' = @{
Description = 'Validate built artifacts (sdist/wheel) with Twine';
Action = {
Start-Log 'twine-check'
$files = Ensure-Dist
& $Python -m twine check ($files.FullName)
Done-Log 'twine-check'
}
}
'deploy' = @{
Description = 'Upload ./dist to Nexus (uses TWINE_* env vars)';
Action = {
Start-Log 'deploy'
$null = Ensure-Dist
& $Python -m twine upload --repository-url $RepositoryUpload dist/*
Done-Log 'deploy'
}
}
'version' = @{
Description = 'Show package version';
Action = {
Start-Log 'version'
& $Python -m $Package -v
Done-Log 'version'
}
}
'help' = @{
Description = 'List available commands (default)';
Action = {
Write-Host "`nAvailable commands:`n"
$Tasks.GetEnumerator() | ForEach-Object {
# Left-pad names to 16 chars for alignment
$name = $_.Key.PadRight(16)
Write-Host (" {0} {1}" -f $name, $_.Value.Description)
}
}
}
'list' = @{
Description = 'Alias for help';
Action = { & $Tasks['help'].Action.Invoke() }
}
}
# -------- Dispatch --------
try {
$key = $Task.ToLower()
if (-not $Tasks.Contains($key)) {
throw "Unknown task '$Task'. Run '.\Makefile.ps1 help' to list tasks."
}
& $Tasks[$key].Action.Invoke()
}
catch {
Write-Error $_.Exception.Message
exit 1
}