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

stages:
  - build
  - test
  - upload-builds
  - release
  - publish-windows
  - upload-windows-updater-release

variables:
  project: "NadekoBot"
  tests: "NadekoBot.Tests"
  LINUX_X64_OUTPUT_DIR: "nadekobot-linux-x64"
  LINUX_X64_RELEASE: "$CI_COMMIT_TAG-linux-x64-build.tar"
  WIN_X64_OUTPUT_DIR: "nadekobot-windows-x64"
  WIN_X64_RELEASE: "$CI_COMMIT_TAG-windows-x64-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:
    - "dotnet publish -c Release -r linux-x64 -o $LINUX_X64_OUTPUT_DIR src/NadekoBot/NadekoBot.csproj"
    - "dotnet publish -c Release -r win7-x64 -o $WIN_X64_OUTPUT_DIR src/NadekoBot/NadekoBot.csproj"
  artifacts:
    paths:
      - "$LINUX_X64_OUTPUT_DIR/"
      - "$WIN_X64_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/*"
    - "zip -r $WIN_X64_RELEASE $WIN_X64_OUTPUT_DIR/*"
    - |
      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 $WIN_X64_RELEASE $PACKAGE_REGISTRY_URL/$WIN_X64_RELEASE

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 can be found [here](https://gitlab.com/Kwoth/nadekobot/-/blob/v3/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\":\"${WIN_X64_RELEASE}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${WIN_X64_RELEASE}\"}"

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

publish-windows:
  stage: publish-windows 
  rules:
    - if: '$CI_COMMIT_TAG'
  image: scottyhardy/docker-wine
  before_script:
    - choco install dotnet-5.0-runtime -y
    - choco install dotnet-5.0-sdk -y
    - choco install innosetup -y
  artifacts:
    paths:
      - "$INSTALLER_OUTPUT_DIR/$INSTALLER_FILE_NAME"
      - "$INSTALLER_OUTPUT_DIR/releases-v3.json"
  script:
    - dotnet clean
    - dotnet restore
    - dotnet publish -c Release --runtime win7-x64 /p:Version=$CI_COMMIT_TAG src/NadekoBot
    - $env:NADEKOBOT_INSTALL_VERSION = $CI_COMMIT_TAG
    - iscc.exe "/O+" ".\exe_builder.iss"
    - echo "{ \"VersionName\":\"$CI_COMMIT_TAG\", \"DownloadLink\":\"https://cdn.nadeko.bot/dl/bot/${INSTALLER_FILE_NAME}\" }" > "${INSTALLER_OUTPUT_DIR}/releases-v3.json"
  tags:
    - windows

upload-windows-updater-release:
  stage: upload-windows-updater-release
  rules:
    - if: '$CI_COMMIT_TAG'
  image: 
    name: amazon/aws-cli
    entrypoint: [""]
  script:
    - aws --version
    - aws --endpoint-url $AWS_SERVICE_URL s3api put-object --bucket "$AWS_BUCKET_NAME" --key "dl/bot/$INSTALLER_FILE_NAME" --acl public-read --body "$INSTALLER_OUTPUT_DIR/$INSTALLER_FILE_NAME"
    - aws --endpoint-url $AWS_SERVICE_URL s3api put-object --bucket "$AWS_BUCKET_NAME" --key "dl/bot/releases-v3.json" --acl public-read --body "$INSTALLER_OUTPUT_DIR/releases-v3.json"