on:
  push:
    tags:
      - 'v*'

name: Release on tag
run-name: Release on tag ${{ github.ref_name }}

jobs:
  get_tag_metadata:
    name: Get tag metadata
    runs-on: ubuntu-latest
    outputs:
      annotation: ${{ steps.tag-data.outputs.annotation }}
      clean_tag: ${{ steps.tag-data.outputs.clean_tag }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Get annotation and cleaned tag
        id: tag-data
        env:
          PREFIX: "v"
        run: |
          if [[ "$GITHUB_REF_NAME" == "$PREFIX"* ]]; then
            CLEAN_TAG="${GITHUB_REF_NAME#$PREFIX}"
          else
            CLEAN_TAG="$GITHUB_REF_NAME"
          fi

          echo "clean_tag=$CLEAN_TAG" >> "$GITHUB_OUTPUT"

          {
            echo 'annotation<<EOF'
            git for-each-ref "$GITHUB_REF" --format '%(contents)'
            echo 'EOF'
          } >> "$GITHUB_OUTPUT"

  build_win:
    name: Build and upload ${{ github.ref_name }} (Windows)
    runs-on: ubuntu-latest
    needs: get_tag_metadata
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Build release
        run: ./build_release_windows.sh "${{ needs.get_tag_metadata.outputs.clean_tag }}"
      - name: Publish release
        uses: softprops/action-gh-release@v2
        with:
          make_latest: true
          name: ${{ needs.get_tag_metadata.outputs.clean_tag }}
          body: ${{ needs.get_tag_metadata.outputs.annotation }}
          files: dist/*
  build_linux:
    name: Build and upload ${{ github.ref_name }} (Linux)
    runs-on: ubuntu-latest
    needs: get_tag_metadata
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Build release
        run: ./build_release_linux.sh "${{ needs.get_tag_metadata.outputs.clean_tag }}"
      - name: Publish release
        uses: softprops/action-gh-release@v2
        with:
          make_latest: true
          name: ${{ needs.get_tag_metadata.outputs.clean_tag }}
          body: ${{ needs.get_tag_metadata.outputs.annotation }}
          files: dist/*
  build_macos:
    name: Build and upload ${{ github.ref_name }} (MacOS)
    runs-on: macos-latest
    needs: get_tag_metadata
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Build release
        run: ./build_release_macos.sh "${{ needs.get_tag_metadata.outputs.clean_tag }}"
      - name: Publish release
        uses: softprops/action-gh-release@v2
        with:
          make_latest: true
          name: ${{ needs.get_tag_metadata.outputs.clean_tag }}
          body: ${{ needs.get_tag_metadata.outputs.annotation }}
          files: dist/*