From f51298ff087df1d74111a7723e453724a565078b Mon Sep 17 00:00:00 2001 From: Hyper <34012267+HyperBE32@users.noreply.github.com> Date: Mon, 7 Mar 2022 00:38:21 +0000 Subject: [PATCH] [Build/CI] Integrate version patcher into build script Ditched the shell script version in favour of using PowerShell - definitely not rewriting the version patcher just for the sake of it. --- .gitattributes | 3 +- .github/workflows/Marathon.yml | 10 +- .github/workflows/VersionPatcher.ps1 | 15 ++- .profiles | 4 - Build.ps1 | 68 ++++++++++---- Build.sh | 134 --------------------------- 6 files changed, 63 insertions(+), 171 deletions(-) delete mode 100644 .profiles delete mode 100644 Build.sh diff --git a/.gitattributes b/.gitattributes index a0665c8c..cc864add 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -*.ps1 linguist-vendored -*.sh linguist-vendored \ No newline at end of file +*.ps1 linguist-vendored \ No newline at end of file diff --git a/.github/workflows/Marathon.yml b/.github/workflows/Marathon.yml index 944ebc4c..363187ba 100644 --- a/.github/workflows/Marathon.yml +++ b/.github/workflows/Marathon.yml @@ -52,17 +52,11 @@ jobs: run: | $version = "${{env.VERSION}}." + (45 + ${{github.run_number}}) echo "VERSION_RESOLVE=$version" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append - - # Patches the assembly information stored in 'Marathon.csproj' and 'Marathon.CLI.csproj' to use the current workflow version. - - name: Patch Version Information - run: | - ./.github/workflows/VersionPatcher.ps1 -ProjectPath "${{env.MARATHON_PROJECT}}" -Version ${{env.VERSION_RESOLVE}} -CommitID ${{github.sha}} - ./.github/workflows/VersionPatcher.ps1 -ProjectPath "${{env.MARATHON_CLI_PROJECT}}" -Version ${{env.VERSION_RESOLVE}} -CommitID ${{github.sha}} - + # Builds Marathon using the PowerShell script. - name: Build Marathon working-directory: ${{github.workspace}} - run: ./Build.ps1 -BuildAll -Clean -Configuration ${{matrix.configuration}} + run: ./Build.ps1 -BuildAll -Clean -CommitID ${{github.sha}} -Configuration ${{matrix.configuration}} -Version ${{env.VERSION_RESOLVE}} # Uploads the compiled Marathon artifacts. - name: Upload Marathon Artifacts diff --git a/.github/workflows/VersionPatcher.ps1 b/.github/workflows/VersionPatcher.ps1 index d46dabaf..5b4549d6 100644 --- a/.github/workflows/VersionPatcher.ps1 +++ b/.github/workflows/VersionPatcher.ps1 @@ -1,9 +1,9 @@ param ( - [String]$ProjectPath, - [String]$Version, [String]$CommitID, - [Switch]$UseFullCommitID + [String]$ProjectPath, + [Switch]$UseFullCommitID, + [String]$Version ) $project = [System.IO.File]::ReadAllLines($ProjectPath) @@ -24,7 +24,14 @@ foreach ($line in $project) } elseif ($lineSplit[0].Contains("", $lineSplit) diff --git a/.profiles b/.profiles deleted file mode 100644 index cfafb992..00000000 --- a/.profiles +++ /dev/null @@ -1,4 +0,0 @@ -win-x86 -win-x64 -linux-x64 -osx-x64 \ No newline at end of file diff --git a/Build.ps1 b/Build.ps1 index b623d3ff..40b28991 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -1,13 +1,17 @@ param ( - [Alias("a")][Switch]$BuildAll, - [Alias("c")][String]$Configuration = "Release", - [Alias("d")][Switch]$Clean, - [Alias("h")][Switch]$Help, - [Alias("p")][String]$Profile + [Switch]$BuildAll, + [String]$CommitID, + [String]$Configuration = "Release", + [Switch]$Clean, + [Switch]$Help, + [String]$Profile, + [Switch]$UseFullCommitID, + [String]$Version ) -$profileData = ".profiles" +$profiles = @("win-x86", "win-x64", "linux-x64", "osx-x64") +$versionPatcher = ".github\workflows\VersionPatcher.ps1" if ($Help) { @@ -16,11 +20,14 @@ if ($Help) echo "All your platforms are belong to us." echo "" echo "Usage:" - echo "-a|-BuildAll - build Marathon with all available profiles." - echo "-c|-Configuration [name] - build Marathon with a specific configuration." - echo "-d|-Clean - cleans the solution before building Marathon." - echo "-h|-Help - display help." - echo "-p|-Profile [name] - build Marathon with a specific profile." + echo "-BuildAll - build Marathon with all available profiles." + echo "-CommitID [id] - set the commit ID to use from GitHub for the version number." + echo "-Configuration [name] - build Marathon with a specific configuration." + echo "-Clean - cleans the solution before building Marathon." + echo "-Help - display help." + echo "-Profile [name] - build Marathon with a specific profile." + echo "-UseFullCommitID - use the full 40 character commit ID for the version number." + echo "-Version [major].[minor].[revision] - set the version number for this build of Marathon." exit } @@ -37,15 +44,38 @@ if ($Clean) dotnet clean } +function PatchVersionInformation([String]$commitID, [Boolean]$useFullCommitID, [String]$version) +{ + # Patch the version number for all projects. + if (![System.String]::IsNullOrEmpty($version)) + { + foreach ($project in [System.IO.Directory]::EnumerateFiles(".", "*.csproj", [System.IO.SearchOption]::AllDirectories)) + { + & "${versionPatcher}" -CommitID $commitID -ProjectPath "${project}" -Version $version + } + } +} + +function Build([String]$configuration, [String]$profile) +{ + # Patch version number before building. + PatchVersionInformation $CommitID $UseFullCommitID $Version + + dotnet publish /p:Configuration="${configuration}" /p:PublishProfile="${profile}" + + # Restore default version number. + PatchVersionInformation "" $false "1.0.0" +} + if (![System.String]::IsNullOrEmpty($Profile)) { - if ([System.Array]::IndexOf([System.IO.File]::ReadAllLines($profileData), $Profile) -eq -1) + if ([System.Array]::IndexOf($profiles, $Profile) -eq -1) { - echo "No such profile exists: $Profile" + echo "No such profile exists: ${Profile}" exit } - dotnet publish /p:Configuration="$Configuration" /p:PublishProfile="$Profile" + Build $Configuration $Profile if (!$BuildAll) { @@ -55,19 +85,19 @@ if (![System.String]::IsNullOrEmpty($Profile)) if ($BuildAll) { - foreach ($line in [System.IO.File]::ReadAllLines($profileData)) + foreach ($currentProfile in $profiles) { # Skip profile if it was already specified and built. - if ($line -eq $Profile) + if ($currentProfile -eq $Profile) { continue } - dotnet publish /p:Configuration="$Configuration" /p:PublishProfile="$line" + Build $Configuration $currentProfile } } else { - dotnet publish /p:Configuration="$Configuration" /p:PublishProfile="win-x86" - dotnet publish /p:Configuration="$Configuration" /p:PublishProfile="win-x64" + Build $Configuration "win-x86" + Build $Configuration "win-x64" } \ No newline at end of file diff --git a/Build.sh b/Build.sh deleted file mode 100644 index ec80ed55..00000000 --- a/Build.sh +++ /dev/null @@ -1,134 +0,0 @@ -#!/bin/bash - -PROFILE_DATA=".profiles" -CONFIG="Release" - -if [ $# -ne 0 ] -then - while getopts ac:dhp: option - do - case $option in - a) - BUILD_ALL=1 - ;; - c) - CONFIG="${OPTARG}" - ;; - d) - CLEAN=1 - ;; - h) - echo "Marathon Build Script" - echo "" - echo "All your platforms are belong to us." - echo "" - echo "Usage:" - echo "-a - build Marathon with all available profiles." - echo "-c [name] - build Marathon with a specific configuration." - echo "-d - cleans the solution before building Marathon." - echo "-h - display help." - echo "-p [name] - build Marathon with a specific profile." - exit - ;; - p) - PROFILE="${OPTARG}" - ;; - esac - done -fi - -if [[ "${OSTYPE}" == "cygwin" ]] || [[ "${OSTYPE}" == "msys" ]] || [[ "${OSTYPE}" == "win32" ]] || [[ $(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip') ]] -then - PLATFORM="Linux" - IS_WINDOWS=1 -elif [ "${OSTYPE}" == "linux-gnu"* ] -then - PLATFORM="Linux" -elif [ "${OSTYPE}" == "darwin"* ] -then - PLATFORM="macOS" -fi - -# Check if the .NET SDK is installed. -if ! [ -x "$(command -v dotnet)" ] -then - echo ".NET SDK is required to build Marathon." - - case "${PLATFORM}" in - "Linux") - echo "See the official .NET documentation on installing the SDK for Linux here: https://docs.microsoft.com/dotnet/core/install/linux" - ;; - "macOS") - echo "You can install the required .NET SDK for macOS from here: https://dotnet.microsoft.com/download/dotnet/thank-you/sdk-6.0.200-macos-x64-installer" - ;; - esac - - if [ IS_WINDOWS ] - then - echo "Please use the provided PowerShell script to build for Windows." - fi - - exit -fi - -if [ $CLEAN ] -then - dotnet clean -fi - -if [ -n "${PROFILE}" ] -then - PROFILE_COUNT=$(wc -l < ${PROFILE_DATA}) - - for (( i = 1; i <= $PROFILE_COUNT + 1; i++ )) - do - PROFILE_CURRENT=$(cut -f1 "${PROFILE_DATA}" | sed "${i}q;d") - - # Skip profile if it was already specified and built. - if [[ $PROFILE_CURRENT == $PROFILE ]] - then - PROFILE_EXISTS=1 - break - fi - done - - if [ ! $PROFILE_EXISTS ] - then - echo "No such profile exists: ${PROFILE}" - exit - fi - - dotnet publish /p:Configuration="${CONFIG}" /p:PublishProfile="${PROFILE}" - - if [ ! $BUILD_ALL ] - then - exit - fi -fi - -if [ $BUILD_ALL ] -then - PROFILE_COUNT=$(wc -l < ${PROFILE_DATA}) - - for (( i = 1; i <= $PROFILE_COUNT + 1; i++ )) - do - PROFILE_CURRENT=$(cut -f1 "${PROFILE_DATA}" | sed "${i}q;d") - - # Skip profile if it was already specified and built. - if [[ $PROFILE_CURRENT == $PROFILE ]] - then - continue - fi - - dotnet publish /p:Configuration="${CONFIG}" /p:PublishProfile="${PROFILE_CURRENT}" - done -else - case "${PLATFORM}" in - "Linux") - dotnet publish /p:Configuration="${CONFIG}" /p:PublishProfile="linux-x64" - ;; - "macOS") - dotnet publish /p:Configuration="${CONFIG}" /p:PublishProfile="osx-x64" - ;; - esac -fi \ No newline at end of file