forked from Jaykul/BoxStarter-Boxes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstall.ps1
97 lines (87 loc) · 4.75 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
<#
.SYNOPSIS
Installs all my favorite tools on Windows
#>
[CmdletBinding()]
param (
# Where to install tools, or at least, symlinks to them
$UserLocalBinFolder = $(if($IsLinux){ "/usr/local/bin" } else { Join-Path $Env:LocalAppData Programs }),
# Which WSL distro to install
$WslDistro = "ubuntu",
# Large, or Extra Large? If you don't set this you get dev-mode and insider builds of all the things
[switch]$Release
)
Set-StrictMode -off
$ErrorActionPreference = 'Stop'
Push-Location
if ($Boxstarter) {
# When this script is called via BoxStarter...
# Chocolatey should be installed (by BoxStarter)
# But we need to use it to install git and clone the repo
if (!(Get-Command choco -ErrorAction Ignore)) {
Write-Host "Bootstrapping Chocolatey"
Invoke-Expression (Invoke-RestMethod https://community.chocolatey.org/install.ps1)
# Update the environment so that it works without a restart:
$Env:ChocolateyInstall = [System.Environment]::GetEnvironmentVariable("ChocolateyInstall", "Machine")
$Env:PATH = @($Env:PATH -split "\\?;" -ne "$Env:ChocolateyInstall\bin") + "$Env:ChocolateyInstall\bin" -join ";"
# Aliases are faster than path searching
Set-Alias choco (Convert-Path "$Env:ChocolateyInstall\bin\choco.exe")
}
Import-Module (Convert-Path "$Env:ChocolateyInstall\helpers\chocolateyInstaller.psm1")
choco upgrade -y git.install --package-parameters="'/GitOnlyOnPath /WindowsTerminal /NoShellIntegration /SChannel'"
RefreshEnv # git isn't shimmed, so we need it's path
# If this script is being run via Boxstarter, we need to clone the rest of the repository
if (!$PSScriptRoot -or (Convert-Path [015]*\Install.ps1).Count -lt 3) {
$gitempdir = Join-Path ([IO.Path]::GetTempPath()) ([IO.Path]::GetRandomFileName())
New-Item -Type Directory -Path $gitempdir | Out-Null
Set-Location $gitempdir
Write-Host "Cloning BoxStarter-Boxes"
& { # Git sucks:
$fatal = @();
$outputStream = ""
$outputMessage = @()
$ErrorActionPreference, $oldErrorAction = 'Continue', $ErrorActionPreference
$Output = git clone https://github.com/Jaykul/BoxStarter-Boxes.git Boxes 2>&1
$ErrorActionPreference = $oldErrorAction
# This empty error record causes an extra iteration to flush the output
switch (@($Output) + ([System.Management.Automation.ErrorRecord]::new([Exception]::new(""), "NotAnError", "NotSpecified", $null))) {
{ $_ -is [System.Management.Automation.ErrorRecord] } {
# git writes "fatal" (Terminating), "error" (non-terminating), "warning", and verbose output to stderr.
# We output them as they come in, but we save "fatal" for the end because it ends the command
$null = $_.Exception.Message -match "^((?<stream>fatal|error|warning):)?\s*(?<message>.*)$"
$message = $Matches["message"]
$stream = if ($Matches["stream"]) { $Matches["stream"] } else { "Verbose" }
# Write-Host "$([char]27)[38;2;255;0;0m$([char]27)[48;2;255;255;255m $stream $([char]27)[38;2;255;255;255m$([char]27)[49m $message"
# If this is the same stream as the last one, then append the output
if ($outputStream -eq $stream -and $message.Length) {
$outputMessage = @($outputMessage) + $message
} else {
# Otherwise, if we've captured output, write the output and start anew
if ($outputMessage) {
if ($outputStream -eq "fatal") {
$fatal = @($fatal) + $outputMessage
} else {
. "Write-$outputStream" ($outputMessage.ForEach("Trim") -Join "`n") -ErrorAction $ErrorActionPreference -Verbose:$($VerbosePreference -notin "Ignore", "SilentlyContinue")
}
}
$outputMessage = @($message)
}
$outputStream = $stream
}
default { $_ } # Normal output just passes through
}
if ($fatal -or $LASTEXITCODE -ne 0) {
Write-Error "LASTEXITCODE: $LASTEXITCODE. Failed to clone BoxStarter-Boxes. $fatal"
exit 1
}
}
Set-Location Boxes
}
} else {
Write-Warning "This script is meant to be called from Boxstarter or used from a local copy of my BoxStarter-Boxes repository"
}
& (Convert-Path 5*\Install.ps1) @PSBoundParameters
Pop-Location
if ($gitempdir) {
Remove-Item $gitempdir -Recurse -Force -ErrorAction Continue
}