-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdot.ps1
More file actions
70 lines (63 loc) · 2.3 KB
/
dot.ps1
File metadata and controls
70 lines (63 loc) · 2.3 KB
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
#
# nullxception's Dotfiles install script
#
# only support reading from .install file :
#
# module_target_win32=path/to/target
# module_target=path/to/target
#
# for convenience, module_target_win32 can be set to "inherit"
# to use module_target instead. This is useful when the target path
# is same on both win32 and linux.
#
# usage:
# ./dot.ps1 <module>
#
$ModuleData = ".install"
function Resolve-ModuleTarget {
$arg = $args[0]
try {
$str = Invoke-Expression -Command "`"$arg`""
return $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($str)
} catch {
return Invoke-Expression -Command "$arg"
}
}
function Install-Mod($ModulePath) {
$ModuleName = Split-Path $ModulePath -Leaf
$ModuleRPath = Resolve-Path $ModulePath\$ModuleData -ErrorAction SilentlyContinue
if (!(Test-Path -Path $ModuleRPath -PathType Leaf)) {
Return "Module $ModuleName does not exists or has no $ModuleData"
}
$InstallContent = Get-Content $ModuleRPath
# $ModuleRPath file contains bash variable `module_target_win32` that act as target path
# e.g. module_target_win32="$env:LOCALAPPDATA\nvim"
# so we need to evaluate it in PowerShell somehow
$target_win32 = $InstallContent | ForEach-Object {
if ($_ -match '^\s*module_target_win32\s*=\s*"(.*)"\s*$') {
if ($Matches[1] -eq "inherit") {
return "inherit"
}
return Resolve-ModuleTarget $Matches[1]
}
}
$target_def = $InstallContent | ForEach-Object {
if ($_ -match '^\s*module_target\s*=\s*"(.*)"\s*$') {
return Resolve-ModuleTarget $Matches[1]
}
}
if ($target_win32 -and $target_win32 -ne "inherit") {
$module_target = $target_win32
} elseif ($target_win32 -eq "inherit" -and $target_def) {
$module_target = $target_def
} else {
Return "Module $ModuleName has no valid module_target_win32"
}
Write-Output "installing module $ModuleName to $module_target"
if (!(Test-Path -Path $module_target -PathType Container)) {
New-Item -ItemType Directory -Path $module_target
}
Get-ChildItem -Path $ModulePath -Exclude $ModuleData,.install,README.md |
Copy-Item -Destination $module_target -Recurse -Force
}
$args | ForEach-Object { Install-Mod $_ }