name: Publish VS Code Extension

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  publish:
    runs-on: ubuntu-latest
    env:
      VSCE_PAT: ${{ secrets.VSCE_PAT }}
      OVSX_PAT: ${{ secrets.OVSX_PAT }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup bun
        uses: oven-sh/setup-bun@v2

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install dependencies
        run: bun install --frozen-lockfile

      - name: Build VS Code extension
        run: bun run --cwd packages/vscode build

      - name: Package extension
        run: cd packages/vscode && bunx vsce package --no-dependencies

      - name: Publish to VS Code Marketplace
        if: ${{ env.VSCE_PAT != '' }}
        run: cd packages/vscode && bunx vsce publish -p "$VSCE_PAT" --no-dependencies

      - name: Publish to Open VSX
        if: ${{ env.OVSX_PAT != '' }}
        run: bunx ovsx publish packages/vscode/*.vsix -p "$OVSX_PAT"

      - name: Upload VSIX artifact
        uses: actions/upload-artifact@v4
        with:
          name: openchamber-vscode-vsix
          path: packages/vscode/*.vsix

      - name: Attach VSIX to GitHub Release
        if: startsWith(github.ref, 'refs/tags/')
        uses: softprops/action-gh-release@v2
        with:
          files: packages/vscode/*.vsix
          generate_release_notes: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
