This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.ps1
632 lines (473 loc) · 17.9 KB
/
install.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# Vim's Setup Utils on Windows
# Le Tan (tamlokveer at gmail.com)
# https://github.com/tamlok/tvim
param([switch]$Force=$false, [string]$WorkingDirectory='')
function Main
{
Write-Host 'Welcome! Let me help you setup Vim on Windows'
if ($WorkingDirectory -ne '') {
Set-Location -Path "$WorkingDirectory"
}
Write-Host "Working directory is" (Get-Location).Path
Check-Admin
Print-Usage
. '.\install_font.ps1'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$action = Get-Action
switch ($action) {
1 { Install-Neovim }
2 { Setup-Neovim }
3 { Setup-Vim }
4 { Pack-Neovim }
5 { Pack-Vim }
6 { Install-Fonts }
9 { Write-Host 'Bye!'; exit 0 }
default {Write-Error 'Invalid action'; exit -1}
}
}
function Print-Usage
{
Write-Host "Usage:"
Write-Host "-Force: force to update configurations"
Write-Host ""
}
function Get-Action
{
Write-Host ''
Write-Host 'Actions:'
Write-Host '1. Install Neovim'
Write-Host '2. Setup Neovim'
Write-Host '3. Setup Vim'
Write-Host '4. Pack Neovim'
Write-Host '5. Pack Vim'
Write-Host '6. Install fonts'
Write-Host '9. Exit'
Write-Host ''
$action = Read-Host -Prompt 'Please choose the right action to perform'
return $action
}
function Install-Neovim
{
Write-Host ''
Write-Host '==Install Neovim=='
$targetLocation = Read-Host -Prompt 'Enter the location you want to put Neovim'
if ($targetLocation -eq "" -or !(Test-Path -Path $targetLocation)) {
Write-Error "Target location does not exist $targetLocation"
exit -1
}
$release = Get-Neovim-Latest-Release
Write-Host 'Downloading latest Neovim...'
$targetZipFile = $env:TEMP + '\tvim_' + $release.File
Invoke-WebRequest -Uri $release.Url -OutFile $targetZipFile -UseBasicParsing
$targetFolder = $targetLocation + '\Neovim'
if (Test-Path -Path $targetFolder) {
Write-Host "Target folder $targetFolder exists and will be removed"
Remove-Directory-Recursively $targetFolder
}
if (!(Unzip-Neovim $targetZipFile $targetLocation)) {
Remove-Item $targetZipFile -Force
Write-Error "Fail to unzip $targetZipFile to $targetFolder"
exit -1
}
Remove-Item $targetZipFile -Force
Add-Nvim-Bat $targetFolder
Add-Neovim-To-Path $targetFolder
Add-Neovim-To-Context-Menu $targetFolder
}
function Get-Neovim-Latest-Release
{
$file = 'nvim-win64.zip'
$regularExp = '"browser_download_url":"(https://github.com/neovim/neovim/releases/[^"]+/' + $file + ')"'
$url = 'https://api.github.com/repos/neovim/neovim/releases/latest'
$content = Invoke-WebRequest -Uri $url -UseBasicParsing
$match = $content -match $regularExp
if (!$match) {
Write-Error "Fail to fetch information of Neovim releases"
exit -1
}
$release = New-Object PSObject | Select-Object Url, File
$release.Url = $Matches[1]
$release.File = $file
return $release
}
function Unzip-Neovim
{
Param([string]$zipFile, [string]$outputFolder)
$ret = Expand-Archive $zipFile -DestinationPath $outputFolder
return $true
}
function Add-Nvim-Bat
{
Param([string]$nvimFolder)
$cmd = '@start nvim-qt.exe %*'
$destinationFile = "$nvimFolder\bin\qvim.bat"
Out-File -Encoding ascii -LiteralPath $destinationFile -InputObject $cmd
$cmd = 'if exist .vimrc.user (nvim.exe -u .vimrc.user) else (nvim.exe)'
$destinationFile = "$nvimFolder\bin\tvim.bat"
Out-File -Encoding ascii -LiteralPath $destinationFile -InputObject $cmd
}
function Add-Neovim-To-Path
{
Param([string]$nvimFolder)
$oldPath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
$binFolder = "$nvimFolder\bin"
if (!$oldPath.contains($binFolder)) {
if ($oldPath.EndsWith(';')) {
$newPath = "$oldPath$binFolder"
} else {
$newPath = "$oldPath;$binFolder"
}
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value "$newPath"
}
}
function Add-Neovim-To-Context-Menu
{
Param([string]$nvimFolder)
$neovimKeyStr = 'Registry::HKEY_CLASSES_ROOT\*\shell\Neovim'
if ((Get-Item -LiteralPath "$neovimKeyStr") -eq $null) {
Write-Host "Create registry key $neovimKeyStr"
New-Item "$neovimKeyStr" > $null
}
$displayName = 'Edit with Neovim'
Set-ItemProperty -LiteralPath "$neovimKeyStr" -Name '(Default)' -Value "$displayName"
$icon = "`"$nvimFolder\bin\nvim-qt.exe`""
Set-ItemProperty -LiteralPath "$neovimKeyStr" -Name 'Icon' -Value "$icon"
$commandKeyStr = "$neovimKeyStr\command"
if ((Get-Item -LiteralPath "$commandKeyStr") -eq $null) {
Write-Host "Create registry key $commandKeyStr"
New-Item "$commandKeyStr" > $null
}
$command = "`"$nvimFolder\bin\nvim-qt.exe`" `"%1`""
Set-ItemProperty -LiteralPath "$commandKeyStr" -Name '(Default)' -Value "$command"
}
function Remove-Directory-Recursively
{
Param([string]$directory)
if (!(Test-Path -Path "$directory")) {
return
}
Get-ChildItem -Path "$directory" -Recurse | Remove-Item -Recurse
Remove-Item "$directory" -Recurse
}
function Run-As-Admin
{
Param([string]$command)
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;
if ($myWindowsPrincipal.IsInRole($adminRole)) {
Invoke-Expression $command
} else {
$cmd = "& '$command'"
Start-Process "$PSHome\powershell.exe" -Wait -Verb RunAs -WorkingDirectory (Get-Location).Path -ArgumentList $cmd
}
}
function Setup-Neovim
{
Write-Host ''
Write-Host '==Setup Neovim=='
# Check-Git
$vimBinFolder = Get-Neovim-Folder
$vimFilesFolder = $env:USERPROFILE + '\AppData\Local\nvim'
$vimUtilsFolder = (Get-Item "$vimBinFolder").Parent.FullName + '\share\nvim\runtime\tvim_utils'
Do-Setup-Neovim $vimBinFolder $vimFilesFolder $vimUtilsFolder
}
function Setup-Vim
{
Write-Host ''
Write-Host '==Setup Vim=='
# Check-Git
$vimBinFolder = Get-GVim-Folder
$vimFilesFolder = $env:USERPROFILE + '\vimfiles'
$vimUtilsFolder = "$vimBinFolder\tvim_utils"
Do-Setup-Vim $vimBinFolder $vimFilesFolder $vimUtilsFolder
}
function Check-Git
{
if ((Get-Command 'git' -ErrorAction SilentlyContinue) -eq $null) {
Write-Error 'Fail to locate Git in your PATH'
exit -1
}
Write-Host 'Git is found.'
}
function Get-GVim-Folder
{
$gvim = Get-Command 'gvim' -ErrorAction SilentlyContinue
if ($gvim -eq $null) {
Write-Host "Fail to locate GVim"
exit -1
}
if ($gvim.Path.EndsWith('.bat')) {
$reg = 'set VIM_EXE_DIR=(.+)'
$bat = Get-Content $gvim.Path
$firstMatchLine = ($bat -match $reg)[0]
$match = $firstMatchLine -match $reg
if (!$match) {
Write-Error 'Fail to locate GVim in your PATH'
exit -1
}
$gvimFolder = $Matches[1]
} else {
$gvimFolder = (Get-Item $gvim.Path).Directory.FullName
}
Write-Host "GVim is found in $gvimFolder"
return $gvimFolder
}
function Get-Neovim-Folder
{
$nvim = Get-Command 'nvim' -ErrorAction SilentlyContinue
if ($nvim -eq $null) {
Write-Host "Fail to locate Neovim"
exit -1
}
$nvimFolder = (Get-Item $nvim.Path).Directory.FullName
Write-Host "Neovim is found in $nvimFolder"
return $nvimFolder
}
function Do-Setup-Vim
{
param([string]$binFolder, [string]$filesFolder, [string]$utilsFolder)
Check-TVim-Utils $utilsFolder
Check-Detorte $filesFolder
Check-Plug $filesFolder
Check-Markdown2Ctags $filesFolder
Check-Coc-Settings $filesFolder
Check-Vimrc
Check-Ftdetect $filesFolder
Check-Scripts $filesFolder
}
function Do-Setup-Neovim
{
param([string]$binFolder, [string]$filesFolder, [string]$utilsFolder)
Check-TVim-Utils $utilsFolder
Check-Detorte $filesFolder
Check-Plug $filesFolder
Check-Markdown2Ctags $filesFolder
Check-Coc-Settings $filesFolder
Check-Init_Vim $filesFolder
Check-Ftdetect $filesFolder
Check-Scripts $filesFolder
}
function Check-TVim-Utils
{
param([string]$utilsFolder)
Write-Host "Check tvim_utils in $utilsFolder"
if (Test-Path -Path "$utilsFolder") {
if (!$Force) {
return
} else {
Remove-Directory-Recursively $utilsFolder
}
}
$tempFolder = '.\tmp'
New-Item -ItemType 'directory' -Path "$tempFolder" -Force > $null
$tempUtilsFolder = Get-TVim-Utils "$tempFolder"
robocopy "$tempUtilsFolder" "$utilsFolder" /E /MT /XD .git > $null
Remove-Directory-Recursively $tempFolder
}
function Get-TVim-Utils
{
param([string]$tempFolder)
$url = "https://github.com/tamlok/vim-win-utils/archive/master.zip"
Write-Host "Downloading $url"
$targetName = 'vim-win-utils-master'
$targetZipFile = "$tempFolder\$targetName.zip"
Invoke-WebRequest -Uri $url -OutFile $targetZipFile -UseBasicParsing
Expand-Archive $targetZipFile -DestinationPath $tempFolder
Remove-Item $targetZipFile -Force
$outputFolder = (Get-Item "$tempFolder\$targetName").FullName
Write-Host "Utils downloaded in $outputFolder"
return $outputFolder
}
function Check-Detorte
{
param([string]$filesFolder)
$colorFolder = "$filesFolder\colors"
Write-Host "Check detorte in $colorFolder"
xcopy /Y /i /e .\detorte\colors "$colorFolder\" > $null 2> $null
}
function Check-Plug
{
param([string]$filesFolder)
$plugFolder = "$filesFolder\autoload"
Write-Host "Check Plug in $plugFolder"
xcopy /Y /i .\utils\plug.vim "$plugFolder\" > $null 2> $null
}
function Check-Markdown2Ctags
{
param([string]$filesFolder)
Write-Host "Check markdown2ctags.py in $filesFolder"
xcopy /Y /i .\utils\markdown2ctags.py "$filesFolder\" > $null 2> $null
}
function Check-Coc-Settings
{
param([string]$filesFolder)
Write-Host "Check coc-settings.json in $filesFolder"
xcopy /Y /i .\utils\coc-settings.json "$filesFolder\" > $null 2> $null
}
function Check-Vimrc
{
$userFolder = $env:USERPROFILE
Write-Host "Check _vimrc and .vimrc in $userFolder"
xcopy /Y /i .\vimrc "$userFolder\" > $null 2> $null
copy .\vimrc "$userFolder\_vimrc" > $null 2> $null
}
function Check-Init_Vim
{
param([string]$filesFolder)
Write-Host "Check init.vim and ginit.vim in $filesFolder"
copy .\vimrc "$filesFolder\init.vim" > $null 2> $null
copy .\ginit.vim "$filesFolder\ginit.vim" > $null 2> $null
}
function Check-Ftdetect
{
param([string]$filesFolder)
$ftdetectFolder = "$filesFolder\ftdetect"
Write-Host "Check ftdetect in $ftdetectFolder"
if (Test-Path -Path "$ftdetectFolder") {
Remove-Directory-Recursively $ftdetectFolder
}
robocopy ".\ftdetect" "$ftdetectFolder" /E /MT > $null
}
function Check-Scripts
{
param([string]$filesFolder)
$scriptsFolder = "$filesFolder\scripts"
Write-Host "Check scripts in $scriptsFolder"
if (Test-Path -Path "$scriptsFolder") {
Remove-Directory-Recursively $scriptsFolder
}
robocopy ".\scripts" "$scriptsFolder" /E /MT > $null
}
function Pack-Neovim
{
Write-Host ''
Write-Host '==Pack-Neovim=='
$vimBinFolder = Get-Neovim-Folder
$vimFilesFolder = $env:USERPROFILE + '\AppData\Local\nvim'
Do-Pack-Neovim $vimBinFolder $vimFilesFolder
}
function Pack-Vim
{
Write-Host ''
Write-Host '==Pack-Vim=='
$vimBinFolder = Get-GVim-Folder
$vimFilesFolder = $env:USERPROFILE + '\vimfiles'
Do-Pack-Vim $vimBinFolder $vimFilesFolder
}
function Do-Pack-Vim
{
param([string]$binFolder, [string]$filesFolder)
$portableZipFile = 'vim_portable.zip'
Write-Host "Packing Vim into $portableZipFile"
$portableFolder = '.\vim_portable'
Remove-Directory-Recursively $portableFolder
if (Test-Path -Path $portableZipFile) {
Remove-Item $portableZipFile
}
New-Item -ItemType 'directory' -Path "$portableFolder" > $null
$userFolder = $env:USERPROFILE
copy "$userFolder\_vimrc" "$portableFolder\_vimrc" > $null 2> $null
copy .\README.md "$portableFolder\README.md" > $null 2> $null
$binFolderName = (Get-Item "$binFolder").Name
xcopy /Y /i /e "$binFolder" "$portableFolder\$binFolderName\" > $null
$binParentFolder = (Get-Item "$binFolder").Parent.FullName
xcopy /Y /i /e "$binParentFolder\vimfiles" "$portableFolder\vimfiles\" > $null
# Ignore coc.nvim since it does not work properly.
$ignoredFolders = "$filesFolder\plugged\coc.nvim"
robocopy "$filesFolder" "$portableFolder\vimfiles" /E /MT /XD "$ignoredFolders" > $null
Write-Vim-Init_BAT $portableFolder
Compress-Archive -CompressionLevel Fastest -Path $portableFolder -DestinationPath ".\$portableZipFile"
Remove-Directory-Recursively $portableFolder
Write-Host "Vim is packed as" (Get-Item $portableZipFile).FullName
}
function Do-Pack-Neovim
{
param([string]$binFolder, [string]$filesFolder)
$portableZipFile = 'nvim_portable.zip'
Write-Host "Packing Neovim into $portableZipFile"
$portableFolder = '.\nvim_portable'
Remove-Directory-Recursively $portableFolder
if (Test-Path -Path $portableZipFile) {
Remove-Item $portableZipFile
}
New-Item -ItemType 'directory' -Path "$portableFolder" > $null
copy .\README.md "$portableFolder\README.md" > $null 2> $null
$binFolderName = (Get-Item "$binFolder").Name
xcopy /Y /i /e "$binFolder" "$portableFolder\$binFolderName\" > $null
$binParentFolder = (Get-Item "$binFolder").Parent.FullName
xcopy /Y /i /e "$binParentFolder\share" "$portableFolder\share\" > $null
$destFilesFolder = "$portableFolder\share\nvim\vimfiles\"
xcopy /Y /i /e "$filesFolder" "$destFilesFolder" > $null
Write-Neovim-Init_BAT $portableFolder
Compress-Archive -CompressionLevel Fastest -Path $portableFolder -DestinationPath ".\$portableZipFile"
Remove-Directory-Recursively $portableFolder
Write-Host "Neovim is packed as" (Get-Item $portableZipFile).FullName
}
function Check-Admin
{
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;
if ($myWindowsPrincipal.IsInRole($adminRole)) {
return
} else {
Write-Host "Continue as Administrator"
$arguments = "-NoExit -ExecutionPolicy Bypass -File `"$PSCommandPath`""
if ($Force) {
$arguments += ' -Force'
}
$arguments += " -WorkingDirectory `"" + (Get-Location).Path + '"'
Start-Process powershell.exe -Verb RunAs -ArgumentList $arguments
exit 0
}
}
function Write-Neovim-Init_BAT
{
param([string]$destFolder)
$batFile = "$destFolder\init.bat"
$content = "@echo off`r`n"
$content += "echo Vim configurations by Le Tan (https://github.com/tamlok/tvim)`r`n"
$content += "set cur_dir=%~dp0`r`n"
$content += 'if /I "%cur_dir:~-1%" EQU "\" set cur_dir=%cur_dir:~0,-1%' + "`r`n"
$content += 'set vimfiles=%cur_dir%\share\nvim\vimfiles' + "`r`n"
$content += 'set userVimFiles=%USERPROFILE%\AppData\Local\nvim' + "`r`n"
$content += '@echo on' + "`r`n"
$content += 'mkdir "%userVimFiles%"' + "`r`n"
$content += 'copy "%vimfiles%\init.vim" "%userVimFiles%\init.vim"' + "`r`n"
$content += 'copy "%vimfiles%\ginit.vim" "%userVimFiles%\ginit.vim"' + "`r`n"
$content += 'copy "%vimfiles%\coc-settings.json" "%userVimFiles%\coc-settings.json"' + "`r`n"
$content += 'robocopy "%vimfiles%\scripts" "%userVimFiles%\scripts" /E /MT' + "`r`n"
$content += '@echo off' + "`r`n"
$content += "pause"
Out-File -Encoding ascii -LiteralPath $batFile -InputObject $content
}
function Write-Vim-Init_BAT
{
param([string]$destFolder)
$batFile = "$destFolder\init.bat"
$content = "@echo off`r`n"
$content += "echo Vim configurations by Le Tan (https://github.com/tamlok/tvim)`r`n"
$content += "set cur_dir=%~dp0`r`n"
$content += 'if /I "%cur_dir:~-1%" EQU "\" set cur_dir=%cur_dir:~0,-1%' + "`r`n"
$content += 'set vimrc=%cur_dir%\_vimrc' + "`r`n"
$content += 'set cocsettings=%cur_dir%\vimfiles\coc-settings.json' + "`r`n"
$content += 'set scripts=%cur_dir%\vimfiles\scripts' + "`r`n"
$content += 'set userFolder=%USERPROFILE%' + "`r`n"
$content += 'set userVimFiles=%USERPROFILE%\vimfiles' + "`r`n"
$content += '@echo on' + "`r`n"
$content += 'mkdir "%userVimFiles%"' + "`r`n"
$content += 'copy "%vimrc%" "%userFolder%\_vimrc"' + "`r`n"
$content += 'copy "%vimrc%" "%userFolder%\.vimrc"' + "`r`n"
$content += 'copy "%cocsettings%" "%userVimFiles%\coc-settings.json"' + "`r`n"
$content += 'robocopy "%scripts%" "%userVimFiles%\scripts" /E /MT' + "`r`n"
$content += '@echo off' + "`r`n"
$content += "pause"
Out-File -Encoding ascii -LiteralPath $batFile -InputObject $content
}
function Install-Fonts
{
$fontFolder = (Get-Item '.\fonts').FullName
Install-Font "$fontFolder"
}
Main