# SPDX-FileCopyrightText: 2023 Nicolas Fella <nicolas.fella@gmx.de>
# SPDX-FileCopyrightText: 2023 Simon Redman <simon@ergotech.com>
# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#
# Test this pipeline locally by:
# - Set up Docker on your machine, if not already set up
# - Set up gitlab-runner, as described here: https://stackoverflow.com/a/52724374
# - Run `gitlab-runner exec docker --docker-privileged assembleDebug`

image: openjdk:11-jdk

variables:
  ANDROID_COMPILE_SDK: "31"
  ANDROID_BUILD_TOOLS: "30.0.3"
  ANDROID_SDK_TOOLS:   "6609375"

before_script:
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
  - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-6609375_latest.zip
  - unzip -d cmdline-tools android-sdk.zip
  - echo y | cmdline-tools/tools/bin/sdkmanager "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null
  - echo y | cmdline-tools/tools/bin/sdkmanager "platform-tools" >/dev/null
  - echo y | cmdline-tools/tools/bin/sdkmanager "patcher;v4" >/dev/null
  - echo y | cmdline-tools/tools/bin/sdkmanager "emulator" >/dev/null
  - echo y | cmdline-tools/tools/bin/sdkmanager "platform-tools" >/dev/null
  - echo y | cmdline-tools/tools/bin/sdkmanager "tools" >/dev/null
  - echo y | cmdline-tools/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
  - export ANDROID_HOME=$PWD/cmdline-tools
  - export PATH=$PATH:$PWD/cmdline-tools/platform-tools/
  - chmod +x ./gradlew
  # temporarily disable checking for EPIPE error and use yes to accept all licenses
  - set +o pipefail
  - yes | cmdline-tools/tools/bin/sdkmanager --licenses
    # Gradle will automatically download parts of the SDK if they are missing,
    # assuming the licenses are accepted. Even if you don't need it to download
    # anything, it will fail if it detects the liceses are not accepted.
    # https://developer.android.com/studio/intro/update.html#download-with-gradle
    # Unfortunately, it looks for the license acceptances in a different location than
    # the sdkmanager creates them!
    # Copy them to the location Gradle expects to find them to make everything run smoothly.
  - cp -ap "${PWD}/licenses" "${PWD}/cmdline-tools/licenses"
  - set -o pipefail

stages:
  - build
  - test

lintDebug:
  stage: build
  script:
    - ./gradlew generateLicenseReport
    - ./gradlew -Pci --console=plain lintDebug -PbuildDir=lint

assembleDebug:
  stage: build
  script:
    - ./gradlew assembleDebug
  artifacts:
    paths:
    - build/outputs/

debugTests:
  stage: test
  script:
    - ./gradlew -Pci --console=plain testDebug
