antlr-rust 0.2.2

ANTLR4 runtime for Rust
name: CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches:
      - master
      - v0.3
  
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always
  RUST_VERSION: nightly-2021-12-23

jobs:
  tests-antlr:
    name: ANTLR4 testsuite
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          repository: rrevenantt/antlr4
          ref: rust-target
          submodules: true
      - run: |
          git fetch origin $GITHUB_REF
          git checkout FETCH_HEAD
        working-directory: runtime/Rust
      - name: Set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8
      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ env.RUST_VERSION }}
          default: true
      - name: Cache local Maven repository
        uses: actions/cache@v2
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-maven-
      - name: Build with Maven
        run : mvn -DskipTests install -q
      - name : Maven tests
        run: |
          mvn test -Dtest=rust.* -q
          rc=$?;
          cat target/surefire-reports/*.dumpstream || true;
          exit $rc
        working-directory: runtime-testsuite
      - uses: actions/upload-artifact@v2
        if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
        with:
          name: antlr-tool
          path: tool/target/antlr4-4.8-2-SNAPSHOT-complete.jar

  tests-rust:
    name: cargo test
    runs-on: ubuntu-latest
    outputs:
      new_version: ${{ steps.check_ver.outputs.new_version }}
    steps:
      - uses: actions/checkout@v2
      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ env.RUST_VERSION }}
          default: true
          components: rustfmt
      - name: Build
        run: cargo build --verbose
      - name: Run tests
        run: cargo test --verbose
      - name: Formatting
        run: cargo fmt -- --check
      - uses: actions/checkout@v2
        if: ${{ github.event_name == 'push' }}
        with:
          ref: ${{ github.event.before }}
          path: base_branch
      - name: check version bump
        if: ${{ github.event_name == 'push' }}
        id: check_ver
        run: |
          NEW_VER=$(cat Cargo.toml | sed -nE "s/^version.*\"([0-9a-z.\-]+)\".*$/\1/p")
          BASE_VER=$(cat base_branch/Cargo.toml | sed -nE "s/^version.*\"([0-9a-z.\-]+)\".*$/\1/p")
          if [[ "$NEW_VER" != "$BASE_VER" ]]; then
            echo "::set-output name=new_version::$NEW_VER"
          fi


  publish:
    runs-on: ubuntu-latest
    if: |
      success() && github.event_name == 'push'
      && needs.tests-rust.outputs.new_version != null
      && needs.tests-rust.outputs.new_version != ''
    needs: [tests-rust,tests-antlr]
    env:
      RELEASE_NAME: ${{ format('antlr4-4.8-2-Rust{0}',needs.tests-rust.outputs.new_version) }}
      PRERELEASE: ${{ contains(needs.tests-rust.outputs.new_version, '-') }}
    steps:
      - uses: actions/checkout@v2
      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ env.RUST_VERSION }}
          default: true
      - name: Cargo publish check
        run: |
          cargo publish --dry-run
      - uses: actions/download-artifact@v2
        with:
          name: antlr-tool
      - name: Create release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ env.RELEASE_NAME }}
          release_name: ${{ env.RELEASE_NAME }}
          # make release a draft first in case `cargo publish` still fail
          draft: true
          prerelease: ${{ env.PRERELEASE }}
      - name: Upload Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: antlr4-4.8-2-SNAPSHOT-complete.jar
          asset_name: antlr4-4.8-2-SNAPSHOT-complete.jar
          asset_content_type: application/java-archive
      - name: Cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_PUBLISH_TOKEN }}
        run: |
          git clean -f
          cargo publish
      - name: Publish github release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          release_name: ${{ env.RELEASE_NAME }}
          allowUpdates: true
          omitNameDuringUpdate: true
          omitPrereleaseDuringUpdate: true
          replacesArtifacts: false
          draft: false