stages:
  - hlint
  - test
  - release

variables:
  GIT_SSL_NO_VERIFY: "1"

  # Commit of ghc/ci-images repository from which to pull Docker images
  DOCKER_REV: 8d0224e6b2a08157649651e69302380b2bd24e11

############################################################
# CI Step
############################################################

.debian:
  image: "registry.gitlab.haskell.org/ghc/ci-images/x86_64-linux-deb9:$DOCKER_REV"
  tags:
    - x86_64-linux
  variables:
    OS: "LINUX"
    ARCH: "64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"
    CROSS: ""

.alpine:64bit:
  image: "alpine:3.12"
  tags:
    - x86_64-linux
  variables:
    OS: "LINUX"
    ARCH: "64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.alpine:32bit:
  image: "i386/alpine:3.12"
  tags:
    - x86_64-linux
  variables:
    OS: "LINUX"
    ARCH: "32"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.linux:armv7:
  image: "registry.gitlab.haskell.org/ghc/ci-images/armv7-linux-deb10:$DOCKER_REV"
  tags:
    - armv7-linux
  variables:
    OS: "LINUX"
    ARCH: "ARM"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.linux:aarch64:
  image: "registry.gitlab.haskell.org/ghc/ci-images/aarch64-linux-deb10:$DOCKER_REV"
  tags:
    - aarch64-linux
  variables:
    OS: "LINUX"
    ARCH: "ARM64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.darwin:
  tags:
    - x86_64-darwin
  variables:
    OS: "DARWIN"
    ARCH: "64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.darwin:aarch64:
  tags:
    - aarch64-darwin-m1
  variables:
    OS: "DARWIN"
    ARCH: "ARM64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.freebsd:
  tags:
    - x86_64-freebsd
  variables:
    OS: "FREEBSD"
    ARCH: "64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.windows:
  tags:
    - new-x86_64-windows
  variables:
    OS: "WINDOWS"
    ARCH: "64"
    CABAL_DIR: "$CI_PROJECT_DIR/cabal"

.root_cleanup:
  after_script:
    - bash ./.gitlab/after_script.sh

.test_ghcup_version:
  script:
    - bash ./.gitlab/script/ghcup_version.sh
  variables:
    JSON_VERSION: "0.0.5"
  artifacts:
    expire_in: 2 week
    paths:
      - golden
    when: on_failure

# .test_ghcup_scoop:
  # script:
    # - cl /O1 scoop-better-shimexe/shim.c

.test_ghcup_version:linux:
  extends:
    - .test_ghcup_version
    - .debian
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh

.test_ghcup_version:linux32:
  extends:
    - .test_ghcup_version
    - .alpine:32bit
  before_script:
    - ./.gitlab/before_script/linux/alpine/install_deps.sh

.test_ghcup_version:armv7:
  extends:
    - .test_ghcup_version
    - .linux:armv7
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh

.test_ghcup_version:aarch64:
  extends:
    - .test_ghcup_version
    - .linux:aarch64
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh

.test_ghcup_version:darwin:
  extends:
    - .test_ghcup_version
    - .darwin
    - .root_cleanup
  before_script:
    - ./.gitlab/before_script/darwin/install_deps.sh

.test_ghcup_version:darwin:aarch64:
  extends:
    - .test_ghcup_version
    - .darwin:aarch64
    - .root_cleanup
  script: |
    set -Eeuo pipefail
    function runInNixShell() {
      time nix-shell $CI_PROJECT_DIR/.gitlab/shell.nix \
        -I nixpkgs=https://github.com/angerman/nixpkgs/archive/75f7281738b.tar.gz \
        --argstr system "aarch64-darwin" \
        --pure \
        --keep CI_PROJECT_DIR \
        --keep MACOSX_DEPLOYMENT_TARGET \
        --keep JSON_VERSION \
        --keep ARTIFACT \
        --keep OS \
        --keep ARCH \
        --keep CABAL_DIR \
        --keep GHC_VERSION \
        --keep CABAL_VERSION \
        --run "$1" 2>&1
    }
    runInNixShell ./.gitlab/before_script/darwin/install_deps.sh 2>&1
    runInNixShell ./.gitlab/script/ghcup_version.sh 2>&1

.test_ghcup_version:freebsd:
  extends:
    - .test_ghcup_version
    - .freebsd
    - .root_cleanup
  before_script:
    - ./.gitlab/before_script/freebsd/install_deps.sh

.test_ghcup_version:windows:
  extends:
    - .test_ghcup_version
    - .windows
    - .root_cleanup
  before_script:
    - set CABAL_DIR="$CI_PROJECT_DIR/cabal"
    - bash ./.gitlab/before_script/windows/install_deps.sh

# .test_ghcup_scoop:windows:
  # extends:
    # - .windows
    # - .test_ghcup_scoop
    # - .root_cleanup

