Skip to content

Commit 5a54d82

Browse files
committed
chore: add build scripts
1 parent 5e81fe7 commit 5a54d82

File tree

4 files changed

+288
-0
lines changed

4 files changed

+288
-0
lines changed

Build.ps1

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<#
2+
.SYNOPSIS
3+
Build script for the NuGet package template.
4+
5+
.DESCRIPTION
6+
This script handles various setup and build tasks for the NuGet package template.
7+
#>
8+
9+
#───────────────────────────────────────────────────────────────────────────────
10+
# Function Definitions
11+
#───────────────────────────────────────────────────────────────────────────────
12+
13+
# Writes informational messages
14+
function Write-HostInfo($message) {
15+
Write-Host $message -ForegroundColor Blue
16+
}
17+
18+
# Writes success messages
19+
function Write-HostSuccess($message) {
20+
Write-Host $message -ForegroundColor Green
21+
}
22+
23+
# Checks exit code and exits on failure
24+
function Assert-ExitCode($command) {
25+
if ($LASTEXITCODE -ne 0) {
26+
Write-Host "$command failed with exit code $LASTEXITCODE" -ForegroundColor Red
27+
exit $LASTEXITCODE
28+
}
29+
}
30+
31+
#───────────────────────────────────────────────────────────────────────────────
32+
# Script Execution
33+
#───────────────────────────────────────────────────────────────────────────────
34+
35+
# Configure git to use the existing .gitmessage template for this repo
36+
git config --local commit.template .gitmessage
37+
Write-HostSuccess 'Git commit message template configured'
38+
39+
# Clean previous build artifacts
40+
Write-Host
41+
Write-HostInfo 'Cleaning previous build artifacts...'
42+
dotnet clean --nologo
43+
Get-ChildItem -Path $PSScriptRoot -Include 'TestResults' -Directory -Recurse `
44+
| ForEach-Object { Remove-Item -Path $_.FullName -Recurse -Force }
45+
Assert-ExitCode 'Clean'
46+
47+
# Restore .NET tools
48+
Write-Host
49+
Write-HostInfo 'Restoring .NET tools...'
50+
dotnet tool restore
51+
Assert-ExitCode 'Tool Restore'
52+
53+
# Restore NuGet packages
54+
Write-Host
55+
Write-HostInfo 'Restoring NuGet packages...'
56+
dotnet restore --nologo
57+
Assert-ExitCode 'Package Restore'
58+
59+
# Build the solution
60+
Write-Host
61+
Write-HostInfo 'Building solution...'
62+
dotnet build --nologo --no-restore
63+
Assert-ExitCode 'Build'
64+
65+
Write-Host
66+
Write-HostSuccess 'Build completed successfully.'

Test.ps1

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<#
2+
.SYNOPSIS
3+
Test script for the NuGet package template.
4+
5+
.DESCRIPTION
6+
This script runs tests with code coverage and generates a report.
7+
It calls the build script first to ensure everything is built correctly.
8+
#>
9+
10+
#───────────────────────────────────────────────────────────────────────────────
11+
# Variable Definitions
12+
#───────────────────────────────────────────────────────────────────────────────
13+
14+
$TestDir = Join-Path $PSScriptRoot 'test'
15+
$ResultsDir = Join-Path $TestDir 'TestResults'
16+
17+
#───────────────────────────────────────────────────────────────────────────────
18+
# Prerequisites
19+
#───────────────────────────────────────────────────────────────────────────────
20+
21+
# Import the build script and execute it
22+
. (Join-Path $PSScriptRoot 'build.ps1')
23+
24+
#───────────────────────────────────────────────────────────────────────────────
25+
# Function Definitions
26+
#───────────────────────────────────────────────────────────────────────────────
27+
28+
# Function to display the coverage summary
29+
function Show-CoverageSummary($file) {
30+
Get-Content $file | ForEach-Object {
31+
if ($_ -Match "Line coverage:|Branch coverage:|Method coverage:") {
32+
Write-HostSuccess $_
33+
}
34+
else {
35+
Write-Host $_
36+
}
37+
}
38+
}
39+
40+
#───────────────────────────────────────────────────────────────────────────────
41+
# Script Execution
42+
#───────────────────────────────────────────────────────────────────────────────
43+
44+
# Run the tests with code coverage
45+
Write-Host
46+
Write-HostInfo 'Running tests with code coverage...'
47+
dotnet test `
48+
--nologo `
49+
--no-restore `
50+
--no-build `
51+
--collect:'XPlat Code Coverage' `
52+
--settings:"$(Join-Path $TestDir 'Test.runsettings')"
53+
Assert-ExitCode 'Tests'
54+
55+
# Generate reports from the coverage results
56+
Write-Host
57+
Write-HostInfo 'Generating coverage report...'
58+
reportgenerator `
59+
-reports:"$(Join-Path '**' 'coverage.cobertura.xml')" `
60+
-reporttypes:'HtmlInline;TextSummary' `
61+
-targetdir:"$ResultsDir"
62+
Assert-ExitCode 'Report Generation'
63+
64+
# Display coverage results in the console
65+
Write-Host
66+
Show-CoverageSummary (Join-Path $ResultsDir 'Summary.txt')
67+
Write-Host
68+
Write-HostSuccess "Tests complete. Report generated in: $ResultsDir"
69+
70+
# Open the HTML report
71+
Start-Process (Join-Path $ResultsDir 'index.html')

build.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Build script for the NuGet package template.
4+
#
5+
# This script handles various setup and build tasks for the NuGet package template.
6+
7+
#───────────────────────────────────────────────────────────────────────────────
8+
# Function Definitions
9+
#───────────────────────────────────────────────────────────────────────────────
10+
11+
# Writes informational messages
12+
echo_info() {
13+
echo -e "\e[34m$1\e[0m"
14+
}
15+
16+
# Writes success messages
17+
echo_success() {
18+
echo -e "\e[32m$1\e[0m"
19+
}
20+
21+
# Checks exit code and exits on failure
22+
assert_exit_code() {
23+
if [ $? -ne 0 ]; then
24+
echo -e "\e[31m$1 failed with exit code $?\e[0m"
25+
exit $?
26+
fi
27+
}
28+
29+
#───────────────────────────────────────────────────────────────────────────────
30+
# Script Execution
31+
#───────────────────────────────────────────────────────────────────────────────
32+
33+
# Configure git to use the existing .gitmessage template for this repo
34+
git config --local commit.template .gitmessage
35+
echo_success 'Git commit message template configured'
36+
37+
# Clean previous build artifacts
38+
echo
39+
echo_info 'Cleaning previous build artifacts...'
40+
dotnet clean --nologo
41+
find "$(dirname "$0")" -name 'TestResults' -type d -exec rm -rf {} +
42+
assert_exit_code 'Clean'
43+
44+
# Restore .NET tools
45+
echo
46+
echo_info 'Restoring .NET tools...'
47+
dotnet tool restore
48+
assert_exit_code 'Tool Restore'
49+
50+
# Restore NuGet packages
51+
echo
52+
echo_info 'Restoring NuGet packages...'
53+
dotnet restore --nologo
54+
assert_exit_code 'Package Restore'
55+
56+
# Build the solution
57+
echo
58+
echo_info 'Building solution...'
59+
dotnet build --nologo --no-restore
60+
assert_exit_code 'Build'
61+
62+
echo
63+
echo_success 'Build completed successfully.'

test.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Test script for the NuGet package template.
4+
#
5+
# This script runs tests with code coverage and generates a report.
6+
# It calls the build script first to ensure everything is built correctly.
7+
8+
#───────────────────────────────────────────────────────────────────────────────
9+
# Variable Definitions
10+
#───────────────────────────────────────────────────────────────────────────────
11+
12+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
13+
TEST_DIR="$SCRIPT_DIR/test"
14+
RESULTS_DIR="$TEST_DIR/TestResults"
15+
HTML_FILE="$RESULTS_DIR/index.html"
16+
17+
#───────────────────────────────────────────────────────────────────────────────
18+
# Prerequisites
19+
#───────────────────────────────────────────────────────────────────────────────
20+
21+
# Import the build script and execute it
22+
. "$SCRIPT_DIR/build.sh"
23+
24+
#───────────────────────────────────────────────────────────────────────────────
25+
# Function Definitions
26+
#───────────────────────────────────────────────────────────────────────────────
27+
28+
# Function to display the coverage summary
29+
show_coverage_summary() {
30+
while IFS= read -r line; do
31+
if [[ $line =~ (Line coverage:|Branch coverage:|Method coverage:) ]]; then
32+
echo_success "$line"
33+
else
34+
echo "$line"
35+
fi
36+
done < "$1"
37+
}
38+
39+
#───────────────────────────────────────────────────────────────────────────────
40+
# Script Execution
41+
#───────────────────────────────────────────────────────────────────────────────
42+
43+
# Run the tests with code coverage
44+
echo
45+
echo_info 'Running tests with code coverage...'
46+
dotnet test \
47+
--nologo \
48+
--no-restore \
49+
--no-build \
50+
--collect:'XPlat Code Coverage' \
51+
--settings:"$TEST_DIR/Test.runsettings"
52+
assert_exit_code 'Tests'
53+
54+
# Generate reports from the coverage results
55+
echo
56+
echo_info 'Generating coverage report...'
57+
reportgenerator \
58+
-reports:'**/coverage.cobertura.xml' \
59+
-reporttypes:'HtmlInline;TextSummary' \
60+
-targetdir:"$RESULTS_DIR"
61+
assert_exit_code 'Report Generation'
62+
63+
# Display coverage results in the console
64+
echo
65+
show_coverage_summary "$RESULTS_DIR/Summary.txt"
66+
echo
67+
echo_success "Tests complete. Report generated in: $RESULTS_DIR"
68+
69+
# Open the HTML report
70+
if [[ "$OSTYPE" == "darwin"* ]]; then # macOS
71+
open "$HTML_FILE"
72+
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then # Linux
73+
if command -v xdg-open > /dev/null; then
74+
xdg-open "$HTML_FILE"
75+
elif command -v gnome-open > /dev/null; then
76+
gnome-open "$HTML_FILE"
77+
fi
78+
elif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]] || [[ "$OSTYPE" == "win32" ]]; then # Windows
79+
start "$HTML_FILE"
80+
elif [[ -n "$(uname -r | grep -i microsoft)" ]]; then # Windows Subsystem for Linux (WSL)
81+
if command -v wslview > /dev/null; then
82+
wslview "$HTML_FILE"
83+
elif command -v powershell.exe > /dev/null; then
84+
powershell.exe -Command "Start-Process '$(wslpath -w "$HTML_FILE")'"
85+
elif command -v cmd.exe > /dev/null; then
86+
cmd.exe /c start "$(wslpath -w "$HTML_FILE")"
87+
fi
88+
fi

0 commit comments

Comments
 (0)