image: mcr.microsoft.com/dotnet/sdk:6.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](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-6.0-runtime -y
#    - choco install dotnet-6.0-sdk -y
#    - choco install innosetup -y
#  artifacts:
#    paths:
#      - "$INSTALLER_OUTPUT_DIR/$INSTALLER_FILE_NAME"
#  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"
#  tags:
#    - windows

#upload-windows-updater-release:
#  stage: upload-windows-updater-release
#  rules:
#    - if: '$CI_COMMIT_TAG'
#  image: 
#    name: amazon/aws-cli
#    entrypoint: [""]
#  script:  
#    - sed -i "s/_INSTALLER_FILE_NAME_/$INSTALLER_FILE_NAME/g" releases-v3.json
#    - sed -i "s/_VERSION_/$CI_COMMIT_TAG/g" releases-v3.json
#    - 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 "releases-v3.json"

#docker-build:
#  # Use the official docker image.
#  image: docker:latest
#  stage: build
#  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_REF_SLUG"
#        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
#      exists:
#        - Dockerfile
