name: CI

on:
  push:
    branches:
      - master
  pull_request:
  release:
    types: [published]

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    runs-on: ubuntu-latest

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

    - name: Rust Caching
      uses: Swatinem/rust-cache@v1

    - name: Install toolchain
      uses: actions-rs/toolchain@v1
      with:
        profile: minimal
        toolchain: stable
        override: true

    # Using thumbv6m-none-eabi as ARMv6-M arbitrary common choice for a bare-minimum target.
    # More info: https://docs.rs/cortex-m-rt/latest/cortex_m_rt/
    #
    # Can be replaced by other targets that guarantee bare-minimum no-std
    - name: Install toolchain no-std
      uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        target: thumbv6m-none-eabi
        override: true

    - name: Install Cargo.toml linter
      uses: baptiste0928/cargo-install@v1
      with:
        crate: cargo-toml-lint
        version: "0.1"

    - name: Run Cargo.toml linter
      run:  git ls-files | grep Cargo.toml$ | xargs --verbose -n 1 cargo-toml-lint

    - name: Install rustfmt
      run: rustup component add rustfmt

    - name: Check formatting
      uses: actions-rs/cargo@v1
      with:
        command: fmt
        args: --all --verbose -- --check

    - name: Build
      uses: actions-rs/cargo@v1
      with:
        command: build
        args: --verbose

    - name: Build no-std
      uses: actions-rs/cargo@v1
      with:
        command: build
        args: --verbose --target thumbv6m-none-eabi --no-default-features

    - name: Build no-std serde
      uses: actions-rs/cargo@v1
      with:
        command: build
        args: --verbose --target thumbv6m-none-eabi --no-default-features --features serde

    - name: Run tests
      uses: actions-rs/cargo@v1
      with:
        command: test
        args: --verbose

    - name: Run tests no-std
      uses: actions-rs/cargo@v1
      with:
        command: test
        args: --verbose --no-default-features

    - name: Run serde tests
      uses: actions-rs/cargo@v1
      with:
        command: test
        args: --verbose --features serde

    - name: Run serde tests no-std
      uses: actions-rs/cargo@v1
      with:
        command: test
        args: --verbose --no-default-features --features serde

  publish:
    # Only do this job if publishing a release
    needs: build
    if: github.event_name == 'release' && github.event.action == 'published'
    runs-on: ubuntu-latest

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

      - name: Install toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true

      - name: Verify tag version
        run: |
          curl -sSLf "https://github.com/TomWright/dasel/releases/download/v1.24.3/dasel_linux_amd64" -L -o dasel && chmod +x dasel
          mv ./dasel /usr/local/bin/dasel
          ./.github/workflows/scripts/verify_tag.sh ${{ github.ref_name }} Cargo.toml

      - name: Publish crate
        uses: katyo/publish-crates@v1
        with:
          registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
