-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathstart.ps1
More file actions
executable file
·236 lines (218 loc) · 7.05 KB
/
start.ps1
File metadata and controls
executable file
·236 lines (218 loc) · 7.05 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
#!/usr/bin/env pwsh
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# Set UTF-8 encoding for console output to properly display Unicode characters
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
# Enable ANSI escape sequence support in PowerShell 7+
$PSVersionTable.PSVersion.Major -ge 7 | Out-Null
if ($PSVersionTable.PSVersion.Major -ge 7) {
$env:TERM = "xterm-256color"
}
$ScriptDir = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
$RepoRoot = $ScriptDir
function Get-Python {
$venvWin = Join-Path $RepoRoot ".venv/Scripts/python.exe"
$venvNix = Join-Path $RepoRoot ".venv/bin/python"
if (Test-Path $venvWin) { return $venvWin }
if (Test-Path $venvNix) { return $venvNix }
if (Get-Command python3 -ErrorAction SilentlyContinue) { return "python3" }
return "python"
}
function Invoke-Cmd {
$flatArgs = @()
foreach ($a in $args) {
if ($a -is [System.Collections.IEnumerable] -and -not ($a -is [string])) {
$flatArgs += $a
} else {
$flatArgs += $a
}
}
Write-Host "`n>>> $($flatArgs -join ' ')`n" -ForegroundColor Cyan
Push-Location $RepoRoot
try {
if ($flatArgs.Count -eq 0) {
throw "No command specified"
}
$exe = $flatArgs[0]
$cmdArgs = @()
if ($flatArgs.Count -gt 1) {
$cmdArgs = @($flatArgs[1..($flatArgs.Count - 1)])
}
$global:LASTEXITCODE = 0
# Send command stdout directly to the host so menu callers can ignore the
# boolean return value without swallowing the child command output.
& $exe @cmdArgs | Out-Host
$commandSucceeded = $?
$exitCode = if ($commandSucceeded) { $LASTEXITCODE } else { [Math]::Max($LASTEXITCODE, 1) }
}
catch {
Write-Host ""
Write-Host "Command failed: $_" -ForegroundColor Red
Write-Host ""
return $false
}
finally {
Pop-Location
}
if ($exitCode -ne 0) {
Write-Host ""
Write-Host "Command exited with code $exitCode" -ForegroundColor Yellow
Write-Host ""
return $false
}
return $true
}
function Show-AccountInfo {
$python = Get-Python
$code = @"
from pathlib import Path
import json
import sys
import os
root = Path(os.getcwd())
shared = root / "shared" / "python"
if str(shared) not in sys.path:
sys.path.insert(0, str(shared))
try:
import azure_resources as az
info = az.get_account_info()
except Exception as exc: # noqa: BLE001
print(f"Failed to read Azure account info: {exc}")
"@
Push-Location $RepoRoot
try {
& $python -c $code
}
finally {
Pop-Location
}
}
function Test-Uv {
return [bool](Get-Command uv -ErrorAction SilentlyContinue)
}
function PyRun {
param(
[Parameter(ValueFromRemainingArguments=$true)]
[string[]] $Args
)
if (Test-Uv) {
Invoke-Cmd "uv" (@("run", "python") + $Args)
} else {
Invoke-Cmd (Get-Python) $Args
}
}
while ($true) {
Write-Host ""
Write-Host "APIM Samples Developer CLI" -ForegroundColor Cyan
Write-Host "==========================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Setup" -ForegroundColor Yellow
Write-Host " 1) Complete environment setup"
Write-Host " 2) Azure CLI login"
Write-Host " u) Update & sync uv dependencies (refresh uv.lock)"
Write-Host ""
Write-Host "Verify" -ForegroundColor Yellow
Write-Host " 3) Verify local setup"
Write-Host " 4) Show Azure account info"
Write-Host " 5) Show soft-deleted resources"
Write-Host " 6) Show all deployed infrastructures"
Write-Host ""
Write-Host "Tests" -ForegroundColor Yellow
Write-Host " 7) Run python ruff"
Write-Host " 8) Run python tests (shows detailed test results)"
Write-Host " 9) Run Bicep lint"
Write-Host " 10) Run full checks (most statistics)"
Write-Host ""
Write-Host "Presentation" -ForegroundColor Yellow
Write-Host " p) Serve & view presentation (auto-opens browser)"
Write-Host " e) Export presentation as self-contained HTML"
Write-Host " w) Serve & view GitHub Pages website (auto-opens browser)"
Write-Host ""
Write-Host "Cleanup" -ForegroundColor Yellow
Write-Host " c) Clean local artifacts (preserves .env)"
Write-Host ""
Write-Host "Misc" -ForegroundColor Yellow
Write-Host " 0) Exit"
Write-Host ""
$choice = Read-Host "Select an option"
switch ($choice) {
'1' {
$null = PyRun "$RepoRoot/setup/local_setup.py" "--complete-setup"
}
'2' {
Write-Host ""
$useTenantId = Read-Host "Do you want to specify a tenant ID? (y/n)"
if ($useTenantId -eq 'y' -or $useTenantId -eq 'Y') {
$tenantId = Read-Host "Enter tenant ID"
if ($tenantId) {
$cmd = "az login --tenant $tenantId"
Write-Host "`n>>> $cmd`n" -ForegroundColor Cyan
& az login --tenant $tenantId
} else {
Write-Host "Tenant ID is required." -ForegroundColor Red
}
} else {
$cmd = "az login"
Write-Host "`n>>> $cmd`n" -ForegroundColor Cyan
& az login
}
}
'3' {
$null = PyRun "$RepoRoot/setup/verify_local_setup.py"
}
'4' {
Show-AccountInfo
}
'5' {
$null = PyRun "$RepoRoot/shared/python/show_soft_deleted_resources.py"
}
'6' {
$null = PyRun "$RepoRoot/shared/python/show_infrastructures.py"
}
'7' {
$null = Invoke-Cmd "$RepoRoot/tests/python/run_ruff.ps1"
}
'8' {
$null = Invoke-Cmd "$RepoRoot/tests/python/run_tests.ps1"
}
'9' {
$null = Invoke-Cmd "$RepoRoot/tests/bicep/run_bicep_lint.ps1"
}
'10' {
$null = Invoke-Cmd "$RepoRoot/tests/python/check_python.ps1"
}
'p' {
$null = PyRun "$RepoRoot/setup/serve_presentation.py"
}
'e' {
$null = PyRun "$RepoRoot/setup/export_presentation.py"
}
'w' {
$null = PyRun "$RepoRoot/setup/serve_website.py"
}
'c' {
$null = Invoke-Cmd "$RepoRoot/setup/clean-local-artifacts.ps1"
}
'u' {
if (Test-Uv) {
$lockOk = Invoke-Cmd "uv" @("lock", "--upgrade")
if ($lockOk) {
$null = Invoke-Cmd "uv" @("sync")
}
} else {
Write-Host ""
Write-Host "uv is not installed or not on PATH. Install uv first (see setup/README.md)." -ForegroundColor Red
Write-Host ""
}
}
'0' {
Write-Host ""
Write-Host "Goodbye!" -ForegroundColor Green
Write-Host ""
exit 0
}
Default {
Write-Host "Invalid option. Please try again." -ForegroundColor Red
}
}
}