name: release
on:
  push:
    tags:
      - "v*"

jobs:
  build-release:
    name: build-release
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - macos-latest
          - ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Install Node
        uses: actions/setup-node@v4

      - name: Install npm dependencies
        run: npm ci

      - name: Ensure generated parser files are up to date
        run: npx tree-sitter generate

      - name: Ensure tests pass
        run: npx tree-sitter test

      - name: Compile library file
        run: cc -shared -fPIC -g -O2 -I src src/parser.c src/scanner.c -o tree-sitter-gleam.so

      - name: Create archive
        run: |
          VERSION="${GITHUB_REF#refs/tags/}"
          case ${{ matrix.os }} in
            "ubuntu-latest") FAMILY="linux";;
            "macos-latest") FAMILY="macos";;
          esac
          ARCHIVE="tree-sitter-gleam-$VERSION-$FAMILY.tar.gz"

          tar -czf $ARCHIVE tree-sitter-gleam.so

          echo "ASSET=$ARCHIVE" >> $GITHUB_ENV

      - name: Upload release archive
        uses: softprops/action-gh-release@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          draft: true
          prerelease: false
          fail_on_unmatched_files: true
          files: |
            ${{ env.ASSET }}