.release_ghcup:
  script:
    - bash ./.gitlab/script/ghcup_release.sh
  artifacts:
    expire_in: 2 week
    paths:
      - out
  only:
    - tags
  variables:
    JSON_VERSION: "0.0.5"

######## stack test ########

test:linux:stack:
  stage: test
  before_script:
    - ./.gitlab/before_script/linux/install_deps_minimal.sh
  script:
    - ./.gitlab/script/ghcup_stack.sh
  extends:
    - .debian
  needs: []

######## bootstrap test ########

test:linux:bootstrap_script:
  stage: test
  before_script:
    - ./.gitlab/before_script/linux/install_deps_minimal.sh
  script:
    - ./.gitlab/script/ghcup_bootstrap.sh
  variables:
    GHC_VERSION: "8.10.5"
    CABAL_VERSION: "3.4.0.0"
  extends:
    - .debian
    - .root_cleanup
  needs: []

test:windows:bootstrap_powershell_script:
  stage: test
  script:
    - ./bootstrap-haskell.ps1 -InstallDir $CI_PROJECT_DIR -BootstrapUrl $CI_PROJECT_DIR/bootstrap-haskell -InBash
  after_script:
    - "[Environment]::SetEnvironmentVariable('GHCUP_INSTALL_BASE_PREFIX', $null, [System.EnvironmentVariableTarget]::User)"
    - "[Environment]::SetEnvironmentVariable('GHCUP_MSYS2', $null, [System.EnvironmentVariableTarget]::User)"
    - "[Environment]::SetEnvironmentVariable('CABAL_DIR', $null, [System.EnvironmentVariableTarget]::User)"
    - bash ./.gitlab/after_script.sh
  variables:
    GHC_VERSION: "8.10.5"
    CABAL_VERSION: "3.4.0.0"
  extends:
    - .windows
  needs: []

######## linux test ########

test:linux:recommended:
  stage: test
  extends: .test_ghcup_version:linux
  variables:
    GHC_VERSION: "8.10.5"
    CABAL_VERSION: "3.4.0.0"
  needs: []

test:linux:latest:
  stage: test
  extends: .test_ghcup_version:linux
  variables:
    GHC_VERSION: "9.0.1"
    CABAL_VERSION: "3.4.0.0"
  needs: []

test:linux:cross-armv7:
  stage: test
  extends:
    - .test_ghcup_version
    - .debian
  variables:
    GHC_VERSION: "8.10.4"
    GHC_TARGET_VERSION: "8.10.5"
    CABAL_VERSION: "3.4.0.0"
    CROSS: "arm-linux-gnueabihf"
  needs: []
  when: manual
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh
  script:
    - ./.gitlab/script/ghcup_cross.sh


######## linux 32bit test ########

test:linux:recommended:32bit:
  stage: test
  extends: .test_ghcup_version:linux32
  variables:
    GHC_VERSION: "8.10.5"
    CABAL_VERSION: "3.2.0.0"
  needs: []

######## arm tests ########

test:linux:recommended:armv7:
  stage: test
  extends: .test_ghcup_version:armv7
  variables:
    GHC_VERSION: "8.10.4"
    CABAL_VERSION: "3.4.0.0"
    CROSS: ""
  when: manual
  needs: []

test:linux:recommended:aarch64:
  stage: test
  extends: .test_ghcup_version:aarch64
  variables:
    GHC_VERSION: "8.10.4"
    CABAL_VERSION: "3.4.0.0"
    CROSS: ""
  when: manual
  needs: []

######## darwin test ########

test:mac:recommended:
  stage: test
  extends: .test_ghcup_version:darwin
  variables:
    GHC_VERSION: "8.10.4"
    CABAL_VERSION: "3.4.0.0"
  needs: []

test:mac:latest:
  stage: test
  extends: .test_ghcup_version:darwin
  variables:
    GHC_VERSION: "9.0.1"
    CABAL_VERSION: "3.4.0.0"
  needs: []

test:mac:recommended:aarch64:
  stage: test
  extends: .test_ghcup_version:darwin:aarch64
  variables:
    GHC_VERSION: "8.10.5"
    CABAL_VERSION: "3.4.0.0"
  needs: []
  allow_failure: true


######## freebsd test ########

test:freebsd:recommended:
  stage: test
  extends: .test_ghcup_version:freebsd
  variables:
    GHC_VERSION: "8.10.4"
    CABAL_VERSION: "3.4.0.0"
  allow_failure: true # freebsd runners are unreliable
  when: manual
  needs: []

######## windows test ########

test:windows:recommended:
  stage: test
  extends: .test_ghcup_version:windows
  variables:
    GHC_VERSION: "8.10.5"
    CABAL_VERSION: "3.4.0.0"
  needs: []

# test:windows:scoop:
  # stage: test
  # extends: .test_ghcup_scoop:windows
  # needs: []

######## linux release ########

