From a9ca8028733c8162f5d1ec25478efff06d91e7ee Mon Sep 17 00:00:00 2001 From: "Frank R. Haugen" Date: Tue, 8 Aug 2023 21:52:46 +0200 Subject: [PATCH] "Refactor build script for customizable build version and cleanup" This commit provides refactoring to the PowerShell build script. The script now accepts optional version parameters for the build, enabling customizable versioning for the final build output. A "RemoveProjectsFromSolution" function was introduced to streamline the removal of necessary projects, removing redundancy and enhancing readability. Also, the output messages from dotnet build, publish, and pack are piped to null to clean up console output during the build. --- src/build.ps1 | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/build.ps1 b/src/build.ps1 index 2086f795..bb7ffdbe 100644 --- a/src/build.ps1 +++ b/src/build.ps1 @@ -1,11 +1,25 @@ param ( [Parameter(Mandatory=$false)] [ValidateSet('Debug', 'Release')] - [string]$configuration = 'Debug' + [string]$configuration = 'Debug', + [Parameter(Mandatory=$false)] + [string]$version = (Get-Date -Format 'yyyy.MM.dd'), + [Parameter(Mandatory=$false)] + [string]$build = (Get-Date -Format 'HHmmss'), + [Parameter(Mandatory=$false)] + [string]$versionSuffix = '' ) -# Get the current date and time -$date = Get-Date -Format 'yyyy.MM.dd.HHmmss' +function RemoveProjectsFromSolution ($solutionPath, $projectsToRemove) { + Write-Host "Removing projects from solution:" -ForegroundColor DarkCyan + foreach ($project in $projectsToRemove) { + Write-Host " $($project.Name)" -ForegroundColor DarkCyan + dotnet sln $solutionPath remove $project.FullName | Out-Null + } +} + +$version = "$version.$build" + $publishDir = "./.artifacts/publish" $packageDir = "./.artifacts/packages" $tempSln = "./temp.sln" @@ -18,27 +32,25 @@ Copy-Item $solution -Destination $tempSln $testProjects = Get-ChildItem -Path . -Filter *.csproj -Recurse | Where-Object {$_.FullName.Contains(".Tests") -or $_.FullName.Contains("TestingInfrastructure")} # Remove all test projects from the temporary solution -foreach ($project in $testProjects) { - Write-Host "Removing $($project.FullName) from the solution..." -ForegroundColor DarkCyan - dotnet sln $tempSln remove $project.FullName -} +RemoveProjectsFromSolution -solutionPath $tempSln -projectsToRemove $testProjects # Clean output directories if (Test-Path $publishDir) { Remove-Item "$publishDir/*" -Recurse } if (Test-Path $packageDir) { Remove-Item "$packageDir/*" -Recurse } # Build the solution in the specified mode -Write-Host "Building solution in $configuration mode with version $date..." -ForegroundColor DarkCyan -dotnet build $tempSln --configuration $configuration /p:Version=$date +Write-Host "Building solution in $configuration mode with version $version..." -ForegroundColor DarkCyan +dotnet build $tempSln --configuration $configuration /p:Version=$version | Out-Null Write-Host "Build in $configuration mode completed." -ForegroundColor Green # Publish the solution and pack nugets Write-Host "Publishing the solution..." -ForegroundColor DarkCyan -dotnet publish $tempSln --configuration $configuration --output $publishDir /p:Version=$date +dotnet publish $tempSln --configuration $configuration --output $publishDir /p:Version=$version /p:PackageVersion=$version$versionSuffix | Out-Null Write-Host "Solution has been published." -ForegroundColor Green +# Pack NuGet packages Write-Host "Packing NuGet packages..." -ForegroundColor DarkCyan -dotnet pack $tempSln --configuration $configuration --output $packageDir /p:Version=$date +dotnet pack $tempSln --configuration $configuration --output $packageDir /p:Version=$version /p:PackageVersion=$version$versionSuffix | Out-Null Write-Host "NuGet packages have been packed." -ForegroundColor Green # Delete the temporary solution file