platform: x64
version: '{build}'
environment:
  # Keep the path as short as possible, just in case.
  STACK_ROOT: c:\s
  STACK_VER: 1.6.3
  RELEASE_USER: purescript
  RELEASE_REPO: purescript
branches:
  # Only build master and tagged versions, i.e. not feature branches; feature
  # branches already get built after opening a pull request.
  #
  # Note that, unlike Travis CI, there is no need for a semver tag regexp
  # here, as AppVeyor seems to build all tags which are reachable from any
  # of the branches listed below.
  only:
    - master
cache:
- c:\s -> appveyor/cache-buster.txt
install:
- git submodule update --init
- ps: Install-Product node 6
- npm install -g bower

- ps: |
    New-Item -ItemType Directory -Force -Path C:\tools
    $env:Path += ";C:\tools"
    $stackRelease = "stack-$env:STACK_VER-windows-x86_64"
    $downloadUrl = "https://github.com/commercialhaskell/stack/releases/download/v$env:STACK_VER/$stackRelease.zip"
    (New-Object Net.WebClient).DownloadFile($downloadUrl, 'c:\tools\stack.zip')
    pushd c:\tools
    7z x c:\tools\stack.zip stack.exe
    popd

- stack --no-terminal --verbosity=error setup 1>stack-setup.log 2>&1 || type stack-setup.log

build_script:
# Override the default build script.
# In PowerShell it seems to be necessary to redirect stderr to stdout because
# any text sent to stderr seems to cause appveyor to think the build has
# failed.
- echo ""
test_script:
- ps: |
    $stack_extra_flags=""
    if ($env:APPVEYOR_REPO_TAG_NAME)
    {
      $stack_extra_flags+="--flag purescript:RELEASE"
    }
    echo "stack_extra_flags = $stack_extra_flags"

    # This is an incredibly stupid workaround for a bizarre PowerShell
    # 'feature' where any text printed to stderr is treated as the command
    # having failed; see
    # https://stackoverflow.com/questions/10666101/lastexitcode-0-but-false-in-powershell-redirecting-stderr-to-stdout-gives-n
    cmd /c "stack --jobs=1 --no-terminal test --pedantic $stack_extra_flags 2>&1"
on_success:
- ps: |
    function UploadFile
    {
      github-release upload --user $env:RELEASE_USER --repo $env:RELEASE_REPO --tag $env:APPVEYOR_REPO_TAG_NAME --file $args[0] --name $args[0]
    }

    if ($env:APPVEYOR_REPO_TAG_NAME)
    {
      bash ./bundle/build.sh win64

      (New-Object Net.WebClient).DownloadFile('https://github.com/aktau/github-release/releases/download/v0.6.2/windows-amd64-github-release.zip', 'c:\tools\github-release.zip')
      pushd c:\tools
      7z x github-release.zip bin/windows/amd64/github-release.exe
      Copy-Item bin/windows/amd64/github-release.exe github-release.exe
      popd

      pushd bundle
      UploadFile win64.tar.gz
      UploadFile win64.sha
      popd
    }
