name: gh-pages
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    timeout-minutes: 420
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-24.04, windows-2025]
    steps:
      - uses: actions/checkout@v6.1.0

      - uses: moonrepo/setup-rust@v1.3.0
        with:
          channel: "nightly-2025-05-09"
          components: "clippy,rust-src,rustfmt"
          targets: "wasm32-unknown-unknown"

      # Elixir and Gleam: the notebooks run through spiral/apps/cells (`mix spiral.notebook`, via its spi/run_notebook.ps1,
      # which also installs .NET 11 and builds the Spiral compiler into ~/.cache/spiral-bin when missing). Gleam >= 1.14:
      # kino's manifest.toml pins gleam_stdlib 1.0.5. jupyter (html outputs) comes from polyglot's requirements.txt.
      - uses: erlef/setup-beam@v1.24.1
        with:
          otp-version: "29"
          elixir-version: "1.20"
          gleam-version: "1.19.2"
          rebar3-version: "3.27.3"

      - uses: actions/setup-dotnet@v5.4.0
        with:
          dotnet-version: "9.0.202"

      - uses: oven-sh/setup-bun@v2.2.0

      - uses: mlugg/setup-zig@v2.2.1
        with:
          version: 0.17.0

      - uses: actions/setup-python@v6.3.0
        with:
          python-version: "3.12.10"

      - uses: actions/setup-go@v5.6.0

      - uses: awalsh128/cache-apt-pkgs-action@master
        if: contains(matrix.os, 'ubuntu')
        with:
          packages: pandoc fonts-nanum-coding
          version: 1.0

      - uses: wtfjoke/setup-tectonic@v3.1.5
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}

      - run: pwsh scripts/workflow.ps1

      - name: tectonic diagnostics
        if: failure()
        shell: bash
        run: |
          printf '# t\n\nhello\n' > /tmp/tt.md
          tectonic --version || true
          command -v crowbook && crowbook --verbose --to pdf --single /tmp/tt.md --output /tmp/tt.pdf --set tex.command tectonic || true
          fc-list | grep -i -E 'nanum' | head -n 10 || true

      - uses: actions/upload-artifact@v5.0.0
        if: contains(matrix.os, 'ubuntu')
        with:
          name: gh-pages
          path: dist
          retention-days: 30

  push:
    needs: build
    timeout-minutes: 180
    runs-on: ubuntu-24.04
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v6.1.0

      - uses: actions/download-artifact@v6.0.0
        with:
          name: gh-pages
          path: gh-pages

      - uses: peaceiris/actions-gh-pages@v4.1.0
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: gh-pages
