Has anyone had any luck storing the unity hubs new CLI stuff in a variable in powershell? I’m trying to make some build scripts but the Hub seems to do some very strange things to the displayed console buffer (it returns immediately and writes over the top of it after that)
I’m using Unity Hub 2.1.1 on Windows 10
When I type this command in powershell
& ".\Unity Hub.exe" --headless editors
Nothing shows and the cursor immediately returns and then strangely goes to the next line
When I type this command
& ".\Unity Hub.exe" -- --headless editors
The list of editors is shown, but only after the command has returned and then proceeds to write over the top of where the cursor is and beyond, often making the console unusable
I have tried assigning it to variables and such but no luck
For those wondering if there’s a way in the hub CLI to be used with a scripting language, I have managed to get my build machines using it with powershell (only on windows for now) with the following script:
[CmdletBinding()]
[OutputType([bool])]
param (
# Parameter help description
[Parameter(Mandatory=$true)]
[string]
$UnityVersion,
[Parameter(Mandatory=$true)]
[string]
$UnityChangeset,
[Parameter(Mandatory=$false)]
[string[]]
$Modules = @(),
[Parameter(Mandatory=$false)]
[string]
$UnityHubLocation = 'C:\Program Files\Unity Hub\Unity Hub.exe',
[Parameter(Mandatory=$false)]
[int]
$TimeOutMilliseconds = 3600000
)
[Diagnostics.ProcessStartInfo]$pInfo = [Diagnostics.ProcessStartInfo]::new()
$pInfo.FileName = $UnityHubLocation
$pInfo.RedirectStandardError = $false
$pInfo.RedirectStandardOutput = $false
$pInfo.UseShellExecute = $false
$pInfo.Arguments = "-- --headless install --version $UnityVersion --changeset $UnityChangeset"
foreach ($module in $modules) {
$pInfo.Arguments += " --module $module"
}
[Diagnostics.Process]$p = [Diagnostics.Process]::new()
$p.StartInfo = $pInfo
Write-Host "Waiting for Unity Hub command '$($pInfo.Arguments)'"
$started = $p.Start()
if ($started -eq $false)
{
Write-Warning "Start for Unity Hub process returned false"
}
$exited = $p.WaitForExit($TimeOutMilliseconds)
Write-Host "Finished waiting for Unity Hub command"
if ($exited -eq $false)
{
Write-Error "Unity Hub process did no exit after 1 hour, killing..."
$p.Kill()
return $false
}
return $p.ExitCode -eq 0
Hope that helps people looking for the same thing
2 Likes
tom_e_roberts:
For those wondering if there’s a way in the hub CLI to be used with a scripting language, I have managed to get my build machines using it with powershell (only on windows for now) with the following script:
[CmdletBinding()]
[OutputType([bool])]
param (
# Parameter help description
[Parameter(Mandatory=$true)]
[string]
$UnityVersion,
[Parameter(Mandatory=$true)]
[string]
$UnityChangeset,
[Parameter(Mandatory=$false)]
[string[]]
$Modules = @(),
[Parameter(Mandatory=$false)]
[string]
$UnityHubLocation = 'C:\Program Files\Unity Hub\Unity Hub.exe',
[Parameter(Mandatory=$false)]
[int]
$TimeOutMilliseconds = 3600000
)
[Diagnostics.ProcessStartInfo]$pInfo = [Diagnostics.ProcessStartInfo]::new()
$pInfo.FileName = $UnityHubLocation
$pInfo.RedirectStandardError = $false
$pInfo.RedirectStandardOutput = $false
$pInfo.UseShellExecute = $false
$pInfo.Arguments = "-- --headless install --version $UnityVersion --changeset $UnityChangeset"
foreach ($module in $modules) {
$pInfo.Arguments += " --module $module"
}
[Diagnostics.Process]$p = [Diagnostics.Process]::new()
$p.StartInfo = $pInfo
Write-Host "Waiting for Unity Hub command '$($pInfo.Arguments)'"
$started = $p.Start()
if ($started -eq $false)
{
Write-Warning "Start for Unity Hub process returned false"
}
$exited = $p.WaitForExit($TimeOutMilliseconds)
Write-Host "Finished waiting for Unity Hub command"
if ($exited -eq $false)
{
Write-Error "Unity Hub process did no exit after 1 hour, killing..."
$p.Kill()
return $false
}
return $p.ExitCode -eq 0
Hope that helps people looking for the same thing
I’ve ran into the exact same problem. Many thanks for your script, It was super helpful !
Exit code keeps coming back 1 for me whenever I run a command, even tho it seems like it finishes successfully.
Also this seems to work on MacOS and Windows fine, but Linux commands using the same functions and code paths just don’t work at all.
PowerShell:
$version = "m_EditorVersionWithRevision: 2019.1.14f1 (148b5891095a)"
$pattern = '(?<version>(?:(?<major>\d+)\.)?(?:(?<minor>\d+)\.)?(?:(?<patch>\d+[fab]\d+)\b))|((?:\((?<revision>\w+))\))'
$versonMatches = [regex]::Matches($version, $pattern)
$UnityVersion = $versonMatches[0].Groups['version'].Value.Trim()
$UnityVersionChangeSet = $versonMatches[1].Groups['revision'].Value.Trim()
if ( (-not $global:PSVersionTable.Platform) -or ($global:PSVersionTable.Platform -eq "Win32NT") ) {
$hubPath = "C:\Program Files\Unity Hub\Unity Hub.exe"
$editorRootPath = "C:\Program Files\Unity\Hub\Editor\"
$editorFileEx = "\Editor\Unity.exe"
$modules = @('windows-il2cpp', 'universal-windows-platform', 'lumin', 'webgl', 'android')
#"Unity Hub.exe" -- --headless help
#. 'C:\Program Files\Unity Hub\Unity Hub.exe' -- --headless help
function unity-hub {
$p = Start-Process -Verbose -NoNewWindow -PassThru -Wait -FilePath "$hubPath" -ArgumentList (@('--','--headless') + $args.Split(" "))
}
}
elseif ( $global:PSVersionTable.OS.Contains("Darwin") ) {
$hubPath = "/Applications/Unity Hub.app/Contents/MacOS/Unity Hub"
This file has been truncated. show original
Test Results:
https://mmoargames.visualstudio.com/devops%20build%20test/_build/results?buildId=530&view=results