name: Github Deploy
on:
  push:
    branches: [main]
    tags: [v*]
jobs:
  cargo-publish:
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - run: |
          rustup update
          cargo login ${{ secrets.CRATE_IO }}
          cargo publish -p four-bar || true
          cargo publish -p four-bar-ui || true
  gh-test-release:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            asset_name: four-bar-linux-amd64
            artifact_name: target/release/four-bar
          - os: ubuntu-latest
            asset_name: four-bar-wasm-unknown
            artifact_name: four-bar-ui/dist/
          - os: macos-latest
            asset_name: four-bar-macos-amd64
            artifact_name: target/release/four-bar
          - os: windows-latest
            asset_name: four-bar-windows-amd64
            artifact_name: target/release/four-bar.exe
    runs-on: ${{ matrix.os }}
    env:
      GITHUB_TOKEN: ${{ secrets.PUBLISH_TOKEN }}
    steps:
      - uses: actions/checkout@v5
      - if: matrix.asset_name == 'four-bar-linux-amd64'
        name: Linux requirements
        run: |
          rustup update
          sudo apt update
          sudo apt install -y libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev libgtk-3-dev
      - if: matrix.asset_name == 'four-bar-wasm-unknown'
        name: WASM Build
        working-directory: four-bar-ui
        run: |
          rustup target add wasm32-unknown-unknown
          cargo install trunk
          trunk build --release --filehash=false
          # For GH-Pages
          trunk build --release --filehash=false --public-url=/four-bar-rs/ --dist=docs
      - if: matrix.asset_name != 'four-bar-wasm-unknown'
        name: Native Build
        run: |
          cargo clippy -- -D warnings
          cargo test -p four-bar
          cargo build --release --features=native-win-release
      - name: Upload artifact
        uses: actions/upload-artifact@v5
        with:
          name: ${{ matrix.asset_name }}
          path: ${{ matrix.artifact_name }}
          if-no-files-found: error
      - if: startsWith(github.ref, 'refs/tags/v')
        name: Release binary
        uses: softprops/action-gh-release@v1
        with:
          token: ${{ secrets.PUBLISH_TOKEN }}
          files: ${{ matrix.asset_name }}.zip
      - if: matrix.asset_name == 'four-bar-wasm-unknown'
        name: Deploy to gh-pages
        uses: JamesIves/github-pages-deploy-action@releases/v4
        with:
          token: ${{ secrets.PUBLISH_TOKEN }}
          folder: four-bar-ui/docs
          clean: true
