name: Document
on:
  push:
    branches:
      - '**'
      - '!dependabot/**'
    tags:
      - '**'
  pull_request:
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v6
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: ruby
          bundler-cache: true
      - name: Install requirements
        run: |
          bundle exec ruby extconf.rb
          make -j$(nproc)
          make install
      - name: Generate document
        run: |
          GI_GIR_PATH=/usr/share/gir-1.0 \
            bundle exec rake -m doc:generate
          find yard_docs \
            -name '*.html' \
            -exec sed -i -e 's/Generated on .* by/Generated by/g' '{}' ';'
          case ${GITHUB_REF} in
            refs/tags/*)
              version=${GITHUB_REF##refs/tags}
              ;;
            *)
              version=dev
              ;;
          esac
          mkdir -p public/doc
          rm -rf public/doc/${version}
          mv yard_docs public/doc/${version}
          if [ "${version}" != "dev" ]; then
            rm -rf public/doc/latest
            cp -a public/doc/${version} public/doc/latest
          fi
      - name: Publish Documentation on GitHub Pages
        if: github.event_name == 'push' && github.ref_name == 'main'
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./public
          keep_files: true
          # We can use this with v4
          # force_orphan: true
          user_name: 'github-actions[bot]'
          user_email: 'github-actions[bot]@users.noreply.github.com'
