-
Notifications
You must be signed in to change notification settings - Fork 1
/
Install-Vimfiles.ps1
executable file
·471 lines (412 loc) · 14.1 KB
/
Install-Vimfiles.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
<#PSScriptInfo
.VERSION 3.0
.GUID e7b6d3ed-1459-4dee-9dcf-675756b14510
.AUTHOR John D. Fisher <[email protected]>
.COMPANYNAME
.COPYRIGHT 2023 John D. Fisher
.TAGS
.LICENSEURI https://github.com/jfishe/vimfiles/blob/master/LICENSE
.PROJECTURI https://github.com/jfishe/vimfiles
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA
#>
<#
.SYNOPSIS
Install and symlink vimfiles to $HOME
.DESCRIPTION
Backup existing installation to BackupPath.
Clone Repository to Path and update submodules.
Symlink dotfiles and vimfiles to LinkPath. Use Junction and Hardlink if
SeCreateSymbolicLink permission denied.
Copy Ubuntu wamerican dictionary and Moby-thesaurus to the locations
specified in the vimrc.
Create/update compatible conda environment for Vim and modify Vim batch
files to activate the conda environment.
Create Start Menu shortcuts for Vim batch files.
.EXAMPLE
PS> .\Install-Vimfiles.ps1 -Clone
.EXAMPLE
PS> .\Install-Vimfiles.ps1 -Link
#>
[CmdletBinding()]
Param(
# Symlink dotfiles and vimfiles to $HOME. Use Junction and Hardlink if
# SeCreateSymbolicLink permission denied.
[Parameter(
Mandatory = $true,
ParameterSetName = 'Link',
HelpMessage = 'Symlink dotfiles and vimfiles to $HOME. Use Junction and Hardlink if SeCreateSymbolicLink permission denied.'
)]
[switch]
$Link
,
# Clone Repository to Path and update submodules.
[Parameter(
Mandatory = $true,
ParameterSetName = 'Clone',
HelpMessage = 'Clone Repository to Path and update submodules.'
)]
[switch]
$Clone
,
# Create/update Ubuntu wamerican dictionary.
[Parameter(
Mandatory = $true,
ParameterSetName = 'Dictionary',
HelpMessage = 'Create/update Ubuntu wamerican dictionary.'
)]
[switch]
$Dictionary
,
# Create/update Moby-thesaurus.
[Parameter(
Mandatory = $true,
ParameterSetName = 'Thesaurus',
HelpMessage = 'Create/update Moby-thesaurus.'
)]
[switch]
$Thesaurus
,
# Create/update conda environment for Vim (vim-python).
# Copy Vim batch files to UserAppDir.
[Parameter(
Mandatory = $true,
ParameterSetName = 'Conda',
HelpMessage = 'Create/update conda environment for Vim (vim-python).'
)]
[switch]
$Conda
,
# Create Start Menu folder and shortcuts for Vim batch files.
# Create shortcut for default WSL gvim.
[Parameter(
Mandatory = $true,
ParameterSetName = 'Shortcut',
HelpMessage = 'Create Start Menu folder and shortcuts for Vim batch files.'
)]
[switch]
$Shortcut
,
# Path to clone repository.
[Parameter(
Mandatory = $false,
ParameterSetName = 'Clone',
HelpMessage = 'Path to clone repository.'
)]
[Parameter(ParameterSetName = 'Link')]
[Parameter(ParameterSetName = 'Dictionary')]
[Parameter(ParameterSetName = 'Thesaurus')]
[Parameter(ParameterSetName = 'Conda')]
[Alias("PSPath")]
[ValidateNotNullOrEmpty()]
[string]
$Path = "$env:LOCALAPPDATA\vimfiles"
,
# URI for git repository.
[Parameter(
Mandatory = $false,
ParameterSetName = 'Clone',
HelpMessage = 'URI for git repository'
)]
[string]
$Repository = 'https://github.com/jfishe/vimfiles.git'
,
# Path for backups.
[Parameter(
Mandatory = $false,
ParameterSetName = 'Clone',
HelpMessage = 'Path for backups.'
)]
[Parameter(ParameterSetName = 'Link')]
[string]
$BackupPath = "$HOME\old"
,
# Path to directory to create symbolic links.
[Parameter(
Mandatory = $false,
ParameterSetName = 'Link',
HelpMessage = 'Path to directory to create symbolic links.'
)]
[string]
$LinkPath = "$HOME"
,
# Path to directory to copy user modified Vim batch files
# to activate conda environment vim-python.
# The directory should be in $Env:PATH.
[Parameter(
Mandatory = $false,
ParameterSetName = 'Conda',
HelpMessage = 'Path to directory to copy user modified Vim batch files.'
)]
[Parameter(ParameterSetName = 'Shortcut')]
[string]
$UserAppDir = "$env:LOCALAPPDATA\Microsoft\WindowsApps"
,
# Path to directory where Vim installer placed batch files.
# Not used if UserAppDir already contains Vim batch files.
[Parameter(
Mandatory = $false,
ParameterSetName = 'Conda',
HelpMessage = 'Path to directory where Vim installer placed batch files.'
)]
[string]
$GlobalAppDir = "$env:WINDIR"
,
# Path to Vim icon file, e.g., gvim.exe.
[Parameter(
Mandatory = $false,
ParameterSetName = 'Shortcut',
HelpMessage = 'Path to Vim icon file.'
)]
[string]
$IconLocation = (
"$env:ProgramFiles",
"${env:ProgramFiles(x86)}",
"$env:SystemDrive\tools",
"$env:LOCALAPPDATA\Programs"
).ForEach(
{
Resolve-Path -Path "$_\Vim\*\gvim.exe" -ErrorAction SilentlyContinue
}
)
)
<#
.SYNOPSIS
Create a symbolic link.
.DESCRIPTION
Creates a symbolic link.
MKLINK [[/D] | [/H] | [/J]] Link Target
/D Creates a directory symbolic link. Default is a file
symbolic link.
/H Creates a hard link instead of a symbolic link.
/J Creates a Directory Junction.
Link Specifies the new symbolic link name.
Target Specifies the path (relative or absolute) that the new link
refers to.
#>
function New-Symlink {
[CmdletBinding()]
param (
[Parameter(
Mandatory = $false,
Position = 0,
ValueFromPipelineByPropertyName = $true
)]
[ValidateSet("", '/D', '/H', '/J')]
[string]
$ItemType = ""
,
[Parameter(
Mandatory = $true,
Position = 1,
ValueFromPipelineByPropertyName = $true
)]
[string]
$Link
,
[Parameter(
Mandatory = $true,
Position = 2,
ValueFromPipelineByPropertyName = $true
)]
[string]
$Target
)
begin {
}
process {
Write-Verbose "cmd.exe /c mklink $ItemType $Link $Target"
$Output = cmd.exe /c mklink $ItemType $Link $Target 2>&1
$Output | Out-String | Write-Verbose
$ErrorMessage = "You do not have sufficient privilege to perform this operation."
if ("$Output" -eq "$ErrorMessage") {
Write-Verbose "Trying Junction and Hardlink instead."
if ( $ItemType -eq "/D" ) {
$OldItemType = "/J"
} else {
$OldItemType = "/H"
}
$Output = cmd.exe /c mklink $OldItemType $Link $Target 2>&1
$Output | Out-String | Write-Verbose
}
}
end {
}
}
function Backup-Item {
[CmdletBinding()]
param (
[Parameter(
Mandatory = $true,
Position = 0,
ValueFromPipelineByPropertyName = $true
)]
[string]
$Link,
[Parameter(
Mandatory = $false
)]
[string]
$Destination = "$HOME\old"
)
begin {
Write-Verbose "Validate/create $Destination"
New-Item -Path "$Destination" -ItemType Directory -ErrorAction SilentlyContinue | Out-String | Write-Verbose
$Destination = Get-Item -Path "$Destination" -ErrorAction Stop
}
process {
Move-Item -Path $Link -Destination "$Destination" `
-ErrorAction SilentlyContinue
}
end {
Get-ChildItem -Path "$Destination" | Out-String | Write-Verbose
}
}
if ($Clone) {
Backup-Item -Link "$Path" -Destination "$BackupPath\repo" -ErrorAction Stop
git config --global core.eol lf
git config --global core.autocrlf false
$Output = git clone "$Repository" "$Path" 2>&1
$Output | Out-String | Write-Verbose
Push-Location -Path "$Path"
$Output = git submodule update --init --recursive 2>&1
$Output | Out-String | Write-Verbose
vim -c 'packloadall | helptags ALL | qa'
Pop-Location
}
if ($Link) {
[array] $Vimfiles = Get-Item -Path "$Path\vimfiles" | ForEach-Object -Process {
# ~\vimfiles for Windows Vim
[PSCustomObject]@{
Link = "$LinkPath\$($_.Name)"
Target = "$($_.FullName)"
ItemType = '/D'
},
# ~\.vim for Mintty and git-scm Vim
[PSCustomObject]@{
Link = "$LinkPath\.vim"
Target = "$($_.FullName)"
ItemType = '/D'
}
}
# Terminal config for Mintty and git-scm Vim
[array] $Vimfiles += Get-Item -Path "$Path\mintty" | ForEach-Object -Process {
[PSCustomObject]@{
Link = "$LinkPath\.config\$($_.Name)"
Target = "$($_.FullName)"
ItemType = '/D'
}
}
New-Item -Path "$LinkPath\.config" -ItemType Directory -ErrorAction SilentlyContinue
[array] $Vimfiles += Get-ChildItem -Path "$Path\dotfiles" | ForEach-Object -Process {
if ($_.PSIsContainer) {
$ItemType = '/D'
} else {
$ItemType = ''
}
[PSCustomObject]@{
Link = "$LinkPath\.$($_.Name)"
Target = "$($_.FullName)"
ItemType = "$ItemType"
}
}
$Vimfiles | Backup-Item -Destination "$BackupPath"
$Vimfiles | New-Symlink
}
if ($Dictionary) {
# Assume WSL defaults to Ubuntu.
$WslDictionary = '/usr/share/dict/words'
wsl --exec bash -c "sudo apt-get update && sudo apt-get install wamerican-huge"
$Words = wsl --exec bash -c "wslpath -w ``realpath $WslDictionary``"
$OutFile = "$Path\vimfiles\dictionary\words"
New-Item -Path (Split-Path $OutFile) -ItemType Directory -ErrorAction SilentlyContinue
Copy-Item -Path $Words -Destination $OutFile
}
if ($Thesaurus) {
$Uri = 'https://raw.githubusercontent.com/zeke/moby/master/words.txt'
$OutFile = "$Path\vimfiles\thesaurus\mthesaur.txt"
New-Item -Path (Split-Path $OutFile) -ItemType Directory -ErrorAction SilentlyContinue
Invoke-WebRequest -Uri $Uri -OutFile $OutFile
}
if ($Conda) {
# Install Miniconda3 if needed.
try {
Get-Command Invoke-Conda -ErrorAction Stop
} catch [System.Management.Automation.CommandNotFoundException] {
$_ | Out-String | Write-Verbose
$webrequestParams = @{
Uri = 'https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe'
OutFile = "$HOME\Downdloads\Miniconda3-latest-Windows-x86_64.exe"
}
Invoke-WebRequest @webrequestParams
& "$($webrequestParams.OutFile)"
# Miniconda3 may install in different locations.
"$env:LOCALAPPDATA", "$env:USERPROFILE" | ForEach-Object -Process {
if (Test-Path ($condaexe = Join-Path $_ "miniconda3\Scripts\conda.exe") -PathType Leaf) {
break
}
}
(& "$condaexe" "shell.powershell" "hook") | Out-String | ? {$_} | Invoke-Expression
} finally {
Get-Command Invoke-Conda -ErrorAction Stop | Out-String | Write-Verbose
}
# Create or update conda env vim-python.
Push-Location "$Path"
Get-Item .\environment.yml -ErrorAction Stop | Out-String | Write-Verbose
if (Invoke-Conda env list | Select-String -Pattern 'vim-python' -CaseSensitive) {
Invoke-Conda env update --file environment.yml
} else {
Invoke-Conda env create --file environment.yml
}
Pop-Location
# Copy Vim batch files to userappdir.
$UserAppDir = Get-Item "$UserAppDir"
$GlobalAppDir = Get-Item "$GlobalAppDir"
$UserVimCmd = Join-Path $UserAppDir '*vim*' | Get-ChildItem
$GlobalVimCmd = Join-Path $GlobalAppDir '*vim*' | Get-ChildItem
if (-not $UserVimCmd -and $GlobalVimCmd) {
$GlobalVimCmd | Copy-Item -Destination $UserAppDir
}
Write-Verbose 'reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_EXPAND_SZ /d "%"USERPROFILE"%\.init.cmd" /f'
cmd /c 'reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_EXPAND_SZ /d "%"USERPROFILE"%\.init.cmd" /f'
}
if ($Shortcut) {
# Copy WSL gvim script.
$Wslscript = Get-Item -Path "$Path\pwsh\gvim-wsl.bat"
$Wslscript = Copy-Item -Path $Wslscript -Destination $UserAppDir -PassThru
Write-Verbose "$Wslscript assumes default WSL add wsl --distro, as needed."
# Locate Vim batch files.
$UserAppDir = Get-Item "$UserAppDir"
[array] $SourceFileLocation = Join-Path $UserAppDir '*vim*.bat' | Get-ChildItem
[array] $SourceFileLocation += Join-Path $UserAppDir '*view*.bat' | Get-ChildItem
# Destination directory needs to exist.
$ShortcutLocation = "$Env:AppData\Microsoft\Windows\Start Menu\Programs\Vim"
New-Item $ShortCutLocation -ItemType Directory -ErrorAction SilentlyContinue
# Copy Visual Basic Script to open gv*.bat without cmd.exe window.
$BatLauncher = Get-Item -Path "$Path\pwsh\bat-launcher.vbs"
$BatLauncher = Copy-Item -Path $BatLauncher -Destination $UserAppDir -PassThru
$WorkingDirectory = '%HOMEDRIVE%%HOMEPATH%'
$SourceFileLocation | ForEach-Object -Process {
$item = Join-Path $ShortcutLocation "$($_.BaseName).lnk"
# New-Object : Creates an instance of a Microsoft .NET Framework or COM object.
# -ComObject WScript.Shell: This creates an instance of the COM object that represents the WScript.Shell for invoke CreateShortCut
$WScriptShell = New-Object -ComObject WScript.Shell
$Newlink = $WScriptShell.CreateShortcut($item)
if ( $_.BaseName -match '^gv' ) {
$Newlink.TargetPath = "$BatLauncher"
$Newlink.Arguments = "$($_.BaseName) --"
} else {
$Newlink.TargetPath = "$_"
}
$Newlink.WindowStyle = 7 # Minimized
$Newlink.IconLocation = "$IconLocation"
$Newlink.WorkingDirectory = "$WorkingDirectory"
$Newlink | Out-String | Write-Verbose
#Save the Shortcut to the TargetPath
$Newlink.Save()
}
}