name: Release

on:
  push:
    tags: ["v*"]

jobs:
  create-release:
    name: Create Release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Create Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release create ${{ github.ref_name }} --draft --verify-tag --title ${{ github.ref_name }}

  build-plugin:
    needs: ["create-release"]
    name: Build Roblox Studio Plugin
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true

      - name: Setup Rokit
        uses: CompeyDev/setup-rokit@v0.1.2
        with:
          version: 'v1.1.0'

      - name: Build Plugin
        run: rojo build plugin.project.json --output Rojo.rbxm

      - name: Upload Plugin to Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release upload ${{ github.ref_name }} Rojo.rbxm

      - name: Upload Plugin to Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: Rojo.rbxm
          path: Rojo.rbxm

  build:
    needs: ["create-release"]
    strategy:
      fail-fast: false
      matrix:
        # https://doc.rust-lang.org/rustc/platform-support.html
        include:
          - host: linux
            os: ubuntu-22.04
            target: x86_64-unknown-linux-gnu
            label: linux-x86_64

          - host: linux
            os: ubuntu-22.04-arm
            target: aarch64-unknown-linux-gnu
            label: linux-aarch64

          - host: windows
            os: windows-latest
            target: x86_64-pc-windows-msvc
            label: windows-x86_64

          - host: windows
            os: windows-11-arm
            target: aarch64-pc-windows-msvc
            label: windows-aarch64

          - host: macos
            os: macos-latest
            target: x86_64-apple-darwin
            label: macos-x86_64

          - host: macos
            os: macos-latest
            target: aarch64-apple-darwin
            label: macos-aarch64

    name: Build (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    env:
      BIN: rojo
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Restore Rust Cache
        uses: actions/cache/restore@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ matrix.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Build Release
        run: cargo build --release --locked --verbose --target ${{ matrix.target }}

      - name: Save Rust Cache
        uses: actions/cache/save@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ matrix.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Generate Artifact Name
        shell: bash
        env:
          TAG_NAME: ${{ github.ref_name }}
        run: |
          echo "ARTIFACT_NAME=$BIN-${TAG_NAME#v}-${{ matrix.label }}.zip" >> "$GITHUB_ENV"

      - name: Create Archive and Upload to Release
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mkdir staging

          if [ "${{ matrix.host }}" = "windows" ]; then
            cp "target/${{ matrix.target }}/release/$BIN.exe" staging/
            cd staging
            7z a ../$ARTIFACT_NAME *
          else
            cp "target/${{ matrix.target }}/release/$BIN" staging/
            cd staging
            zip ../$ARTIFACT_NAME *
          fi

          gh release upload ${{ github.ref_name }} ../$ARTIFACT_NAME

      - name: Upload Archive to Artifacts
        uses: actions/upload-artifact@v4
        with:
          path: ${{ env.ARTIFACT_NAME }}
          name: ${{ env.ARTIFACT_NAME }}
