image: mcr.microsoft.com/dotnet/sdk:8.0

stages:
  - build
  - test
  - build-installer
  - upload-builds
  - release
  - publish-medusa-package

variables:
  project: "NadekoBot"
  tests: "NadekoBot.Tests"
  LINUX_X64_OUTPUT_DIR: "nadekobot-linux-x64"
  LINUX_X64_RELEASE: "$CI_COMMIT_TAG-linux-x64-build.tar"
  LINUX_ARM64_OUTPUT_DIR: "nadekobot-linux-arm64"
  LINUX_ARM64_RELEASE: "$CI_COMMIT_TAG-linux-arm64-build.tar"
  MACOS_X64_OUTPUT_DIR: "nadekobot-osx-x64"
  MACOS_X64_RELEASE: "$CI_COMMIT_TAG-osx-x64-build.tar"
  MACOS_ARM64_OUTPUT_DIR: "nadekobot-osx-arm64"
  MACOS_ARM64_RELEASE: "$CI_COMMIT_TAG-osx-arm64-build.tar"
  WIN_X64_OUTPUT_DIR: "nadekobot-windows-x64"
  WIN_X64_RELEASE: "$CI_COMMIT_TAG-windows-x64-build.zip"
  WIN_ARM64_OUTPUT_DIR: "nadekobot-windows-arm64"
  WIN_ARM64_RELEASE: "$CI_COMMIT_TAG-windows-arm64-build.zip"
  PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/NadekoBot-build/${CI_COMMIT_TAG}"
  INSTALLER_OUTPUT_DIR: "nadeko-installers/${CI_COMMIT_TAG}"
  INSTALLER_FILE_NAME: "nadeko-setup-${CI_COMMIT_TAG}.exe"

build:
  stage: build
  script:
    - |
      VERSION_STRING=""
      if [ -n "$CI_COMMIT_TAG" ]; then
        VERSION_STRING="-p:Version=$CI_COMMIT_TAG"
      fi
    - "dotnet publish -c Release -r linux-x64 --self-contained $VERSION_STRING -o $LINUX_X64_OUTPUT_DIR src/NadekoBot/NadekoBot.csproj"
    - "dotnet publish -c Release -r linux-arm64 --self-contained $VERSION_STRING -o $LINUX_ARM64_OUTPUT_DIR src/NadekoBot/NadekoBot.csproj"
    - "dotnet publish -c Release -r win-x64 --self-contained $VERSION_STRING -o $WIN_X64_OUTPUT_DIR src/NadekoBot/NadekoBot.csproj"
    - "dotnet publish -c Release -r win-arm64 --self-contained $VERSION_STRING -o $WIN_ARM64_OUTPUT_DIR src/NadekoBot/NadekoBot.csproj"
    - "dotnet publish -c Release -r osx-x64 --self-contained $VERSION_STRING -o $MACOS_X64_OUTPUT_DIR src/NadekoBot/NadekoBot.csproj"
    - "dotnet publish -c Release -r osx-arm64 --self-contained $VERSION_STRING -o $MACOS_ARM64_OUTPUT_DIR src/NadekoBot/NadekoBot.csproj"
  artifacts:
    paths:
      - "$LINUX_X64_OUTPUT_DIR/"
      - "$LINUX_ARM64_OUTPUT_DIR/"
      - "$WIN_X64_OUTPUT_DIR/"
      - "$WIN_ARM64_OUTPUT_DIR/"
      - "$MACOS_X64_OUTPUT_DIR/"
      - "$MACOS_ARM64_OUTPUT_DIR/"

upload-builds:
 stage: upload-builds
 image: alpine:latest
 rules:
   - if: $CI_COMMIT_TAG
 script:
   - apk add --no-cache curl tar zip
   - "tar cvf $LINUX_X64_RELEASE $LINUX_X64_OUTPUT_DIR/*"
   - "tar cvf $LINUX_ARM64_RELEASE $LINUX_ARM64_OUTPUT_DIR/*"
   - "tar cvf $MACOS_X64_RELEASE $MACOS_X64_OUTPUT_DIR/*"
   - "tar cvf $MACOS_ARM64_RELEASE $MACOS_ARM64_OUTPUT_DIR/*"
   - "zip -r $WIN_X64_RELEASE $WIN_X64_OUTPUT_DIR/*"
   - "zip -r $WIN_ARM64_RELEASE $WIN_ARM64_OUTPUT_DIR/*"
   - "mv $INSTALLER_OUTPUT_DIR/$INSTALLER_FILE_NAME $INSTALLER_FILE_NAME"
   - |
     curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file $LINUX_X64_RELEASE $PACKAGE_REGISTRY_URL/$LINUX_X64_RELEASE
   - |
     curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file $LINUX_ARM64_RELEASE $PACKAGE_REGISTRY_URL/$LINUX_ARM64_RELEASE
   - |
     curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file $WIN_X64_RELEASE $PACKAGE_REGISTRY_URL/$WIN_X64_RELEASE
   - |
     curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file $WIN_ARM64_RELEASE $PACKAGE_REGISTRY_URL/$WIN_ARM64_RELEASE
   - |
     curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file $MACOS_X64_RELEASE $PACKAGE_REGISTRY_URL/$MACOS_X64_RELEASE
   - |
     curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file $MACOS_ARM64_RELEASE $PACKAGE_REGISTRY_URL/$MACOS_ARM64_RELEASE
   - |
     curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file $INSTALLER_FILE_NAME $PACKAGE_REGISTRY_URL/$INSTALLER_FILE_NAME

