SafeBite/build_apks.ps1
2026-04-25 10:26:13 -04:00

106 lines
3.1 KiB
PowerShell

param(
[switch]$Major,
[switch]$Minor,
[switch]$Patch,
[switch]$Help
)
if ($Help) {
Write-Host "Usage: .\build_apks.ps1 [-Major | -Minor | -Patch] [-Help]" -ForegroundColor White
Write-Host ""
Write-Host "Options:" -ForegroundColor White
Write-Host " -Major Increment the major version (X.0.0) and rebuild."
Write-Host " -Minor Increment the minor version (x.Y.0) and rebuild."
Write-Host " -Patch Increment the patch version (x.y.Z) and rebuild."
Write-Host " -Help Show this help message."
Write-Host ""
Write-Host "If no version switch is provided, the current version is used."
exit 0
}
$versionFile = Join-Path $PSScriptRoot "version.properties"
if (-not (Test-Path $versionFile)) {
Write-Host "version.properties not found! Creating default..." -ForegroundColor Yellow
"MAJOR=1`nMINOR=0`nPATCH=0`nCODE=1" | Out-File $versionFile -Encoding ascii
}
# Read properties
$properties = @{}
Get-Content $versionFile | ForEach-Object {
if ($_ -match "^(.*?)=(.*)$") {
$properties[$matches[1].Trim()] = $matches[2].Trim()
}
}
[int]$vMajor = $properties["MAJOR"]
[int]$vMinor = $properties["MINOR"]
[int]$vPatch = $properties["PATCH"]
[int]$vCode = $properties["CODE"]
$versionChanged = $false
if ($Major) {
$vMajor++
$vMinor = 0
$vPatch = 0
$versionChanged = $true
} elseif ($Minor) {
$vMinor++
$vPatch = 0
$versionChanged = $true
} elseif ($Patch) {
$vPatch++
$versionChanged = $true
}
if ($versionChanged) {
$vCode++
$newContent = "MAJOR=$vMajor`r`nMINOR=$vMinor`r`nPATCH=$vPatch`r`nCODE=$vCode"
Set-Content -Path $versionFile -Value $newContent -Encoding Ascii
Write-Host "Version incremented to: $vMajor.$vMinor.$vPatch (Code: $vCode)" -ForegroundColor Cyan
} else {
Write-Host "Current version: $vMajor.$vMinor.$vPatch (Code: $vCode)" -ForegroundColor Cyan
}
Write-Host "`n--- Cleaning and Building ---" -ForegroundColor Green
# Use cmd /c to ensure gradlew runs correctly from PowerShell if it's not in the PATH
$gradlew = ".\gradlew.bat"
if (-not (Test-Path $gradlew)) {
$gradlew = ".\gradlew"
if (-not (Test-Path $gradlew)) {
Write-Host "gradlew not found!" -ForegroundColor Red
exit 1
}
}
Write-Host "Task: :app:assembleDebug" -ForegroundColor Gray
if ($gradlew.EndsWith(".bat")) {
& cmd.exe /c $gradlew :app:assembleDebug
} else {
& $gradlew :app:assembleDebug
}
if ($LASTEXITCODE -ne 0) {
Write-Host "Debug build failed!" -ForegroundColor Red
exit $LASTEXITCODE
}
Write-Host "`nTask: :app:assembleRelease" -ForegroundColor Gray
if ($gradlew.EndsWith(".bat")) {
& cmd.exe /c $gradlew :app:assembleRelease
} else {
& $gradlew :app:assembleRelease
}
if ($LASTEXITCODE -ne 0) {
Write-Host "Release build failed!" -ForegroundColor Red
exit $LASTEXITCODE
}
Write-Host "`n--- Build Successful ---" -ForegroundColor Green
Write-Host "APKs available at:"
Write-Host " Debug: app\build\outputs\apk\debug\app-debug.apk" -ForegroundColor Yellow
Write-Host " Release: app\build\outputs\apk\release\app-release.apk" -ForegroundColor Yellow