name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    name: Build - ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            artifact: obsidian-forge-x86_64-unknown-linux-musl.tar.gz
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact: obsidian-forge-aarch64-apple-darwin.tar.gz
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact: obsidian-forge-x86_64-apple-darwin.tar.gz
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact: obsidian-forge-x86_64-pc-windows-msvc.zip

    steps:
      - uses: actions/checkout@v6

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Add cross-compile target
        run: rustup target add ${{ matrix.target }}

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

      - name: Install musl-tools (Linux)
        if: matrix.target == 'x86_64-unknown-linux-musl'
        run: sudo apt-get install -y musl-tools

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

      - name: Package (Unix)
        if: runner.os != 'Windows'
        run: |
          tar -czf ${{ matrix.artifact }} \
            -C target/${{ matrix.target }}/release obsidian-forge of

      - name: Package (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          Compress-Archive `
            -Path @(
              "target/${{ matrix.target }}/release/obsidian-forge.exe",
              "target/${{ matrix.target }}/release/of.exe"
            ) `
            -DestinationPath ${{ matrix.artifact }}

      - name: Upload artifact
        uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.artifact }}
          path: ${{ matrix.artifact }}

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v6

      - name: Download all artifacts
        uses: actions/download-artifact@v8
        with:
          merge-multiple: true

      - name: Publish Release
        uses: softprops/action-gh-release@v3
        with:
          generate_release_notes: true
          files: |
            obsidian-forge-*.tar.gz
            obsidian-forge-*.zip

  publish-crates:
    name: Publish to crates.io
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Publish
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
