1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-22 23:56:13 +01:00
CommandStation-EX/installer.ps1

158 lines
5.2 KiB
PowerShell
Raw Normal View History

2023-04-05 07:54:48 +02:00
<#
2023-04-04 07:48:11 +02:00
# © 2023 Peter Cole
#
# This file is part of EX-CommandStation
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# It is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
2023-04-05 07:54:48 +02:00
#>
2023-04-04 07:48:11 +02:00
2023-04-05 07:54:48 +02:00
<############################################
For script errors set ExecutionPolicy:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass
############################################>
2023-04-04 07:48:11 +02:00
2023-04-05 07:54:48 +02:00
<############################################
Optional command line parameters:
$buildDirectory - specify an existing directory rather than generating a new unique one
$version - specify an exact version to download
############################################>
2023-04-04 21:30:00 +02:00
Param(
2023-04-04 07:48:11 +02:00
[Parameter()]
2023-04-05 07:54:48 +02:00
[String]$buildDirectory,
[Parameter()]
[String]$version
2023-04-04 07:48:11 +02:00
)
2023-04-05 07:54:48 +02:00
<############################################
Define global parameters here such as known URLs etc.
############################################>
$installerVersion = "v0.0.1"
2023-04-04 21:30:00 +02:00
$gitHubAPITags = "https://api.github.com/repos/DCC-EX/CommandStation-EX/git/refs/tags"
2023-04-05 07:54:48 +02:00
$gitHubURLPrefix = "https://github.com/DCC-EX/CommandStation-EX/archive/"
2023-04-04 07:48:11 +02:00
if ((Get-WmiObject win32_operatingsystem | Select-Object osarchitecture).osarchitecture -eq "64-bit") {
2023-04-04 11:30:59 +02:00
$arduinoCLIURL = "https://downloads.arduino.cc/arduino-cli/arduino-cli_latest_Windows_64bit.zip"
$arduinoCLIZip = $env:TEMP + "\" + "arduino-cli_latest_Windows_64bit.zip"
2023-04-04 07:48:11 +02:00
} else {
2023-04-04 11:30:59 +02:00
$arduinoCLIURL = "https://downloads.arduino.cc/arduino-cli/arduino-cli_latest_Windows_32bit.zip"
$arduinoCLIZip = $env:TEMP + "\" + "arduino-cli_latest_Windows_32bit.zip"
2023-04-04 07:48:11 +02:00
}
2023-04-05 07:54:48 +02:00
$arduinoCLIDirectory = $env:TEMP + "\" + "arduino-cli_installer"
$arduinoCLI = $arduinoCLIDirectory + "\arduino-cli.exe"
<############################################
Set default action for progress indicators, warnings, and errors
############################################>
$global:ProgressPreference = "SilentlyContinue"
$global:WarningPreference = "SilentlyContinue"
$global:ErrorActionPreference = "SilentlyContinue"
<############################################
If $buildDirectory not provided, generate a new time/date stamp based directory to use
############################################>
if (!$PSBoundParameters.ContainsKey('buildDirectory')) {
$buildDate = Get-Date -Format 'yyyyMMdd-HHmmss'
$buildDirectory = $env:TEMP + "\" + $buildDate
}
$commandStationDirectory = $buildDirectory + "\CommandStation-EX"
2023-04-04 07:48:11 +02:00
2023-04-05 07:54:48 +02:00
<############################################
Write out intro message and prompt to continue
############################################>
@"
Welcome to the DCC-EX PowerShell installer for EX-CommandStation ($installerVersion)
2023-04-04 07:48:11 +02:00
2023-04-05 07:54:48 +02:00
Current installer options:
- EX-CommandStation will be built in $commandStationDirectory
- Arduino CLI will be in $arduinoCLIDirectory
"@
2023-04-04 07:48:11 +02:00
2023-04-05 07:54:48 +02:00
<############################################
Create build directory if it doesn't exist, or fail
############################################>
if (!(Test-Path -PathType Container -Path $buildDirectory)) {
try {
New-Item -ItemType Directory -Path $buildDirectory | Out-Null
}
catch {
Write-Output "Could not create build directory $buildDirectory"
Exit
}
}
2023-04-04 07:48:11 +02:00
2023-04-05 07:54:48 +02:00
<############################################
See if we have the Arduino CLI already, otherwise download and extract it
############################################>
if (!(Test-Path -PathType Leaf -Path $arduinoCLI)) {
if (!(Test-Path -PathType Container -Path $arduinoCLIDirectory)) {
try {
New-Item -ItemType Directory -Path $arduinoCLIDirectory | Out-Null
}
catch {
Write-Output "Arduino CLI does not exist and cannot create directory $arduinoCLIDirectory"
Exit
}
}
Write-Output "Downloading and extracting Arduino CLI"
try {
Invoke-WebRequest -Uri $arduinoCLIURL -OutFile $arduinoCLIZip
}
catch {
Write-Output "Failed to download Arduino CLI"
Exit
}
try {
Expand-Archive -Path $arduinoCLIZip -DestinationPath $arduinoCLIDirectory -Force
}
catch {
Write-Output "Failed to extract Arduino CLI"
}
}
2023-04-05 21:31:11 +02:00
<#
Get the list of tags
#>
try {
$tagList = Invoke-RestMethod -Uri $gitHubAPITags
}
catch {
Write-Output "Failed to obtain list of available EX-CommandStation versions"
Exit
}
<#
Get latest two Prod and Devel releases, add to hash table for selection by user
#>
foreach ($tag in $tagList | Sort-Object -Property ref -Descending) {
if ($tag.ref -Like "*Prod") {
Write-Output $tag.ref
}
}
foreach ($tag in $tagList | Sort-Object -Property ref -Descending) {
if ($tag.ref -Like "*Devel") {
Write-Output $tag.ref
}
}
2023-04-05 07:54:48 +02:00
<#
2023-04-04 11:30:59 +02:00
Write-Output "Installing using directory $buildDirectory"
2023-04-04 21:30:00 +02:00
$tagList = Invoke-RestMethod -Uri $gitHubAPITags
foreach ($tag in $tagList) {
$version = $tag.ref.split("/")[2]
2023-04-05 07:54:48 +02:00
$versionURL = $gitHubURLPrefix + $tag.ref
2023-04-04 21:30:00 +02:00
Write-Output "$version : $versionURL"
2023-04-04 11:30:59 +02:00
}
2023-04-05 07:54:48 +02:00
#>