release:
 stage: release
 image: registry.gitlab.com/gitlab-org/release-cli:latest
 rules:
   - if: $CI_COMMIT_TAG
 script:
   - |
     release-cli create --name "NadekoBot v$CI_COMMIT_TAG" --description "## [Changelog](https://gitlab.com/kwoth/nadekobot/-/blob/v5/CHANGELOG.md#$(echo "$CI_COMMIT_TAG" | sed "s/\.//g")-$(date +%d%m%Y))" --tag-name $CI_COMMIT_TAG \
       --assets-link "{\"name\":\"${LINUX_X64_RELEASE}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${LINUX_X64_RELEASE}\"}" \
       --assets-link "{\"name\":\"${LINUX_ARM64_RELEASE}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${LINUX_ARM64_RELEASE}\"}" \
       --assets-link "{\"name\":\"${WIN_X64_RELEASE}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${WIN_X64_RELEASE}\"}" \
       --assets-link "{\"name\":\"${WIN_ARM64_RELEASE}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${WIN_ARM64_RELEASE}\"}" \
       --assets-link "{\"name\":\"${MACOS_X64_RELEASE}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${MACOS_X64_RELEASE}\"}" \
       --assets-link "{\"name\":\"${MACOS_ARM64_RELEASE}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${MACOS_ARM64_RELEASE}\"}" \
       --assets-link "{\"name\":\"${INSTALLER_FILE_NAME}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${INSTALLER_FILE_NAME}\"}"

test:
  stage: test
  variables:
    tests_path: "src/$tests"
  script:
    - "cd $tests_path"
    - "dotnet test"

build-installer:
 stage: build-installer
 rules:
   - if: "$CI_COMMIT_TAG"
 image: scottyhardy/docker-wine
 before_script:
   - choco install dotnet-runtime --version=8.0.4 -y
   - choco install dotnet-sdk --version=8.0.204 -y
   - choco install innosetup -y
 artifacts:
   paths:
     - "$INSTALLER_OUTPUT_DIR/$INSTALLER_FILE_NAME"
 script:
   - dotnet clean
   - dotnet restore -f --no-cache -v n
   - dotnet publish -c Release --self-contained --runtime win-x64 /p:Version=$CI_COMMIT_TAG src/NadekoBot
   - $env:NADEKOBOT_INSTALL_VERSION = $CI_COMMIT_TAG
   - iscc.exe "/O+" ".\exe_builder.iss"
 tags:
   - saas-windows-medium-amd64

publish-medusa-package:
 stage: publish-medusa-package
 allow_failure: true
 rules:
   - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
     when: never
   - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_TAG
 script:
   - LAST_TAG=$(git describe --tags --abbrev=0)
   - if [ $CI_COMMIT_TAG ];then MEDUSA_VERSION="$CI_COMMIT_TAG"; else MEDUSA_VERSION="$LAST_TAG-alpha$CI_COMMIT_SHORT_SHA"; fi
   - cd src/Nadeko.Medusa/
   - dotnet pack -c Release /p:Version=$MEDUSA_VERSION -o bin/Release/packed
   - dotnet nuget push bin/Release/packed/ --source https://www.myget.org/F/nadeko/api/v2/package --api-key "$MYGET_API_KEY"

docker-build:
 # Use the official docker image.
 image: docker:latest
 stage: build
 allow_failure: true
 services:
   - docker:dind
 before_script:
   - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
 # Default branch leaves tag empty (= latest tag)
 # All other branches are tagged with the escaped branch name (commit ref slug)
 script:
   - |
     if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
       tag=""
       echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
     else
       tag=":$CI_COMMIT_SHA"
       echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
     fi
   - docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" .
   - docker push "$CI_REGISTRY_IMAGE${tag}"
 # Run this job in a branch where a Dockerfile exists
 rules:
   - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_TAG
     exists:
       - Dockerfile
