Files
logistics_support_tool/scripts/build.ps1
2025-07-12 16:37:45 -05:00

124 lines
3.4 KiB
PowerShell

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
$rootDir = Join-Path $scriptDir ".."
$releaseScript = Join-Path $scriptDir "release.ps1"
$packageJsonPath = Join-Path $rootDir "package.json"
# Path to BUILD_NUMBER file, assuming root is one level up from scripts
$buildNumberFile = Join-Path $scriptDir "..\BUILD_NUMBER"
$envFile = Join-Path $rootDir ".env"
if (Test-Path $envFile) {
Write-Host "Loading environment variables from $envFile"
Get-Content $envFile | ForEach-Object {
if ($_ -match "^\s*([^#][^=]*)=(.*)$") {
$name = $matches[1].Trim()
$value = $matches[2].Trim()
[System.Environment]::SetEnvironmentVariable($name, $value)
}
}
} else {
Write-Host ".env file not found at $envFile"
}
if (-not $env:BUILD_NAME) {
Write-Warning "BUILD_NAME environment variable is not set. Cannot create BUILD_NUMBER file."
exit 1
}
function Get-PackageVersion {
param (
[string]$packageJsonPath
)
if (-not (Test-Path $packageJsonPath)) {
Write-Warning "package.json not found at $packageJsonPath"
return $null
}
$jsonContent = Get-Content $packageJsonPath -Raw
try {
$json = $jsonContent | ConvertFrom-Json
return $json.version
}
catch {
Write-Warning "Failed to parse package.json: $_"
return $null
}
}
$version = Get-PackageVersion -packageJsonPath $packageJsonPath
Write-Host "Project version from package.json is: $version"
$buildNumberFile = Join-Path $rootDir "BUILD_NUMBER"
if (-not (Test-Path $buildNumberFile)) {
$initialBuildValue = "1-$($env:BUILD_NAME)"
Write-Host "BUILD_NUMBER file not found. Creating with value: $initialBuildValue"
Set-Content -Path $buildNumberFile -Value $initialBuildValue
} else {
Write-Host "BUILD_NUMBER file exists at $buildNumberFile, and will not be created"
}
function Update-BuildNumber {
if (-not (Test-Path $buildNumberFile)) {
Write-Warning "BUILD_NUMBER file not found at $buildNumberFile"
return $null
}
$current = Get-Content $buildNumberFile -Raw
$current = $current.Trim()
if ($current -match '^(\d+)-(.*)$') {
$number = [int]$matches[1]
$name = $matches[2]
$newNumber = $number + 1
$newBuildNumber = "$newNumber-$name"
Set-Content -Path $buildNumberFile -Value $newBuildNumber
Write-Host "Build number updated from $current to $newBuildNumber"
return $newBuildNumber
} else {
Write-Warning "BUILD_NUMBER file content '$current' is not in the expected 'number-name' format."
return $null
}
}
Push-Location $rootDir/backend
Write-Host "Building the app"
go build -ldflags "-X main.version=$($version)-$($initialBuildValue)" -o lst_backend.exe ./main.go
if ($LASTEXITCODE -ne 0) {
Write-Warning "Backend build failed!"
Pop-Location
break
}
Write-Host "Backend build finished successfully."
Pop-Location
Push-Location $rootDir/frontend
Write-Host "Building the frontend."
npm run build
if ($LASTEXITCODE -ne 0) {
Write-Warning "Fronend build failed!"
Pop-Location
break
}
Write-Host "Fronend build finished successfully."
Pop-Location
Update-BuildNumber
Write-Host "Zipping up the release"
npm run release:createZip
break