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

stages:
  - build
  - test
  - build-installer
  - upload-builds
  - release

variables:
  project: "EllieBot"
  tests: "EllieBot.Tests"
  LINUX_X64_OUTPUT_DIR: "elliebot-linux-x64"
  LINUX_X64_RELEASE: "$CI_COMMIT_TAG-linux-x64-build.tar"
  LINUX_ARM64_OUTPUT_DIR: "elliebot-linux-arm64"
  LINUX_ARM64_RELEASE: "$CI_COMMIT_TAG-linux-arm64-build.tar"
  MACOS_X64_OUTPUT_DIR: "elliebot-osx-x64"
  MACOS_X64_RELEASE: "$CI_COMMIT_TAG-osx-x64-build.tar"
  MACOS_ARM64_OUTPUT_DIR: "elliebot-osx-arm64"
  MACOS_ARM64_RELEASE: "$CI_COMMIT_TAG-osx-arm64-build.tar"
  WIN_X64_OUTPUT_DIR: "elliebot-windows-x64"
  WIN_X64_RELEASE: "$CI_COMMIT_TAG-windows-x64-build.zip"
  WIN_ARM64_OUTPUT_DIR: "elliebot-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/EllieBot-build/${CI_COMMIT_TAG}"
  INSTALLER_OUTPUT_DIR: "ellie-installers/${CI_COMMIT_TAG}"
  INSTALLER_FILE_NAME: "ellie-setup-${CI_COMMIT_TAG}.exe"

build:
  stage: build
  script:
    - "dotnet publish -c Release -r linux-x64 --self-contained -o $LINUX_X64_OUTPUT_DIR src/EllieBot/EllieBot.csproj"
    - "dotnet publish -c Release -r linux-arm64 --self-contained -o $LINUX_ARM64_OUTPUT_DIR src/EllieBot/EllieBot.csproj"
    - "dotnet publish -c Release -r win-x64 --self-contained -o $WIN_X64_OUTPUT_DIR src/EllieBot/EllieBot.csproj"
    - "dotnet publish -c Release -r win-arm64 --self-contained -o $WIN_ARM64_OUTPUT_DIR src/EllieBot/EllieBot.csproj"
    - "dotnet publish -c Release -r osx-x64 --self-contained -o $MACOS_X64_OUTPUT_DIR src/EllieBot/EllieBot.csproj"
    - "dotnet publish -c Release -r osx-arm64 --self-contained -o $MACOS_ARM64_OUTPUT_DIR src/EllieBot/EllieBot.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 "EllieBot v$CI_COMMIT_TAG" --description "## [Changelog](https://gitlab.com/toastiet0ast/EllieBot/-/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/EllieBot
   - $env:ELLIEBOT_INSTALL_VERSION = $CI_COMMIT_TAG
   - iscc.exe "/O+" ".\exe_builder.iss"
 tags:
   - saas-windows-medium-amd64