name: Manual Bump

on:
  - workflow_dispatch

jobs:
  bump-pub:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      # https://github.com/actions/cache/blob/master/examples.md#rust---cargo
      - name: Cache cargo registry
        uses: actions/cache@v1
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
      - name: Cache cargo index
        uses: actions/cache@v1
        with:
          path: ~/.cargo/git
          key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
      # - name: Cache cargo build
      #   uses: actions/cache@v1
      #   with:
      #     path: target
      #     key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}

      - run: cargo install cargo-bump

      - run: |
          PREV_VERSION=`cargo metadata --no-deps --format-version=1 | jq '.packages[0].version' | sed 's/"//g'`
          cargo bump patch
          CURRENT_VERSION=`cargo metadata --no-deps --format-version=1 | jq '.packages[0].version' | sed 's/"//g'`
          git config --global user.name 'github actions'
          git config --global user.email 'github.actions@example.com'
          git commit -m ":up: $PREV_VERSION -> $CURRENT_VERSION" -a
          git remote set-url origin https://hppRC:${GITHUB_TOKEN}@github.com/hppRC/kuon.git
          git push origin HEAD
          git tag v$CURRENT_VERSION
          git push origin v$CURRENT_VERSION

          cargo login $CARGO_TOKEN
          cargo publish
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CARGO_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
