Files
logistics_support_tool/scripts/build.ps1
Blake Matthes 392a9ef407 refactor(build): changes to remove the build name as it was not really realvent
we will soon include it in the main file if anyone else starts to build with us
2025-07-21 17:13:24 -05:00

186 lines
4.9 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"
# tmp scripts to get lstv2 into this build and zipped up with everything
. (Join-Path $PSScriptRoot "lstV2Build.ps1")
# will run the lstv2 build first so we know for sure its copied over
Build-LstV2-And-Copy
# 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. Please make sure you have entered the correct info the env"
# 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"
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 get
go build -ldflags "-X main.version=$($version)-$($initialBuildValue)" -o lst_app.exe ./main.go
if ($LASTEXITCODE -ne 0) {
Write-Warning "app build failed!"
Pop-Location
break
}
Write-Host "app build finished successfully."
Pop-Location
Push-Location $rootDir/frontend
Write-Host "Building the frontend."
npm i
npm run build
if ($LASTEXITCODE -ne 0) {
Write-Warning "Fronend build failed!"
Pop-Location
break
}
Write-Host "Fronend build finished successfully."
Pop-Location
Write-Host "Building wrapper"
Push-Location $rootDir/LstWrapper
#remove the publish folder as we done need it
if (-not (Test-Path "publish")) {
Write-Host "The publish folder is already deleted nothing else to do"
} else {
Remove-Item -LiteralPath "publish" -Force -Recurse
}
dotnet publish -c Release -o ./publish
Pop-Location
Write-Host "Building Docs"
Push-Location $rootDir/lst-docs
npm i
npm run build
Pop-Location
try {
Update-BuildNumber
Write-Host "Zipping up the release"
npm run createZip
if ($LASTEXITCODE -ne 0) {
throw "Failed to create release zip"
}
$choice = Read-Host "Are we going to create a release? y/N"
if ($choice -eq "y" -or $choice -eq "Y") {
Write-Host "Creating release..."
# This will:
# 1. Update version in package.json
# 2. Generate changelog
# 3. Create git tag
# 4. Push to remote
# 5. Create Gitea release via our script
npm run release
# deleteing the temp folder so we always cleaned up
if ($LASTEXITCODE -ne 0) {
throw "Failed to create Gitea release"
}
}
} catch {
Write-Warning "Release process failed: $_"
# deleteing the temp folder so we always cleaned up
exit 1
}
Delete-Tmp-Folder
break