-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.ps1
242 lines (200 loc) · 7.93 KB
/
link.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
# vim:ft=ps1
# Source: https://github.com/SARDONYX-sard/.dots/blob/main/windows/setup/symlink.ps1
Param([switch]$f, [switch]$force, [switch]$d, [switch]$isDebug)
$force = $f.IsPresent -or $force.IsPresent
$isDebug = $d.IsPresent -or $isDebug.IsPresent
<#
.SYNOPSIS
Create Symlink format
.DESCRIPTION
Create Symlink format and give formatted data to Set-Symlink function
.EXAMPLE
# Debug mode. Output `result.txt` file not called Set-Symlink function.
pwsh symlink.ps1 -d
# Force mode.
pwsh symlink.ps1 -f
.OUTPUTS
Symlink files
#>
# Are you root?
if ($isDebug -eq $false) {
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
[Security.Principal.WindowsBuiltInRole] "Administrator")) {
Invoke-Expression "sudo $PsCommandPath $(if ($force) {"-force"})"
exit $?
}
}
# --------------------------------------------------------------------------------------------------
# Files
# --------------------------------------------------------------------------------------------------
<#
.NOTES
Specification
target(require):
1. target is the actual directory or file. Relative from `$HOME/.dots` or absolute path.
#! 2. A single string will be converted to target even if it is not specified as target.
e.g.: @{"windows/config/.bashrc" } → @{ target= "$HOME/.dots/windows/config/.bashrc"; path= "$HOME/.bashrc"; name= ".bashrc" }
which one(optional):
path(option):
1. The path is relative to $HOME/.dots.
(e.g.: "windows/config")
fullpath(option):
1. Path to derive path; if name is not specified, the last path will be used for name.
(e.g.: "$HOME")
name(option):
1. the name of the symbolic file
(e.g.: ".bashrc")
#>
$files = @(
# vim
# @{ target = "config\vim"; path = ""; name = ".vimrc" }
# neovim
@{ target = "config\vim-init"; fullpath = [IO.Path]::Combine($HOME, "AppData/Local/nvim/init.vim"); }
@{ target = "config\vim-ftdetect"; fullpath = [IO.Path]::Combine($HOME, "AppData/Local/nvim/ftdetect"); }
@{ target = "config\vim-ftplugin"; fullpath = [IO.Path]::Combine($HOME, "AppData/Local/nvim/ftplugin"); }
@{ target = "config\vim-partials"; fullpath = [IO.Path]::Combine($HOME, "AppData/Local/nvim/partials"); }
# Terminal (Alacritty)
@{ target = "config\alacritty"; fullpath = [IO.Path]::Combine($HOME, "AppData/Roaming/alacritty/alacritty.toml"); }
# $terminalPath = [IO.Path]::Combine($env:LocalAppData, "Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState/settings.json")
# $terminalPreviewPath = $terminalPath.Replace("Terminal", "TerminalPreview")
# @{ target = "config\windows-terminal"; fullpath = $terminalPath; name = "settings.json" } # mode = "copy" }
# @{ target = "config\windows-terminal-preview"; fullpath = $terminalPreviewPath; name = "settings.json" } # mode = "copy" }
# global git
@{ target = "config\gitignore"; fullpath = [IO.Path]::Combine($HOME, ".config/git/ignore"); name = "ignore" } # mode = "copy" }
@{ target = "config\git"; path = ""; name = ".gitconfig" }
# if (Test-Path navi) {
# @{ target = "config\navi-config.yml"; fullpath = "$(navi info config-path | Write-Output)"; name = "config.yml" } #manual
# }
# @{ target = "scripts/startup.py"; fullpath = [IO.Path]::Combine($env:AppData, "Microsoft\Windows\Start Menu\Programs\Startup\startup.py"); name = "startup.py" }
# msys2 HomeDir
# if (Test-Path "$HOME\scoop\apps\msys2") {
# $UserName = (Split-Path $HOME -Leaf)
# $msys2HomeDir = [IO.Path]::Combine($HOME, "scoop\apps\msys2\current\home\$UserName")
# if ($force) { Remove-Item -f $msys2HomeDir }
# @{ target = $HOME; fullpath = $msys2HomeDir; }
#
# "linux/.bashrc"
# "linux/.fihrc.fish"
# "linux/.zshrc"
# }
)
$pwshProfilePath = @($(pwsh -NoProfile -Command "`$profile"))[0]
$powerShellProfilePath = @($(powerShell -NoProfile -Command "`$profile"))[0]
# If you have a profile, add it and add it to $files.
if ( (Get-Command pwsh -ea 0) -and (pwsh -NoProfile -Command "`$profile")) {
$files += @(@{target = "system\profile_win.ps1"; fullpath = $pwshProfilePath })
# create symlink from `pwsh's modules` to `powershell's modules`
$pwshModulePath = [IO.Path]::Combine(($pwshProfilePath | Split-Path), "Modules")
$powershellModulePath = [IO.Path]::Combine(($powerShellProfilePath | Split-Path), "Modules")
$files += @(@{ target = $pwshModulePath; fullpath = $powershellModulePath; })
}
if ( (Get-Command powershell -ea 0) -and (powershell -NoProfile -Command "`$profile")) {
$files += @(@{target = "system\profile_win.ps1"; fullpath = $powerShellProfilePath })
}
# --------------------------------------------------------------------------------------------------
# Create Symbolic link
# --------------------------------------------------------------------------------------------------
function Set-Symlink {
[CmdletBinding()]
param (
[string]$FromFileOrDirPath,
[string]$ToFileOrDirPath,
[hashtable]$Hash,
[switch]$Force
)
$file = $null # Does not use ternary operators for `powershell compatibility`
if ($Hash) { $file = $Hash }
if ($file) {
$PathName = [IO.Path]::Combine($file.path, $file.name)
Write-Host ""
Write-Host "now: $($PathName)"
$fullPathName = $PathName
if (Test-Path $fullPathName) {
Write-Host "Already exists." -ForegroundColor Blue
if ($force) {
Write-Host "Recreating..." -ForegroundColor Yellow
(Get-Item $fullPathName).Delete()
}
else { return }
}
if (!(Test-Path $fullPathName)) {
if (!(Test-Path ($file.path))) {
mkdir ($file.path)
}
# Copy or SymbolicLink
if ($file.mode -eq "copy") {
Write-Host "Copying..." -ForegroundColor Green
Copy-Item $file.target -Destination $file.path -Force
}
else {
Write-Host "Making symbolic link..." -ForegroundColor Green
#? Passing whitespace as a whole object will not result in a path error.
New-Item -ItemType SymbolicLink @file | Out-Null
}
if (!$?) { Write-Error "${@file} is null. Failed to create symbolic link." }
}
return
}
Write-Host ""
Write-Host "now: $FromFileOrDirPath"
if (Test-Path $ToFileOrDirPath) {
Write-Host "Already exists." -ForegroundColor Blue
if ($force) {
Write-Host "Recreating..." -ForegroundColor Yellow
(Get-Item $fullPathName).Delete()
}
else { return }
}
if (!(Test-Path $ToFileOrDirPath)) {
if (!(Test-Path ($ToDirPath))) { mkdir ($ToDirPath) }
$name = Split-Path $ToFileOrDirPath -Leaf
$ToDirPath = Split-Path $ToFileOrDirPath
Write-Host "Making symbolic link..." -ForegroundColor Green
New-Item -Value $FromFileOrDirPath -Path $ToDirPath -Name $Name -ItemType SymbolicLink | Out-Null
if (!$?) { Write-Error "${@file} is null. Failed to create symbolic link." }
}
}
function create_symlink($files) {
if ($isDebug) {
Write-Host "Debug mode is running..." -ForegroundColor Blue
if (Test-Path result.txt) {
Write-Host "result.txt already exists. Delete it." -ForegroundColor Yellow
Remove-Item result.txt -Force
}
}
else {
Write-Host "Symbolic links mode is running..." -ForegroundColor Green
}
foreach ($file in $files) {
if ($file.GetType() -eq [string]) {
if (!$file) { continue }
$file = @{target = $file; path = "" }
}
$isDrive = (Split-Path $file.target) -match "^[A-Z]:\\"
if ($isDrive -eq $false) {
$file.target = [IO.Path]::Combine("$HOME/.dots", $file.target)
if (!$file.name) { $file.name = Split-Path $file.target -Leaf }
}
if ( !$file.path ) { $file.path = "$HOME" }
else { $file.path = [IO.Path]::Combine($HOME, $file.path) }
if (!$file.name) { $file.name = Split-Path $file.path -Leaf }
# Overwrite `path`& `name` property if `fullpath` is specified.
if ($file.fullpath) {
$file.name = Split-Path $file.fullpath -Leaf
$file.path = Split-Path $file.fullpath
$file.Remove("fullpath")
}
if ($isDebug) {
Write-Output @file "`n" | Out-File result.txt -Append # for debug
Write-Host @file "`n"
continue
}
# Make symlink
if (!(Test-Path $file.path)) {
mkdir $file.path
}
if ($Force) { Set-SymLink -Hash $file -Force }
else { Set-SymLink -Hash $file }
}
}
create_symlink $files