-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.ps1
executable file
·337 lines (304 loc) · 13.9 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
param (
[Parameter(Mandatory=$true)][Int32]$PSMajorVersion,
[Parameter(Mandatory=$true)][Int32]$PSMinorVersion,
[Parameter(Mandatory=$true)][Int32]$PSBuildVersion
)
# Ensure we're running on the version we expect
$ExpectedVersion = [System.Management.Automation.SemanticVersion]::new($PSMajorVersion, $PSMinorVersion, $PSBuildVersion)
If (($PSVersionTable.PSVersion).CompareTo($ExpectedVersion) -lt 0) {
Write-Error -Message "Expected version $($ExpectedVersion.ToString()), but we are running on $($PSVersionTable.PSVersion.ToString())"
exit 1
}
Function Get-MenuMultipleChoice() {
PARAM (
[string] $caption,
[string] $message,
[System.Management.Automation.Host.ChoiceDescription[]] $choices
)
PROCESS {
if ($choices.Length -eq 0) {
throw "No choices available to prompt for"
}
$modifiedChoices = $choices | ForEach-Object {
$choice = $_.PsObject.Copy()
if ($choice.Label.IndexOf("&") -ge 0) {
$hotkey = $choice.Label.Substring($choice.Label.IndexOf("&") + 1, 1).ToUpper()
} else {
$hotkey = $null
}
$label = $choice.Label.Replace("&", "")
$choice | Add-Member -NotePropertyMembers @{ HotKey = $hotkey; DisplayLabel = $label; Selected = $true } -Force
Write-Output $choice
}
Write-Host $caption -ForegroundColor White
Write-Host (New-Object string("-", $caption.Length))
if ($choices.Length -gt 1) {
Write-Host $message
Write-Host
$menuLinePosition = $host.UI.RawUi.CursorPosition
$maxDisplayLabel = ($modifiedChoices | Select-Object -ExpandProperty DisplayLabel | Measure-Object -Property Length -Maximum).Maximum
$selectionCompleted = $false
While ($selectionCompleted -eq $false) {
$host.UI.RawUI.CursorPosition = $menuLinePosition
$modifiedChoices | ForEach-Object {
if ($_.Selected) {
$selectedText = "X"
} else {
$selectedText = " "
}
$output = "`t[$selectedText] "
if ($_.HotKey) {
$output += "[$($_.HotKey)] "
}
$output += $_.DisplayLabel.PadRight($maxDisplayLabel, ' ')
if ($_.HelpMessage) {
$output += " - $($_.HelpMessage)"
}
Write-Output $output
} | Write-Host
Write-Host (New-Object string(" ", 80))
$choiceLinePosition = $host.UI.RawUI.CursorPosition
$host.UI.RawUI.CursorPosition = $choiceLinePosition
Write-Host (New-Object string(" ", 80))
Write-Host (New-Object string(" ", 80))
$host.UI.RawUI.CursorPosition = $choiceLinePosition
Write-Host "Toggle Component (No value to continue)? " -NoNewline -ForegroundColor Cyan
$userInput = Read-Host
$selectedChoice = $modifiedChoices | Where-Object { $_.HotKey -eq $userInput -or $_.DisplayLabel -eq $userInput }
if ($null -eq $selectedChoice) {
$selectionCompleted = $true
} else {
$selectedChoice.Selected = !$selectedChoice.Selected
}
}
} else {
Write-Host -NoNewline "Automatically choosing the only option of `"" -ForegroundColor Cyan
Write-Host -NoNewline $modifiedChoices[0].DisplayLabel -ForegroundColor White
Write-Host "`"" -ForegroundColor Cyan
}
Write-Host
Write-Output $modifiedChoices | Where-Object { $_.Selected -eq $true }
}
}
Function Install-Chocolatey() {
Param (
[Parameter(Mandatory = $true)][string]$install_dir
)
$CHOCO_DIR = (Join-Path $install_dir "tools" "chocolatey")
Write-Verbose "CHOCO_DIR = $CHOCO_DIR"
Write-Host "Looping through Chocolatey packages..."
if (Test-Path $CHOCO_DIR) {
Write-Verbose "Packages: $packages"
$packages = Get-ChildItem -Path (Join-Path $CHOCO_DIR "*") -Include *.psm1 | ForEach-Object {
$ImportedModule = Import-Module -Name (Get-Item $_).FullName -PassThru
$result = [PSCustomObject]@{ PackageName = Get-PackageName }
Write-Verbose "Package Name: $($result.PackageName)"
if (Test-IsInstalled) {
$result | Add-Member -NotePropertyMembers @{Status="Installed"} -PassThru
} else {
$result | Add-Member -NotePropertyMembers @{Status="NotInstalled"} -PassThru
}
$ImportedModule | Remove-Module
}
$installedPackageNames = $packages | Where-Object { $_.Status -eq "Installed" } | Select-Object -ExpandProperty PackageName
if ($installedPackageNames.Length -gt 0) {
& choco upgrade $installedPackageNames --yes
}
$notinstalledPackageNames = $packages | Where-Object { $_.Status -eq "NotInstalled" } | Select-Object -ExpandProperty PackageName
if ($notinstalledPackageNames.Length -gt 0) {
& choco install $notinstalledPackageNames --yes
}
}
}
Function Install-Bash() {
Param (
[Parameter(Mandatory = $true)][string]$install_dir
)
$SymbolicFile = (Join-Path (Resolve-Path ~) ".bashrc")
$TargetFile = (Join-Path $install_dir ".bash" "bashrc")
Write-Host "Installing $SymbolicFile ==> $TargetFile"
New-Item -ItemType SymbolicLink -Force -Path $SymbolicFile -Target $TargetFile | Out-Null
}
Function Install-Git() {
Param (
[Parameter(Mandatory = $true)][string]$install_dir
)
$SymbolicFile = (Join-Path (Resolve-Path ~) ".gitconfig")
$TargetFile = (Join-Path $install_dir ".git-config" "config")
Write-Host "Installing $SymbolicFile ==> $TargetFile"
New-Item -ItemType SymbolicLink -Force -Path $SymbolicFile -Target $TargetFile | Out-Null
$SymbolicFile = (Join-Path (Resolve-Path ~) ".git-config")
$TargetFile = (Join-Path $install_dir ".git-config")
Write-Host "Installing $SymbolicFile ==> $TargetFile"
New-Item -ItemType SymbolicLink -Force -Path $SymbolicFile -Target $TargetFile | Out-Null
}
Function Install-Powershell() {
Param (
[Parameter(Mandatory = $true)][string]$install_dir
)
$SymbolicFile = $PROFILE.CurrentUserCurrentHost
$TargetFile = (Join-Path $install_dir "powershell" "profile" "current-user-current-host.ps1")
Write-Host "Installing $SymbolicFile ==> $TargetFile"
New-Item -ItemType SymbolicLink -Force -Path $SymbolicFile -Target $TargetFile | Out-Null
}
Function Install-PowershellInstallers() {
Param (
[Parameter(Mandatory = $true)][string]$install_dir
)
$VerbosePreference = "Continue"
$POWERSHELL_DIR = (Join-Path $install_dir "tools" "powershell")
Write-Verbose "POWERSHELL_DIR = $POWERSHELL_DIR"
if (Test-Path $POWERSHELL_DIR) {
Write-Host "Executing Powershell script installers..."
Get-ChildItem -Path (Join-Path $POWERSHELL_DIR "*") -Include *.ps1 | ForEach-Object {
$FILE = (Resolve-Path $_)
Write-Host "Executing `"$FILE`""
& $FILE
}
}
}
Function Install-Tmux() {
Param (
[Parameter(Mandatory = $true)][string]$install_dir
)
$SymbolicFile = (Join-Path (Resolve-Path ~) ".tmux.conf")
$TargetFile = (Join-Path $install_dir "_tmux.conf")
Write-Host "Installing $SymbolicFile ==> $TargetFile"
New-Item -ItemType SymbolicLink -Force -Path $SymbolicFile -Target $TargetFile | Out-Null
}
Function Install-Vim() {
Param (
[Parameter(Mandatory = $true)][string]$install_dir
)
$SymbolicFile = (Join-Path (Resolve-Path ~) ".vimrc")
$TargetFile = (Join-Path $install_dir ".vim" "_vimrc")
Write-Host "Installing $SymbolicFile ==> $TargetFile"
New-Item -ItemType SymbolicLink -Force -Path $SymbolicFile -Target $TargetFile | Out-Null
$SymbolicFile = (Join-Path (Resolve-Path ~) ".gvimrc")
$TargetFile = (Join-Path $install_dir ".vim" "gvimrc")
Write-Host "Installing $SymbolicFile ==> $TargetFile"
New-Item -ItemType SymbolicLink -Force -Path $SymbolicFile -Target $TargetFile | Out-Null
$SymbolicFile = (Join-Path (Resolve-Path ~) ".vim")
$TargetFile = (Join-Path $install_dir ".vim")
Write-Host "Installing $SymbolicFile ==> $TargetFile"
New-Item -ItemType SymbolicLink -Force -Path $SymbolicFile -Target $TargetFile | Out-Null
# Windows requires the plugins to be in ~/vimfiles and not ~/.vim
# So symlink both...
$SymbolicFile = (Join-Path (Resolve-Path ~) "vimfiles")
$TargetFile = (Join-Path $install_dir ".vim")
Write-Host "Installing $SymbolicFile ==> $TargetFile"
New-Item -ItemType SymbolicLink -Force -Path $SymbolicFile -Target $TargetFile | Out-Null
Write-Host "Installing vim plugins"
vim +PlugInstall +qall
}
Function Install-GitRepo() {
Param (
[Parameter(Mandatory = $true)][string]$install_dir
)
$VerbosePreference = "Continue"
if ($null -eq $env:XDG_DATA_HOME) {
$env:XDG_DATA_HOME = (Join-Path (Resolve-Path ~) ".local" "share")
}
$GIT_REPO_DIR = (Join-Path $install_dir "tools" "git")
Write-Verbose "GIT_REPO_DIR = $GIT_REPO_DIR"
Write-Host "Starting to clone git repos..."
if (Test-Path $GIT_REPO_DIR) {
Get-ChildItem -Path (Join-Path $GIT_REPO_DIR "*") -Include *.clone | ForEach-Object {
$FILE = (Get-Item $_)
$REPO_FILE_NAME = $FILE.Basename
$CLONE_PATH = (Join-Path $env:XDG_DATA_HOME "cloned-repos" $REPO_FILE_NAME)
if (-not (Test-Path $CLONE_PATH)) {
Write-Host "Need to make the git clone directory `"$CLONE_PATH`""
New-Item -ItemType Directory -Path $CLONE_PATH | Out-Null
& git -C "$CLONE_PATH" init -q
& git -C "$CLONE_PATH" config remote.origin.url "$(Get-Content $FILE)"
& git -C "$CLONE_PATH" config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
& git -C "$CLONE_PATH" config core.autocrlf "false"
} else {
Write-Host "Updating $REPO_FILE_NAME"
}
Write-Host "git -C `"$CLONE_PATH`" fetch origin master:origin/master --tags --force"
& git -C "$CLONE_PATH" fetch origin master:origin/master --tags --force
& git -C "$CLONE_PATH" reset --hard "origin/master"
Write-Host ">>$GIT_REPO_DIR\$REPO_FILE_NAME`.install.ps1"
& (Join-Path $GIT_REPO_DIR $REPO_FILE_NAME".install.ps1")
Write-Host " ...DONE"
}
}
}
Function Is-In-Git-Repo() {
Write-Output $true
}
#==============================================================================
# Menu configuration
#==============================================================================
$Menu_Text = "Select all that you would like to install"
$Menu_Message = "Press [Enter] to continue installation"
$Option_Bash = New-Object System.Management.Automation.Host.ChoiceDescription '&Bash', 'Configuration & supporting artifacts'
$Option_Git = New-Object System.Management.Automation.Host.ChoiceDescription '&Git', 'User-level Configuration'
$Option_Powershell = New-Object System.Management.Automation.Host.ChoiceDescription '&Powershell', 'User-level Configuration'
$Option_Tmux = New-Object System.Management.Automation.Host.ChoiceDescription '&TMUX', 'Configuration'
$Option_Vim = New-Object System.Management.Automation.Host.ChoiceDescription '&Vim', 'Configuration & Plugins'
$Option_GitRepo = New-Object System.Management.Automation.Host.ChoiceDescription 'Git &Repos', 'Direct Git repositories'
$Option_PowershellInstallers = New-Object System.Management.Automation.Host.ChoiceDescription 'P&owershell Installs', 'Installations from pure Powershell scripts'
$Option_Chocolatey = New-Object System.Management.Automation.Host.ChoiceDescription '&Chocolatey', 'Chocolatey packages'
$options = [System.Management.Automation.Host.ChoiceDescription[]](
$Option_Bash,
$Option_Git,
$Option_Powershell,
$Option_Tmux,
$Option_Vim,
$Option_PowershellInstallers,
$Option_GitRepo,
$Option_Chocolatey)
$userChoices = Get-MenuMultipleChoice -caption $Menu_Text -message $Menu_Message -choices $options
# Should the user clear all the checkboxes, there's nothing else for us to do.
# So bail early
if ($userChoices.Length -eq 0) {
exit 0
}
if (Is-In-Git-Repo -eq $true) {
Write-Host "We're in a git repo, so assume this is the install directory"
#TODO: Loop through remotes and make sure one of the matches
#TODO: Find the root of the git repo
$Install_Dir = $PWD
} else {
Write-Host "Where do you want to download the repo to?"
Write-Host "The 'dot-files' directory will be created automatically"
#TODO: Prompt for directory
$Install_Dir = $PWD
}
#==============================================================================
# Perform installs
#==============================================================================
#TODO: Use $($PSVersionTable.OS) to determine
Write-Host "Install directory is `"$Install_Dir`""
switch ($($userChoices | Select-Object -ExpandProperty Label)) {
$Option_Bash.Label {
Install-Bash $Install_Dir
}
$Option_Git.Label {
Install-Git $Install_Dir
}
$Option_Tmux.Label {
Install-Tmux $Install_Dir
}
$Option_Vim.Label {
Install-Vim $Install_Dir
}
$Option_GitRepo.Label {
Install-GitRepo $Install_Dir
}
$Option_Powershell.Label {
Install-Powershell $Install_Dir
}
$Option_PowershellInstallers.Label {
Install-PowershellInstallers $Install_Dir
}
$Option_Chocolatey.Label {
Install-Chocolatey $Install_Dir
}
default {
Write-Host "I have no idea what to do with `"$_`""
}
}