forked from semaphoreci/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-self-hosted-toolbox.ps1
66 lines (54 loc) · 2.23 KB
/
install-self-hosted-toolbox.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
function Install-PSModule {
param (
[string] $ModuleLocation,
[string] $ModuleName
)
Write-Output "Installing $ModuleName module in $ModuleLocation..."
if (-not (Test-Path $ModuleLocation)) {
Write-Output "No $ModuleLocation directory found. Creating it..."
New-Item -ItemType Directory -Path $ModuleLocation > $null
if (-not (Test-Path $ModuleLocation)) {
Write-Output "Error creating $ModuleLocation"
Exit 1
}
}
$ModulePath = Join-Path -Path $ModuleLocation -ChildPath $ModuleName
if (Test-Path $ModulePath) {
Write-Output "$ModuleName module directory already exists. Overriding it..."
Remove-Item -Path $ModulePath -Force -Recurse
}
Write-Output "Creating $ModuleName module directory at $ModulePath..."
New-Item -ItemType Directory -Path $ModulePath > $null
if (-not (Test-Path $ModulePath)) {
Write-Output "Error creating $ModulePath"
Exit 1
}
Write-Output "Copying .psm1 file to $ModuleName module directory..."
# The .psm1 file name needs to match the module directory name, otherwise powershell will ignore it
Copy-Item "$HOME\.toolbox\$ModuleName.psm1" -Destination "$ModulePath\$ModuleName.psm1"
if (-not $?) {
Write-Output "Error copying .psm1 module to $ModulePath"
Exit 1
}
}
# To make the binaries available,
# we include the $HOME\.toolbox\bin directory in the user's PATH.
function Install-BinaryFolder {
$toolboxPath = Join-Path $HOME ".toolbox" | Join-Path -ChildPath "bin"
Write-Output "Adding $toolboxPath to the PATH..."
$currentPaths = [Environment]::GetEnvironmentVariable('PATH', 'User') -split ';'
$updatePaths = @($currentPaths | Where-Object { $_ -ne $toolboxPath })
$updatePaths += $toolboxPath
[Environment]::SetEnvironmentVariable('PATH', ($updatePaths -join ';'), 'User')
}
$ErrorActionPreference = "Stop"
# PowerShell Core 6.0+ has $isWindows set, but older versions don't.
# For those versions, we use the $env:OS variable, which is only set in Windows.
if ($IsWindows -or $env:OS) {
$ModulePath = $Env:PSModulePath.Split(";")[0]
} else {
$ModulePath = $Env:PSModulePath.Split(":")[0]
}
Install-PSModule -ModuleLocation $ModulePath -ModuleName 'Checkout'
Install-BinaryFolder
Write-Output "Installation completed successfully."