stages:
  - build
  - analyze
  - release

variables:
  DEV_NAMESPACE: default

  RED: '\033[0;31m'
  YELLOW: '\033[0;33m'
  NC: '\033[0m' # No Color


##############
# Abstract stages
##############
.file-watcher:
  only:
    refs:
      - master
    changes:
      - build.gradle


.base-build:
  extends: .file-watcher
  stage: build


.base-analyze:
  extends: .file-watcher
  stage: analyze


.base-release:
  extends: .file-watcher
  stage: release
  image: curlimages/curl:7.79.1



##############
# BUILD
##############

plugin:
  extends: .base-build

  image: kuczaq/gradle-node:7.6.0-jdk17

  script:
    - echo "gitLabTokenName=Job-Token" >> gradle.properties
    - echo "gitLabPrivateToken=$CI_JOB_TOKEN" >> gradle.properties

      ## You can use the following to set up access to your private npm registry
      ## Lines below use a private registry hosted on GitLab for packages that are in the @ventum scope.
      ## E.g. (package.json): "name": "@ventum/some-private-package"
      ## In this case, all the packages were published to the GitLab registry of a single container project, hence the project ID 436.
    - echo "@ventum:registry=https://git.ventum.com/api/v4/projects/436/packages/npm/" >> sources/frontend/.npmrc
    - echo "//git.ventum.com/api/v4/projects/436/packages/npm/:_authToken=$CI_JOB_TOKEN" >> sources/frontend/.npmrc


    - gradle --build-cache packageClean --stacktrace
    - VERSION=$(gradle -q printVersion)
    - ZIP_BASE_NAME=$(gradle -q printZipName)
    - DISPLAY_NAME=$(gradle -q printDisplayName)
    - echo "ZIP_BASE_NAME=${ZIP_BASE_NAME}" >> build.env
    - echo "DISPLAY_NAME=${DISPLAY_NAME}" >> build.env
    - echo "VERSION=${VERSION}" >> build.env
    - echo "URL=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${DISPLAY_NAME}/${VERSION}" >> build.env

  cache:
    key: "$CI_COMMIT_REF_NAME"
    policy: push
    paths:
      - build
      - .gradle


  coverage: '/Total.*?([0-9]{1,3})%/'

  artifacts:
    when: always
    expire_in: 1 week
    paths:
      - build/*.zip
    reports:
      dotenv: build.env # Save environment variables for use in later stages



latex:
  extends: .base-build

  needs:
    - job: plugin

  image: registry.gitlab.com/islandoftex/images/texlive:TL2021-2021-08-15-04-05

  variables:
    SRC_FILE: "./sources/latex/doc.tex"
    OUT_DIR: "./build"

  script:
    - echo "Building latex documentation for version ${VERSION}"
    - sed -i "s|%%VERSION_NUMBER%%|${VERSION}|g" $SRC_FILE
    - mkdir $OUT_DIR -p
    - pdflatex -output-directory $OUT_DIR $SRC_FILE
    - pdflatex -output-directory $OUT_DIR $SRC_FILE # second compile is needed for tableofcontents

    - echo "LATEX_BUILD=true" >> build.env

  artifacts:
    when: always
    expire_in: 1 week
    paths:
      - $OUT_DIR/doc.pdf

  allow_failure: true

#############
# RELEASE
#############
upload:zip:
  extends: .base-release

  needs:
    - job: plugin
      optional: true



  script:
    - ZIP_NAME=${ZIP_BASE_NAME}.zip

    # Sanity checks
    - echo ${URL}
    - echo ${ZIP_NAME}

    - >
      code=$(
      curl
      -s
      -o /dev/null
      -w "%{http_code}"
      -X GET
      -H "JOB-TOKEN: $CI_JOB_TOKEN"
      $URL/$ZIP_NAME)

    - >
      if [ "$code" == "200" ]; then
      printf "
      ${RED}\n##########################################\n
      ERROR: File already exists! \n
      ##########################################\n\n
      Please delete the file ${ZIP_NAME} or increase the version number first! \n\n
      ${NC}";
      exit 1; fi;

    - > # Upload zip file
      curl
      --header "JOB-TOKEN: $CI_JOB_TOKEN"
      --upload-file ./build/${ZIP_NAME}
      $URL/$ZIP_NAME

upload:pdf:
  extends: .base-release

  needs:
    - job: plugin
      optional: true
    - job: latex
      optional: true


  script:
    - PDF_NAME=${ZIP_BASE_NAME}-doc.pdf

    # Sanity checks
    - echo ${URL}
    - echo ${PDF_NAME}

    - >
      code=$(
      curl
      -s
      -o /dev/null
      -w "%{http_code}"
      -X GET
      -H "JOB-TOKEN: $CI_JOB_TOKEN"
      $URL/$PDF_NAME)

    - >
      if [ "$code" == "200" ]; then
      printf "
      ${RED}\n##########################################\n
      ERROR: File already exists! \n
      ##########################################\n\n
      Please delete the file ${PDF_NAME} or increase the version number first! \n\n
      ${NC}";
      exit 1; fi;

    - > # Upload documentation
      curl
      --header "JOB-TOKEN: $CI_JOB_TOKEN"
      --upload-file ./build/doc.pdf
      $URL/$PDF_NAME

  allow_failure: true