From 17014506254b2e7a7fc5809c82c9a0cd6a17bf91 Mon Sep 17 00:00:00 2001 From: LeoKle Date: Sat, 6 Apr 2024 15:51:14 +0200 Subject: [PATCH] amend workflow build and versioning --- .github/workflows/build-develop.yml | 42 ++++++++++++---- .github/workflows/build-release.yml | 29 ++++++++--- .github/workflows/clang-format.yml | 2 +- .github/workflows/cmake_project_version.py | 25 ++++++++++ .github/workflows/determine_dev_release.py | 57 ++++++++++++++++++++++ .github/workflows/version_handler.py | 34 +++++++++++++ CMakeLists.txt | 9 +++- CMakeSettings.json | 11 +++-- src/Version.h.in | 4 ++ 9 files changed, 192 insertions(+), 21 deletions(-) create mode 100644 .github/workflows/cmake_project_version.py create mode 100644 .github/workflows/determine_dev_release.py create mode 100644 .github/workflows/version_handler.py diff --git a/.github/workflows/build-develop.yml b/.github/workflows/build-develop.yml index 2690b16..d443b40 100644 --- a/.github/workflows/build-develop.yml +++ b/.github/workflows/build-develop.yml @@ -3,21 +3,50 @@ name: Build Plugin Dev Release on: push: branches: - - develop + - "develop" env: DLL_NAME: vACDM.dll BUILD_CONFIGURATION: Release GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + VERSION: "0" + DEV_RELEASE: "-1" jobs: - build-and-test: + formatting-check: + uses: ./.github/workflows/clang-format.yml + + build: + needs: formatting-check runs-on: windows-latest name: "Build plugin" steps: - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: "3.x" + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install PyGithub + + - name: Run version handler + env: + REPOSITORY: ${{ github.repository }} + REF: ${{ github.ref }} + run: | + python .github/workflows/version_handler.py + + - name: Determine DEV_RELEASE value + id: find_latest_dev_release + env: + REPOSITORY: ${{ github.repository }} + run: python .github/workflows/determine_dev_release.py + - name: Install MSVC toolset run: choco install visualstudio2019buildtools --package-parameters "--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64" @@ -28,23 +57,18 @@ jobs: Enter-VsDevShell -VsInstallPath "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools" - name: Configure CMake - run: cmake -B build -DCMAKE_BUILD_TYPE=${{env.BUILD_CONFIGURATION}} -A Win32 + run: cmake -B build -DCMAKE_BUILD_TYPE=${{env.BUILD_CONFIGURATION}} -A Win32 -DDEV_BUILD=ON -DDEV_RELEASE_NUMBER="${{ env.DEV_RELEASE }}" - name: Build DLL run: cmake --build build --config ${{env.BUILD_CONFIGURATION}} - - name: Extract version from CMakeLists.txt - run: | - $version = Select-String -Path CMakeLists.txt -Pattern 'PROJECT\(.*?VERSION\s+"(\d+\.\d+\.\d+)"' | ForEach-Object { $_.Matches.Groups[1].Value } - echo "VERSION=${version}-dev" >> $env:GITHUB_ENV - - name: Create GitHub Release id: create_release uses: actions/create-release@v1 with: tag_name: v${{ env.VERSION }} release_name: v${{ env.VERSION }} - draft: false + draft: true # to allow amendment of release notes prerelease: true env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index e5307ea..bcfdf52 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -9,9 +9,14 @@ env: DLL_NAME: vACDM.dll BUILD_CONFIGURATION: Release GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + VERSION: "0" jobs: - build-and-test: + formatting-check: + uses: ./.github/workflows/clang-format.yml + + build: + needs: formatting-check runs-on: windows-latest name: "Build plugin" @@ -21,6 +26,23 @@ jobs: - name: Install MSVC toolset run: choco install visualstudio2019buildtools --package-parameters "--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64" + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: "3.x" + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install PyGithub + + - name: Run version handler + env: + REPOSITORY: ${{ github.repository }} + REF: ${{ github.ref }} + run: | + python .github/workflows/version_handler.py + - name: Set up MSVC environment shell: pwsh run: | @@ -33,11 +55,6 @@ jobs: - name: Build DLL run: cmake --build build --config ${{env.BUILD_CONFIGURATION}} - - name: Extract version from CMakeLists.txt - run: | - $version = Select-String -Path CMakeLists.txt -Pattern 'PROJECT\(.*?VERSION\s+"(\d+\.\d+\.\d+)"' | ForEach-Object { $_.Matches.Groups[1].Value } - echo "VERSION=${version}" >> $env:GITHUB_ENV - - name: Create GitHub Release id: create_release uses: actions/create-release@v1 diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml index 0aa6019..7e42428 100644 --- a/.github/workflows/clang-format.yml +++ b/.github/workflows/clang-format.yml @@ -1,5 +1,5 @@ name: clang-format Check -on: [push, pull_request] +on: [workflow_call, pull_request] jobs: formatting-check: name: Formatting Check diff --git a/.github/workflows/cmake_project_version.py b/.github/workflows/cmake_project_version.py new file mode 100644 index 0000000..3a2f211 --- /dev/null +++ b/.github/workflows/cmake_project_version.py @@ -0,0 +1,25 @@ +""" Finds the project version defined in CMakeLists.txt""" + +import sys +import re + + +def extract_version_from_cmakelists(): + # Define the pattern to search for in the file + pattern = r'PROJECT\(.*?VERSION\s+"(\d+\.\d+\.\d+)"' + + # Read the contents of CMakeLists.txt + with open("CMakeLists.txt", "r") as file: + contents = file.read() + + # Search for the pattern + match = re.search(pattern, contents) + + # If a match is found, extract the version + if match: + print("Found version number in CMakeLists.txt: ", match.group(1)) + return match.group(1) + + # exit if the version could not be found + print("Could not find version in CMakeLists.txt") + sys.exit(1) diff --git a/.github/workflows/determine_dev_release.py b/.github/workflows/determine_dev_release.py new file mode 100644 index 0000000..e0c3617 --- /dev/null +++ b/.github/workflows/determine_dev_release.py @@ -0,0 +1,57 @@ +import sys +import re +from github import Github + + +def parse_semantic_version(version): + # Parse the semantic version and extract major and minor versions + match = re.match(r"v?(\d+)\.(\d+)\.(\d+)", version) + if match: + print(match.groups()) + major, minor, patch = match.groups() + return int(major), int(minor), int(patch) + else: + return None + + +def find_highest_dev_release(repo, major, minor): + # Initialize a GitHub instance + g = Github() + + # Get the repository object + repository = g.get_repo(repo) + + # Fetch all releases + releases = repository.get_releases() + + # Filter pre-releases with matching major and minor versions in their titles + pre_releases = [ + release + for release in releases + if release.prerelease and release.title.startswith(f"v{major}.{minor}") + ] + + # Extract DEV_RELEASE numbers from titles + dev_releases = [int(release.title.split(".")[-1]) for release in pre_releases] + + # Find the highest DEV_RELEASE number + highest_dev_release = max(dev_releases, default=0) + + return highest_dev_release + + +def determine_dev_release(version, repository): + if version == "0": + result = 0 + else: + # Parse the semantic version to extract major and minor versions + major, minor, _ = parse_semantic_version(version) + if major is not None and minor is not None: + result = find_highest_dev_release(repository, major, minor) + else: + print("Invalid semantic version format") + sys.exit(1) + + print("Determined dev release version as ", result) + + return result diff --git a/.github/workflows/version_handler.py b/.github/workflows/version_handler.py new file mode 100644 index 0000000..3969190 --- /dev/null +++ b/.github/workflows/version_handler.py @@ -0,0 +1,34 @@ +""" """ + +import sys +import os +import re + +from cmake_project_version import extract_version_from_cmakelists +from determine_dev_release import determine_dev_release + +REPOSITORY_NAME = os.environ.get("REPOSITORY") +REF = os.environ.get("REF") + +# determine the branch that triggered the workflow +match = re.match(r"refs/heads/(.*)", REF) +if not match: + sys.exit(1) + +branch_name = match.group(1) +print("Determined branch name: ", branch_name) + +version = extract_version_from_cmakelists() + +is_dev_branch = branch_name == "develop" +dev_release = None +if is_dev_branch: + last_dev_release = determine_dev_release(version, REPOSITORY_NAME) + dev_release = str(last_dev_release + 1) + version += "-dev." + dev_release + +# Write the version and dev release to GitHub environment variable +with open(os.getenv("GITHUB_ENV"), "a") as env_file: + env_file.write(f"VERSION={version}\n") + if is_dev_branch and dev_release: + env_file.write(f"DEV_RELEASE={dev_release}\n") diff --git a/CMakeLists.txt b/CMakeLists.txt index 69ead6c..08f6d46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.14) -PROJECT(vACDM VERSION "1.3.1") +PROJECT(vACDM VERSION "1.3.0") SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON) SET(CMAKE_CXX_STANDARD 20) SET(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -23,8 +23,13 @@ IF (MSVC) ADD_DEFINITIONS(/D_USRDLL) ENDIF () +if(DEV_BUILD) + ADD_DEFINITIONS(-DDEV_BUILD) +endif() + if(CMAKE_BUILD_TYPE STREQUAL "Debug") - ADD_DEFINITIONS(-DDEBUG_BUILD=1) + ADD_DEFINITIONS(-DDEBUG_BUILD=1) # enables log output to console window + ADD_DEFINITIONS(-DDEV_BUILD) endif() CONFIGURE_FILE( diff --git a/CMakeSettings.json b/CMakeSettings.json index 82577b4..d59dcf7 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -17,11 +17,16 @@ "configurationType": "Debug", "buildRoot": "${projectDir}\\out\\build\\${name}", "installRoot": "${projectDir}\\out\\install\\${name}", - "cmakeCommandArgs": "", - "buildCommandArgs": "", + "cmakeCommandArgs": "-DDEV_BUILD=ON", "ctestCommandArgs": "", "inheritEnvironments": ["msvc_x86"], - "variables": [] + "variables": [ + { + "name": "DEV_RELEASE_NUMBER", + "value": "-1", + "type": "STRING" + } + ] } ] } diff --git a/src/Version.h.in b/src/Version.h.in index 6bdf55d..275ba60 100644 --- a/src/Version.h.in +++ b/src/Version.h.in @@ -3,7 +3,11 @@ // prevents C2018 during compilation namespace { const char *PLUGIN_NAME{ "vACDM" }; +#if DEV_BUILD +const char *PLUGIN_VERSION{ "@CMAKE_PROJECT_VERSION@-DEV.@DEV_RELEASE_NUMBER@"}; +#else const char *PLUGIN_VERSION{ "@CMAKE_PROJECT_VERSION@" }; +#endif const char *PLUGIN_AUTHOR{ "vACDM Team" }; const char *PLUGIN_LICENSE{ "GPLv3" };