-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcyginstall.ps1
More file actions
267 lines (233 loc) · 11.5 KB
/
cyginstall.ps1
File metadata and controls
267 lines (233 loc) · 11.5 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
# Cygwin Quick Setup Script
# Enhanced version with fixed Windows Terminal profile
param(
[string]$Mirror = "https://mirrors.kernel.org/sourceware/cygwin/",
[string]$InstallDir = "C:\cygwin64",
[string]$SetupDir = "C:\Apps\cygwin",
[string[]]$Packages = @("bind-utils", "curl", "fzf", "fzf-bash", "fzf-bash-completion", "git", "inetutils", "make", "nano", "rsync", "tmux", "wget")
)
# Script configuration
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
Write-Host "🐧 Cygwin Quick Setup" -ForegroundColor Cyan
Write-Host "📦 Installing Cygwin with $($Packages.Count) essential development packages" -ForegroundColor Green
Write-Host ""
# Check if running as administrator
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if ($isAdmin) {
Write-Host "🔧 🔑 Running with admin privileges - system-wide installation" -ForegroundColor Yellow
$adminFlag = ""
} else {
Write-Host "🔧 👤 Running without admin privileges - user-only installation" -ForegroundColor Yellow
$adminFlag = "--no-admin"
}
# Create directories
Write-Host "🔧 Creating directory structure..." -ForegroundColor Blue
try {
if (!(Test-Path $SetupDir)) {
New-Item -ItemType Directory -Path $SetupDir -Force | Out-Null
Write-Host "✅ Created directory: $SetupDir" -ForegroundColor Green
}
$packagesDir = Join-Path $SetupDir "packages"
if (!(Test-Path $packagesDir)) {
New-Item -ItemType Directory -Path $packagesDir -Force | Out-Null
}
} catch {
Write-Host "❌ Failed to create directories: $_" -ForegroundColor Red
exit 1
}
# Download Cygwin installer
$setupExe = Join-Path $SetupDir "setup-x86_64.exe"
if (!(Test-Path $setupExe)) {
Write-Host "🔧 Downloading Cygwin installer..." -ForegroundColor Blue
try {
$setupUrl = "https://www.cygwin.com/setup-x86_64.exe"
Write-Host " 🌐 Downloading from: $setupUrl" -ForegroundColor Gray
Invoke-WebRequest -Uri $setupUrl -OutFile $setupExe -UseBasicParsing
Write-Host "✅ Downloaded Cygwin installer to: $setupExe" -ForegroundColor Green
} catch {
Write-Host "❌ Failed to download Cygwin installer: $_" -ForegroundColor Red
exit 1
}
} else {
Write-Host "✅ Cygwin installer already exists: $setupExe" -ForegroundColor Green
}
# Create Start Menu shortcut
Write-Host "🔧 Creating Start Menu shortcut..." -ForegroundColor Blue
try {
$startMenuPath = [System.IO.Path]::Combine($env:APPDATA, "Microsoft", "Windows", "Start Menu", "Programs")
$shortcutPath = Join-Path $startMenuPath "Cygwin Setup.lnk"
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = $setupExe
$shortcut.Arguments = "$adminFlag -M -d"
$shortcut.WorkingDirectory = $SetupDir
$shortcut.Description = "Cygwin Package Manager"
$shortcut.Save()
Write-Host "✅ Created Start Menu shortcut: $shortcutPath" -ForegroundColor Green
Write-Host " 🎯 Target: $setupExe $adminFlag -M -d" -ForegroundColor Gray
Write-Host " 📁 Start in: $SetupDir" -ForegroundColor Gray
} catch {
Write-Host "⚠️ Could not create Start Menu shortcut: $_" -ForegroundColor Yellow
}
# Install Cygwin
Write-Host "🔧 Installing Cygwin with essential packages..." -ForegroundColor Blue
Write-Host " 📁 Root directory: $InstallDir" -ForegroundColor Gray
Write-Host " 🪞 Mirror: $Mirror" -ForegroundColor Gray
Write-Host " 📦 Packages: $($Packages -join ', ')" -ForegroundColor Gray
Write-Host ""
try {
$packageList = $Packages -join ","
$arguments = @(
$adminFlag
"--quiet-mode"
"--root", $InstallDir
"--local-package-dir", $packagesDir
"--site", $Mirror
"--packages", $packageList
) | Where-Object { $_ -ne "" }
Write-Host "🔄 Running Cygwin installer (this may take several minutes)..." -ForegroundColor Blue
$process = Start-Process -FilePath $setupExe -ArgumentList $arguments -Wait -PassThru -NoNewWindow
if ($process.ExitCode -eq 0) {
Write-Host "✅ Cygwin installation completed successfully" -ForegroundColor Green
} else {
Write-Host "⚠️ Cygwin installer finished with exit code: $($process.ExitCode)" -ForegroundColor Yellow
}
} catch {
Write-Host "❌ Failed to run Cygwin installer: $_" -ForegroundColor Red
exit 1
}
# Verify installation
Write-Host "🔧 Verifying installation..." -ForegroundColor Blue
$cygwinBin = Join-Path $InstallDir "bin"
if (Test-Path $cygwinBin) {
Write-Host "✅ Cygwin installation verified" -ForegroundColor Green
# Check for key executables
$keyTools = @("git.exe", "curl.exe", "nano.exe", "tmux.exe")
$foundTools = @()
foreach ($tool in $keyTools) {
$toolPath = Join-Path $cygwinBin $tool
if (Test-Path $toolPath) {
$foundTools += $tool.Replace(".exe", "")
}
}
if ($foundTools.Count -gt 0) {
Write-Host "✅ Verified packages: $($foundTools -join ', ')" -ForegroundColor Green
}
} else {
Write-Host "❌ Cygwin installation verification failed" -ForegroundColor Red
exit 1
}
# Create home directory if it doesn't exist
$homeDir = Join-Path $InstallDir "home\$env:USERNAME"
if (!(Test-Path $homeDir)) {
Write-Host "🔧 Creating user home directory..." -ForegroundColor Blue
try {
New-Item -ItemType Directory -Path $homeDir -Force | Out-Null
Write-Host "✅ Created home directory: $homeDir" -ForegroundColor Green
} catch {
Write-Host "⚠️ Could not create home directory: $_" -ForegroundColor Yellow
Write-Host " 💡 Home directory will be created on first Cygwin launch" -ForegroundColor Gray
}
}
# Add to PATH
Write-Host "🔧 Adding Cygwin to user PATH..." -ForegroundColor Blue
try {
$currentPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
if ($currentPath -notlike "*$cygwinBin*") {
$newPath = if ($currentPath) { "$currentPath;$cygwinBin" } else { $cygwinBin }
[System.Environment]::SetEnvironmentVariable("Path", $newPath, "User")
Write-Host "✅ Added $cygwinBin to user PATH (lowest priority)" -ForegroundColor Green
Write-Host " 💡 Note: Restart terminal to use updated PATH" -ForegroundColor Gray
} else {
Write-Host "✅ Cygwin already in PATH" -ForegroundColor Green
}
} catch {
Write-Host "⚠️ Could not update PATH: $_" -ForegroundColor Yellow
}
# Add Windows Terminal profile
Write-Host "🔧 Adding Windows Terminal profile..." -ForegroundColor Blue
try {
$terminalSettingsPath = Join-Path $env:LOCALAPPDATA "Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
if (Test-Path $terminalSettingsPath) {
# Create backup
$backupPath = "$terminalSettingsPath.backup.$(Get-Date -Format 'yyyyMMdd-HHmmss')"
Copy-Item $terminalSettingsPath $backupPath
Write-Host " 💾 Created backup: $backupPath" -ForegroundColor Gray
# Read current settings
$settings = Get-Content $terminalSettingsPath -Raw | ConvertFrom-Json
# Generate new GUID
$newGuid = [System.Guid]::NewGuid().ToString()
# Determine starting directory (use home if exists, otherwise root)
$startingDir = if (Test-Path $homeDir) { $homeDir } else { $InstallDir }
# Create new profile
$newProfile = @{
guid = "{$newGuid}"
name = "Cygwin"
commandline = "$cygwinBin\bash.exe --login -i"
startingDirectory = $startingDir.Replace('\', '\\')
icon = "$InstallDir\Cygwin-Terminal.ico".Replace('\', '\\')
hidden = $false
}
# Check if Cygwin profile already exists
$existingProfile = $settings.profiles.list | Where-Object { $_.name -eq "Cygwin" }
if ($existingProfile) {
# Update existing profile
$existingProfile.commandline = $newProfile.commandline
$existingProfile.startingDirectory = $newProfile.startingDirectory
$existingProfile.icon = $newProfile.icon
Write-Host "✅ Updated existing Cygwin profile in Windows Terminal" -ForegroundColor Green
} else {
# Add new profile
$settings.profiles.list += $newProfile
Write-Host "✅ Added Cygwin profile to Windows Terminal" -ForegroundColor Green
}
# Save settings
$settings | ConvertTo-Json -Depth 10 | Set-Content $terminalSettingsPath -Encoding UTF8
Write-Host " 🆔 GUID: {$newGuid}" -ForegroundColor Gray
Write-Host " 📁 Starting directory: $startingDir" -ForegroundColor Gray
Write-Host " 🎨 Icon: $InstallDir\Cygwin-Terminal.ico" -ForegroundColor Gray
} else {
Write-Host "⚠️ Windows Terminal not found - profile not created" -ForegroundColor Yellow
}
} catch {
Write-Host "⚠️ Could not add Windows Terminal profile: $_" -ForegroundColor Yellow
Write-Host " 💡 You can add manually using the settings provided below" -ForegroundColor Gray
}
# Summary
Write-Host ""
Write-Host "📊 Installation Summary:" -ForegroundColor Cyan
Write-Host " 📁 Installation Directory: $InstallDir" -ForegroundColor White
Write-Host " 🏠 User Home Directory: $homeDir" -ForegroundColor White
$shortcutPath = Join-Path ([System.IO.Path]::Combine($env:APPDATA, "Microsoft", "Windows", "Start Menu", "Programs")) "Cygwin Setup.lnk"
Write-Host " 🔧 Start Menu Shortcut: $shortcutPath" -ForegroundColor White
Write-Host " 🛣️ Added to PATH: $cygwinBin (lowest priority)" -ForegroundColor White
Write-Host " 🪞 Mirror: $Mirror" -ForegroundColor White
Write-Host " 📦 Packages: $($Packages.Count) essential development tools" -ForegroundColor White
Write-Host ""
Write-Host "🎉 Setup Complete!" -ForegroundColor Green
Write-Host ""
Write-Host "📝 Next Steps:" -ForegroundColor Cyan
Write-Host " 1. Restart terminal/PowerShell to use updated PATH" -ForegroundColor White
Write-Host " 2. Launch Cygwin via Windows Terminal or: $InstallDir\bin\mintty.exe" -ForegroundColor White
Write-Host " 3. Add more packages via Start Menu 'Cygwin Setup' shortcut" -ForegroundColor White
Write-Host ""
Write-Host "💡 Quick Commands:" -ForegroundColor Cyan
Write-Host " • Open Cygwin Terminal: $InstallDir\bin\mintty.exe" -ForegroundColor White
Write-Host " • Run Cygwin Bash: $InstallDir\bin\bash.exe --login -i" -ForegroundColor White
Write-Host " • Windows Terminal: Look for 'Cygwin' profile" -ForegroundColor White
Write-Host " • Package Manager: Start Menu → 'Cygwin Setup'" -ForegroundColor White
Write-Host ""
# Manual profile instructions if automatic creation failed
if (!(Test-Path $terminalSettingsPath)) {
Write-Host "🔧 Manual Windows Terminal Profile Setup:" -ForegroundColor Yellow
Write-Host " Name: Cygwin" -ForegroundColor White
Write-Host " Command: $cygwinBin\bash.exe --login -i" -ForegroundColor White
$startingDirForManual = if (Test-Path $homeDir) { $homeDir } else { $InstallDir }
Write-Host " Starting Directory: $startingDirForManual" -ForegroundColor White
Write-Host " Icon: $InstallDir\Cygwin-Terminal.ico" -ForegroundColor White
Write-Host ""
}
Write-Host "🔧 Recovery Info:" -ForegroundColor Cyan
Write-Host " • Settings backup created in case of issues" -ForegroundColor White
Write-Host " • Look for .backup files in LocalState folder if needed" -ForegroundColor White