# Copyright (c) 2017 Intel Corporation. All rights reserved.
# Use of this source code is governed by a Apache License
# Version 2.0 license that can be found in the LICENSE file.
version: 2.1.{build}
branches:
  only:
  - development
skip_tags: true
environment:
  nodejs_version: "6"
image: Visual Studio 2015
clone_folder: c:\projects\librealsense
init:
- ps:  >-
      $startTime = (Get-Date)

      echo "Script started on $startTime"
- cmd: 
install:
- ps: Install-Product node $env:nodejs_version
- cmd: >-
    cd c:\projects\librealsense\.. && git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git 

    set OLDPATH=%PATH% && set PATH=%PATH%;c:\projects\depot_tools

    cd c:\projects\librealsense\wrappers\nodejs\tools && npm install

    cd c:\projects\librealsense\wrappers\nodejs && node .\tools\linter.js && set PATH=%OLDPATH% && set OLDPATH=
before_build:
- cmd: >-
    cd c:\projects\librealsense && mkdir build && cd build

    cmake .. -DBUILD_EXAMPLES=true -DBUILD_PYTHON_BINDINGS=true -DBUILD_NODEJS_BINDINGS:BOOL=true -DBUILD_UNIT_TESTS:BOOL=true
build:
  project: ./build/librealsense2.sln
  parallel: true                           # Is not in use on "Free" plans -  https://github.com/appveyor/ci/issues/216. Leaved for future consideration
  verbosity: minimal

#Unit-Test Suite
# Each successful build will invoke a set of predefined unit-test that will run from recorded live test sessions
# The records are available for download from the location specified in the tests, and are arranged in the following convention
# |
# |---| ..
# |   | Records.txt
# |   |
# |   | single_cam
# |               | ..
# |               | Recorded_Test_1
# |               | Recorded_Test_2
# |               | ......
# |
# |   | multi_cam
# |               | ..
# |               | Recorded_Test_3
# |               | Recorded_Test_4
# |               | ......
# |
# The content of Records.txt is a list of all record paths one per line, e.g:
# single_cam/Recorded_Test_1
# single_cam/Recorded_Test_2
# multi_cam/Recorded_Test_3
# multi_cam/Recorded_Test_4
#
test_script:
- ps: >-
    $url_records_path = "https://librealsense.intel.com/rs-tests/lrs_2.8.3/"

    $url_records_list = $url_records_path + "records.txt"

    $out_records_list = "records.txt"

    Invoke-WebRequest -Uri $url_records_list -OutFile $out_records_list

    $records = Get-Content $out_records_list

    mkdir single_cam

    mkdir multi_cam

    foreach ($element in $records)
    {

        $url_cur = $url_records_path + $element

        Invoke-WebRequest -Uri $url_cur -OutFile $element
    }

    $test_failed = @()

    & cmd.exe /c C:/projects/librealsense/build/Debug/live-test.exe -d yes -i [software-device]

    if ($LASTEXITCODE -ne 0)
    {
        $test_failed +=  "software-device"
    }

    foreach ($element in $records) 
    {

        $url_cur = $url_records_path + $element

        & cmd.exe /c C:/projects/librealsense/build/Debug/live-test.exe from $element -d yes -i

        if ($LASTEXITCODE -ne 0)
        {
            $test_failed +=  $element
        }
    }

    if ($test_failed.length -ne 0)
    {
        $err_msg = "The following tests failed: "

        foreach ($elem in $test_failed)
        {

            $err_msg = $err_msg + $elem + ", "

        }

        throw  $err_msg
    }
    # TODO(halton): Add unit test for Node.js binding

#---------------------------------#
#        global handlers          #
#---------------------------------#

# on successful build
on_success:
- ps: >-
      echo Script completed successfuly

# on build failure
on_failure:
- ps: >-
      echo "Script Failed"

# after build failure or success
on_finish:
- ps: >-
      $endTime  = (Get-Date)

      echo "Ended on $endTime"

      ($endTime-$startTime).ToString('''Execution time: ''mm'' min ''ss'' sec''')
