From f3c652c029e17c5ce15fee396f1db7a29afd9d73 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sat, 25 Jan 2025 23:35:58 +0100 Subject: [PATCH] Fix gen_flatpak_manifest --- .github/workflows/Build.yml | 4 +- tools/gen_flatpak_manifest.py | 79 +++++++++++++++++++++++++++++++++++ tools/gen_flatpak_manifest.sh | 66 ----------------------------- 3 files changed, 81 insertions(+), 68 deletions(-) create mode 100755 tools/gen_flatpak_manifest.py delete mode 100755 tools/gen_flatpak_manifest.sh diff --git a/.github/workflows/Build.yml b/.github/workflows/Build.yml index 427e01b0..6d5bead9 100644 --- a/.github/workflows/Build.yml +++ b/.github/workflows/Build.yml @@ -92,7 +92,7 @@ jobs: - name: Create the flatpak manifest run: | git config --global --add safe.directory $(pwd) - tools/gen_flatpak_manifest.sh --git https://github.com/${{github.repository}}.git > io.github.wivrn.wivrn.yml + tools/gen_flatpak_manifest.py --git https://github.com/${{github.repository}}.git > io.github.wivrn.wivrn.yml # TODO: replace with version > v6.3 when it's released - uses: flatpak/flatpak-github-actions/flatpak-builder@b8be6bc0ede43cb3a2cc55895654312b14f84a0d @@ -507,7 +507,7 @@ jobs: git reset --hard upstream/master git config --global --add safe.directory $(pwd) - ../wivrn/tools/gen_flatpak_manifest.sh --git https://github.com/${{github.repository}}.git > io.github.wivrn.wivrn.yml + ../wivrn/tools/gen_flatpak_manifest.py --git https://github.com/${{github.repository}}.git > io.github.wivrn.wivrn.yml git checkout -b $WIVRN_TAG git add io.github.wivrn.wivrn.yml diff --git a/tools/gen_flatpak_manifest.py b/tools/gen_flatpak_manifest.py new file mode 100755 index 00000000..19edaa6f --- /dev/null +++ b/tools/gen_flatpak_manifest.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +import argparse +import os +import re +import subprocess + +TEMPLATE = "flatpak/io.github.wivrn.wivrn.yml.in" + + +class CMakeFile: + def __init__(self, filename: str): + self.entries = {} + with open(filename) as f: + data = f.read() + for match in re.findall("FetchContent_Declare\\(([^)]*)\\)", data): + words = [word for word + in match.replace("\n", " ").split(" ") + if word] + self.entries[words[0]] = words[1:] + + def get(self, target: str, key: str) -> str: + entry = self.entries[target] + index = entry.index(key) + return entry[index + 1] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--git") + parser.add_argument("--gitlocal", action="store_true") + parser.add_argument("--dir", action="store_true") + + args = parser.parse_args() + if not (args.git or args.dir): + args.gitlocal = True + + root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + cmake = CMakeFile(os.path.join(root, "CMakeLists.txt")) + with open(os.path.join(root, TEMPLATE)) as f: + template = f.read() + + monado_commit = cmake.get("monado", "GIT_TAG") + boostpfr_url = cmake.get("boostpfr", "URL") + boostpfr_sha256 = cmake.get("boostpfr", "URL_HASH").split("=")[-1] + + try: + git_commit = subprocess.check_output( + ["git", "describe", "--exact-match", "--tags"] + ).decode().strip() + except subprocess.CalledProcessError: + git_commit = subprocess.check_output( + ["git", "rev-parse", "HEAD"] + ).decode().strip() + git_desc = subprocess.check_output( + ["git", "describe", "--tags", "--always"] + ).decode().strip() + + if args.git or args.gitlocal: + template = template.replace("WIVRN_SRC1", "type: git") + if args.git: + template = template.replace("WIVRN_SRC2", f"url: {args.git}") + else: + template = template.replace("WIVRN_SRC2", f"url: {root}") + template = template.replace("WIVRN_SRC3", f"tag: {git_commit}") + else: + template = template.replace("WIVRN_SRC1", "type: dir") + template = template.replace("WIVRN_SRC2", f"path: {root}") + template = template.replace("WIVRN_SRC3", "") + + template = template.replace("WIVRN_GIT_DESC", git_desc) + template = template.replace("WIVRN_GIT_COMMIT", git_commit) + + template = template.replace("BOOSTPFR_URL", boostpfr_url) + template = template.replace("BOOSTPFR_SHA256", boostpfr_sha256) + template = template.replace("MONADO_COMMIT", monado_commit) + + print(template) diff --git a/tools/gen_flatpak_manifest.sh b/tools/gen_flatpak_manifest.sh deleted file mode 100755 index 455b860f..00000000 --- a/tools/gen_flatpak_manifest.sh +++ /dev/null @@ -1,66 +0,0 @@ -#!/bin/bash - -set -e - -WIVRN_SRC_TYPE=gitlocal - -while [ ! -z "$1" ] -do - case "$1" in - "--git") - WIVRN_SRC_TYPE=git - shift - WIVRN_SRC2="url: $1" - ;; - "--gitlocal") - WIVRN_SRC_TYPE=gitlocal - ;; - "--dir") - WIVRN_SRC_TYPE=dir - ;; - esac - - shift -done - -cd $(dirname $0) - -GIT_TAG=$(git describe --exact-match --tags 2>/dev/null || true) -GIT_SHA=$(git rev-parse HEAD) -GIT_COMMIT=$(git describe --exact-match --tags 2>/dev/null || git rev-parse HEAD) -GIT_DESC=$(git describe --tags --always) - -MONADO_COMMIT=$(grep -zo "FetchContent_Declare(monado[^)]*)" ../CMakeLists.txt | sed -ze "s/^.*GIT_TAG *\([^ )]*\).*$/\1/" | tr -d '\0') -BOOSTPFR_URL=$(grep -zo "FetchContent_Declare(boostpfr[^)]*)" ../CMakeLists.txt | sed -ze "s/^.*URL *\([^ )]*\).*$/\1/" | tr -d '\0') -BOOSTPFR_SHA256=$(curl --silent --location $BOOSTPFR_URL | sha256sum | cut -f1 -d' ') - -if [ $WIVRN_SRC_TYPE = git ] -then - WIVRN_SRC1="type: git" - WIVRN_SRC3="tag: ${GIT_COMMIT}" -elif [ $WIVRN_SRC_TYPE = gitlocal ] -then - WIVRN_SRC1="type: git" - WIVRN_SRC2="url: "$(realpath $(pwd)/..) - WIVRN_SRC3="tag: ${GIT_COMMIT}" -else - WIVRN_SRC1="type: dir" - WIVRN_SRC2="path: "$(realpath $(pwd)/..) - WIVRN_SRC3="" -fi - -echo "Git commit: $GIT_COMMIT" >&2 -echo "Git tag: $GIT_DESC" >&2 -echo "Monado commit: $MONADO_COMMIT" >&2 -echo "Boost.PFR URL: $BOOSTPFR_URL" >&2 -echo "Boost.PFR SHA256: $BOOSTPFR_SHA256" >&2 - -cat ../flatpak/io.github.wivrn.wivrn.yml.in | sed \ - -e s,WIVRN_SRC1,"$WIVRN_SRC1", \ - -e s,WIVRN_SRC2,"$WIVRN_SRC2", \ - -e s,WIVRN_SRC3,"$WIVRN_SRC3", \ - -e s,WIVRN_GIT_DESC,$GIT_DESC, \ - -e s,WIVRN_GIT_COMMIT,$GIT_COMMIT, \ - -e s,BOOSTPFR_URL,$BOOSTPFR_URL, \ - -e s,BOOSTPFR_SHA256,$BOOSTPFR_SHA256, \ - -e s,MONADO_COMMIT,$MONADO_COMMIT,