name: Check README parser info

on:
  push:
    branches:
      - master

jobs:
  update-readme:
    name: Update README.md
    runs-on: ubuntu-latest
    env:
      APPIMAGE: nvim-linux-x86_64.appimage
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3

      - name: Install tree-sitter CLI
        run: npm i -g tree-sitter-cli

      - name: Prepare
        run: |
          sudo apt-get update && sudo apt-get install libfuse2
          sudo add-apt-repository universe
          wget https://github.com/neovim/neovim/releases/download/nightly/${APPIMAGE}
          chmod u+x ${APPIMAGE}
          mkdir -p ~/.local/share/nvim/site/pack/nvim-treesitter-textobject/start
          ln -s $(pwd) ~/.local/share/nvim/site/pack/nvim-treesitter-textobject/start
          mkdir -p ~/.local/share/nvim/site/pack/nvim-treesitter/start
          cd ~/.local/share/nvim/site/pack/nvim-treesitter/start
          git clone https://github.com/nvim-treesitter/nvim-treesitter.git

      - name: Compile parsers
        run: ./${APPIMAGE} --headless -c "TSInstallSync all" -c "q"

      # inspired by nvim-lspconfigs
      - name: Check README
        run: |
          git config user.email "actions@github"
          git config user.name "Github Actions"
          ./${APPIMAGE} --headless -c "luafile ./scripts/update-readme.lua" -c "q" || echo "Needs update"
          git add README.md
          git commit -m "docs: update queries in README" || echo 'No commit necessary!'
          git clean -xf

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v4
        with:
          commit-message: "docs: update queries in README"
          title: "docs: update queries in README"
          branch: update-readme
          base: ${{ github.head_ref }}
