name: 'Compile Mermaid to MD'

on:
  push:
    paths:
      - 'docs/**/*.mmd'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Project
        uses: actions/checkout@v2
        with:
          fetch-depth: 2

      - name: Get Changed Files
        id: getfile
        run: |
          echo "::set-output name=files::$(git diff-tree --no-commit-id --name-only -r ${{ github.sha }} | grep -e '.*\.mmd$' | xargs)"

      - name: Print mmd Files Changed
        run: |
          echo ${{ steps.getfile.outputs.files }}

      - name: Compile Mermaid Files
        uses: neenjaw/compile-mermaid-markdown-action@0.3.1
        with:
          files: ${{ steps.getfile.outputs.files }}
          output: '.resources'
        env:
          HIDE_CODEBLOCKS: 1
          ABSOLUTE_IMAGE_LINKS: 1
          OUTPUT_FILE_TYPE: "svg"

      - name: Show Changes
        run: |
          git status

      - name: Commit Files
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add -A
          git diff-index --quiet HEAD || git commit -a -m 'Updating Diagram'

      - name: Push Changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}
