aboutsummaryrefslogtreecommitdiff
path: root/scripts/azure-pipelines/osx/Install-Prerequisites.ps1
blob: 1202025834d4a34c69b375ea4cfb808edd820d1a (plain)
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
#!pwsh
#Requires -Version 6.0

<#
.SYNOPSIS
Installs the set of prerequisites for the macOS CI hosts.

.DESCRIPTION
Install-Prerequisites.ps1 installs all of the necessary prerequisites
to run the vcpkg macOS CI in a vagrant virtual machine,
skipping all prerequisites that are already installed and of the right version.

.INPUTS
None

.OUTPUTS
None
#>
[CmdletBinding()]
Param()

Set-StrictMode -Version 2

if (-not $IsMacOS) {
    Write-Error 'This script should only be run on a macOS host'
    throw
}

Import-Module "$PSScriptRoot/Utilities.psm1"

$Installables = Get-Content "$PSScriptRoot/configuration/installables.json" | ConvertFrom-Json

$Installables.Applications | ForEach-Object {
    $VersionCommand = $_.VersionCommand
    $InstalledVersion = (& $VersionCommand[0] $VersionCommand[1..$VersionCommand.Length])
    if (-not $?) {
        Write-Host "$($_.Name) not installed; installing now"
    } else {
        $InstalledVersion = $InstalledVersion -join "`n"
        if ($InstalledVersion -match $_.VersionRegex) {
            if ($Matches.Count -ne 2) {
                Write-Error "$($_.Name) has a malformed version regex ($($_.VersionRegex)); it should have a single capture group
    (it has $($Matches.Count - 1))"
                throw
            }
            if ($Matches[1] -eq $_.Version) {
                Write-Host "$($_.Name) already installed and at the correct version ($($Matches[1]))"
                return
            } else {
                Write-Host "$($_.Name) already installed but with the incorrect version
    installed version: '$($Matches[1])'
    required version : '$($_.Version)'
upgrading now."
            }
        } else {
            Write-Warning "$($_.Name)'s version command ($($VersionCommand -join ' ')) returned a value we did not expect:
    $InstalledVersion
    expected a version matching the regex: $($_.VersionRegex)
Installing anyways."
        }
    }

    if ($null -ne (Get-Member -InputObject $_ -Name 'DmgUrl')) {
        $pathToDmg = "~/Downloads/$($_.Name).dmg"
        Get-RemoteFile -OutFile $pathToDmg -Uri $_.DmgUrl -Sha256 $_.Sha256

        hdiutil attach $pathToDmg -mountpoint /Volumes/setup-installer
        sudo installer -pkg "/Volumes/setup-installer/$($_.InstallerPath)" -target /
        hdiutil detach /Volumes/setup-installer
    } elseif ($null -ne (Get-Member -InputObject $_ -Name 'PkgUrl')) {
        $pathToPkg = "~/Downloads/$($_.Name).pkg"
        Get-RemoteFile -OutFile $pathToPkg -Uri $_.PkgUrl -Sha256 $_.Sha256

        sudo installer -pkg $pathToPkg -target /
    } else {
        Write-Error "$($_.Name) does not have an installer in the configuration file."
        throw
    }
}

$Installables.Brew | ForEach-Object {
    $installable = $_
    if ($null -eq (Get-Member -InputObject $installable -Name 'Kind')) {
        brew install $installable.Name
    } else {
        switch ($installable.Kind) {
            'cask' { brew install --cask $installable.Name }
            default {
                Write-Error "Invalid kind: $_. Expected either empty, or 'cask'."
            }
        }
    }
}
brew upgrade

$installedVagrantPlugins = @{}
vagrant plugin list --machine-readable | ForEach-Object {
    $timestamp, $target, $type, $data = $_ -split ','
    switch ($type) {
        # these are not important
        'ui' { }
        'plugin-version-constraint' { }
        'plugin-name' {
            $installedVagrantPlugins[$data] = $Null
        }
        'plugin-version' {
            $version = $data -replace '%!\(VAGRANT_COMMA\)',','
            if ($version -notmatch '^(.*), global') {
                Write-Error "Invalid version string for plugin ${target}: $version"
                throw
            }
            $installedVagrantPlugins[$target] = $Matches[1]
        }
        default {
            Write-Warning "Unknown plugin list member type $type for plugin $target"
        }
    }
}
$Installables.VagrantPlugins | ForEach-Object {
    if (-not $installedVagrantPlugins.Contains($_.Name)) {
        Write-Host "$($_.Name) not installed; installing now"
    } elseif ($installedVagrantPlugins[$_.Name] -ne $_.Version) {
        Write-Host "$($_.Name) already installed but with the incorrect version
    installed version: '$($installedVagrantPlugins[$_.Name])'
    required version:  '$($_.Version)'"
    } else {
        Write-Host "$($_.Name) already installed and at the correct version ($($_.Version))"
        return
    }

    vagrant plugin install $_.Name --plugin-version $_.Version
}