-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.ps1
134 lines (123 loc) · 5.01 KB
/
setup.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
#Requires -Version 5.1
[CmdletBinding()]
param ()
$ErrorActionPreference = "Stop" # Stop immediately whatever an error occurred
# Ensure XDG_CONFIG_HOME is defined
if ($null -eq $env:XDG_CONFIG_HOME) {
Write-Error "Environment variable XDG_CONFIG_HOME is not set."
exit 3
}
function New-Directory {
[CmdletBinding()]
param (
[string]$Path
)
Write-Verbose "(dir) $Path"
if (Test-Path -Path $Path -PathType Leaf) {
Write-Warning "Cannot make a directory because the target exists: $Path"
}
elseif (-not (Test-Path -Path $Path -PathType Container)) {
$null = New-Item -ErrorAction $ErrorActionPreference -ItemType Directory $Path
}
}
function New-SymlinkFile {
[CmdletBinding()]
param (
[string]$LinkTarget,
[string]$Path
)
Write-Verbose "(link) $Path -> $LinkTarget"
if (-not (Test-Path -Path $Path)) {
$null = New-Item -ErrorAction $ErrorActionPreference `
-Verbose `
-ItemType SymbolicLink `
-Path $Path `
-Target $LinkTarget
}
}
function Get-LinkPathAndTarget {
[CmdletBinding()]
param (
[string]$Source,
[string]$Destination
)
return Get-ChildItem -File -Recurse $Source | ForEach-Object {
try {
Push-Location $Source
$target = $_
$path = Join-Path $Destination (Resolve-Path -Relative $_)
$path = $path.Replace("\.\", "\")
return [Tuple]::Create($target, $path)
}
finally {
Pop-Location
}
}
}
function Insert-Line($line, $file_path) {
# Ensure that the target file exists
if (!(Test-Path $file_path)) {
New-Item -ItemType File -Path $file_path -Force | Out-Null
}
# Insert a line to "source" the target file if not inserted yet
if ((Select-String -Path $file_path -SimpleMatch -Pattern $line -Quiet) -eq $false) {
Write-Host "Inserting ""$line"" into ""$file_path"""
Add-Content -Path $file_path -Value $line
}
}
# Nushell
Insert-Line "source $PSScriptRoot\nu\env.nu" $env:APPDATA\nushell\env.nu
Insert-Line "source $PSScriptRoot\nu\config.nu" $env:APPDATA\nushell\config.nu
# Git configurations
git config --global include.path $PSScriptRoot\git\config
Copy-Item "$PSScriptRoot\git\ignore" "$env:USERPROFILE\.gitignore"
# Create directories
$dirs = [System.Collections.ArrayList]::new(
@(
# VIM ------------------------------------------------------------------
"$env:USERPROFILE\_vim",
"$env:USERPROFILE\_vim\autoload",
"$env:USERPROFILE\.vim",
"$env:USERPROFILE\.vim\autoload",
"$env:USERPROFILE\vimfiles",
"$env:USERPROFILE\vimfiles\autoload",
# Neovim ---------------------------------------------------------------
"$env:XDG_CONFIG_HOME\nvim",
"$env:XDG_CONFIG_HOME\nvim\autoload"
)
)
Get-ChildItem -Recurse -Directory -Path "$PSScriptRoot\nvim" | ForEach-Object {
$path = Join-Path $env:XDG_CONFIG_HOME (Resolve-Path -Relative -Path $_)
$path = $path.Replace("\.\", "\")
$null = $dirs.Add($path)
}
$dirs | ForEach-Object { New-Directory $_ }
# Create symbolic links
$links = [ordered]@{ # <link file to create> = <link target>
# VIM
"$env:USERPROFILE\_vim\vimrc" = "$PSScriptRoot\vimfiles\vimrc";
"$env:USERPROFILE\_vim\common.vim" = "$PSScriptRoot\vimfiles\common.vim";
"$env:USERPROFILE\_vim\keymaps.vim" = "$PSScriptRoot\vimfiles\keymaps.vim";
"$env:USERPROFILE\_vim\regular.vim" = "$PSScriptRoot\vimfiles\regular.vim";
"$env:USERPROFILE\_vim\autoload\plug.vim" = "$PSScriptRoot\vimfiles\autoload\plug.vim";
"$env:USERPROFILE\.vim\vimrc" = "$PSScriptRoot\vimfiles\vimrc";
"$env:USERPROFILE\.vim\common.vim" = "$PSScriptRoot\vimfiles\common.vim";
"$env:USERPROFILE\.vim\keymaps.vim" = "$PSScriptRoot\vimfiles\keymaps.vim";
"$env:USERPROFILE\.vim\regular.vim" = "$PSScriptRoot\vimfiles\regular.vim";
"$env:USERPROFILE\.vim\autoload\plug.vim" = "$PSScriptRoot\vimfiles\autoload\plug.vim";
"$env:USERPROFILE\vimfiles\vimrc" = "$PSScriptRoot\vimfiles\vimrc";
"$env:USERPROFILE\vimfiles\common.vim" = "$PSScriptRoot\vimfiles\common.vim";
"$env:USERPROFILE\vimfiles\keymaps.vim" = "$PSScriptRoot\vimfiles\keymaps.vim";
"$env:USERPROFILE\vimfiles\regular.vim" = "$PSScriptRoot\vimfiles\regular.vim";
"$env:USERPROFILE\vimfiles\autoload\plug.vim" = "$PSScriptRoot\vimfiles\autoload\plug.vim";
# Neovim
"$env:XDG_CONFIG_HOME\nvim\vimrc" = "$PSScriptRoot\vimfiles\vimrc";
"$env:XDG_CONFIG_HOME\nvim\autoload\plug.vim" = "$PSScriptRoot\vimfiles\autoload\plug.vim";
}
(Get-LinkPathAndTarget -Source "$PSScriptRoot\nvim" -Destination "$env:XDG_CONFIG_HOME\nvim") `
| ForEach-Object {
$target = $_[0]
$path = $_[1]
$links[$path] = $target
}
$links.GetEnumerator() | ForEach-Object { New-SymlinkFile -LinkTarget $_.Value -Path $_.Key }