Skip to content

Instantly share code, notes, and snippets.

@arturaz
Created December 17, 2016 21:51
Show Gist options
  • Save arturaz/b16b383f0eeaee0e3492564dc6b7b6e8 to your computer and use it in GitHub Desktop.
Save arturaz/b16b383f0eeaee0e3492564dc6b7b6e8 to your computer and use it in GitHub Desktop.
# #!/bin/bash
$ErrorActionPreference = "Stop"
# Set-PSDebug -trace 2 -strict # Debugging
[string] $CMD_TEST = "test"
[string] $CMD_BUILD = "build"
if ($args.Count -lt 5) {
Write-Host
Write-Host "Usage: $($MyInvocation.MyCommand.Name) unity_version path_to_project log_file_basename unity_build_target command"
Write-Host
Write-Host "Where unity_build_target is one of:"
Write-Host " win32, win64, osx, linux, linux64, ios, android, web, webstreamed,"
Write-Host " webgl, xbox360, xboxone, ps4, psp2, wsaplayer, tizen, samsungtv"
Write-Host
Write-Host "Where command is:"
Write-Host
Write-Host " $CMD_TEST"
Write-Host " run unit tests"
Write-Host
Write-Host " $CMD_BUILD Assets/build_file.asset output_dir dist_dir"
Write-Host " build a project"
Write-Host
exit 1
}
[string] $unityVersion, $projectPathStr, $logFileBase, $unityBuildTarget, $cmd, $args = $args
[System.Management.Automation.PathInfo] $projectPath = Resolve-Path $projectPathStr
[string] $unityHomeVar = "UNITY_HOME_$($unityVersion.Replace(".", "_"))"
if (! (Test-Path $projectPath)) {
Write-Host "project path $projectPath does not exist, aborting!"
exit 2
}
else {
Write-Host "project path: $projectPath"
}
if (! (Test-Path env:$unityHomeVar)) {
Write-Host "env variable $unityHomeVar is not set, aborting!"
exit 2
}
[string] $unityHome = (Get-Item env:$unityHomeVar).Value
[string] $unity = "$unityHome/Editor/Unity.exe"
function Wait-For-File([string] $file) {
[bool] $waited = $false
while (! (Test-Path $file)) {
if (! $waited) {
Write-Host -NoNewline "File '$file' does not yet exist, waiting..."
}
Write-Host -NoNewline .
Start-Sleep -Milliseconds 100
$waited = $true
}
if ($waited) {
Write-Host
Write-Host "File '$file' appeared."
Write-Host
}
}
function rmv([string] $path) {
if (Test-Path $path) {
Write-Host "Removing $path"
Remove-Item -Force -Recurse $path
}
}
function Run-Unity([string] $log) {
rmv $log
Write-Host "Running unity (target=${unityBuildTarget}): '$args'"
[string[]] $procArgs = @(
"-batchmode", "-quit", "-projectPath", $projectPath,
"-logFile", $log, "-buildTarget", $unityBuildTarget,
$args
)
[System.Diagnostics.Process] $unityProc = `
Start-Process -PassThru $unity $procArgs
Wait-For-File $log
Write-Host
Write-Host "Tailing '$log' until Unity is complete..."
Write-Host
$job = Start-Job `
-Init ([ScriptBlock]::Create("Set-Location $pwd")) `
-ArgumentList @($log) `
-Script {
param([string] $log)
Get-Content -Wait $log | ForEach-Object {
[string] $line = $_
write-host $line
if ($line -match "Timestamps \(\d+\) and assets \(\d+\) maps out of sync.") {
# Abort if asset database is corrupted
break
}
}
}
[bool] $aborted = $false
while (! $unityProc.HasExited) {
Receive-Job $job | Write-Host
if ($job.State -eq "Completed") {
Write-Host "Unity asset database corrupted, aborting!"
Stop-Process $unityProc
$aborted = $true
}
else {
Start-Sleep -Milliseconds 100
}
}
Stop-Job $job
Remove-Job $job
if ($aborted) {
Write-Host "Cleaning up unity library"
$toRemove = Get-ChildItem "$projectPath/Library" | Where-Object {
$_.PSIsContainer -or $_.BaseName.StartsWith("assetDatabase")
}
Write-Host "Going to remove from library:"
$toRemove | ForEach-Object { Write-Host $_.FullName }
Write-Host
$toRemove | ForEach-Object { rmv $_.FullName }
Write-Host "Library cleanup complete."
Write-Host "Restarting Unity with '$cmd'"
Run-Unity $log @args
}
else {
Write-Host
$content =
if ($unityProc.ExitCode -ne 0) { "Unity exit status for '$cmd': '${unityProc.ExitCode}'" }
else { "Unity exited successfully." }
Write-Host $content
$unityProc.ExitCode
}
}
function Run-Unity-Executor([string] $cmd) {
Run-Unity `
"${logFileBase}_${cmd}.log" `
-executeMethod com.tinylabproductions.TLPGame.BuildTools.Editor.command_line_executor.BuildScriptExecutor.main `
-- $cmd $args
}
function Run-Unity-Test {
rmv "$projectPath/EditorTestResults.xml"
[int] $status = Run-Unity "${logFileBase}_test.log" -runEditorTests
if ($status -ne 0) {
$results = "EditorTestResults.xml"
Write-Host
Write-Host "########## Results ###########"
Write-Host
$content =
if (Test-Path $results) { Get-Content $results }
else { "No results file: $results" }
Write-Host $content
}
}
switch ($cmd) {
$CMD_TEST {
Run-Unity-Test
}
$CMD_BUILD {
if ($args.Length -lt 3) {
Write-Host "Bad argument count, see help!"
exit 1
}
$buildScript, $outputDir, $distDir, $args = $args
}
Default {
Write-Host "Unknown command '$cmd', aborting!"
exit 3
}
}
# if [ "$CMD" == "$CMD_TEST" ]; then
# run_unity_test
# elif [ "$CMD" == "$CMD_BUILD" ]; then
# if [ "$#" -lt 3 ]; then
# echo "Bad argument count, see help!"
# exit 1
# fi
# BUILD_SCRIPT="$1"
# output_dir="$(realpath "$2")"
# dist_dir="$(realpath "$3")"
# echo "Setting up $BUILD_SCRIPT for $UNITY_BUILD_TARGET to '$output_dir'"
# run_unity_executor setup --script="$BUILD_SCRIPT"
# run_unity_test
# run_unity_executor build --script="$BUILD_SCRIPT" --build-dir="$output_dir"
# if [ "$UNITY_BUILD_TARGET" == "android" ]; then
# cd "$output_dir"
# echo "Running gradle"
# time gradle assembleArmProdRelease assembleX86ProdRelease
# info_dir="info"
# build_name=$(cat "$info_dir/build_name.txt")
# main_dir=$(cat "$info_dir/product_name.txt")
# dist="$dist_dir/$build_name"
# mkdir -p "$dist"
# cp -fv "$main_dir/build/outputs/apk/"*-release.apk "$dist/"
# cp -fv "$info_dir/publish_info.json" "$dist/"
# fi
# else
# fi
# echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment