variables:
  PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pdftk-java"
  GRADLE_VERSION: "7.5.1"

.ant-job:
  stage: test
  except:
    variables:
      - $WEB_JOB != null && $WEB_JOB != "test"
  image: openjdk:11
  before_script:
  - apt-get update -yqq
  - apt-get install ant ivy -yqq

php-pdftk:
  extends: .ant-job
  script:
    - ant -lib /usr/share/java
    - echo -e '#!/bin/bash\njava -cp' `pwd`'/build/jar/pdftk.jar:'`pwd`'/lib/bcprov-jdk18on-1.76.jar:'`pwd`'/lib/commons-lang3-3.12.0.jar com.gitlab.pdftk_java.pdftk "$@"' > /usr/bin/pdftk
    - chmod +x /usr/bin/pdftk
    - apt-get install php-mbstring php-xml composer -yqq
    - git clone --depth 1 https://github.com/marcvinyals/php-pdftk
    - cd php-pdftk
    - composer install
    - pdftk --version
    - ./vendor/bin/phpunit

test:
  extends: .ant-job
  script:
    - apt-get install poppler-utils -yqq
    - ant -lib /usr/share/java test-resolve
    - ant -lib /usr/share/java -lib ./lib test
    - perl -ne 'm/Total.*?([0-9]{1,3})%/; print "Coverage $1%\n"' coverage/index.html
  artifacts:
    paths:
      - coverage/

pages:
  only:
    - master
  stage: deploy
  dependencies:
    - test
  script:
    - mkdir public/
    - mv coverage/ public/
  artifacts:
    paths:
      - public/

.gradle-job-script: &grade-job-script
  - gradle wrapper --gradle-version=$GRADLE_VERSION
  - ./gradlew shadowJar
  - java -jar build/libs/pdftk-all.jar --version

.gradle-job:
  only:
    - tags
  stage: deploy
  image: gradle:jdk11
  script:
    - *grade-job-script

gradle:
  extends: .gradle-job
  script:
    - *grade-job-script
    - |
      curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file build/libs/pdftk-all.jar ${PACKAGE_REGISTRY_URL}/${CI_COMMIT_TAG}/pdftk-all.jar
  artifacts:
    paths:
      - build/libs/pdftk-all.jar

gradle-7.3:
  extends: .gradle-job
  variables:
    GRADLE_VERSION: "7.3"

gradle-web:
  extends: .gradle-job
  stage: test
  only:
    variables:
      - $CI_PIPELINE_SOURCE == "web" && $WEB_JOB == "gradle"
  artifacts:
    paths:
      - build/libs/pdftk-all.jar
    expire_in: 1 month

spotless:
  stage: test
  except:
    variables:
      - $WEB_JOB != null
  image: gradle:jdk11
  script:
    - gradle spotlessCheck

nativeimage:
  only:
    - tags
  stage: deploy
  # cf https://gitlab.com/pdftk-java/pdftk/-/issues/68 before updating GraalVM
  # cf https://gitlab.com/pdftk-java/pdftk/-/issues/113 before updating OracleLinux
  image: ghcr.io/graalvm/graalvm-ce:ol7-java11-21.0.0
  before_script:
    - gu install native-image
    - yum install -y wget unzip
    - wget https://services.gradle.org/distributions/gradle-$GRADLE_VERSION-bin.zip
    - unzip gradle-$GRADLE_VERSION-bin.zip
  script:
    - ./gradle-$GRADLE_VERSION/bin/gradle nativeImage
    - ./build/native-image/pdftk --version
    - |
      curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file build/native-image/pdftk ${PACKAGE_REGISTRY_URL}/${CI_COMMIT_TAG}/pdftk
  artifacts:
    paths:
      - build/native-image/pdftk

nativeimage-web:
  extends: nativeimage
  stage: test
  only:
    variables:
      - $CI_PIPELINE_SOURCE == "web" && $WEB_JOB == "nativeimage"
  script:
    - ./gradle-$GRADLE_VERSION/bin/gradle --warning-mode all -Pgeneratedebuginfo=1 nativeImage
    - ./build/native-image/pdftk --version
  artifacts:
    paths:
      - build/native-image/pdftk
    expire_in: 1 month

release:
  stage: .post
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - |
      release-cli create --name "Release $CI_COMMIT_TAG" --tag-name $CI_COMMIT_TAG \
        --assets-link "{\"name\":\"x86_64\",\"url\":\"${PACKAGE_REGISTRY_URL}/${CI_COMMIT_TAG}/pdftk\"}" \
        --assets-link "{\"name\":\"jar\",\"url\":\"${PACKAGE_REGISTRY_URL}/${CI_COMMIT_TAG}/pdftk-all.jar\"}"
