-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
134 lines (108 loc) · 3.14 KB
/
install.ps1
File metadata and controls
134 lines (108 loc) · 3.14 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
# install.ps1 - install templit on Windows
# Run from the repo root:
# Unblock-File -Path .\install.ps1
# .\install.ps1
#Requires -Version 5.1
[CmdletBinding()]
param()
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
# helpers
function Write-Ok {
param([string]$Msg)
Write-Host " $([char]0x2713) $Msg" -ForegroundColor Green
}
function Write-Err {
param([string]$Msg)
Write-Host " $([char]0x2717) $Msg" -ForegroundColor Red
exit 1
}
function Write-Info {
param([string]$Msg)
Write-Host " . $Msg" -ForegroundColor Cyan
}
function Write-Warn {
param([string]$Msg)
Write-Host " ! $Msg" -ForegroundColor Yellow
}
function Write-Banner {
Write-Host ""
Write-Host " templit installer" -ForegroundColor Cyan
Write-Host " ------------------------------" -ForegroundColor DarkGray
Write-Host ""
}
# config
$InstallDir = Join-Path $env:USERPROFILE ".templit"
$ScriptDir = $PSScriptRoot
# checks
Write-Banner
Write-Info "Checking requirements..."
$PythonCmd = $null
foreach ($candidate in @("python", "python3", "py")) {
try {
$ver = & $candidate --version 2>&1
if ($ver -match "Python (\d+)\.(\d+)") {
$major = [int]$Matches[1]
$minor = [int]$Matches[2]
if ($major -ge 3 -and $minor -ge 9) {
$PythonCmd = $candidate
Write-Ok "Python $major.$minor ($candidate)"
break
}
}
}
catch {
# candidate not found, try next
}
}
if (-not $PythonCmd) {
Write-Err "Python 3.9+ not found. Download it from https://python.org"
}
# virtualenv
Write-Info "Creating virtualenv at $InstallDir ..."
if (Test-Path $InstallDir) {
Write-Warn "Directory $InstallDir already exists - reinstalling into it."
}
& $PythonCmd -m venv $InstallDir
if ($LASTEXITCODE -ne 0) {
Write-Err "Failed to create virtualenv."
}
Write-Ok "Virtualenv ready"
# install package
$Pip = Join-Path $InstallDir "Scripts\pip.exe"
Write-Info "Upgrading pip..."
& $Pip install --quiet --upgrade pip
if ($LASTEXITCODE -ne 0) {
Write-Err "pip upgrade failed."
}
Write-Info "Installing templit..."
& $Pip install --quiet -e $ScriptDir
if ($LASTEXITCODE -ne 0) {
Write-Err "templit installation failed."
}
Write-Ok "Package installed"
# add to PATH
$BinDir = Join-Path $InstallDir "Scripts"
$UserScope = [System.EnvironmentVariableTarget]::User
$CurrentPath = [System.Environment]::GetEnvironmentVariable("PATH", $UserScope)
if ($CurrentPath -notlike "*$BinDir*") {
Write-Info "Adding $BinDir to user PATH..."
[System.Environment]::SetEnvironmentVariable(
"PATH",
"$BinDir;$CurrentPath",
$UserScope
)
Write-Ok "Added to user PATH"
Write-Warn "Open a new terminal for the PATH change to take effect."
}
else {
Write-Ok "$BinDir is already in user PATH"
}
$env:PATH = "$BinDir;$env:PATH"
# done
Write-Host ""
Write-Host " All done! " -ForegroundColor Green -NoNewline
Write-Host "Run: " -NoNewline
Write-Host "templit list" -ForegroundColor White
Write-Host " (or open a new terminal if the command isn't found yet)" -ForegroundColor DarkGray
Write-Host ""