From c0d665095e4dc9f96eb335ca476597ec319a7d87 Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Fri, 7 Jan 2022 19:44:33 +0100 Subject: [PATCH] Add script to automatically bump the version Closes #44 --- .github/workflows/build.yml | 2 +- cmake/bump_version.cmake | 70 +++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 cmake/bump_version.cmake diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1974cb7..4d25124 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -50,7 +50,7 @@ jobs: string: ${{ runner.os }} - name: Compress artifacts - uses: papeloto/action-zip@v1 + uses: vimtor/action-zip@v1 with: files: install/bin/ dest: ${{ github.event.repository.name }}-${{ steps.osname.outputs.lowercase }}.zip diff --git a/cmake/bump_version.cmake b/cmake/bump_version.cmake new file mode 100644 index 0000000..ba6465e --- /dev/null +++ b/cmake/bump_version.cmake @@ -0,0 +1,70 @@ +cmake_minimum_required(VERSION 3.20) + +if(NOT CMAKE_SCRIPT_MODE_FILE) + message(FATAL_ERROR "Usage: cmake -P bump_version.cmake [1.2.3]") +endif() + +if(NOT EXISTS "${CMAKE_SOURCE_DIR}/cmake.toml") + message(FATAL_ERROR "Cannot find cmake.toml") +endif() + +if(NOT EXISTS "${CMAKE_SOURCE_DIR}/cmake/cmkr.cmake") + message(FATAL_ERROR "Cannot find cmkr.cmake") +endif() + +file(READ "${CMAKE_SOURCE_DIR}/cmake.toml" CMAKE_TOML) +string(FIND "${CMAKE_TOML}" "[project]" PROJECT_INDEX) +string(SUBSTRING "${CMAKE_TOML}" ${PROJECT_INDEX} -1 CMAKE_TOML_PROJECT) +set(SEMVER_REGEX "([0-9]+)\\.([0-9]+)\\.([0-9]+)") +set(VERSION_REGEX "version = \"${SEMVER_REGEX}\"") +if(CMAKE_TOML_PROJECT MATCHES "${VERSION_REGEX}") + set(MAJOR "${CMAKE_MATCH_1}") + set(MINOR "${CMAKE_MATCH_2}") + set(PATCH "${CMAKE_MATCH_3}") + set(OLDVERSION "${MAJOR}.${MINOR}.${PATCH}") +else() + message(FATAL_ERROR "Failed to match semantic version in cmake.toml") +endif() + +if(CMAKE_ARGV3) + if(NOT CMAKE_ARGV3 MATCHES "${SEMVER_REGEX}") + message(FATAL_ERROR "Invalid semantic version number '${CMAKE_ARGV3}'") + endif() + set(NEWVERSION "${CMAKE_ARGV3}") +else() + math(EXPR NEWPATCH "${PATCH} + 1") + set(NEWVERSION "${MAJOR}.${MINOR}.${NEWPATCH}") +endif() + +message(STATUS "Version ${OLDVERSION} -> ${NEWVERSION}") + +find_program(CMKR_EXECUTABLE "cmkr" PATHS "${CMAKE_SOURCE_DIR}/build" NO_CACHE REQUIRED) +message(STATUS "Found cmkr: ${CMKR_EXECUTABLE}") + +# Replace version in cmake.toml +string(REPLACE "version = \"${OLDVERSION}\"" "version = \"${NEWVERSION}\"" CMAKE_TOML "${CMAKE_TOML}") +file(CONFIGURE + OUTPUT "${CMAKE_SOURCE_DIR}/cmake.toml" + CONTENT "${CMAKE_TOML}" + @ONLY + NEWLINE_STYLE LF +) + +# Run cmkr gen +execute_process(COMMAND "${CMKR_EXECUTABLE}" gen RESULT_VARIABLE CMKR_EXEC_RESULT) +if(NOT CMKR_EXEC_RESULT EQUAL 0) + message(FATAL_ERROR "cmkr gen failed (exit code ${CMKR_EXEC_RESULT})") +endif() + +# Replace version in cmkr.cmake +file(READ "${CMAKE_SOURCE_DIR}/cmake/cmkr.cmake" CMKR_CMAKE) +string(REGEX REPLACE "CMKR_TAG \"[^\"]+\"" "CMKR_TAG \"v${NEWVERSION}\"" CMKR_CMAKE "${CMKR_CMAKE}") +file(CONFIGURE + OUTPUT "${CMAKE_SOURCE_DIR}/cmake/cmkr.cmake" + CONTENT "${CMKR_CMAKE}" + @ONLY + NEWLINE_STYLE LF +) + +# Print git commands +message(STATUS "Git commands to create new version:\ngit commit -a -m \"Bump to ${NEWVERSION}\"\ngit tag v${NEWVERSION}\ngit push origin main v${NEWVERSION}") \ No newline at end of file