name: Ci

on: [push, pull_request]

defaults:
  run:
    shell: bash

jobs:
  build:
    name: Check Style & Suite Test
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: true
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup Deno
        uses: denoland/setup-deno@v1
        id: setup-deno
        with:
          deno-version: 1.x

      - name: Cache Deno (Linux)
        if: ${{ startsWith(matrix.os, 'ubuntu') }}
        uses: actions/cache@v2
        with:
          path: |
            ~/.deno
            ~/.cache/deno
          key: ${{ runner.os }}-deno-${{ hashFiles('**/deps.ts') }}

      - name: Cache Deno (Windows)
        if: ${{ startsWith(matrix.os, 'windows') }}
        uses: actions/cache@v2
        with:
          path: |
            ~\.deno
            %LocalAppData%\deno
          key: ${{ runner.os }}-deno-${{ hashFiles('**/deps.ts') }}

      - name: Cache Deno (MacOS)
        if: ${{ startsWith(matrix.os, 'macos') }}
        uses: actions/cache@v2
        with:
          path: |
            ~/.deno
            ~/Library/Caches/deno
          key: ${{ runner.os }}-deno-${{ hashFiles('**/deps.ts') }}

      - name: Lint Code
        run: deno lint --unstable

      - name: Format Code
        run: deno fmt --check --unstable

      - name: Run Test
        run: deno test --allow-all  --coverage=coverage --doc --import-map ./import_map.json --jobs 4 --unstable

      - name: Generate Coverage
        run: deno coverage --lcov ./coverage --unstable > coverage.lcov

      - name: Upload Coverage
        uses: codecov/codecov-action@v2
        with:
          files: coverage.lcov
          fail_ci_if_error: true
          verbose: true

  release:
    name: Create Release
    if: ${{ startsWith(github.ref, 'refs/tags/v') }}
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Create Release
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.DEFAULT_USER_TOKEN }}
        with:
          name: ${{ github.ref }}
          prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') || contains(github.ref, 'rc') }}

          body: |
            See the [changelog](CHANGELOG.md).
