version: 0.3.2.{build}

os:
- Windows Server 2012 R2

shallow_clone: true

environment:
  LR_EXTERNAL: c:\external
  CURL_VER: 7.74.0

  matrix:
  - LUA: "lua 5.1"
  - LUA: "lua 5.2"
  - LUA: "lua 5.3"
  - LUA: "lua 5.4"

platform:
  - x64
  - x86
  # - mingw

cache:
  - c:\hererocks -> appveyor.yml
  - c:\external -> appveyor.yml

install:
  - set PATH=C:\Python27\Scripts;%LR_EXTERNAL%;%PATH%
  - if /I "%platform%"=="x86" set HR_TARGET=vs_32
  - if /I "%platform%"=="x64" set HR_TARGET=vs_64
  - if /I "%platform%"=="mingw" set HR_TARGET=mingw
  - if /I "%platform%"=="mingw" set PATH=C:\MinGW\bin;%PATH%
  - if not exist "%LR_EXTERNAL%" (
      mkdir "%LR_EXTERNAL%" &&
      mkdir "%LR_EXTERNAL%\lib" &&
      mkdir "%LR_EXTERNAL%\include"
    )
  - if not exist c:\hererocks (
      pip install hererocks &&
      hererocks c:\hererocks --%LUA% --target %HR_TARGET% -rlatest
    )
  - call c:\hererocks\bin\activate
  - luarocks show luarocks-fetch-gitrec >nul 2>&1 || luarocks install luarocks-fetch-gitrec

before_build:
  # external deps
  - call .appveyor\install_curl.bat

build_script:
  - set LUA_CURL_VER=scm-0
  - if "%APPVEYOR_REPO_TAG%" == "true" set LUA_CURL_VER=%APPVEYOR_REPO_TAG_NAME:~1%-1
  - echo "Making lua-curl-%LUA_CURL_VER% ..."
  - luarocks make rockspecs/lua-curl-%LUA_CURL_VER%.rockspec

before_test:
  # test deps
  - if "%LUA%"=="lua 5.1" luarocks show bit32 >nul 2>&1 || luarocks install bit32
  - luarocks show lunitx >nul 2>&1 || luarocks install lunitx
  - luarocks show dkjson >nul 2>&1 || luarocks install dkjson
  - luarocks show luafilesystem >nul 2>&1 || luarocks install luafilesystem
  - luarocks show lua-path >nul 2>&1 || luarocks install lua-path
  - luarocks show luarocks-fetch-gitrec > /dev/null 2>&1 || luarocks install luarocks-fetch-gitrec
  - luarocks show lua-http-parser  > /dev/null 2>&1 || luarocks install lua-http-parser || luarocks install test/deps/lua-http-parser-2.7-1.rockspec
  - luarocks show pegasus >nul 2>&1 || luarocks install pegasus http.parser 
    --server=http://luarocks.org/manifests/moteus
  - luarocks show pegasus-router >nul 2>&1 || luarocks install pegasus-router 
    --server=http://luarocks.org/dev
  - ps: $TestServer = Start-Process lua -ArgumentList test/server.lua -RedirectStandardOutput "$env:APPVEYOR_BUILD_FOLDER\server.stdout.txt" -RedirectStandardError "$env:APPVEYOR_BUILD_FOLDER\server.stderr.txt" -PassThru
  - curl -s http://127.0.0.1:7090/get

test_script:
  - echo "Testing..."
  - cd %APPVEYOR_BUILD_FOLDER%\test
  - lua run.lua
  - lua test_pause02.c.lua
  - lua test_multi_callback.lua
  - lua test_multi_nested_callback.lua

after_test:
  - cd %APPVEYOR_BUILD_FOLDER%
  - .appveyor\pack_artifact.bat lua-curl bin-rock

on_failure:
  - ps: Stop-Process -Id $TestServer.Id
  - ps: $path = "$env:APPVEYOR_BUILD_FOLDER\server.stderr.txt"; if (Test-Path $path -PathType Leaf) { Push-AppveyorArtifact $path; } else { echo "File $path does not exist"; }
  - ps: $path = "$env:APPVEYOR_BUILD_FOLDER\server.stdout.txt"; if (Test-Path $path -PathType Leaf) { Push-AppveyorArtifact $path; } else { echo "File $path does not exist"; }

on_finish:
  - curl -s http://127.0.0.1:7090/get
  - ps: Stop-Process -Id $TestServer.Id