release:linux:64bit:
  stage: release
  needs: ["test:linux:recommended", "test:linux:latest"]
  extends:
    - .alpine:64bit
    - .release_ghcup
  before_script:
    - ./.gitlab/before_script/linux/alpine/install_deps.sh
  variables:
    ARTIFACT: "x86_64-linux-ghcup"
    GHC_VERSION: "8.10.5"
    CABAL_VERSION: "3.4.0.0"


release:linux:32bit:
  stage: release
  needs: ["test:linux:recommended:32bit"]
  extends:
    - .alpine:32bit
    - .release_ghcup
  before_script:
    - ./.gitlab/before_script/linux/alpine/install_deps.sh
  variables:
    ARTIFACT: "i386-linux-ghcup"
    GHC_VERSION: "8.10.5"
    CABAL_VERSION: "3.2.0.0"

release:linux:armv7:
  stage: release
  needs: ["test:linux:recommended:armv7"]
  extends:
    - .linux:armv7
    - .release_ghcup
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh
  variables:
    ARTIFACT: "armv7-linux-ghcup"
    GHC_VERSION: "8.10.4"
    CABAL_VERSION: "3.4.0.0"
    CROSS: ""

release:linux:aarch64:
  stage: release
  needs: ["test:linux:recommended:aarch64"]
  extends:
    - .linux:aarch64
    - .release_ghcup
  before_script:
    - ./.gitlab/before_script/linux/install_deps.sh
  variables:
    ARTIFACT: "aarch64-linux-ghcup"
    GHC_VERSION: "8.10.4"
    CABAL_VERSION: "3.4.0.0"
    CROSS: ""

######## darwin release ########

release:darwin:
  stage: release
  needs: ["test:mac:recommended", "test:mac:latest"]
  extends:
    - .darwin
    - .release_ghcup
    - .root_cleanup
  before_script:
    - ./.gitlab/before_script/darwin/install_deps.sh
  variables:
    ARTIFACT: "x86_64-apple-darwin-ghcup"
    GHC_VERSION: "8.10.5"
    CABAL_VERSION: "3.4.0.0"
    MACOSX_DEPLOYMENT_TARGET: "10.7"

release:darwin:aarch64:
  stage: release
  needs: ["test:mac:recommended:aarch64"]
  extends:
    - .darwin:aarch64
    - .release_ghcup
    - .root_cleanup
  script: |
    set -Eeuo pipefail
    function runInNixShell() {
      time nix-shell $CI_PROJECT_DIR/.gitlab/shell.nix \
        -I nixpkgs=https://github.com/angerman/nixpkgs/archive/75f7281738b.tar.gz \
        --argstr system "aarch64-darwin" \
        --pure \
        --keep CI_PROJECT_DIR \
        --keep MACOSX_DEPLOYMENT_TARGET \
        --keep JSON_VERSION \
        --keep ARTIFACT \
        --keep OS \
        --keep ARCH \
        --keep CABAL_DIR \
        --keep GHC_VERSION \
        --keep CABAL_VERSION \
        --run "$1" 2>&1
    }
    runInNixShell ./.gitlab/before_script/darwin/install_deps.sh 2>&1
    runInNixShell ./.gitlab/script/ghcup_release.sh 2>&1
  variables:
    ARTIFACT: "aarch64-apple-darwin-ghcup"
    GHC_VERSION: "8.10.5"
    CABAL_VERSION: "3.4.0.0"
    MACOSX_DEPLOYMENT_TARGET: "10.7"
  allow_failure: true


######## freebsd release ########

release:freebsd:
  stage: release
  needs: ["test:freebsd:recommended"]
  extends:
    - .freebsd
    - .release_ghcup
    - .root_cleanup
  before_script:
    - ./.gitlab/before_script/freebsd/install_deps.sh
  variables:
    ARTIFACT: "x86_64-portbld-freebsd-ghcup"
    GHC_VERSION: "8.10.5"
    CABAL_VERSION: "3.4.0.0"
  allow_failure: true

######## windows release ########

release:windows:
  stage: release
  needs: ["test:windows:recommended"]
  extends:
    - .windows
    - .release_ghcup
    - .root_cleanup
  before_script:
    - bash ./.gitlab/before_script/windows/install_deps.sh
  variables:
    ARTIFACT: "x86_64-mingw64-ghcup"
    GHC_VERSION: "8.10.5"
    CABAL_VERSION: "3.4.0.0"

######## hlint ########

hlint:
  stage: hlint
  extends:
    - .alpine:64bit
  before_script:
    - ./.gitlab/before_script/linux/alpine/install_deps.sh
  script:
    - ./.gitlab/script/hlint.sh
  variables:
    GHC_VERSION: "8.10.5"
    CABAL_VERSION: "3.4.0.0"
    JSON_VERSION: "0.0.4"
  allow_failure: true
  artifacts:
    expire_in: 2 week
    paths:
      - report.html
    when: on_failure