name: tinymist::build::vscode::main
on:
  workflow_call:

jobs:
  build:
    strategy:
      matrix:
        include:
          - os: windows-latest
            rust-target: x86_64-pc-windows-msvc
            platform: win32
            arch: x64
            regular_build: 'true'
          - os: windows-latest
            rust-target: aarch64-pc-windows-msvc
            platform: win32
            arch: arm64
          - os: ubuntu-22.04
            rust-target: x86_64-unknown-linux-gnu
            platform: linux
            arch: x64
            regular_build: 'true'
          - os: ubuntu-22.04
            rust-target: aarch64-unknown-linux-gnu
            platform: linux
            arch: arm64
          - os: ubuntu-22.04
            rust-target: arm-unknown-linux-gnueabihf
            platform: linux
            arch: armhf
          - os: macos-14
            rust-target: x86_64-apple-darwin
            platform: darwin
            arch: x64
          - os: macos-14
            rust-target: aarch64-apple-darwin
            platform: darwin
            arch: arm64
            regular_build: 'true'
    name: build (${{ matrix.platform }}-${{ matrix.arch }})
    runs-on: ${{ matrix.os }}
    env:
      target: ${{ matrix.platform }}-${{ matrix.arch }}
      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'))))) || (!startsWith(github.ref, 'refs/tags/') && matrix.regular_build == 'true')) }}
      isTest: ${{ matrix.rust-target == 'x86_64-unknown-linux-gnu' || matrix.rust-target == 'x86_64-pc-windows-msvc' }}
      isUniversal: ${{ matrix.rust-target == 'x86_64-unknown-linux-gnu' }}
    steps:
      - name: "Print Env"
        run: |
          echo "Running on ${{ matrix.os }}"
          echo "Target: ${{ env.target }}"
          echo "Is Release: ${{ fromJson(env.isRelease) }}"
          echo "Is Nightly: ${{ fromJson(env.isNightly) }}"
          echo "Is Test: ${{ fromJson(env.isTest) }}"
          echo "Is Universal (No Server): ${{ fromJson(env.isUniversal) }}"
      - uses: actions/checkout@v4
        with:
          submodules: recursive
        if: (fromJson(env.isRelease) || fromJson(env.isNightly))

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: 'yarn'
        if: (fromJson(env.isRelease) || fromJson(env.isNightly))
      - name: Install deps
        run: yarn install
        if: (fromJson(env.isRelease) || fromJson(env.isNightly))

      - name: Download tinymist binary artifact
        uses: actions/download-artifact@v4
        with:
          name: artifacts-build-local-${{ matrix.rust-target }}
          path: prebuilts
        if: (fromJson(env.isRelease) || fromJson(env.isNightly))
      - name: Unzip tinymist binary artifact (Windows)
        run: 7z x -y -oprebuilts prebuilts/tinymist-${{ matrix.rust-target }}.zip
        if: matrix.platform == 'win32' && (fromJson(env.isRelease) || fromJson(env.isNightly))
      - name: Unzip tinymist binary artifact (Linux)
        run: |
          tar -xvf prebuilts/tinymist-${{ matrix.rust-target }}.tar.gz -C prebuilts
          mv prebuilts/tinymist-${{ matrix.rust-target }}/tinymist prebuilts/tinymist
        if: matrix.platform != 'win32' && (fromJson(env.isRelease) || fromJson(env.isNightly))

      - name: Download VSC Assets
        uses: actions/download-artifact@v4
        with:
          name: vscode-artifacts-tinymist
          path: editors/vscode/out
        if: (fromJson(env.isRelease) || fromJson(env.isNightly))
      - name: Download VSC Assets (L10n)
        uses: actions/download-artifact@v4
        with:
          name: vscode-artifacts-tinymist-l10n
          path: editors/vscode
        if: (fromJson(env.isRelease) || fromJson(env.isNightly))
      - name: Download VSC Assets (Preview)
        uses: actions/download-artifact@v4
        with:
          name: vscode-artifacts-typst-preview
          path: contrib/typst-preview/editors/vscode/out
        if: (fromJson(env.isRelease) || fromJson(env.isNightly))

      - name: Copy binary to output directory
        if: (fromJson(env.isRelease) || fromJson(env.isNightly))
        run: |
          cp "prebuilts/tinymist${{ ( matrix.platform == 'win32' ) && '.exe' || '' }}" "editors/vscode/out/"
          cp "prebuilts/tinymist${{ ( matrix.platform == 'win32' ) && '.exe' || '' }}" "contrib/typst-preview/editors/vscode/out/"
          cp "prebuilts/tinymist${{ ( matrix.platform == 'win32' ) && '.exe' || '' }}" "tinymist-${{ env.target }}${{ ( matrix.platform == 'win32' ) && '.exe' || '' }}"

      - name: Package typst-preview extension
        if: fromJson(env.isRelease)
        run: yarn run package -- --target ${{ env.target }} -o typst-preview-${{ env.target }}.vsix
        working-directory: ./contrib/typst-preview/editors/vscode
      - name: Package tinymist extension
        if: fromJson(env.isRelease)
        run: yarn run package -- --target ${{ env.target }} -o tinymist-${{ env.target }}.vsix
        working-directory: ./editors/vscode
      - name: Package typst-preview extension (Nightly)
        if: fromJson(env.isNightly)
        run: yarn run package -- --target ${{ env.target }} -o typst-preview-${{ env.target }}.vsix --pre-release
        working-directory: ./contrib/typst-preview/editors/vscode
      - name: Package tinymist extension (Nightly)
        if: fromJson(env.isNightly)
        run: yarn run package -- --target ${{ env.target }} -o tinymist-${{ env.target }}.vsix --pre-release
        working-directory: ./editors/vscode

      - name: Upload binary artifact
        if: (fromJson(env.isRelease) || fromJson(env.isNightly))
        uses: actions/upload-artifact@v4
        with:
          name: tinymist-${{ env.target }}
          path: tinymist-${{ env.target }}${{ fromJSON('["", ".exe"]')[matrix.platform == 'win32'] }}
      - name: Upload typst-preview VSIX artifact
        if: (fromJson(env.isRelease) || fromJson(env.isNightly))
        uses: actions/upload-artifact@v4
        with:
          name: typst-preview-${{ env.target }}.vsix
          path: contrib/typst-preview/editors/vscode/typst-preview-${{ env.target }}.vsix
      - name: Upload VSIX artifact
        if: (fromJson(env.isRelease) || fromJson(env.isNightly))
        uses: actions/upload-artifact@v4
        with:
          name: tinymist-${{ env.target }}.vsix
          path: editors/vscode/tinymist-${{ env.target }}.vsix

      - name: Test tinymist extension
        uses: coactions/setup-xvfb@v1
        with:
          run: yarn test
          working-directory: ./editors/vscode
        if: (fromJson(env.isRelease) || fromJson(env.isNightly)) && fromJson(env.isTest)
      - name: Upload Tinymist Testing log
        if: ${{ always() && ((fromJson(env.isRelease) || fromJson(env.isNightly)) && fromJson(env.isTest))}}
        uses: actions/upload-artifact@v4
        with:
          name: tinymist-lsp-tests.${{ env.target }}.log
          path: editors/vscode/e2e-workspaces/simple-docs/tinymist-lsp.log

      # The universal target doesn't bundle the binary. Users of that must install
      # tinymist by themselves.
      - name: Remove server binary
        if: fromJson(env.isUniversal)
        run: rm "editors/vscode/out/tinymist"
      - name: Package extension (Universal)
        if: fromJson(env.isRelease) && fromJson(env.isUniversal)
        run: yarn run package -- -o tinymist-universal.vsix
        working-directory: ./editors/vscode
      - name: Package extension (Universal, Nightly)
        if: fromJson(env.isNightly) && fromJson(env.isUniversal)
        run: yarn run package -- -o tinymist-universal.vsix --pre-release
        working-directory: ./editors/vscode
      - name: Upload tinymist VSIX artifact (Universal)
        if: (fromJson(env.isRelease) || fromJson(env.isNightly)) && fromJson(env.isUniversal)
        uses: actions/upload-artifact@v4
        with:
          name: tinymist-universal.vsix
          path: editors/vscode/tinymist-universal.vsix
