pipeline {
  agent {
    kubernetes {
      yaml '''
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: node
    image: node:22-bookworm
    command: ["sleep"]
    args: ["3600"]
    resources:
      requests: { cpu: "500m", memory: "1Gi" }
      limits: { cpu: "2", memory: "4Gi" }
'''
      defaultContainer 'node'
    }
  }
  options {
    disableConcurrentBuilds()
    lock('langstitch-global')
    timeout(time: 60, unit: 'MINUTES') }
  stages {
    stage('Checkout sibling spec') {
      steps {
        withCredentials([usernamePassword(
          credentialsId: 'gitea-git',
          usernameVariable: 'GIT_USER',
          passwordVariable: 'GIT_PASS'
        )]) {
          sh '''
            set -eu
            SPEC_URL="http://${GIT_USER}:${GIT_PASS}@gitea.lan/LangStitch/langstitch-spec.git"
            rm -rf ../langstitch-spec
            git clone --depth 1 "$SPEC_URL" ../langstitch-spec
            (cd ../langstitch-spec && npm ci)
          '''
        }
      }
    }
    stage('Build VSIX') {
      steps {
        sh '''
          set -eu
          npm install
          npm run build:webview
          npm run compile
          mkdir -p dist-langtailor
          npx @vscode/vsce package --no-dependencies --out dist-langtailor --allow-missing-repository
          ls -la dist-langtailor
        '''
      }
    }
  }
}
