-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.ps1
111 lines (95 loc) · 2.69 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
param (
[switch]
$whatIf
)
$config = Get-Content ./overrides.json | ConvertFrom-Json -AsHashtable
function log($msg) {
Write-Host "💡 $msg"
}
function Resolve-PathSafe($path) {
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
}
function defaultPath($pathName) {
[PSObject] @{
Link = Resolve-PathSafe "~/.$($pathName)"
Target = Resolve-PathSafe $pathName
}
}
function checkConfig($os, $pathName) {
if ($config[$os].length -ge 0) {
if ($config[$os][$pathName] -ne $null) {
$config[$os][$pathName] | Foreach-Object {
[PSObject] @{
Link = Resolve-PathSafe ([System.Environment]::ExpandEnvironmentVariables($_))
Target = Resolve-PathSafe $pathName
}
}
}
}
else {
defaultPath $pathName
}
}
function determinePath($path) {
log "Checking $path..."
$pathName = $path.Name
if ($config['windows'][$pathName] -ne $null -and $IsWindows) {
checkConfig -os 'windows' -pathName $pathName
}
elseif ($config['macos'][$pathName] -ne $null -and $IsMacOS) {
checkConfig -os 'macos' -pathName $pathName
}
elseif ($config['linux'][$pathName] -ne $null -and $IsLinux) {
checkConfig -os 'linux' -pathName $pathName
}
else {
defaultPath $pathName
}
}
function linkFile($map) {
if (Test-Path $map.Link) {
log "Skipping $($map.Link) as it already exists"
return
}
# validate that $map.Target's directory exists and create it if it doesn't
$targetDirectory = Split-Path $map.Target
if (-not (Test-Path $targetDirectory)) {
if ($whatIf) {
log "Creating directory $targetDirectory"
}
else {
New-Item -ItemType Directory -Path $targetDirectory
}
}
if ($whatIf) {
log "Linking $($map.Link) to $($map.Target)"
}
else {
if ((Get-Item $map.Target) -is [System.IO.DirectoryInfo] -and $IsWindows) {
New-Item -Path $_.Link -ItemType Junction -Value $_.Target
}
else {
New-Item -Path $_.Link -ItemType SymbolicLink -Value $_.Target
}
}
}
function main {
Get-ChildItem | Foreach-Object {
$file = $_
if ($config.skip_processing -contains $file.Name) { return }
if ($file.Name.StartsWith('.')) { return }
determinePath $file | Foreach-Object {
if ((Test-Path $_.Target) -and (Get-Item $file).LinkType -ne $null) {
return
}
linkFile $_
}
}
}
try {
Push-Location $PSScriptRoot
main
}
finally {
Pop-Location
}