image: php:8.3

before_script:
    # Install some stuff that the image doesn't come with
    - apt update -yqq
    - apt install git libpq-dev libzip-dev zip make wget gnupg -yqq
    - curl -sSLf -o /usr/local/bin/install-php-extensions https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions
    - chmod +x /usr/local/bin/install-php-extensions
    - install-php-extensions zip

    # Install phive
    - wget -O phive.phar https://phar.io/releases/phive.phar
    - wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc
    - gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79
    - gpg --verify phive.phar.asc phive.phar
    - chmod +x phive.phar
    - mv phive.phar /usr/local/bin/phive

    # Install phab
    - phive install phpab --global --trust-gpg-keys 0x2A8299CE842DD38C

    # Install the latest version of ncc (Nosial Code Compiler)
    - git clone https://git.n64.cc/nosial/ncc.git
    - cd ncc
    - make redist
    - NCC_DIR=$(find build/ -type d -name "ncc_*" | head -n 1)
    - |
      if [ -z "$NCC_DIR" ]; then
        echo "NCC build directory not found"
        exit 1
      fi
    - php "$NCC_DIR/INSTALL" --auto --install-composer
    - cd .. && rm -rf ncc

build:
    stage: build
    script:
        - ncc build --config release --log-level debug
    artifacts:
        paths:
        - build/
    rules:
        - if: $CI_COMMIT_BRANCH
test:
    stage: test
    script:
        - ncc package install --package="build/release/net.nosial.configlib.ncc" --reinstall -y --log-level debug

        - wget https://phar.phpunit.de/phpunit-11.3.phar
        - php phpunit-11.3.phar --configuration phpunit.xml
    rules:
        - if: $CI_COMMIT_BRANCH

release:
  stage: deploy
  script:
    - ncc build --config release --log-level debug
    - >
      curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/release/net.nosial.configlib.ncc "$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/net.nosial.configlib/$CI_COMMIT_REF_NAME/net.nosial.configlib.ncc"
  artifacts:
    paths:
      - build/
  rules:
    - if: $CI_COMMIT_TAG