-
Notifications
You must be signed in to change notification settings - Fork 22
/
pwsh_helper.ps1
207 lines (189 loc) Β· 8.44 KB
/
pwsh_helper.ps1
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
Write-Host "β
Helper script invoked successfully" -ForegroundColor Green
# Tasks to be executed in the background.
function BackgroundTasks {
Update-PowerShell
# Update the local cache of files
CheckScriptFilesForUpdates
Write-Host "π Updated the local cache of files." -ForegroundColor Green
}
# Function for downloading a file
function DownloadFile($filename) {
$primaryUrl = "$githubBaseURL/$filename"
$fallbackUrl = "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/$filename"
try {
# Attempt to download from the primary URL
Invoke-WebRequest -Uri $primaryUrl -OutFile "$baseDir\$filename" -ErrorAction Stop
} catch {
Write-Host "β Failed to download $filename from $primaryUrl. Trying fallback URL..." -ForegroundColor Yellow
try {
# Attempt to download from the fallback URL if the primary URL fails
Invoke-WebRequest -Uri $fallbackUrl -OutFile "$baseDir\$filename" -ErrorAction Stop
} catch {
Write-Error "β Failed to download $filename from both primary and fallback URLs: $_"
}
}
}
# Function for checking and updating script files
function CheckAndUpdateFile($filename) {
$localFileContent = Get-Content "$baseDir\$filename" -Raw
$url = "$githubBaseURL/$filename"
$remoteFileContent = Invoke-WebRequest -Uri $url | Select-Object -ExpandProperty Content
if ($localFileContent -ne $remoteFileContent) {
DownloadFile "$filename"
}
}
function CheckScriptFilesForUpdates {
foreach ($file in $files) {
if (Test-Path "$baseDir\$file") {
CheckAndUpdateFile $file
} else {
DownloadFile $file
}
}
}
Function Test-CommandExists {
Param ($command)
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'
try { if (Get-Command $command) { RETURN $true } }
Catch { Write-Host "$command does not exist"; RETURN $false }
Finally { $ErrorActionPreference = $oldPreference }
}
function Install-NerdFont {
$zipPath = "$env:TEMP\$font_folder.zip"
$extractPath = "$env:TEMP\$font_folder"
$shell = New-Object -ComObject Shell.Application
$fonts = $shell.Namespace(0x14)
try {
# Download the Nerd Font zip file
Write-Host "Downloading $font Nerd Font..." -ForegroundColor Green
Invoke-WebRequest -Uri $font_url -OutFile $zipPath
# Create the directory to extract the files
if (-Not (Test-Path -Path $extractPath)) {
New-Item -ItemType Directory -Path $extractPath | Out-Null
}
# Extract the zip file
Expand-Archive -Path $zipPath -DestinationPath $extractPath -Force
# Find the specific font file
$fontFile = Get-ChildItem -Path $extractPath -Filter $fontFileName | Select-Object -First 1
if (-not $fontFile) {
throw "β Font file '$fontFileName' not found in the extracted files."
}
# Copy the font file to the Windows Fonts directory
Write-Host "Installing $font Nerd Font..." -ForegroundColor Green
$fonts.CopyHere($fontFile.FullName, 0x10)
Write-Host "$font Nerd Font installed successfully!" -ForegroundColor Green
Write-Host "π Make sure to set the font as default in your terminal settings." -ForegroundColor Blue
} catch {
Write-Host "β An error occurred: $_" -ForegroundColor Red
} finally {
# Clean up
Write-Host "Cleaning up temporary files..." -ForegroundColor Green
Remove-Item -Path $zipPath -Force
Remove-Item -Path $extractPath -Recurse -Force
}
}
function Test-ohmyposh {
if (Test-CommandExists oh-my-posh) {
Set-ConfigValue -Key "ohmyposh_installed" -Value "True"
} else {
Write-Host "β OhMyPosh is not installed." -ForegroundColor Red
$installOhMyPosh = Read-Host "Do you want to install Oh-My-Posh? (Y/N)"
if ($installOhMyPosh -eq 'Y' -or $installOhMyPosh -eq 'y') {
winget install JanDeDobbeleer.OhMyPosh --accept-package-agreements --accept-source-agreements
} else {
Write-Host "β Oh-My-Posh installation skipped." -ForegroundColor Yellow
}
}
}
function Test-$font {
$nerdfonts = Get-Font *$font*
if ($nerdfonts) {
Set-ConfigValue -Key "${font}_installed" -Value "True"
} else {
Write-Host "β No Nerd-Fonts are installed." -ForegroundColor Red
$installNerdFonts = Read-Host "Do you want to install $font NerdFont? (Y/N)"
if ($installNerdFonts -eq 'Y' -or $installNerdFonts -eq 'y') {
Install-NerdFont
} else {
Write-Host "β NerdFonts installation skipped." -ForegroundColor Yellow
Set-ConfigValue -Key "$font_installed" -Value "False"
}
}
}
function Update-PowerShell {
if (-not $global:canConnectToGitHub) {
Write-Host "β Skipping PowerShell update or installation check due to GitHub.com not responding within 1 second." -ForegroundColor Yellow
return
}
try {
$isInstalled = $null -ne (Get-Command pwsh -ErrorAction SilentlyContinue)
$updateNeeded = $false
if ($isInstalled) {
$currentVersion = $PSVersionTable.PSVersion.ToString()
} else {
$currentVersion = "0.0"
}
$gitHubApiUrl = "https://api.github.com/repos/PowerShell/PowerShell/releases/latest"
$latestReleaseInfo = Invoke-RestMethod -Uri $gitHubApiUrl
$latestVersion = $latestReleaseInfo.tag_name.Trim('v')
if ($currentVersion -lt $latestVersion) {
$updateNeeded = $true
}
if ($updateNeeded -and $isInstalled) {
Write-Host "Updating PowerShell..." -ForegroundColor Yellow
winget upgrade "Microsoft.PowerShell" --accept-source-agreements --accept-package-agreements
} elseif ($updateNeeded -and -not $isInstalled) {
Write-Host "Installing PowerShell..." -ForegroundColor Yellow
winget install "Microsoft.PowerShell" --accept-source-agreements --accept-package-agreements
} else {
Write-Host "β
PowerShell is up to date." -ForegroundColor Green
}
} catch {
Write-Error "β Failed to update or install PowerShell. Error: $_"
}
}
# Function to show a GUI Message Box
# Source: https://stackoverflow.com/questions/58718191/is-there-a-way-to-display-a-pop-up-message-box-in-powershell-that-is-compatible
function Show-MessageBox {
[CmdletBinding(PositionalBinding=$false)]
param(
[Parameter(Mandatory, Position=0)]
[string] $Message,
[Parameter(Position=1)]
[string] $Title,
[Parameter(Position=2)]
[ValidateSet('OK', 'OKCancel', 'AbortRetryIgnore', 'YesNoCancel', 'YesNo', 'RetryCancel')]
[string] $Buttons = 'OK',
[ValidateSet('Information', 'Warning', 'Stop')]
[string] $Icon = 'Information',
[ValidateSet(0, 1, 2)]
[int] $DefaultButtonIndex
)
$buttonMap = @{
'OK' = @{ buttonList = 'OK'; defaultButtonIndex = 0 }
'OKCancel' = @{ buttonList = 'OK', 'Cancel'; defaultButtonIndex = 0; cancelButtonIndex = 1 }
'AbortRetryIgnore' = @{ buttonList = 'Abort', 'Retry', 'Ignore'; defaultButtonIndex = 2; cancelButtonIndex = 0 }
'YesNoCancel' = @{ buttonList = 'Yes', 'No', 'Cancel'; defaultButtonIndex = 2; cancelButtonIndex = 2 }
'YesNo' = @{ buttonList = 'Yes', 'No'; defaultButtonIndex = 0; cancelButtonIndex = 1 }
'RetryCancel' = @{ buttonList = 'Retry', 'Cancel'; defaultButtonIndex = 0; cancelButtonIndex = 1 }
}
$numButtons = $buttonMap[$Buttons].buttonList.Count
$defaultIndex = [math]::Min($numButtons - 1, ($buttonMap[$Buttons].defaultButtonIndex, $DefaultButtonIndex)[$PSBoundParameters.ContainsKey('DefaultButtonIndex')])
$cancelIndex = $buttonMap[$Buttons].cancelButtonIndex
Add-Type -AssemblyName System.Windows.Forms
# Create a hidden form and set it as TopMost
$form = New-Object System.Windows.Forms.Form
$form.TopMost = $true
$form.ShowInTaskbar = $false
$form.WindowState = 'Minimized'
$form.Show()
$form.Hide()
# Show the message box with the hidden form as the owner
$result = [System.Windows.Forms.MessageBox]::Show($form, $Message, $Title, $Buttons, $Icon, $defaultIndex * 256).ToString()
# Close the hidden form
$form.Close()
$form.Dispose()
# Output the result
return $result
}