name: Release

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

jobs:
  release:
    permissions:
      id-token: write  # Required for OIDC
      contents: write  # for softprops/action-gh-release to create GitHub release
    runs-on: ubuntu-latest
    environment: release
    steps:
      - name: Checkout
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
      - name: Install ldid
        run: |
          sudo apt-get update
          sudo apt-get install git build-essential libplist-dev libssl-dev openssl qemu-user-binfmt pkg-config
          cd /tmp
          git clone https://gitlab.com/opensource-saurik/ldid.git
          cd ldid
          git checkout c2f8abf013b22c335f44241a6a552a7767e73419
          git submodule update --init
          gcc -I. -c -o lookup2.o lookup2.c
          g++ -std=c++11 -o ldid lookup2.o ldid.cpp -I. -lcrypto $(pkg-config --cflags --libs libplist-2.0) -lxml2
          sudo mv ldid /usr/local/bin
      - name: Install pnpm
        uses: pnpm/action-setup@41ff72655975bd51cab0327fa583b6e92b6d3061 # v4.2.0
        with:
          standalone: true
      - name: npm install
        run: pnpm install --global npm@11.6.2
      - name: pnpm install
        # We use --force because we want all artifacts of @reflink/reflink to be installed.
        run: pnpm install --force
      - name: Publish Packages
        continue-on-error: true
        env:
          # setting the "npm_config_//registry.npmjs.org/:_authToken" env variable directly doesn't work.
          # probably "pnpm release" doesn't pass auth tokens to child processes
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          npm config set "//registry.npmjs.org/:_authToken" "${NPM_TOKEN}" # pnpm config set is broken
          pnpm release
      - name: Copy Artifacts
        run: pnpm run copy-artifacts
      - name: Generate release description
        run: pnpm run make-release-description
      - name: Release
        uses: softprops/action-gh-release@5be0e66d93ac7ed76da52eca8bb058f665c3a5fe # v2.4.2
        with:
          draft: true
          files: dist/*
          body_path: RELEASE.md
