-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.ps1
More file actions
377 lines (320 loc) · 11.9 KB
/
install.ps1
File metadata and controls
377 lines (320 loc) · 11.9 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# Git Commit AI Installation Script for Windows
# Run in PowerShell as Administrator for system-wide installation
# Or run as regular user for user installation
param(
[switch]$System,
[switch]$Uninstall,
[switch]$Help
)
$ErrorActionPreference = "Stop"
# Colors for output
function Write-Color {
param(
[string]$Text,
[string]$Color = "White"
)
Write-Host $Text -ForegroundColor $Color
}
# Show help
if ($Help) {
Write-Color "Git Commit AI Installer for Windows" "Cyan"
Write-Host ""
Write-Host "Usage: .\install.ps1 [OPTIONS]"
Write-Host ""
Write-Host "Options:"
Write-Host " -System Install system-wide (requires Administrator)"
Write-Host " -Uninstall Remove git-commitai"
Write-Host " -Help Show this help message"
Write-Host ""
Write-Host "Examples:"
Write-Host " .\install.ps1 # Install for current user"
Write-Host " .\install.ps1 -System # Install system-wide"
Write-Host " .\install.ps1 -Uninstall # Remove installation"
exit 0
}
# Check if running as administrator
function Test-Administrator {
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
# Check prerequisites
function Test-Prerequisites {
$missing = @()
# Check for Python
try {
$pythonVersion = python --version 2>&1
if ($pythonVersion -notmatch "Python 3\.[8-9]|Python 3\.1[0-2]") {
$missing += "Python 3.8 or higher"
}
} catch {
$missing += "Python 3.8 or higher"
}
# Check for Git
try {
git --version | Out-Null
} catch {
$missing += "Git"
}
if ($missing.Count -gt 0) {
Write-Color "Error: Missing required dependencies:" "Red"
foreach ($dep in $missing) {
Write-Host " - $dep"
}
Write-Host ""
Write-Color "Please install:" "Yellow"
Write-Host " Python: https://www.python.org/downloads/"
Write-Host " Git: https://git-scm.com/download/win"
exit 1
}
}
# Download file
function Download-File {
param(
[string]$Url,
[string]$Output
)
try {
Invoke-WebRequest -Uri $Url -OutFile $Output -UseBasicParsing
} catch {
Write-Color "Error: Failed to download from $Url" "Red"
Write-Host $_.Exception.Message
exit 1
}
}
# Install for user
function Install-User {
Write-Color "Installing git-commitai for current user..." "Blue"
# Create directories
$userBin = "$env:LOCALAPPDATA\Programs\git-commitai"
$scriptsDir = "$env:APPDATA\Python\Scripts"
if (-not (Test-Path $userBin)) {
New-Item -ItemType Directory -Path $userBin -Force | Out-Null
}
if (-not (Test-Path $scriptsDir)) {
New-Item -ItemType Directory -Path $scriptsDir -Force | Out-Null
}
# Download Python script
Write-Color "Downloading git-commitai..." "Yellow"
$scriptUrl = "https://raw.githubusercontent.com/semperai/git-commitai/master/git_commitai.py"
$scriptPath = "$userBin\git_commitai.py"
Download-File -Url $scriptUrl -Output $scriptPath
# Create batch wrapper for command line
$batchContent = @"
@echo off
python "$scriptPath" %*
"@
$batchPath = "$scriptsDir\git-commitai.cmd"
Set-Content -Path $batchPath -Value $batchContent
# Create PowerShell wrapper
$ps1Content = @"
#!/usr/bin/env pwsh
python "$scriptPath" `$args
"@
$ps1Path = "$scriptsDir\git-commitai.ps1"
Set-Content -Path $ps1Path -Value $ps1Content
# Set up git alias
Write-Color "Setting up git alias..." "Yellow"
git config --global alias.commitai "!python `"$scriptPath`""
# Add to PATH if needed
$currentPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($currentPath -notlike "*$scriptsDir*") {
Write-Color "Adding to user PATH..." "Yellow"
$newPath = "$currentPath;$scriptsDir"
[Environment]::SetEnvironmentVariable("Path", $newPath, "User")
$env:Path = "$env:Path;$scriptsDir"
Write-Color "Added $scriptsDir to PATH" "Green"
Write-Color "Please restart your terminal for PATH changes to take effect" "Yellow"
}
Write-Color "✓ User installation complete!" "Green"
}
# Install system-wide
function Install-System {
if (-not (Test-Administrator)) {
Write-Color "Error: System-wide installation requires Administrator privileges" "Red"
Write-Color "Please run PowerShell as Administrator and try again" "Yellow"
exit 1
}
Write-Color "Installing git-commitai system-wide..." "Blue"
# Create directories
$systemBin = "$env:ProgramFiles\git-commitai"
if (-not (Test-Path $systemBin)) {
New-Item -ItemType Directory -Path $systemBin -Force | Out-Null
}
# Download Python script
Write-Color "Downloading git-commitai..." "Yellow"
$scriptUrl = "https://raw.githubusercontent.com/semperai/git-commitai/master/git_commitai.py"
$scriptPath = "$systemBin\git_commitai.py"
Download-File -Url $scriptUrl -Output $scriptPath
# Create batch wrapper
$batchContent = @"
@echo off
python "$scriptPath" %*
"@
$batchPath = "$systemBin\git-commitai.cmd"
Set-Content -Path $batchPath -Value $batchContent
# Set up git alias (global)
Write-Color "Setting up git alias..." "Yellow"
git config --system alias.commitai "!python `"$scriptPath`"" 2>$null
# Add to system PATH
$currentPath = [Environment]::GetEnvironmentVariable("Path", "Machine")
if ($currentPath -notlike "*$systemBin*") {
Write-Color "Adding to system PATH..." "Yellow"
$newPath = "$currentPath;$systemBin"
[Environment]::SetEnvironmentVariable("Path", $newPath, "Machine")
$env:Path = "$env:Path;$systemBin"
Write-Color "Added $systemBin to PATH" "Green"
}
Write-Color "✓ System installation complete!" "Green"
}
# Configure environment
function Set-Environment {
Write-Color "`nConfiguring environment variables..." "Blue"
# Check if already configured
if ($env:GIT_COMMIT_AI_KEY) {
Write-Color "✓ GIT_COMMIT_AI_KEY is already set" "Green"
return
}
Write-Color "You need to set up your API credentials:" "Yellow"
Write-Host ""
Write-Host "Select your AI provider:"
Write-Host "1) OpenRouter (Recommended - Access to many models)"
Write-Host "2) OpenAI"
Write-Host "3) Anthropic Claude"
Write-Host "4) Local LLM (Ollama)"
Write-Host "5) Skip configuration"
$choice = Read-Host "Enter choice [1-5]"
switch ($choice) {
"1" {
$apiUrl = "https://openrouter.ai/api/v1/chat/completions"
$model = "anthropic/claude-3.5-sonnet"
Write-Color "Get your API key from: https://openrouter.ai/keys" "Yellow"
$apiKey = Read-Host "Enter your API key"
}
"2" {
$apiUrl = "https://api.openai.com/v1/chat/completions"
$model = "gpt-4o"
Write-Color "Get your API key from: https://platform.openai.com/api-keys" "Yellow"
$apiKey = Read-Host "Enter your API key"
}
"3" {
$apiUrl = "https://api.anthropic.com/v1/messages"
$model = "claude-3-opus-20240229"
Write-Color "Get your API key from: https://console.anthropic.com/settings/keys" "Yellow"
$apiKey = Read-Host "Enter your API key"
}
"4" {
$apiUrl = "http://localhost:11434/v1/chat/completions"
$model = "llama2"
$apiKey = "not-needed"
Write-Color "Make sure Ollama is running locally" "Yellow"
}
"5" {
Write-Color "Skipping configuration. You'll need to set environment variables manually." "Yellow"
return
}
default {
Write-Color "Invalid choice" "Red"
return
}
}
# Set environment variables
Write-Color "Setting environment variables..." "Yellow"
$scope = if ($System) { "Machine" } else { "User" }
[Environment]::SetEnvironmentVariable("GIT_COMMIT_AI_KEY", $apiKey, $scope)
[Environment]::SetEnvironmentVariable("GIT_COMMIT_AI_URL", $apiUrl, $scope)
[Environment]::SetEnvironmentVariable("GIT_COMMIT_AI_MODEL", $model, $scope)
# Also set for current session
$env:GIT_COMMIT_AI_KEY = $apiKey
$env:GIT_COMMIT_AI_URL = $apiUrl
$env:GIT_COMMIT_AI_MODEL = $model
Write-Color "✓ Configuration saved!" "Green"
}
# Test installation
function Test-Installation {
Write-Color "`nTesting installation..." "Blue"
# Test git-commitai command
try {
$cmd = Get-Command git-commitai -ErrorAction Stop
& $cmd.Path --version 2>&1 | Out-Null
Write-Color "✓ git-commitai command found" "Green"
} catch {
Write-Color "⚠ git-commitai not accessible yet (restart terminal)" "Yellow"
}
# Test git alias
$gitAlias = git config --get alias.commitai
if ($gitAlias) {
Write-Color "✓ git commitai alias configured" "Green"
} else {
Write-Color "⚠ git commitai alias not set" "Yellow"
}
# Test API configuration
if ($env:GIT_COMMIT_AI_KEY) {
Write-Color "✓ API key configured" "Green"
} else {
Write-Color "⚠ API key not configured" "Yellow"
}
}
# Uninstall
function Uninstall-GitCommitAI {
Write-Color "Uninstalling git-commitai..." "Blue"
# Remove user installation
$userBin = "$env:LOCALAPPDATA\Programs\git-commitai"
$scriptsDir = "$env:APPDATA\Python\Scripts"
if (Test-Path $userBin) {
Remove-Item -Path $userBin -Recurse -Force
Write-Color "✓ Removed user installation" "Green"
}
if (Test-Path "$scriptsDir\git-commitai.cmd") {
Remove-Item -Path "$scriptsDir\git-commitai.cmd" -Force
Remove-Item -Path "$scriptsDir\git-commitai.ps1" -Force -ErrorAction SilentlyContinue
Write-Color "✓ Removed command wrappers" "Green"
}
# Remove system installation (if admin)
if (Test-Administrator) {
$systemBin = "$env:ProgramFiles\git-commitai"
if (Test-Path $systemBin) {
Remove-Item -Path $systemBin -Recurse -Force
Write-Color "✓ Removed system installation" "Green"
}
}
# Remove git alias
git config --global --unset alias.commitai 2>$null
Write-Color "✓ Removed git alias" "Green"
Write-Color "Note: Environment variables were not removed" "Yellow"
Write-Color "Uninstallation complete!" "Green"
}
# Main execution
Write-Color "╔════════════════════════════════════╗" "Blue"
Write-Color "║ Git Commit AI Installer ║" "Blue"
Write-Color "║ for Windows ║" "Blue"
Write-Color "╚════════════════════════════════════╝" "Blue"
Write-Host ""
# Check prerequisites
Test-Prerequisites
# Handle uninstall
if ($Uninstall) {
Uninstall-GitCommitAI
exit 0
}
# Run installation
if ($System) {
Install-System
} else {
Install-User
}
# Configure environment
Set-Environment
# Test installation
Test-Installation
Write-Color "`n╔════════════════════════════════════╗" "Green"
Write-Color "║ Installation Complete! 🎉 ║" "Green"
Write-Color "╚════════════════════════════════════╝" "Green"
Write-Host ""
Write-Host "Quick start:"
Write-Host " 1. Restart your terminal or command prompt"
Write-Host " 2. Stage some changes: git add ."
Write-Host " 3. Generate commit: git commitai"
Write-Host " 4. View help: git commitai --help"
Write-Host ""
Write-Color "For more information: https://github.com/semperai/git-commitai" "Blue"