-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-neovim-win.ps1
257 lines (219 loc) · 7.61 KB
/
install-neovim-win.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
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
Param(
[switch]$Debug,
[switch]$DryRun
)
## Set path script was launched from as a variable
# Use Set-Location to change to other paths, then
# Set-Location $CWD to return to the originating path.
$CWD = $PWD.Path
## Path to neovim configuration
$NVIM_CONFIG_SRC = "$($CWD)\config\nvim"
## Path where neovim configuration will be symlinked to
# $NVIM_CONFIG_DIR = "$($env:USERPROFILE)\.config\nvim"
$NVIM_CONFIG_DIR = "$($env:LOCALAPPDATA)\nvim"
If ( $Debug ) {
## enable powershell logging
$DebugPreference = "Continue"
}
If ( $DryRun ) {
Write-Host "-DryRun enabled. Actions will be described, instead of taken. Messages will appear in purple where a live action would be taken." -ForegroundColor Magenta
}
Write-Debug "CWD: $($CWD)"
## Packages to install with scoop that are required for neovim
$NeovimDependencies = @(
"win32yank",
"nodejs-lts",
"fzf",
"tree-sitter",
"ripgrep",
"FiraCode-NF-Mono",
"cmake"
"gcc"
)
function Test-IsAdmin {
## Check if the current process is running with elevated privileges (admin rights)
$isAdmin = [Security.Principal.WindowsPrincipal]::new([Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
return $isAdmin
}
function Run-AsAdmin {
param (
[string]$Command
)
# Check if the script is running as admin
$isAdmin = [bool](New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
# Prompt to run as administrator if not already running as admin
$arguments = "-Command `"& {$command}`""
Write-Debug "Running command: Start-Process powershell -ArgumentList $($arguments) -Verb RunAs"
try {
Start-Process powershell -ArgumentList $arguments -Verb RunAs
return $true # Indicate that the script was elevated and the command will run
}
catch {
Write-Error "Error executing command as admin. Details: $($_.Exception.Message)"
}
}
else {
# If already running as admin, execute the command
Invoke-Expression $command
return $false # Indicate that the command was run without elevation
}
}
function Install-ScoopCli {
Write-Information "Install scoop from https://get.scoop.sh"
Write-Host "Download & install scoop"
If ( $DryRun ) {
Write-Host "[DRY RUN] Would download & install scoop." -ForegroundColor Magenta
return
}
If ( -Not (Get-Command scoop) ) {
try {
Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression
}
catch {
Write-Error "Failed to install scoop."
Write-Error "Exception details: $($exc.Message)"
exit 1
}
}
}
function Initialize-ScoopCli {
Write-Host "Installing aria2 for accelerated downloads"
If ( $DryRun ) {
Write-Host "[DRY RUN] Would install aria2." -ForegroundColor Magenta
Write-Host "[Dry RUN] Would enable scoop 'extras' bucket." -ForegroundColor Magenta
Write-Host "[Dry RUN] Would disable aria2 warning." -ForegroundColor Magenta
Write-Host "[Dry RUN] Would install git." -ForegroundColor Magenta
return
}
try {
scoop install aria2
if ( -Not $(scoop config aria2-enabled) -eq $True) {
scoop config aria2-enabled true
}
}
catch {
Write-Error "Failed to install aria2."
Write-Error "Exception details: $($exc.Message)"
}
Write-Host "Enable scoop buckets"
try {
scoop bucket add extras
scoop bucket add nerd-fonts
}
catch {
Write-Error "Failed to enable 1 or more scoop buckets."
Write-Error "Exception details: $($exc.Message)"
}
Write-Host "Disable scoop warning when using aria2 for downloads"
try {
scoop config aria2-warning-enabled false
}
catch {
Write-Error "Failed to disable aria2 warning."
Write-Error "Exception details: $($exc.Message)"
}
Write-Host "Install git"
try {
scoop install git
}
catch {
Write-Error "Failed to install git."
Write-Error "Exception details: $($exc.Message)"
}
}
function Install-Dependencies {
Install-ScoopCli
Initialize-ScoopCli
## Install neovim dependencies with scoop
ForEach ( $app in $NeovimDependencies ) {
If ( $DryRun ) {
Write-Host "[DRY RUN] Would install app: $app." -ForegroundColor Magenta
}
else {
Write-Host "Installing $app"
try {
scoop install $app
}
catch {
Write-Error "Error installing app '$app'. Details: $($_.Exception.message)"
}
}
}
If ( -Not (Get-Command nvim) ) {
## Install neovim
If ( $DryRun ) {
Write-Host "[DRY RUN] Would install neovim." -ForegroundColor Magenta
}
else {
Write-Host "Installing neovim"
try {
scoop install neovim
}
catch {
Write-Error "Error installing neovim. Details: $($_.Exception.Message)"
}
}
}
}
function New-NvimConfigSymlink {
If ( $DryRun ) {
Write-Host "[DRY RUN] Would create symlink from $NVIM_CONFIG_SRC to $NVIM_CONFIG_DIR." -ForegroundColor Magenta
return
}
else {
## Check if config path already exists
If ( Test-Path $NVIM_CONFIG_DIR ) {
## Check if path is directory or junction
$Item = Get-Item $NVIM_CONFIG_DIR
## Check if path is a junction
If ( $Item.Attributes -band [System.IO.FileAttributes]::ReparsePoint ) {
Write-Host "Path is already a junction: $($NVIM_CONFIG_DIR)"
return
}
## Path is a regular directory
Write-Warning "Path already exists: $NVIM_CONFIG_DIR. Moving to $NVIM_CONFIG_DIR.bak"
If ( Test-Path "$NVIM_CONFIG_DIR.bak" ) {
Write-Warning "$NVIM_CONFIG_DIR.bak already exists. Overwriting backup."
Remove-Item -Recurse "$NVIM_CONFIG_DIR.bak"
}
try {
Move-Item $NVIM_CONFIG_DIR "$NVIM_CONFIG_DIR.bak"
}
catch {
Write-Error "Error moving $NVIM_CONFIG_DIR to $NVIM_CONFIG_DIR.bak. Details: $($_.Exception.Message)"
exit 1
}
}
}
Write-Host "Creating symlink from $NVIM_CONFIG_SRC to $NVIM_CONFIG_DIR"
Write-Debug "NVIM_CONFIG_DIR: $($NVIM_CONFIG_DIR)"
Write-Debug "NVIM_CONFIG_SRC: $($NVIM_CONFIG_SRC)"
$SymlinkCommand = "New-Item -Path $NVIM_CONFIG_DIR -ItemType SymbolicLink -Target $NVIM_CONFIG_SRC"
If ( -Not ( Test-IsAdmin ) ) {
Write-Warning "Script was not run as administrator. Running symlink command as administrator."
try {
Run-AsAdmin -Command "$($SymlinkCommand)"
}
catch {
Write-Error "Error creating symlink from $NVIM_CONFIG_SRC to $NVIM_CONFIG_DIR. Details: $($_.Exception.Message)"
}
}
else {
try {
Invoke-Expression $SymlinkCommand
}
catch {
Write-Error "Error creating symlink from $NVIM_CONFIG_SRC to $NVIM_CONFIG_DIR. Details: $($_.Exception.Message)"
exit 1
}
}
}
Install-Dependencies
try {
New-NvimConfigSymlink
}
catch {
Write-Error "Error installing neovim configuration to path '$($NVIM_CONFIG_DIR)'."
exit 1
}