version: 0.11.0.{build}

branches:
  except:
    - v2

install:
    - ps: |
        Write-Host "PowerShell Version:" $PSVersionTable.PSVersion.tostring()
        Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
        Install-Module -Name Pester -RequiredVersion 4.10.1 -Force

init:
    - git config --global core.autocrlf true

build_script:
    - ps: |
        $ErrorActionPreference = 'Stop'
        Update-Help Microsoft.PowerShell.Core, Microsoft.PowerShell.Management, Microsoft.PowerShell.Utility -Force # we need up-to-date help content for tests
        .\build.ps1 -Configuration Release

test_script:
    - dotnet test .\test\Markdown.MAML.Test

after_test:
    - ps: |
        # We run Pester tests in after_test step
        # we don't want to use test_script
        # appveyor doesn't run after_test, if tests failed.
        #
        # Pester tests produce artifacts in .\out directory
        $pesterTestResultsFile = ".\TestsResults.xml"
        $res = Invoke-Pester -OutputFormat NUnitXml -OutputFile $pesterTestResultsFile -PassThru
        (New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path $pesterTestResultsFile))
        # Trying to fail the build, if there are problems
        $errorString = ''
        # All tests should pass
        if ($res.FailedCount -gt 0) {
            $errorString += "$($res.FailedCount) tests failed.`n"
        }
        # Documentation itself should be up-to-date
        Update-MarkdownHelp -Path ./docs
        $diff = git diff
        if ($diff) {
            $errorString += "Help is not up-to-date, run Update-MarkdownHelp: $diff`n"
        }
        if ($errorString) {
            throw $errorString
        }

on_finish:
    - ps: |
        $ErrorActionPreference = 'Stop'
        try {
            $stagingDirectory = (Resolve-Path ..).Path
            $zipFile = Join-Path $stagingDirectory "platyPS-$($env:APPVEYOR_BUILD_VERSION).zip"
            Add-Type -assemblyname System.IO.Compression.FileSystem
            7z a -tzip $zipFile "$pwd\out\platyPS"
            @(
                # You can add other artifacts here
                (ls $zipFile),
                (ls .\out -File)
            # flattering
            ) | % {$_} | ? {$_} |% { Push-AppveyorArtifact $_.FullName }
        } catch {
            Write-Host -Foreground Red $_
        }
