name: tinymist::build::vscode
on:
  workflow_call:
    inputs:
      plan:
        description: 'A description of the plan input'
        required: true # or false, depending on whether the input is mandatory
        type: string # or other appropriate type like boolean, number, etc.

env:
  RUSTFLAGS: '-Dwarnings'
  CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
  CARGO_TARGET_ARM_UNKNOWN_LINUX_GNUEABIHF_LINKER: arm-linux-gnueabihf-gcc

jobs:
  build-vsc-assets:
    uses: ./.github/workflows/build-vsc-assets.yml
  
  build-vscode-main:
    needs: [build-vsc-assets]
    uses: ./.github/workflows/build-vscode-main.yml
  
  build-vscode-others:
    needs: [build-vsc-assets]
    uses: ./.github/workflows/build-vscode-others.yml
  
  release:
    needs: [build-vscode-main, build-vscode-others] # , announce
    runs-on: ubuntu-latest
    if: success() && startsWith(github.ref, 'refs/tags/')
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - uses: actions/download-artifact@v4
        with:
          path: artifacts
          pattern: '{tinymist,typst-preview}-*'
      - name: Display structure of downloaded files
        run: ls -R artifacts
      - uses: ncipollo/release-action@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          artifacts: "artifacts/*/*"
          allowUpdates: true
          omitBodyDuringUpdate: true
          omitDraftDuringUpdate: true
          omitNameDuringUpdate: true
          omitPrereleaseDuringUpdate: true

  publish:
    needs: [build-vscode-main, build-vscode-others] # , announce
    runs-on: ubuntu-latest
    env:
      isRelease: ${{ (startsWith(github.ref, 'refs/tags/') && (!contains(github.ref, 'rc') && (endsWith(github.ref, '0') || endsWith(github.ref, '2') || endsWith(github.ref, '4') || endsWith(github.ref, '6') || endsWith(github.ref, '8')))) }}
      isNightly: ${{ ((startsWith(github.ref, 'refs/tags/') && !((!contains(github.ref, 'rc') && (endsWith(github.ref, '0') || endsWith(github.ref, '2') || endsWith(github.ref, '4') || endsWith(github.ref, '6') || endsWith(github.ref, '8')))))) }}
    if: success() && startsWith(github.ref, 'refs/tags/') && !contains(github.ref, 'rc')
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - uses: actions/download-artifact@v4
      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: 'yarn'
      - name: Install deps
        run: yarn install
      - name: Deploy to VS Code Marketplace
        if: fromJson(env.isRelease)
        run: npx @vscode/vsce publish --packagePath $(find . -type f -iname 'tinymist-*.vsix') --skip-duplicate 
        env:
          VSCE_PAT: ${{ secrets.VSCODE_MARKETPLACE_TOKEN }}
      - name: Deploy to OpenVSX
        if: fromJson(env.isRelease)
        run: npx ovsx publish --packagePath $(find . -type f -iname 'tinymist-*.vsix') --skip-duplicate 
        env:
          OVSX_PAT: ${{ secrets.OPENVSX_ACCESS_TOKEN }}
      - name: Deploy to VS Code Marketplace (Nightly)
        if: fromJson(env.isNightly)
        run: npx @vscode/vsce publish --packagePath $(find . -type f -iname 'tinymist-*.vsix') --skip-duplicate --pre-release
        env:
          VSCE_PAT: ${{ secrets.VSCODE_MARKETPLACE_TOKEN }}
      - name: Deploy to OpenVSX (Nightly)
        if: fromJson(env.isNightly)
        run: npx ovsx publish --packagePath $(find . -type f -iname 'tinymist-*.vsix') --skip-duplicate --pre-release
        env:
          OVSX_PAT: ${{ secrets.OPENVSX_ACCESS_TOKEN }}