# vs 2017 image because Windows SDK 10.0.15063.0 is not available on vs 2015 image
image: Visual Studio 2017

environment:
  GYP_MSVS_VERSION: 2015

  matrix:
    - node_version: '6'
    - node_version: '8'
    - node_version: '10'

install:
  # install the right node version
  - ps: Install-Product node $env:node_version $env:platform
  - ps: echo "running node $env:node_version on $env:platform"
  - ps: | 
        cmd /c "npm install -g node-gyp node-pre-gyp-github npm-windows-upgrade 2>&1"
        if ($LASTEXITCODE -NE 0) { throw "Failed installing node-gyp node-pre-gyp-github npm-windows-upgrade"; }
  # update npm and node-gyp, some configurations wont build without an up to date node-gyp
  - ps: npm-windows-upgrade --npm-version latest
  - ps: npm -v
  - ps: node-gyp -v
  - ps: $publish_binary = 0
  - ps: if ($env:appveyor_repo_tag -match "true" -and $env:appveyor_repo_tag_name -match '^v?[0-9]') { $publish_binary=1; }
  - ps: echo "tag $env:appveyor_repo_tag_name branch $env:APPVEYOR_REPO_BRANCH"
  - ps: |
      # https://github.com/mapbox/node-pre-gyp/issues/209#issuecomment-217690537
      if (($env:platform -ieq "x86") -and ($env:node_version -match "^4")) {
        echo "Configuring NPM cafile to make Node 4 x86 builds work."
        npm config set -g cafile=package.json
        npm config set -g strict-ssl=false
      }

build_script:
  - ps: echo "publish $publish_binary"
  - ps: |
      $ErrorActionPreference = 'Continue'
      $uwp_modules = @("windows.foundation", "windows.devices.bluetooth", "windows.devices.bluetooth.advertisement", "windows.devices.bluetooth.genericattributeprofile", "windows.devices.enumeration", "windows.devices.radios", "windows.storage.streams")
      foreach ($module in $uwp_modules) {
        $path = "./uwp/" + $module
        cd $path
        echo "============================== start building $module =============================="
        Convert-Path .
        node-gyp -v
        npm install --build-from-source 2>&1 | write-host
        if ($LASTEXITCODE -NE 0) { throw "Failed building $module"; }
        ./node_modules/.bin/node-pre-gyp package 2>&1 | write-host
        if ($LASTEXITCODE -NE 0) { throw "Failed packaging $module"; }
        if ($publish_binary -Eq "1") {
          node-pre-gyp-github publish --release 2>&1 | write-host
          if (-Not ($?)) {
            throw "publishing failed";
          }
        }
        cd ../..
      }
      echo "done."
platform:
  - x86
  - x64
