name: Build production assets
on:
  push:
    branches:
      - main
jobs:
  build:
    name: Build and commit
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        php: [8.0]
        node-version: [14.x]
    steps:
      - name: Checkout code
        uses: actions/checkout@v1

      - name: Set up git user
        run: git config --global user.name extendify
      - name: Set up git email
        run: git config --global user.name extendify@users.noreply.github.com

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '${{ matrix.php }}'
          extensions: mysql, zip
          coverage: none
          tools: composer:v1, wp-cli, cs2pr, phpcs

      - name: Get Composer cache directory
        id: composer-cache
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"
      - name: Use Composer cache
        uses: actions/cache@master
        with:
          path: ${{ steps['composer-cache'].outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-
      - name: Build autoloader
        run: composer install --prefer-dist --no-progress --no-suggest --no-dev

      - name: Using Node version ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - name: npm install, build, and test
        run: |
          npm ci
          npm run build
        env:
          CI: true
      - name: Increment version number
        run: |
          perl -i -pe 's/(Stable tag: )([\d.]+)$/$1.($2+.1)/e' readme.txt
      - name: Commit dist folder if needed #it fails if nothing has changed so we allow an error
        run: git commit -am 'Build produciton assets'
        continue-on-error: true
      - name: Push changed files to main
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: main
