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
|
# Dotfiles Management System
if (Test-Path "$HOME\.cfg" -and Test-Path "$HOME\.cfg\refs") {
# Core git wrapper with repository as work-tree
function _config {
param(
[Parameter(Mandatory=$true, ValueFromRemainingArguments=$true)]
[String[]]$Args
)
git --git-dir="$HOME\.cfg" --work-tree="$HOME\.cfg" @Args
}
# Detect OS (cross-platform, PowerShell-native)
$osPlatform = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform
if ($osPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)) {
$global:CFG_OS = "windows"
} elseif ($osPlatform([System.Runtime.InteropServices.OSPlatform]::Linux)) {
$global:CFG_OS = "linux"
} elseif ($osPlatform([System.Runtime.InteropServices.OSPlatform]::OSX)) {
$global:CFG_OS = "macos"
} else {
$global:CFG_OS = "other"
}
# Map system path to repository path
function _repo_path {
param([string]$FilePath)
# If it's an absolute path that's not in HOME, handle it specially
if (($FilePath.StartsWith("\\") -or $FilePath.Contains(":")) -and -not $FilePath.StartsWith($HOME)) {
return "$CFG_OS/" + ($FilePath -replace '^[A-Z]:\\', '' -replace '\\', '/')
}
# Check for paths that should go to the repository root
if ($FilePath -match '^(common|linux|macos|windows|profile)/.*|^README\.md$') {
return $FilePath -replace '\\', '/'
}
# Remove HOME prefix if present
if ($FilePath.StartsWith($HOME)) {
$FilePath = $FilePath.Substring($HOME.Length + 1)
}
# Default: put under OS-specific home
return "$CFG_OS/home/" + ($FilePath -replace '\\', '/')
}
# Map repository path back to system path
function _sys_path {
param([string]$RepoPath)
$osPathPattern = "$CFG_OS/"
# Handle OS-specific files that are not in the home subdirectory
if ($RepoPath.StartsWith($osPathPattern) -and $RepoPath -notmatch '/home/') {
return ($RepoPath.Substring($osPathPattern.Length) -replace '/', '\\')
}
switch -Wildcard ($RepoPath) {
"common/config/*" {
$file = $RepoPath.Substring("common/config/".Length)
switch ($CFG_OS) {
"linux" { return Join-Path ($env:XDG_CONFIG_HOME ?? "$HOME\.config") $file }
"macos" { return Join-Path "$HOME\Library\Application Support" $file }
"windows" { return Join-Path $env:LOCALAPPDATA $file }
default { return Join-Path "$HOME\.config" $file }
}
}
"common/assets/*" { return Join-Path "$HOME\.cfg" $RepoPath }
"common/*" { return Join-Path $HOME ($RepoPath.Substring("common/".Length)) }
"*/home/*" { return Join-Path $HOME ($RepoPath.Substring($RepoPath.IndexOf("home/") + 5)) }
"profile/*" { return Join-Path "$HOME\.cfg" $RepoPath }
"README.md" { return Join-Path "$HOME\.cfg" $RepoPath }
default { return Join-Path "$HOME\.cfg" $RepoPath }
}
}
# Prompts for administrator permissions if needed and runs the command
function _admin_prompt {
param(
[Parameter(Mandatory=$true, ValueFromRemainingArguments=$true)]
[String[]]$Command
)
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Warning: This action requires administrator privileges." -ForegroundColor Yellow
Start-Process powershell.exe -ArgumentList "-NoProfile", "-Command", "Set-Location '$PWD'; & $Command" -Verb RunAs
} else {
& $Command
}
}
# Main config command
function config {
param(
[string]$Command,
[string]$TargetDir = "",
[Parameter(ValueFromRemainingArguments=$true)]
[string[]]$Args
)
# Parse --target flag for add command
if ($Command -eq "add" -and $Args.Count -gt 0) {
$i = 0
while ($i -lt $Args.Count) {
if ($Args[$i] -eq "--target" -or $Args[$i] -eq "-t") {
if ($i + 1 -lt $Args.Count) {
$TargetDir = $Args[$i + 1]
$Args = $Args[0..($i-1)] + $Args[($i+2)..($Args.Count-1)]
break
} else {
Write-Host "Error: --target requires a directory argument" -ForegroundColor Red
return
}
}
$i++
}
}
switch ($Command) {
"add" {
foreach ($file in $Args) {
if (-not $TargetDir) {
$repoPath = _repo_path $file
} else {
$fileName = if ($file.Contains("\\") -or $file.Contains(":")) { Split-Path $file -Leaf } else { $file }
$repoPath = "$TargetDir/$fileName" -replace '\\', '/'
}
$fullRepoPath = Join-Path "$HOME\.cfg" $repoPath
$dir = Split-Path $fullRepoPath
if (-not (Test-Path $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null }
Copy-Item -Path $file -Destination $fullRepoPath -Recurse -Force
_config add $repoPath
Write-Host "Added: $file -> $repoPath" -ForegroundColor Green
}
}
"rm" {
$rmOpts = @()
$fileList = @()
foreach ($arg in $Args) {
if ($arg.StartsWith("-")) {
$rmOpts += $arg
} else {
$fileList += $arg
}
}
foreach ($file in $fileList) {
$repoPath = _repo_path $file
if ($rmOpts -contains "-r") {
_config rm --cached -r $repoPath
} else {
_config rm --cached $repoPath
}
Remove-Item -Path $file -Recurse:($rmOpts -contains "-r") -Force
Write-Host "Removed: $file" -ForegroundColor Yellow
}
}
"sync" {
$direction = if ($Args.Count -gt 0) { $Args[0] } else { "to-repo" }
_config ls-files | ForEach-Object {
$repoFile = $_
$sysFile = _sys_path $repoFile
$fullRepoPath = Join-Path "$HOME\.cfg" $repoFile
if ($direction -eq "to-repo") {
if ((Test-Path $sysFile) -and (Test-Path $fullRepoPath)) {
$diff = Compare-Object (Get-Content $fullRepoPath -ErrorAction SilentlyContinue) (Get-Content $sysFile -ErrorAction SilentlyContinue)
if ($diff) {
Copy-Item $sysFile $fullRepoPath -Force
Write-Host "Synced to repo: $sysFile" -ForegroundColor Cyan
}
}
} elseif ($direction -eq "from-repo") {
if ((Test-Path $fullRepoPath)) {
$diff = if (Test-Path $sysFile) { Compare-Object (Get-Content $fullRepoPath -ErrorAction SilentlyContinue) (Get-Content $sysFile -ErrorAction SilentlyContinue) } else { $true }
if ($diff) {
$destDir = Split-Path $sysFile
if (($sysFile.StartsWith('\\') -or $sysFile.Contains(':')) -and -not $sysFile.StartsWith($HOME)) {
_admin_prompt "Copy-Item '$fullRepoPath' '$sysFile' -Recurse -Force"
} else {
if (-not (Test-Path $destDir)) { New-Item -ItemType Directory -Path $destDir -Force | Out-Null }
Copy-Item $fullRepoPath $sysFile -Recurse -Force
}
Write-Host "Synced from repo: $sysFile" -ForegroundColor Cyan
}
}
}
}
}
"status" {
$autoSynced = @()
_config ls-files | ForEach-Object {
$repoFile = $_
$sysFile = _sys_path $repoFile
$fullRepoPath = Join-Path "$HOME\.cfg" $repoFile
if ((Test-Path $sysFile) -and (Test-Path $fullRepoPath)) {
$diff = Compare-Object (Get-Content $fullRepoPath -ErrorAction SilentlyContinue) (Get-Content $sysFile -ErrorAction SilentlyContinue)
if ($diff) {
Copy-Item $sysFile $fullRepoPath -Force
$autoSynced += $repoFile
}
}
}
if ($autoSynced.Count -gt 0) {
Write-Host "=== Auto-synced Files ===" -ForegroundColor Magenta
foreach ($repoFile in $autoSynced) {
Write-Host "synced: $(_sys_path $repoFile) -> $repoFile" -ForegroundColor Cyan
}
Write-Host
}
_config status
Write-Host
}
"deploy" {
_config ls-files | ForEach-Object {
$repoFile = $_
$sysFile = _sys_path $repoFile
$fullRepoPath = Join-Path "$HOME\.cfg" $repoFile
if ((Test-Path $fullRepoPath) -and $sysFile) {
$destDir = Split-Path $sysFile
if (($sysFile.StartsWith('\\') -or $sysFile.Contains(':')) -and -not $sysFile.StartsWith($HOME)) {
_admin_prompt "New-Item -ItemType Directory -Path '$destDir' -Force; Copy-Item '$fullRepoPath' '$sysFile' -Recurse -Force"
} else {
if (-not (Test-Path $destDir)) { New-Item -ItemType Directory -Path $destDir -Force | Out-Null }
Copy-Item $fullRepoPath $sysFile -Recurse -Force
}
Write-Host "Deployed: $repoFile -> $sysFile" -ForegroundColor Green
}
}
}
"checkout" {
Write-Host "Checking out dotfiles from .cfg..." -ForegroundColor Blue
_config ls-files | ForEach-Object {
$repoFile = $_
$sysFile = _sys_path $repoFile
$fullRepoPath = Join-Path "$HOME\.cfg" $repoFile
if ((Test-Path $fullRepoPath) -and $sysFile) {
$destDir = Split-Path $sysFile
if (($sysFile.StartsWith('\\') -or $sysFile.Contains(':')) -and -not $sysFile.StartsWith($HOME)) {
_admin_prompt "New-Item -ItemType Directory -Path '$destDir' -Force; Copy-Item '$fullRepoPath' '$sysFile' -Recurse -Force"
} else {
if (-not (Test-Path $destDir)) { New-Item -ItemType Directory -Path $destDir -Force | Out-Null }
Copy-Item $fullRepoPath $sysFile -Recurse -Force
}
Write-Host "Checked out: $repoFile -> $sysFile" -ForegroundColor Green
}
}
}
"backup" {
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
$backupDir = Join-Path $HOME ".dotfiles_backup\$timestamp"
Write-Host "Backing up existing dotfiles to $backupDir..." -ForegroundColor Blue
_config ls-files | ForEach-Object {
$repoFile = $_
$sysFile = _sys_path $repoFile
if (Test-Path $sysFile) {
$destDirFull = Join-Path $backupDir (Split-Path $repoFile)
if (-not (Test-Path $destDirFull)) { New-Item -ItemType Directory -Path $destDirFull -Force | Out-Null }
Copy-Item $sysFile (Join-Path $backupDir $repoFile) -Recurse -Force
}
}
Write-Host "Backup complete. To restore, copy files from $backupDir to their original locations." -ForegroundColor Green
}
default {
_config $Command @Args
}
}
}
}
# Shows navigable menu of all options when hitting Tab
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
# Autocompletion for arrow keys
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
New-Alias vi nvim.exe
|