diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a8eaaf94e..29b2c263b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,7 +15,7 @@ jobs: - name: Run version extraction script and set environment variable id: set-version run: | - LIB3MF_VERSION=$(python CI/extract_version.py) + LIB3MF_VERSION=$(python CI/ci_cd_helper.py extract-version) echo "LIB3MF_VERSION=$LIB3MF_VERSION" >> $GITHUB_OUTPUT - name: Echo version for debug run: echo "LIB3MF_VERSION=${{ steps.set-version.outputs.LIB3MF_VERSION }}" @@ -563,7 +563,7 @@ jobs: - name: Get latest lib3mf SDK release info from GitHub API id: get_lib3mf_release run: | - echo "LATEST_LIB3MF_URL=$(curl -s https://api.github.com/repos/3MFConsortium/lib3mf/releases/latest | grep "browser_download_url.*lib3mf_sdk.zip" | cut -d '"' -f 4 | xargs)" >> $GITHUB_ENV + echo "LATEST_LIB3MF_URL=$(python CI/ci_cd_helper.py fetch-sdk-url 0 | xargs)" >> $GITHUB_ENV - name: Download latest lib3mf SDK zip run: | @@ -637,12 +637,12 @@ jobs: - name: Get latest lib3mf SDK release info from GitHub API id: get_latest_release run: | - echo "LATEST_LIB3MF_URL=$(python CI/ci_cd_helper.py 0 --get-url | xargs)" >> $GITHUB_ENV + echo "LATEST_LIB3MF_URL=$(python CI/ci_cd_helper.py fetch-sdk-url 0 | xargs)" >> $GITHUB_ENV - name: Get second latest lib3mf SDK release info from GitHub API id: get_second_latest_release run: | - echo "SECOND_LATEST_LIB3MF_URL=$(python CI/ci_cd_helper.py 1 --get-url | xargs)" >> $GITHUB_ENV + echo "SECOND_LATEST_LIB3MF_URL=$(python CI/ci_cd_helper.py fetch-sdk-url 1 | xargs)" >> $GITHUB_ENV - name: Download latest lib3mf SDK zip run: | @@ -655,6 +655,8 @@ jobs: - name: Unpack the latest SDK run: | unzip latest_lib3mf_sdk.zip -d latest_lib3mf_sdk + mv latest_lib3mf_sdk/lib3mf_sdk/* latest_lib3mf_sdk + rmdir latest_lib3mf_sdk/lib3mf_sdk - name: Unpack the second latest SDK run: | @@ -871,7 +873,7 @@ jobs: - name: Get latest lib3mf SDK release info from GitHub API id: get_latest_release run: | - echo "LATEST_LIB3MF_URL=$(curl -s https://api.github.com/repos/3MFConsortium/lib3mf/releases/latest | grep "browser_download_url.*lib3mf_sdk.zip" | cut -d '"' -f 4 | xargs)" >> $GITHUB_ENV + echo "LATEST_LIB3MF_URL=$(python CI/ci_cd_helper.py fetch-sdk-url 0 | xargs)" >> $GITHUB_ENV LATEST_RELEASE_NAME=$(curl -s https://api.github.com/repos/3MFConsortium/lib3mf/releases/latest | grep '"tag_name"' | cut -d '"' -f 4) echo "LATEST_RELEASE_NAME=${LATEST_RELEASE_NAME}" >> $GITHUB_ENV diff --git a/CI/ci_cd_helper.py b/CI/ci_cd_helper.py index e633d23ae..01b218bf5 100644 --- a/CI/ci_cd_helper.py +++ b/CI/ci_cd_helper.py @@ -1,6 +1,31 @@ import argparse import urllib.request import json +import os +import re + +def extract_version_from_cmake(): + cmake_file = 'CMakeLists.txt' + + if not os.path.exists(cmake_file): + raise FileNotFoundError(f"{cmake_file} not found in the current directory") + + with open(cmake_file, 'r') as file: + content = file.read() + + major = re.search(r'set\(LIB3MF_VERSION_MAJOR\s+([0-9]+)\)', content) + minor = re.search(r'set\(LIB3MF_VERSION_MINOR\s+([0-9]+)\)', content) + micro = re.search(r'set\(LIB3MF_VERSION_MICRO\s+([0-9]+)\)', content) + prerelease = re.search(r'set\(LIB3MF_VERSION_PRERELEASE\s+"([^"]*)"\)', content) + + if not major or not minor or not micro: + raise ValueError("Could not find version components in CMakeLists.txt") + + version = f"{major.group(1)}.{minor.group(1)}.{micro.group(1)}" + if prerelease and prerelease.group(1): + version += f"-{prerelease.group(1)}" + + return version def get_sdk_url(index): url = "https://api.github.com/repos/3MFConsortium/lib3mf/releases" @@ -21,18 +46,33 @@ def get_sdk_url(index): return None def main(): - parser = argparse.ArgumentParser(description="Fetch a specific release URL for the 3MFConsortium/lib3mf repository.") - parser.add_argument('index', type=int, help='Index of the release (0 for latest, 1 for second latest, etc.)') - parser.add_argument('--get-url', action='store_true', help='Get the specified release URL') + parser = argparse.ArgumentParser(description="Multi-utility script for CI/CD.") + + subparsers = parser.add_subparsers(dest='command') + + # Subparser for the fetch-sdk-url command + parser_fetch_sdk_url = subparsers.add_parser('fetch-sdk-url', help='Fetch the SDK URL for a specific release index.') + parser_fetch_sdk_url.add_argument('index', type=int, help='Index of the release (0 for latest, 1 for second latest, etc.)') + + # Subparser for the extract-version command + parser_extract_version = subparsers.add_parser('extract-version', help='Extract the version from CMakeLists.txt.') args = parser.parse_args() - if args.get_url: + if args.command == 'fetch-sdk-url': url = get_sdk_url(args.index) if url: print(url) else: print("FAIL") + elif args.command == 'extract-version': + try: + version = extract_version_from_cmake() + print(version) + except Exception as e: + print("FAIL") + else: + parser.print_help() if __name__ == "__main__": main() diff --git a/CI/extract_version.py b/CI/extract_version.py deleted file mode 100644 index 4fc0b0d53..000000000 --- a/CI/extract_version.py +++ /dev/null @@ -1,32 +0,0 @@ -import os -import re - -def extract_version_from_cmake(): - cmake_file = 'CMakeLists.txt' - - if not os.path.exists(cmake_file): - raise FileNotFoundError(f"{cmake_file} not found in the current directory") - - with open(cmake_file, 'r') as file: - content = file.read() - - major = re.search(r'set\(LIB3MF_VERSION_MAJOR\s+([0-9]+)\)', content) - minor = re.search(r'set\(LIB3MF_VERSION_MINOR\s+([0-9]+)\)', content) - micro = re.search(r'set\(LIB3MF_VERSION_MICRO\s+([0-9]+)\)', content) - prerelease = re.search(r'set\(LIB3MF_VERSION_PRERELEASE\s+"([^"]*)"\)', content) - - if not major or not minor or not micro: - raise ValueError("Could not find version components in CMakeLists.txt") - - version = f"{major.group(1)}.{minor.group(1)}.{micro.group(1)}" - if prerelease and prerelease.group(1): - version += f"-{prerelease.group(1)}" - - return version - -if __name__ == "__main__": - try: - version = extract_version_from_cmake() - print(version) - except Exception as e: - print(f"Error: {str(e)}") \ No newline at end of file