name: ci

on:
  - push
  - pull_request

jobs:
  build:
    name: ${{ matrix.kind }} ${{ matrix.os }}
    if: |
      github.event_name == 'push' ||
      !startsWith(github.event.pull_request.head.label, 'denoland:')
    runs-on: ${{ matrix.os }}
    timeout-minutes: 120
    strategy:
      matrix:
        os:
          - macos-latest
          - ubuntu-16.04
          - windows-2019

    steps:
      - name: Configure git
        run: git config --global core.symlinks true

      - name: Clone repository
        uses: actions/checkout@v1
        with:
          fetch-depth: 10
          submodules: "recursive"

      - name: Install rust
        uses: hecrj/setup-rust-action@v1
        with:
          rust-version: "1.40.0"

      - name: Install python
        uses: actions/setup-python@v1
        with:
          python-version: "2.7.x"
          architecture: x64

      - name: Install and start sccache
        shell: pwsh
        working-directory: ${{ runner.temp }}
        env:
          AWS_ACCESS_KEY_ID: AKIA6QEJVNZDGHRMR2KF
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          SCCACHE_BUCKET: deno-sccache
          SCCACHE_IDLE_TIMEOUT: 0
        run: |
          $version = "0.2.12"
          $platform =
            @{ "macOS"   = "x86_64-apple-darwin"
               "Linux"   = "x86_64-unknown-linux-musl"
               "Windows" = "x86_64-pc-windows-msvc"
             }.${{ runner.os }}
          $basename = "sccache-$version-$platform"
          $url = "https://github.com/mozilla/sccache/releases/download/" +
                 "$version/$basename.tar.gz"

          curl -LO $url
          tar -xzvf "$basename.tar.gz"
          . $basename/sccache --start-server

          echo "::add-path::$(pwd)/$basename"
          echo "::set-env name=RUSTC_WRAPPER::sccache"

      - name: Test
        run: cargo test -vv --locked --all-targets

      - name: Clippy
        run: |
          rustup component add clippy
          cargo clippy --all-targets --locked -- -D clippy::all

      - name: Rustfmt
        run: |
          rustup component add rustfmt
          cargo fmt -- --check

      # TODO: add clang-format and maybe cpplint.

      - name: Package
        run: cargo package -vv --locked

      - name: Publish
        if: >
          startsWith(github.ref, 'refs/tags/') &&
          github.repository == 'denoland/rusty_v8' &&
          runner.os == 'Linux'
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish -vv

      - name: Stop sccache
        if: always()
        run: sccache --stop-server