stages:
  - check
  - build
  - docs
  - deploy

.build-default:
  script:
    - meson setup ${MESON_EXTRA_FLAGS} --prefix /usr _build .
    - meson compile -C _build
    - meson test -C _build
    - meson install -C _build
  artifacts:
    reports:
      junit:
        - _build/meson-logs/testlog.junit.xml
        - _build/meson-logs/testlog-*.junit.xml
    name: "json-glib-${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    when: always
    expire_in: 1 week
    paths:
      - "_build/meson-logs"

.fedora-build:
  image: fedora:39
  before_script:
    - dnf install -y ${FEDORA_DEPS}
    - pip3 install --break-system-packages meson==${MESON_VERSION}
    - export PATH="$PATH:$HOME/.local/bin"

.debian-build:
  image: debian:bookworm
  before_script:
    - apt-get update -qq && apt-get install --no-install-recommends -qq -y ${DEBIAN_DEPS}
    - pip3 install --break-system-packages meson==${MESON_VERSION}
    - export PATH="$PATH:$HOME/.local/bin"

.gidocgen-build:
  image: fedora:latest
  before_script:
    - export PATH="$HOME/.local/bin:$PATH"
    - dnf install -y python3 python3-pip python3-wheel gobject-introspection-devel graphviz ninja-build redhat-rpm-config
    - dnf install -y ${PROJECT_DEPS}
    - pip3 install --user meson==${MESON_VERSION} gi-docgen jinja2 Markdown markupsafe packaging pygments toml typogrify
  script:
    - meson setup ${MESON_EXTRA_FLAGS} ${DOCS_FLAGS} _docs .
    - meson compile -C _docs
    - |
      pushd "_docs/${DOCS_PATH}" > /dev/null
      tar cf ${CI_PROJECT_NAME}-docs.tar .
      popd > /dev/null
    - mv _docs/${DOCS_PATH}/${CI_PROJECT_NAME}-docs.tar .
  artifacts:
    when: always
    name: 'Documentation'
    expose_as: 'Download the API reference'
    paths:
      - ${CI_PROJECT_NAME}-docs.tar

reuse:
  image: fedora:latest
  stage: check
  needs: []
  before_script:
    - dnf install -y python3 python3-pip
    - pip3 install --user reuse
    - export PATH="$PATH:$HOME/.local/bin"
  script:
    - reuse --suppress-deprecation lint

debian-stable-x86_64:
  extends:
    - .debian-build
    - .build-default
  stage: build
  needs: []
  variables:
    DEBIAN_DEPS:
      gcc
      gettext
      git
      gobject-introspection
      libgirepository1.0-dev
      libglib2.0-dev
      ninja-build
      python3
      python3-docutils
      python3-pip
      python3-wheel
    MESON_VERSION: "1.2.0"
    MESON_EXTRA_FLAGS: "--buildtype=debug --default-library=both -Dwerror=true -Dglib:werror=false -Dintrospection=enabled -Ddocumentation=disabled"

fedora-latest-x86_64:
  extends:
    - .fedora-build
    - .build-default
  stage: build
  needs: []
  variables:
    FEDORA_DEPS:
      gcc
      gettext
      git
      glib2-devel
      gobject-introspection-devel
      ninja-build
      python3
      python3-docutils
      python3-pip
      python3-wheel
      redhat-rpm-config
    MESON_VERSION: "1.2.0"
    MESON_EXTRA_FLAGS: "--buildtype=debug --default-library=both -Dwerror=true -Dglib:werror=false -Dintrospection=enabled -Ddocumentation=disabled"

reference:
  stage: docs
  needs: []
  extends: .gidocgen-build
  variables:
    PROJECT_DEPS:
      gcc
      gettext
      git
      glib2-devel
      python3-docutils
    MESON_VERSION: "1.2.0"
    MESON_EXTRA_FLAGS: "-Dman=true -Dintrospection=enabled"
    DOCS_FLAGS: -Ddocumentation=enabled
    DOCS_PATH: doc/json-glib-1.0

pages:
  stage: deploy
  needs: ['reference']
  script:
    - mkdir public && cd public
    - tar xf ../${CI_PROJECT_NAME}-docs.tar
  artifacts:
    paths:
      - public
  only:
    - main
