name: Documentation

on:
  push:
    branches:
      - main
  # Allow running manually
  workflow_dispatch:

# Allow one concurrent deployment
concurrency:
  group: docs-website
  cancel-in-progress: true

jobs:
  prepare:
    name: Version info
    runs-on: ubuntu-24.04-arm

    outputs:
      matrix: ${{ steps.matrix.outputs.result }}
      versions: ${{ steps.versions.outputs.result }}

    steps:
      - name: Checkout version-info
        uses: actions/checkout@v4
        with:
          sparse-checkout: version-info.toml
          sparse-checkout-cone-mode: false

      - name: Produce build matrix
        id: matrix
        uses: mikefarah/yq@master
        with:
          cmd: |
            yq --no-colors --output-format=json '
              [
                .versions
                | to_entries
                | reverse
                | .[]
                | {
                    "ref": .value.branch,
                    "name": .key,
                    "base-href": "/nixvim/"
                  }
                | select(.ref != "main").sub-path = .name
                | select(.ref != "main").base-href = "\(.base-href)\(.name)/"
              ]
            ' version-info.toml

      - name: Produce "other versions" array
        id: versions
        uses: mikefarah/yq@master
        with:
          cmd: |
            yq --no-colors --output-format=json '
              [
                .versions
                | to_entries
                | reverse
                | .[]
                | {
                    "branch": .value.branch,
                    "nixpkgsBranch": .value.channel,
                    "baseHref": "/nixvim/\(.key)/",
                    "status": .value.status
                  }
                | select(.branch == "main").baseHref = "/nixvim/"
              ]
            ' version-info.toml

  build:
    name: Build ${{ matrix.name }}
    runs-on: ubuntu-latest
    needs: prepare

    strategy:
      fail-fast: true
      matrix:
        include: ${{ fromJSON(needs.prepare.outputs.matrix) }}

    steps:
      - name: Install nix
        uses: cachix/install-nix-action@v31

      - name: Configure cachix
        uses: cachix/cachix-action@v16
        with:
          name: nix-community
          authToken: ${{ secrets.CACHIX_AUTH_TOKEN }}

      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ matrix.ref }}

        # Uses the build-docs action from the checked-out nixvim branch
      - name: Build docs
        uses: ./.github/actions/build-docs
        with:
          artifact-name: ${{ matrix.name }}-docs
          sub-path: ${{ matrix.sub-path }}
          base-href: ${{ matrix.base-href }}
          versions: ${{ needs.prepare.outputs.versions }}

  combine:
    name: Combine builds
    runs-on: ubuntu-24.04-arm
    needs: build

    env:
      in: artifacts
      out: combined

    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          path: ${{ env.in }}
          pattern: "*-docs"
          merge-multiple: false

      - name: Extract archives
        run: |
          mkdir -p "$out"
          find "$in" -name artifact.tar \
          | parallel tar \
              --verbose \
              --extract \
              --file {} \
              --directory "$out"

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ${{ env.out }}

  deploy:
    name: Deploy
    runs-on: ubuntu-24.04-arm
    needs: combine

    permissions:
      pages: write # to deploy to Pages
      id-token: write # to verify the deployment originates from an appropriate source

    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}

    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
