From 4e84b0d0a4829e145970d9056c594e988cf1060a Mon Sep 17 00:00:00 2001
From: Pascal Thomet <pthomet@gmail.com>
Date: Tue, 11 Jun 2024 14:48:07 +0200
Subject: [PATCH] CI: do not require admin rights

---
 .github/workflows/ci.yml        | 25 ++++-------
 .github/workflows/codeql.yml    |  7 +--
 .gitignore                      |  1 +
 script/ci_build.sh              | 10 -----
 script/ci_run.sh                | 77 +++++++++++++++++++++++++++++++++
 script/ci_setup_dependencies.sh |  8 ----
 6 files changed, 88 insertions(+), 40 deletions(-)
 delete mode 100755 script/ci_build.sh
 create mode 100644 script/ci_run.sh
 delete mode 100755 script/ci_setup_dependencies.sh

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 29719f76..1ff586f7 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -24,10 +24,8 @@ jobs:
         run: |
           echo "/host_usr_local/bin" >> $GITHUB_PATH
           script/ci_setup_linux.sh
-      - name: Setup Dependencies
-        run: script/ci_setup_dependencies.sh
-      - name: Build
-        run: script/ci_build.sh
+      - name: Build and Test
+        run: script/ci_run.sh run_tests
 
 
   build_clang:
@@ -62,10 +60,8 @@ jobs:
             apt-get install -y --no-install-recommends libunwind-${{ matrix.build_config.version }}-dev;
           fi
           echo "CXXFLAGS=-stdlib=libc++" >> $GITHUB_ENV
-      - name: Setup Dependencies
-        run: script/ci_setup_dependencies.sh
-      - name: Build
-        run: script/ci_build.sh
+      - name: Build and Test
+        run: script/ci_run.sh run_tests
 
 
   build_osx:
@@ -76,10 +72,8 @@ jobs:
       CXX: clang++
     steps:
       - uses: actions/checkout@main
-      - name: Setup
-        run: sudo script/ci_setup_dependencies.sh
-      - name: Build
-        run: script/ci_build.sh
+      - name: Build and Test
+        run: script/ci_run.sh run_tests
 
 
   build_windows_msvc:
@@ -93,12 +87,9 @@ jobs:
     steps:
       - uses: actions/checkout@main
       - uses: ilammy/msvc-dev-cmd@v1
-      - name: Setup
-        shell: bash
-        run: script/ci_setup_dependencies.sh
-      - name: Build
+      - name: Build and Test
         shell: bash
-        run: script/ci_build.sh
+        run: script/ci_run.sh run_tests
 
 
   formatting-check:
diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml
index b4cb3a60..0f8f4c3a 100644
--- a/.github/workflows/codeql.yml
+++ b/.github/workflows/codeql.yml
@@ -16,16 +16,13 @@ jobs:
     steps:
       - name: Checkout
         uses: actions/checkout@main
-      - name: Install dependencies
-        run: sudo script/ci_setup_dependencies.sh
       - name: CodeQL Initialization
         uses: github/codeql-action/init@v3
         with:
           languages: cpp
           queries: +security-and-quality
       - name: Build
-        run: |
-          cmake -S test -B build
-          cmake --build build -j 4
+        shell: bash
+        run: script/ci_run.sh run_build
       - name: CodeQL Analysis
         uses: github/codeql-action/analyze@v3
diff --git a/.gitignore b/.gitignore
index 26a63b45..d7cd7d24 100755
--- a/.gitignore
+++ b/.gitignore
@@ -12,3 +12,4 @@ api_search/frontend/src/Database.elm
 .idea
 cmake-build-*/
 CMakeUserPresets.json
+doctest/
diff --git a/script/ci_build.sh b/script/ci_build.sh
deleted file mode 100755
index 85b7cae6..00000000
--- a/script/ci_build.sh
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/bash
-
-JOBS=4
-BUILD_TYPE=Release
-
-cmake -S test -B build -D CMAKE_BUILD_TYPE=${BUILD_TYPE}
-cmake --build build --config ${BUILD_TYPE} -j ${JOBS}
-
-cd build
-ctest -C ${BUILD_TYPE} -j ${JOBS} --output-on-failure
diff --git a/script/ci_run.sh b/script/ci_run.sh
new file mode 100644
index 00000000..cdc55fc9
--- /dev/null
+++ b/script/ci_run.sh
@@ -0,0 +1,77 @@
+#!/bin/bash
+
+#
+#  Private Impl
+#
+
+# Get the directory of the current script (this is bash's notion of poetry)
+THIS_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
+REPO_DIR="$THIS_DIR"/..
+
+# Determine the install prefix based on the OS
+if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
+    # Windows path handling is a never ending source of ...
+    INSTALL_PREFIX="$USERPROFILE\\.local"
+    INSTALL_PREFIX=$(cygpath -u "$INSTALL_PREFIX")  # Make CMake happy when using git bash under windows
+    export CMAKE_PREFIX_PATH=$INSTALL_PREFIX;$CMAKE_PREFIX_PATH  # Notice the ";" instead of ":"
+else
+    INSTALL_PREFIX="$HOME/.local"
+    export CMAKE_PREFIX_PATH=$INSTALL_PREFIX:$CMAKE_PREFIX_PATH
+fi
+
+# Function to install doctest
+_install_doctest() {
+    cd "$REPO_DIR"
+    git clone --depth=1 --branch=v2.4.11 https://github.com/doctest/doctest
+    cd doctest && mkdir -p build && cd build
+    cmake .. -DDOCTEST_WITH_TESTS=OFF -DDOCTEST_WITH_MAIN_IN_STATIC_LIB=OFF -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX
+    cmake --build . --config Release --target install
+}
+
+# Function to build the project
+_build_impl() {
+    cd "$REPO_DIR"
+    JOBS=4
+    BUILD_TYPE=Release
+    cmake -S test -B build -D CMAKE_BUILD_TYPE=${BUILD_TYPE}
+    cmake --build build --config ${BUILD_TYPE} -j ${JOBS}
+}
+
+# Function to run tests
+_tests_impl() {
+    cd "$REPO_DIR/build"
+    JOBS=4
+    BUILD_TYPE=Release
+    ctest -C ${BUILD_TYPE} -j ${JOBS} --output-on-failure
+}
+
+#
+# API
+#
+
+# API function to run tests
+run_tests() {
+    _install_doctest
+    _build_impl
+    _tests_impl
+}
+
+# API function to build the project
+run_build() {
+    _install_doctest
+    _build_impl
+}
+
+# Main script logic
+case "$1" in
+    run_build)
+        run_build
+        ;;
+    run_tests)
+        run_tests
+        ;;
+    *)
+        echo "Usage: $0 {run_build|run_tests}"
+        exit 1
+        ;;
+esac
diff --git a/script/ci_setup_dependencies.sh b/script/ci_setup_dependencies.sh
deleted file mode 100755
index 77528901..00000000
--- a/script/ci_setup_dependencies.sh
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/bin/bash
-
-
-## Install doctest
-git clone --depth=1 --branch=v2.4.11 https://github.com/doctest/doctest
-cd doctest && mkdir -p build && cd build
-cmake .. -DDOCTEST_WITH_TESTS=OFF -DDOCTEST_WITH_MAIN_IN_STATIC_LIB=OFF
-cmake --build . --config Release --target install