<#
====================================================================================
Rust Project Build & QA Pipeline Script
------------------------------------------------------------------------------------
Author : Alessandro Maestri
Version: 1.3
Date : 2025-10-31
Compatible with: PowerShell 5.x and 7.x
------------------------------------------------------------------------------------
Description:
Automates build, formatting, linting, and testing for Rust projects.
Supports configurable targets, verbosity, and partial pipelines.
Parameters:
--release → build & test in release mode
--target <T> → specify a Rust target (e.g. x86_64-pc-windows-msvc)
--skip-tests → skip test step
--quiet → minimal output (headers + errors only)
--verbose, -vv → enable Cargo verbose output (-vv)
--no-clean → skip cargo clean before building
--only-build → run cargo build only (skip fmt, clippy, and tests)
Exit codes:
0 → all steps succeeded
1 → a step failed or threw an exception
====================================================================================
#>
param(
[switch]$Release,
[Alias('skip-tests', 'skt')]
[switch]$SkipTests,
[Alias('quiet', 'q')]
[switch]$QuietMode,
[Alias('no-clean', 'nc')]
[switch]$NoClean,
[Alias('target', 't')]
[string]$BuildTarget,
[Alias('verbose', 'vv')]
[switch]$BuildVerbose,
[Alias('only-build', 'ob')]
[switch]$OnlyBuild
)
$ErrorActionPreference = "Stop"
# -------------------------------
# FUNCTION: Invoke-Step
# -------------------------------
function Invoke-Step
{
param(
[string]$Step,
[string]$Command,
[bool]$QuietMode
)
Write-Host "====================================================" -ForegroundColor Cyan
Write-Host "Step $Step" -ForegroundColor Cyan
Write-Host "====================================================" -ForegroundColor Cyan
try
{
if ($QuietMode -and -not $BuildVerbose)
{
$output = Invoke-Expression $Command 2>&1 | Out-String
}
else
{
Invoke-Expression $Command
}
if ($LASTEXITCODE -ne 0)
{
Write-Host "❌ Step failed: $Step" -ForegroundColor Red
if ($QuietMode -and -not $BuildVerbose)
{
Write-Host "---- Command output ----" -ForegroundColor DarkGray
Write-Host $output -ForegroundColor DarkGray
Write-Host "------------------------" -ForegroundColor DarkGray
}
Write-Host "Command failed: $Command" -ForegroundColor DarkRed
exit 1
}
}
catch
{
Write-Host "❌ Exception during $Step" -ForegroundColor Red
exit 1
}
}
# -------------------------------
# MAIN EXECUTION FLOW
# -------------------------------
$buildMode = if ($Release)
{
"--release"
}
else
{
""
}
$targetOption = if ($BuildTarget)
{
"--target $BuildTarget"
}
else
{
""
}
$verboseOption = if ($BuildVerbose)
{
"-vv"
}
else
{
""
}
# Display configuration summary
Write-Host "----------------------------------------------------" -ForegroundColor DarkGray
Write-Host " Build configuration summary:" -ForegroundColor DarkGray
if ($Release)
{
Write-Host " • Mode : Release" -ForegroundColor DarkGray
}
else
{
Write-Host " • Mode : Debug" -ForegroundColor DarkGray
}
if ($BuildTarget)
{
Write-Host " • Target : $BuildTarget" -ForegroundColor DarkGray
}
if ($BuildVerbose)
{
Write-Host " • Verbose: Enabled (-vv)" -ForegroundColor DarkGray
}
if ($OnlyBuild)
{
Write-Host " • Mode : Build only (--only-build)" -ForegroundColor DarkGray
}
if ($SkipTests)
{
Write-Host " • Tests : Skipped" -ForegroundColor DarkGray
}
if ($NoClean)
{
Write-Host " • Clean : Skipped" -ForegroundColor DarkGray
}
Write-Host "----------------------------------------------------" -ForegroundColor DarkGray
Write-Host ""
# Step [0]: Clean (unless skipped)
if (-not $NoClean)
{
Invoke-Step "[0/4]: cargo clean" "cargo clean $verboseOption" $QuietMode
}
else
{
Write-Host "====================================================" -ForegroundColor Yellow
Write-Host "⚠️ Cargo clean skipped (--no-clean)" -ForegroundColor Yellow
Write-Host "===================================================="
}
# Step [1]: Build
Invoke-Step "[1/4]: cargo build" "cargo build $buildMode $targetOption $verboseOption" $QuietMode
# Short-circuit: Only build, skip the rest
if ($OnlyBuild)
{
Write-Host ""
Write-Host "====================================================" -ForegroundColor Green
Write-Host "🏗️ Build completed (--only-build mode active)." -ForegroundColor Green
Write-Host "====================================================" -ForegroundColor Green
exit 0
}
# Step [2]: Format check
Invoke-Step "[2/4]: cargo fmt --all -- --check" "cargo fmt --all -- --check" $QuietMode
# Step [3]: Linting (Clippy)
Invoke-Step "[3/4]: cargo clippy" "cargo clippy --all-targets --all-features $verboseOption -- -D warnings" $QuietMode
# Step [4]: Tests
if (-not $SkipTests)
{
Invoke-Step "[4/4]: cargo test" "cargo test --all $buildMode $targetOption $verboseOption" $QuietMode
}
else
{
Write-Host "====================================================" -ForegroundColor Yellow
Write-Host "⚠️ Tests skipped (--skip-tests)" -ForegroundColor Yellow
Write-Host "===================================================="
}
# -------------------------------
# FINAL SUCCESS MESSAGE
# -------------------------------
Write-Host ""
Write-Host "====================================================" -ForegroundColor Green
Write-Host "✅ Build OK! All steps completed successfully." -ForegroundColor Green
Write-Host "====================================================" -ForegroundColor Green
Write-Host ""
exit 0