-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-and-push.ps1
More file actions
141 lines (116 loc) · 3.63 KB
/
build-and-push.ps1
File metadata and controls
141 lines (116 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
param(
[string]$Variant = "debug"
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
function Invoke-GradleBuild {
param(
[Parameter(Mandatory = $true)]
[string]$ProjectDir,
[Parameter(Mandatory = $true)]
[string]$GradleTask,
[switch]$NoDaemon
)
Push-Location $ProjectDir
try {
$gradleArgs = @("gradlew.bat")
if ($NoDaemon) {
$gradleArgs += "--no-daemon"
}
$gradleArgs += "--configuration-cache"
$gradleArgs += $GradleTask
$process = Start-Process `
-FilePath "cmd.exe" `
-ArgumentList "/c", ($gradleArgs -join " ") `
-WorkingDirectory $ProjectDir `
-NoNewWindow `
-Wait `
-PassThru
return ($process.ExitCode -eq 0)
} finally {
Pop-Location
}
}
function Stop-GradleDaemons {
param(
[Parameter(Mandatory = $true)]
[string]$ProjectDir
)
Push-Location $ProjectDir
try {
& .\gradlew.bat --stop | Out-Host
} finally {
Pop-Location
}
}
function New-BuildMirror {
param(
[Parameter(Mandatory = $true)]
[string]$SourceDir,
[Parameter(Mandatory = $true)]
[string]$Variant
)
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
$mirrorDir = Join-Path $env:TEMP "QuestDevSettings-build-$Variant-$timestamp"
New-Item -ItemType Directory -Path $mirrorDir | Out-Null
$robocopyArgs = @(
$SourceDir,
$mirrorDir,
"/MIR",
"/XD",
".git",
".gradle",
"build",
"app\build"
)
& robocopy @robocopyArgs | Out-Null
if ($LASTEXITCODE -gt 7) {
throw "Failed to create temp build mirror."
}
return $mirrorDir
}
function Resolve-AdbPath {
$sdkAdbPath = Join-Path $env:LOCALAPPDATA "Android\Sdk\platform-tools\adb.exe"
if (Test-Path $sdkAdbPath) {
return $sdkAdbPath
}
return "adb"
}
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $scriptDir
$capitalizedVariant = $Variant.Substring(0, 1).ToUpper() + $Variant.Substring(1)
$gradleTask = "assemble$capitalizedVariant"
$apkName = "app-$Variant.apk"
$workspaceApkPath = Join-Path $scriptDir "app\build\outputs\apk\$Variant\$apkName"
$buildRoot = $scriptDir
$apkPath = $workspaceApkPath
Write-Host "Running gradle task: $gradleTask"
if (-not (Invoke-GradleBuild -ProjectDir $scriptDir -GradleTask $gradleTask)) {
Write-Warning "Workspace build failed. Retrying from a clean temp copy."
Stop-GradleDaemons -ProjectDir $scriptDir
$buildRoot = New-BuildMirror -SourceDir $scriptDir -Variant $Variant
$apkPath = Join-Path $buildRoot "app\build\outputs\apk\$Variant\$apkName"
Write-Host "Running gradle task in temp copy: $buildRoot"
if (-not (Invoke-GradleBuild -ProjectDir $buildRoot -GradleTask $gradleTask -NoDaemon)) {
throw "Gradle build failed in both the workspace and temp copy (task $gradleTask)."
}
}
if (-not (Test-Path $apkPath)) {
throw "APK not found at $apkPath. Verify the variant name."
}
$adbPath = Resolve-AdbPath
Write-Host "Checking for connected devices..."
$devicesOutput = & $adbPath devices
if ($LASTEXITCODE -ne 0) {
throw "Failed to run adb devices."
}
$connectedDevices = @($devicesOutput -split "`n" | Where-Object { $_ -match "\tdevice$" })
if ($connectedDevices.Count -eq 0) {
throw "No adb device detected. Connect your headset and enable USB debugging."
}
Write-Host "Installing $apkPath"
& $adbPath install -r $apkPath
if ($LASTEXITCODE -ne 0) {
throw "adb install failed."
}
Write-Host "Build and install complete."