stages:
  - build
  - pages
  - tarball
  - release

variables:
  TARBALL_PATH_PREFIX: ${CI_PROJECT_NAME}-${CI_COMMIT_TAG}
  TARBALL_NAME: ${TARBALL_PATH_PREFIX}.tar.xz

include:
  - component: gitlab.gnome.org/GNOME/citemplates/release-service@master
    inputs:
      job-stage: release
      dist-job-name: "tarball"
      tarball-artifact-path: "${TARBALL_NAME}"

fedora:
  image: registry.gitlab.gnome.org/gnome/evolution/fedora:v1
  stage: build
  script:
    - export SOURCE=$PWD
    - export PREFIX=$HOME/_prefix
    - mkdir $PREFIX
    - pushd po/ >/dev/null && export TXT=`intltool-update -m 2>&1` && popd >/dev/null
    - if [ ! -z "$TXT" ]; then echo -e "\\033[1;31m" 1>&2; echo "$TXT" 1>&2; echo -e "\\033[0m" 1>&2; exit 1; fi
    - mkdir _build && cd _build
    - cmake -G "Ninja" \
            -DCMAKE_BUILD_TYPE=Release
            -DCMAKE_INSTALL_PREFIX=$PREFIX
            -DENABLE_GOA=ON
            -DENABLE_GTK=ON
            -DENABLE_GTK4=ON
            -DENABLE_EXAMPLES=ON
            -DENABLE_TESTS=ON
            -DENABLE_INTROSPECTION=ON
            -DENABLE_VALA_BINDINGS=ON
            -DENABLE_INSTALLED_TESTS=ON
            -DENABLE_GTK_DOC=ON
            -DENABLE_GI_DOCGEN=ON
            -DWITH_PRIVATE_DOCS=ON
            -DWITH_LIBDB=OFF
            -DWITH_PHONENUMBER=ON
            ..
    - ninja
    - ninja install
    - ctest --force-new-ctest-process --output-on-failure
    - cp $SOURCE/docs/*.html ./docs/
    - cp $SOURCE/docs/*.js ./docs/
  artifacts:
    name: "${CI_PROJECT_NAME}-${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    when: always
    paths:
      - "_build/evolution-data-server-config.h"
      - "_build/docs"

flatpak:
  image: "quay.io/gnome_infrastructure/gnome-runtime-images:gnome-master"
  stage: build
  tags:
    - flatpak
  variables:
    BUNDLE: "evolution-data-server-nightly.flatpak"
    MANIFEST_PATH: "data/org.gnome.EvolutionDataServer.Devel.json"
    FLATPAK_MODULE: "evolution-data-server"
    APP_ID: "org.gnome.EvolutionDataServer.Devel"
    RUNTIME_REPO: "https://nightly.gnome.org/gnome-nightly.flatpakrepo"
  when: manual
  before_script:
    - export PATH="/usr/lib/ccache:$PATH"
    - export CCACHE_BASEDIR="$PWD"
    - export CCACHE_DIR="$PWD/ccache"
  script:
    - jq -M '(.modules[] | select(.name=="evolution-data-server")).sources[0].url = env.CI_REPOSITORY_URL' ${MANIFEST_PATH} > tmp.json && mv tmp.json ${MANIFEST_PATH}
    - jq -M 'del((.modules[] | select(.name=="evolution-data-server")).sources[0].branch)' ${MANIFEST_PATH} > tmp.json && mv tmp.json ${MANIFEST_PATH}
    - jq -M '(.modules[] | select(.name=="evolution-data-server")).sources[0].commit = env.CI_COMMIT_SHA' ${MANIFEST_PATH} > tmp.json && mv tmp.json ${MANIFEST_PATH}

    - flatpak-builder --verbose --keep-build-dirs --user --disable-rofiles-fuse flatpak_app --repo=repo --ccache ${BRANCH:+--default-branch=$BRANCH} ${MANIFEST_PATH}
    - flatpak build-bundle repo ${BUNDLE} --runtime-repo=${RUNTIME_REPO} ${APP_ID} ${BRANCH}
  cache:
    paths:
      - ccache
      - .flatpak-builder
  artifacts:
    name: "Flatpak artifacts"
    expose_as: "Get Flatpak bundle here"
    when: "always"
    paths:
      - "${BUNDLE}"
    expire_in: 14 days

pages:
  image: registry.gitlab.gnome.org/gnome/evolution/fedora:v1
  stage: pages
  dependencies:
    - fedora
  interruptible: false
  variables:
    FLATPAK_MODULE: "evolution-data-server"
  script:
    - "mkdir public"
    - "mv _build/docs/* ./public"
    - "rm -R ./public/reference"
  artifacts:
    paths:
    - public
  rules:
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

tarball:
  stage: tarball
  image: "quay.io/gnome_infrastructure/gnome-runtime-images:gnome-master"
  dependencies:
    - fedora
  allow_failure: false
  script:
    - git config --global --add safe.directory `pwd`
    - git archive --prefix=${TARBALL_PATH_PREFIX}/ HEAD | xz -z > ${TARBALL_NAME}
  artifacts:
    name: "${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    expire_in: 14 days
    paths:
      - "${TARBALL_NAME}"
  rules:
    - if: $CI_COMMIT_TAG && $CI_COMMIT_REF_PROTECTED
