version: '{build}'

cache:
- x86_64-4.9.2-release-win32-seh-rt_v4-rev4.7z
- i686-4.9.2-release-win32-dwarf-rt_v4-rev4.7z
- Nim

matrix:
  # We always want 32 and 64-bit compilation
  fast_finish: false

platform:
  - x86
  - x64

install:
  - setlocal EnableExtensions EnableDelayedExpansion

  - IF "%PLATFORM%" == "x86" (
      SET "MINGW_ARCHIVE=i686-4.9.2-release-win32-dwarf-rt_v4-rev4.7z" &
      SET "MINGW_URL=https://sourceforge.net/projects/mingw-w64/files/Toolchains%%20targetting%%20Win32/Personal%%20Builds/mingw-builds/4.9.2/threads-win32/dwarf/i686-4.9.2-release-win32-dwarf-rt_v4-rev4.7z" &
      SET "MINGW_DIR=mingw32"
    ) ELSE (
      IF "%PLATFORM%" == "x64" (
        SET "MINGW_ARCHIVE=x86_64-4.9.2-release-win32-seh-rt_v4-rev4.7z" &
        SET "MINGW_URL=https://sourceforge.net/projects/mingw-w64/files/Toolchains%%20targetting%%20Win64/Personal%%20Builds/mingw-builds/4.9.2/threads-win32/seh/x86_64-4.9.2-release-win32-seh-rt_v4-rev4.7z" &
        SET "MINGW_DIR=mingw64"
      ) else (
        echo "Unknown platform"
      )
    )

  - SET PATH=%CD%\%MINGW_DIR%\bin;%CD%\Nim\bin;%PATH%

  # Unpack mingw
  - IF NOT EXIST "%MINGW_ARCHIVE%" appveyor DownloadFile "%MINGW_URL%" -FileName "%MINGW_ARCHIVE%"
  - 7z x -y "%MINGW_ARCHIVE%" > nul

  # build nim from our own branch - this to avoid the day-to-day churn and
  # regressions of the fast-paced Nim development while maintaining the
  # flexibility to apply patches
  - SET "NEED_REBUILD="

  - IF NOT EXIST "Nim\\.git\\" (
      git clone https://github.com/status-im/Nim.git
    ) ELSE (
      ( cd Nim ) &
      ( git pull ) &
      ( cd .. )
    )

  # Rebuild Nim if HEAD has moved or if we don't yet have a cached version
  - IF NOT EXIST "Nim\\ver.txt" (
      SET NEED_REBUILD=1
    ) ELSE (
      ( CD Nim ) &
      ( git rev-parse HEAD > ..\\cur_ver.txt ) &
      ( fc ver.txt ..\\cur_ver.txt || SET NEED_REBUILD=1 ) &
      ( cd .. )
    )

  - IF NOT EXIST "Nim\\bin\\nim.exe" SET NEED_REBUILD=1
  - IF NOT EXIST "Nim\\bin\\nimble.exe" SET NEED_REBUILD=1

  # after building nim, wipe csources to save on cache space
  - IF DEFINED NEED_REBUILD (
      cd Nim &
      ( IF EXIST "csources" rmdir /s /q csources ) &
      git clone --depth 1 https://github.com/nim-lang/csources &
      cd csources &
      ( IF "%PLATFORM%" == "x64" ( build64.bat ) else ( build.bat ) ) &
      cd .. &
      bin\nim c koch &
      koch boot -d:release &
      koch nimble &
      git rev-parse HEAD > ver.txt &
      rmdir /s /q csources
    )

build_script:
  - cd C:\projects\%APPVEYOR_PROJECT_SLUG%
  - nimble install -y
test_script:
  - nimble test

deploy: off