ci(builds): added better logic to building
This commit is contained in:
172
scripts/build.ps1
Normal file
172
scripts/build.ps1
Normal file
@@ -0,0 +1,172 @@
|
||||
$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
|
||||
}
|
||||
}
|
||||
|
||||
while ($true) {
|
||||
Write-Host "Select build option:"
|
||||
Write-Host "1) Backend"
|
||||
Write-Host "2) Frontend"
|
||||
Write-Host "3) All"
|
||||
Write-Host "4) Exit"
|
||||
|
||||
$choice = Read-Host "Enter your choice (1-4)"
|
||||
|
||||
switch ($choice) {
|
||||
'1' {
|
||||
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
|
||||
npm run release:createZip
|
||||
Update-BuildNumber
|
||||
break
|
||||
}
|
||||
'2' {
|
||||
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
|
||||
npm run release:createZip
|
||||
Update-BuildNumber
|
||||
break
|
||||
}
|
||||
'3' {
|
||||
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
|
||||
npm run release:createZip
|
||||
Update-BuildNumber
|
||||
|
||||
break
|
||||
}
|
||||
'4' {
|
||||
Write-Host "Exiting script."
|
||||
exit 0
|
||||
}
|
||||
default {
|
||||
Write-Warning "Invalid choice, please enter 1, 2, 3, or 4."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user