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

permissions:
  id-token: "write"
  contents: "write"
env:
  GH_TOKEN: ${{ github.token }}

jobs:
  build_and_release:
    runs-on: macos-12

    steps:
      - run: echo "VERSION=${GITHUB_REF_NAME#v}" >> $GITHUB_ENV
      - uses: actions/checkout@v4
        with:
          ref: master

      - name: Update manifest.json version
        uses: jossef/action-set-json-field@v2.2
        with:
          file: src/manifest.json
          field: version
          value: ${{ env.VERSION }}

      - name: Update manifest.v2.json version
        uses: jossef/action-set-json-field@v2.2
        with:
          file: src/manifest.v2.json
          field: version
          value: ${{ env.VERSION }}

      - name: Push files
        continue-on-error: true
        run: |
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          git config --global user.name "github-actions[bot]"
          git commit -am "release v${{ env.VERSION }}"
          git push

      - run: |
          gh release create ${{github.ref_name}} -d -F CURRENT_CHANGE.md -t ${{github.ref_name}}

      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm ci

      - uses: actions/setup-python@v5
        with:
          python-version: '3.10' # for appdmg
      - uses: maxim-lobanov/setup-xcode@v1
        with:
          xcode-version: 14.2
      - run: sed -i '' "s/0.0.0/${{ env.VERSION }}/g" safari/project.pre.patch
      - run: sed -i '' "s/0.0.0/${{ env.VERSION }}/g" safari/project.patch
      - run: npm run build:safari

      - run: |
          gh release upload ${{github.ref_name}} build/chromium.zip
          gh release upload ${{github.ref_name}} build/firefox.zip
          gh release upload ${{github.ref_name}} build/safari.dmg
          gh release upload ${{github.ref_name}} build/chromium-without-katex-and-tiktoken.zip
          gh release upload ${{github.ref_name}} build/firefox-without-katex-and-tiktoken.zip

      - run: |
          gh release edit ${{github.ref_name}} --draft=false
