name: Dev Release


on:
  push:
    branches:
    - 'dev'


env:
  TAG : 'latest-dev'


concurrency:
  group: ${{ github.workflow }}
  cancel-in-progress: true


jobs:

  build:
    strategy:
      matrix:
        emu : [ Switch, Emulators ]
    runs-on: ubuntu-latest
    outputs:
      filename1: ${{ steps.set-output.outputs.filename-Switch }}
      filename2: ${{ steps.set-output.outputs.filename-Emulators }}
    steps:
    -
      name : Checkout
      uses : actions/checkout@v3
    -
      name  : Environment
      id    : env
      shell : bash
      run: |
        echo "::set-output name=prefix::$(date +'%Y%m%d_%H%M%S_' --utc)"
    -
      name : Build artifacts
      id   : build
      uses : ./.github/actions/build
      with:
        prefix : ${{ steps.env.outputs.prefix }}
        emu    : ${{ matrix.emu }}
    -
      name  : Set output
      id    : set-output
      shell : bash
      run   : |
        echo "::set-output name=filename-${{ matrix.emu }}::${{ steps.build.outputs.filename }}"

  attach:
    needs: build
    runs-on: ubuntu-latest
    steps:
    -
      name : Checkout
      uses : actions/checkout@v3
    -
      name  : Get release info
      id    : release
      shell : bash
      run: |
        url=`curl -sS --fail ${{ github.api_url }}/repos/${{ github.repository }}/releases/tags/${{ env.TAG }} | jq .upload_url -r`
        echo "::set-output name=upload_url::$url"
    -
      name : Attach build artifacts to release (Switch)
      uses : ./.github/actions/attach
      with:
        filename     : ${{ needs.build.outputs.filename1 }}
        upload_url   : ${{ steps.release.outputs.upload_url }}
        GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
    -
      name : Attach build artifacts to release (Emulators)
      uses : ./.github/actions/attach
      with:
        filename     : ${{ needs.build.outputs.filename2 }}
        upload_url   : ${{ steps.release.outputs.upload_url }}
        GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}

  move:
    needs: attach
    runs-on: ubuntu-latest
    steps:
    -
      name : Checkout
      uses : actions/checkout@v3
    -
      name  : Move ${{ env.TAG }} tag
      shell : bash
      run: |
        git config user.email "${{ github.actor }}@users.noreply.github.com"
        git tag -f ${{ env.TAG }}
        git push --force origin ${{ env.TAG }}
