name: Release

on:
  push:
    tags:
      - "v*.*.*"
  workflow_dispatch:

jobs:
  build-release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Zip font files
        run: |
          zip -r font.zip font
      - name: Extract version from tag
        run: |
          VERSION=$(echo ${GITHUB_REF} | sed 's/refs\/tags\///')
          echo "VERSION=${VERSION#v}" >> $GITHUB_ENV
      - name: Get Changelog
        run: |
          {
            echo 'RELEASE_NOTES<<EOF'
            grep -A 1000 "^# \[$VERSION\]" CHANGELOG.md | sed '1d;/^# \[/,$d'
            echo 'EOF'
          } >> $GITHUB_ENV
      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          body: ${{ env.RELEASE_NOTES }}
          files: |
            styles.css
            font.zip
          generate_release_notes: true
          fail_on_unmatched_files: true

  deploy-site:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Install rust with wasm
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rust-src
          target: wasm32-unknown-unknown
      - name: Install Binstall
        uses: cargo-bins/cargo-binstall@main
      - name: Install trunk
        run: "cargo binstall --no-confirm trunk"
      - name: Build website
        run: "cd site && trunk build"
      - name: Deploy 🚀
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          folder: site/dist

