name: Update docs

on:
  push:
    branches: [main]
    paths:
      - "modules/home-manager/**"
      - "modules/nixos/**"
  workflow_dispatch:

jobs:
  update:
    name: Run update
    runs-on: ubuntu-latest
    # we only want this running on our repo
    if: github.repository == 'catppuccin/nix'

    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.PUSH_TOKEN }}

      - name: Install Nix
        uses: cachix/install-nix-action@v26

      - name: Set Git user info
        run: |
          git config user.name 'github-actions[bot]'
          git config user.email 'github-actions[bot]@users.noreply.github.com'

      - name: Get short revision
        id: rev
        run:
          echo "rev=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"

      - name: Build & update docs
        run: |
          for module in "nixos" "home-manager"; do
            nix build --print-build-logs --show-trace ./dev#"$module"-doc
            cat result > docs/"$module"-options.md
            rm result
          done

      - name: Commit changes
        run: |
          if ! git diff --color=always --exit-code; then
            git commit -am "docs: update for ${{ steps.rev.outputs.rev }}"
            git push
          fi
