xpipe-mirror/get-xpipe.ps1

188 lines
5 KiB
PowerShell
Raw Normal View History

2023-06-24 07:09:45 +00:00
<#
.SYNOPSIS
Downloads and installs XPipe on the local machine.
.DESCRIPTION
Retrieves the XPipe msi for the latest or a specified version, and
downloads and installs the application to the local machine.
#>
[CmdletBinding(DefaultParameterSetName = 'Default')]
param(
# Specifies a target version of XPipe to install. By default, the latest
# stable version is installed.
[Parameter(Mandatory = $false)]
[string]
2023-06-24 10:12:19 +00:00
$XPipeVersion = $xpipeVersion,
2023-06-24 07:09:45 +00:00
# If set, will download releases from the staging repository instead.
[Parameter(Mandatory = $false)]
[switch]
2023-06-24 10:12:19 +00:00
$UseStageDownloads = $useStageDownloads
2023-06-24 07:09:45 +00:00
)
#region Functions
function Get-Downloader {
<#
.SYNOPSIS
Gets a System.Net.WebClient that respects relevant proxies to be used for
downloading data.
.DESCRIPTION
Retrieves a WebClient object that is pre-configured according to specified
environment variables for any proxy and authentication for the proxy.
Proxy information may be omitted if the target URL is considered to be
bypassed by the proxy (originates from the local network.)
.PARAMETER Url
Target URL that the WebClient will be querying. This URL is not queried by
the function, it is only a reference to determine if a proxy is needed.
.EXAMPLE
Get-Downloader -Url $fileUrl
Verifies whether any proxy configuration is needed, and/or whether $fileUrl
is a URL that would need to bypass the proxy, and then outputs the
already-configured WebClient object.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]
$Url
)
$downloader = New-Object System.Net.WebClient
$defaultCreds = [System.Net.CredentialCache]::DefaultCredentials
if ($defaultCreds) {
$downloader.Credentials = $defaultCreds
}
$downloader
}
function Request-File {
<#
.SYNOPSIS
Downloads a file from a given URL.
.DESCRIPTION
Downloads a target file from a URL to the specified local path.
Any existing proxy that may be in use will be utilised.
.PARAMETER Url
URL of the file to download from the remote host.
.PARAMETER File
Local path for the file to be downloaded to.
Downloads the file to the path specified in $targetFile.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]
$Url,
[Parameter(Mandatory = $false)]
[string]
$File
)
Write-Host "Downloading $url to $file"
(Get-Downloader $url).DownloadFile($url, $file)
}
function Uninstall {
[CmdletBinding()]
param()
# Quick heuristic to see whether is can be possibly installed
2023-09-13 06:58:32 +00:00
if (-not (Test-Path "$env:LOCALAPPDATA\$ProductName" -PathType Container)) {
2023-06-24 07:09:45 +00:00
return
}
2023-09-13 06:58:32 +00:00
Write-Host "Looking for previous $ProductName installations ..."
2023-06-24 07:09:45 +00:00
2023-09-13 06:58:32 +00:00
$cim = Get-CimInstance Win32_Product | Where {$_.Name -match "$ProductName" } | Select-Object -First 1
2023-06-24 07:09:45 +00:00
if ($cim) {
$message = @(
2023-09-13 06:58:32 +00:00
"Uninstalling existing $ProductName $($cim.Version) installation ..."
2023-06-24 07:09:45 +00:00
) -join [Environment]::NewLine
Write-Host $message
2023-06-24 08:41:52 +00:00
$cimResult = Invoke-CimMethod -InputObject $cim -Name Uninstall
2023-06-24 07:09:45 +00:00
}
}
#endregion Functions
#region Pre-check
if ($UseStageDownloads) {
2023-09-13 06:36:02 +00:00
$XPipeRepoUrl = "https://github.com/xpipe-io/xpipe-ptb"
2023-09-13 06:58:32 +00:00
$ProductName = "XPipe PTB"
2023-06-24 07:09:45 +00:00
} else {
$XPipeRepoUrl = "https://github.com/xpipe-io/xpipe"
2023-09-13 06:58:32 +00:00
$ProductName = "XPipe"
2023-06-24 07:09:45 +00:00
}
if ($XPipeVersion) {
$XPipeDownloadUrl = "$XPipeRepoUrl/releases/download/$XPipeVersion"
} else {
$XPipeDownloadUrl = "$XPipeRepoUrl/releases/latest/download"
}
2023-09-13 06:58:32 +00:00
Uninstall
#endregion Pre-check
#region Setup
2023-06-24 07:09:45 +00:00
$XPipeDownloadUrl = "$XPipeDownloadUrl/xpipe-installer-windows-x86_64.msi"
if (-not $env:TEMP) {
$env:TEMP = Join-Path $env:SystemDrive -ChildPath 'temp'
}
$xpipeTempDir = Join-Path $env:TEMP -ChildPath "xpipe"
$tempDir = Join-Path $xpipeTempDir -ChildPath "install"
if (-not (Test-Path $tempDir -PathType Container)) {
$null = New-Item -Path $tempDir -ItemType Directory
}
#endregion Setup
2023-06-24 08:41:52 +00:00
#region Download
2023-06-24 07:09:45 +00:00
$file = Join-Path $tempDir "xpipe-installer.msi"
2023-09-13 06:58:32 +00:00
Write-Host "Getting $ProductName from $XPipeRepoUrl."
2023-06-24 07:09:45 +00:00
Request-File -Url $XPipeDownloadUrl -File $file
2023-06-24 08:41:52 +00:00
#endregion Download
2023-06-24 07:09:45 +00:00
#region Install XPipe
2023-09-13 06:58:32 +00:00
Write-Host "Installing $ProductName ..."
2023-06-24 07:09:45 +00:00
2023-06-24 08:41:52 +00:00
# Wait for completion
2023-06-24 09:09:08 +00:00
# The file variable can contain spaces, so we have to accommodate for that
Start-Process -FilePath "msiexec" -Wait -ArgumentList "/i", "`"$file`"", "/quiet"
2023-06-24 07:09:45 +00:00
# Update current process PATH environment variable
$env:Path=(
[System.Environment]::GetEnvironmentVariable("Path", "Machine"),
[System.Environment]::GetEnvironmentVariable("Path", "User")
) -match '.' -join ';'
2023-08-16 16:49:11 +00:00
Write-Host
2023-09-27 00:47:51 +00:00
Write-Host 'XPipe has been successfully installed. You should be able to find it in your applications.'
2023-08-16 16:49:11 +00:00
Write-Host
2023-06-24 08:41:52 +00:00
# Use absolute path as we can't assume that the user has selected to put XPipe into the Path
2023-09-13 06:58:32 +00:00
& "$env:LOCALAPPDATA\$ProductName\cli\bin\xpipe.exe" open
2023-06-24 07:09:45 +00:00
#endregion Install XPipe