-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.ps1
119 lines (105 loc) · 2.75 KB
/
bootstrap.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
param(
[Parameter(HelpMessage = "Dry run")]
[switch]$DryRun,
[Parameter(HelpMessage = "Show this help message and exit")]
[switch]$Help,
[Parameter(HelpMessage = "Action to perform")]
[string]$Action = $null
)
# Exit if not running on Windows
if ($ENV:OS -ne "Windows_NT" -and [System.Environment]::OSVersion.Platform -ne "Win32NT") {
Write-Host "This script is only supported on Windows"
exit 1
}
# Global variables
Get-Content .vars | ForEach-Object { Invoke-Expression "`$$_" }
$DRY_RUN = $false
if ($Help) {
Show-Help
return
}
if ($DryRun) {
$DRY_RUN = $true
}
# Functions
function Templatize {
param(
[Parameter(Mandatory = $true)]
[string]$TemplatePath
)
Get-Content "$TemplatePath" -Raw | ForEach-Object { $ExecutionContext.InvokeCommand.ExpandString($_) }
}
function Insert-Line {
param(
[Parameter(Mandatory = $true)]
[string]$Pattern,
[Parameter(Mandatory = $true)]
[string]$Line,
[Parameter(Mandatory = $true)]
[string]$Path
)
Write-Host "Inserting line '$Line' into '$Path'"
if (!$DRY_RUN) {
if ((Get-Content "$Path" | Select-String -Pattern "$Pattern" -Quiet) -eq $false) {
Write-Output "$Line" | Out-File -Append "$Path"
}
}
}
function Link-File {
param(
[Parameter(Mandatory = $true)]
[string]$Source,
[Parameter(Mandatory = $true)]
[string]$Destination
)
Write-Host "Linking $Source -> $Destination"
if (!$DRY_RUN) {
New-Item -ItemType Directory -Path (Split-Path $Destination -Parent) -Force > $null
New-Item -ItemType SymbolicLink -Path "$Destination" -Target "$Source" -Force > $null
}
}
function Install {
if ($DRY_RUN) {
Write-Host "Dry run, won't install anything"
}
else {
Write-Host "Installing dotfiles..."
}
Write-Host
Get-ChildItem "$PSScriptRoot\*\install.ps1" | ForEach-Object {
Write-Host "===== Installing '$($_.Directory.BaseName)' dotfiles..."
& $_.FullName
Write-Host
}
Write-Host "Done installing dotfiles"
}
function Show-Help {
Write-Host "Usage: bootstrap.ps1 [-DryRun] [-Help] [-Action <action>]"
Write-Host " -DryRun: Dry run"
Write-Host " -Help: Show this help message"
Write-Host " -Action: Action to perform"
Write-Host " install: Install dotfiles"
Write-Host " help: Show this help message and exit"
Get-ChildItem "$PSScriptRoot\scripts\*.ps1" | ForEach-Object { Write-Host " $(($_).BaseName)" }
}
switch ($Action) {
"install" {
Install
return
}
"help" {
Show-Help
return
}
default {
$scripts = @((Get-ChildItem "$PSScriptRoot\scripts\*.ps1").BaseName)
for ($i = 0; $i -lt $scripts.Length; $i++) {
if ($Action -eq $scripts[$i]) {
& "$PSScriptRoot\scripts\$($scripts[$i])"
return
}
}
Show-Help
exit 1
}
}