name: Publish VSCode Extension

on:
  workflow_call:
    inputs:
      ref:
        description: "Git ref to checkout (tag or branch)"
        type: string
        required: false
        default: ""
    secrets:
      VSCE_PAT:
        description: "VS Code Marketplace Personal Access Token"
        required: true
      OVSX_PAT:
        description: "Open VSX Registry Personal Access Token"
        required: true
  workflow_dispatch:

jobs:
  publish:
    name: Publish to VS Code Marketplace
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.ref || github.ref }}

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "npm"
          cache-dependency-path: vscode-rovo/package-lock.json

      - name: Install dependencies
        working-directory: vscode-rovo
        run: npm ci

      - name: Package extension
        working-directory: vscode-rovo
        run: npm run package

      - name: Publish to VS Code Marketplace
        working-directory: vscode-rovo
        run: npx vsce publish
        env:
          VSCE_PAT: ${{ secrets.VSCE_PAT }}

      - name: Publish to Open VSX Registry
        working-directory: vscode-rovo
        run: npx ovsx publish -p ${{ secrets.OVSX_PAT }}
