Files
Reims-Go/plugins/usb-redir/build.ps1
T

86 lines
3.5 KiB
PowerShell

#Requires -Version 7.0
# SPDX-License-Identifier: Apache-2.0
# Packaging adapted from mango6i/reims-go-usb-passthrough v0.1.0-beta.6.
[CmdletBinding()]
param(
[Parameter(Mandatory)] [string]$ReimsGoExe,
[Parameter(Mandatory)] [string]$OutputPath
)
$ErrorActionPreference = 'Stop'
$repo = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
$reims = (Get-Item -LiteralPath $ReimsGoExe).FullName
$output = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutputPath)
if (Test-Path -LiteralPath $output) { throw 'The output ZIP already exists.' }
function Invoke-ReimsCli([string[]]$CliArguments) {
$start = [Diagnostics.ProcessStartInfo]::new($reims)
$start.UseShellExecute = $false
$start.CreateNoWindow = $true
$start.RedirectStandardOutput = $true
$start.RedirectStandardError = $true
foreach ($argument in $CliArguments) { $start.ArgumentList.Add($argument) }
$proc = [Diagnostics.Process]::Start($start)
try {
$stdout = $proc.StandardOutput.ReadToEndAsync()
$stderr = $proc.StandardError.ReadToEndAsync()
if (!$proc.WaitForExit(75000)) {
$proc.Kill($true)
throw 'The Reims-Go CLI timed out.'
}
$outText = $stdout.GetAwaiter().GetResult().Trim()
$errText = $stderr.GetAwaiter().GetResult().Trim()
if ($outText) { Write-Host $outText }
if ($proc.ExitCode -ne 0) { throw "Reims-Go CLI failed ($($proc.ExitCode)). $errText" }
if ($errText) { Write-Host $errText }
} finally { $proc.Dispose() }
}
$tmp = Join-Path $repo 'scripts/debug/tmp.mjs'
$stage = & node $tmp new usb-redir-plugin
if ($LASTEXITCODE -ne 0 -or !$stage) { throw 'Could not create a plugin build session.' }
$options = @{
GOOS = 'windows'
GOARCH = 'amd64'
CGO_ENABLED = '0'
GOMAXPROCS = '2'
GOTMPDIR = $stage
REIMS_GO_DATA_ROOT = (Join-Path $stage 'data-root')
REIMS_GO_LOCATION_FILE = (Join-Path $stage 'location.json')
TEMP = $stage
TMP = $stage
}
$previous = @{}
$finished = $false
Push-Location $repo
try {
foreach ($name in $options.Keys) {
$previous[$name] = [Environment]::GetEnvironmentVariable($name, 'Process')
[Environment]::SetEnvironmentVariable($name, $options[$name], 'Process')
}
$package = (New-Item -ItemType Directory -Path (Join-Path $stage 'package')).FullName
$panel = (New-Item -ItemType Directory -Path (Join-Path $package 'panel')).FullName
Copy-Item -LiteralPath (Join-Path $PSScriptRoot 'plugin.json'), (Join-Path $PSScriptRoot 'README.md'), (Join-Path $repo 'LICENSE') -Destination $package
$page = Join-Path $PSScriptRoot 'panel/usb.html'
Copy-Item -LiteralPath $page -Destination $panel
# Support hosts that serve the panel entry relative to the package root.
Copy-Item -LiteralPath $page -Destination $package
& go build -p 2 -trimpath '-ldflags=-s -w' -o (Join-Path $package 'reims-usb-redir.exe') ./plugins/usb-redir
if ($LASTEXITCODE -ne 0) { throw 'USB sidecar build failed.' }
Invoke-ReimsCli @('plugin', 'pack', '--out', $output, $package)
Invoke-ReimsCli @('plugin', 'doctor', '--json', $output)
$finished = $true
} finally {
Pop-Location
foreach ($name in $previous.Keys) {
[Environment]::SetEnvironmentVariable($name, $previous[$name], 'Process')
}
if ($finished) {
& node $tmp close $stage
if ($LASTEXITCODE -ne 0) { Write-Warning "Could not close the build session $stage" }
} else {
Write-Host "Build files: $stage"
}
}