name: Release
on:
  push:
    branches:
      - release
    paths:
      - '**/Cargo.toml'
      - '.github/workflows/release.yml'

jobs:
  publish:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      max-parallel: 1
      matrix:
        package:
          - name: async-graphql-value
            registryName: async-graphql-value
            path: value
          - name: async-graphql-parser
            registryName: async-graphql-parser
            path: parser
          - name: async-graphql-derive
            registryName: async-graphql-derive
            path: derive
          - name: async-graphql
            registryName: async-graphql
            path: .
          - name: async-graphql-actix-web
            registryName: async-graphql-actix-web
            path: integrations/actix-web
          - name: async-graphql-warp
            registryName: async-graphql-warp
            path: integrations/warp
          - name: async-graphql-tide
            registryName: async-graphql-tide
            path: integrations/tide
#          - name: async-graphql-rocket
#            registryName: async-graphql-rocket
#            path: async-graphql-rocket
    steps:
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
      - name: Checkout
        uses: actions/checkout@v2
      - name: get version
        working-directory: ${{ matrix.package.path }}
        run: echo PACKAGE_VERSION=$(sed -nE 's/^\s*version = "(.*?)"/\1/p' Cargo.toml) >> $GITHUB_ENV
      - name: check published version
        run: echo PUBLISHED_VERSION=$(cargo search ${{ matrix.package.registryName }} --limit 1 | sed -nE 's/^[^"]*"//; s/".*//1p' -) >> $GITHUB_ENV
      - name: cargo login
        if: env.PACKAGE_VERSION != env.PUBLISHED_VERSION
        run: cargo login ${{ secrets.CRATES_TOKEN }}
      - name: cargo package
        if: env.PACKAGE_VERSION != env.PUBLISHED_VERSION
        working-directory: ${{ matrix.package.path }}
        run: |
          cargo package
          echo "We will publish:" $PACKAGE_VERSION
          echo "This is current latest:" $PUBLISHED_VERSION
      - name: Publish ${{ matrix.package.name }}
        if: env.PACKAGE_VERSION != env.PUBLISHED_VERSION
        working-directory: ${{ matrix.package.path }}
        run: |
          echo "# Cargo Publish"
          cargo publish --no-verify
