name: ci

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  scripts:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Run tests for scripts
        run: |
          ./bin/test/test.sh

  emacs:
    runs-on: ubuntu-latest
    continue-on-error: ${{ matrix.emacs_version == 'snapshot' }}

    strategy:
      fail-fast: false
      matrix:
        emacs_version:
          - '27.2'
          - 'snapshot'

    steps:
      - uses: actions/checkout@v2

      - uses: purcell/setup-emacs@master
        with:
          version: ${{ matrix.emacs_version }}

      - name: Install dependencies for Doom
        run: |
          sudo apt-get install ripgrep

      - name: Install some other dependencies
        run: |
          mkdir -p $HOME/src/github.com/mssola
          cd $HOME/src/github.com/mssola
          git clone https://github.com/mssola/soria.git

      - name: Install Doom Emacs
        run: |
          git clone --depth 1 https://github.com/hlissner/doom-emacs ~/.emacs.d
          ~/.emacs.d/bin/doom install --no-config --no-env --no-fonts

      - name: Setup my configuration
        run: |
          cp -r $(realpath .doom.d) $HOME/.doom.d
          echo "(package! soria-theme)" >> $HOME/.doom.d/packages.el
          cat $HOME/.doom.d/packages.el

      - name: Sync
        run: |
          ~/.emacs.d/bin/doom sync

      - name: Check Doom installation
        run: |
          ~/.emacs.d/bin/doom doctor
          test -z "$(~/.emacs.d/bin/doom doctor | grep 'errors!')"

      - name: Try to start Emacs
        run: |
          echo "Attempting startup..."
          ${EMACS:=emacs} -nw --batch \
            --eval '(let ((debug-on-error t)
                          (url-show-status nil)
                          (user-emacs-directory default-directory)
                          (custom-theme-directory default-directory)
                          (user-init-file (expand-file-name "init.el")))
                         (load-file user-init-file)
                         (run-hooks (quote after-init-hook)))'
          echo "Startup successful"
