typst_font_manager 0.1.0

A CLI tool to manage fonts for Typst projects
name: Rust

on:
  push:
    branches: [ "main" ]
    paths-ignore:
      - 'README.md'
      - 'LICENSE'
  pull_request:
    branches: [ "main" ]
    paths-ignore:
      - 'README.md'
      - 'LICENSE'

env:
  CARGO_TERM_COLOR: always

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    name: Build and Release
    if: "!contains(github.event.head_commit.message, '[skip ci]')"

    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact_suffix: linux
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact_suffix: macos-intel
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact_suffix: macos-apple-silicon
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact_suffix: windows.exe

    runs-on: ${{ matrix.os }}

    outputs:
      binary_name: ${{ steps.binary_name.outputs.BINARY_NAME }}
      # Pass the binary name `typfont` to the release job

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

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

      - name: Cache Dependencies
        uses: Swatinem/rust-cache@v2
        with:
          key: rust-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}
          shared-key: rust-${{ matrix.target }}
          cache-on-failure: true

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

      - name: Extract Binary Name
        id: binary_name
        shell: bash
        run: |

          BINARY_NAME=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[0].targets[] | select(.kind[] == "bin") | .name')
          echo "BINARY_NAME=${BINARY_NAME}" >> $GITHUB_ENV
          echo "BINARY_NAME=${BINARY_NAME}" >> $GITHUB_OUTPUT

      - name: Verify Binary Existence
        shell: bash
        run: |

          if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
            [[ -f target/${{ matrix.target }}/release/${{ env.BINARY_NAME }}.exe ]] || { echo "Binary not found on Windows"; exit 1; }
          else
            [[ -f target/${{ matrix.target }}/release/${{ env.BINARY_NAME }} ]] || { echo "Binary not found on Unix-based OS"; exit 1; }
          fi

      - name: Archive Binary
        shell: bash
        run: |

          mkdir -p artifacts
          if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
            cp target/${{ matrix.target }}/release/${{ env.BINARY_NAME }}.exe artifacts/${{ env.BINARY_NAME }}_${{ matrix.artifact_suffix }}
          else
            cp target/${{ matrix.target }}/release/${{ env.BINARY_NAME }} artifacts/${{ env.BINARY_NAME }}_${{ matrix.artifact_suffix }}
          fi

      - name: Upload Build Artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.BINARY_NAME }}-${{ matrix.artifact_suffix }}
          path: artifacts/${{ env.BINARY_NAME }}_${{ matrix.artifact_suffix }}

  release:
    name: Create GitHub Release
    if: github.ref == 'refs/heads/main'
    needs: build
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Create Git Tag
        run: |

          TAG="v$(date +%Y%m%d%H%M%S)"
          echo "TAG=${TAG}" >> $GITHUB_ENV
          git tag ${TAG}
          git push origin ${TAG}

      - name: Download Artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Debug Artifact Paths
        run: |

          echo "Files in artifacts directory:"
          find artifacts -type f

      - name: Flatten Artifacts
        run: |

          mkdir -p release_artifacts
          find artifacts -type f -exec cp {} release_artifacts/ \;

      - name: List Final Artifacts
        run: ls -al release_artifacts

      - name: Upload Binaries to Release
        uses: softprops/action-gh-release@v2
        with:
          files: |

            release_artifacts/${{ needs.build.outputs.binary_name }}_linux
            release_artifacts/${{ needs.build.outputs.binary_name }}_macos-apple-silicon
            release_artifacts/${{ needs.build.outputs.binary_name }}_macos-intel
            release_artifacts/${{ needs.build.outputs.binary_name }}_windows.exe
          tag_name: ${{ env.TAG }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}