name: Build Docs

on:
  push:
    branches: [ main ]
    paths:
      - docs/**
      - NewHorizons/Schemas/**

  pull_request:
    paths:
      - docs/**
      - NewHorizons/Schemas/**

  workflow_dispatch:
    inputs:
      relative_path:
        description: "Path to set for relative files, set to a blank string for local builds"
        required: false
        default: "/"

env:
  URL_PREFIX:  ${{ github.events.inputs.relative_path }}
  PIPENV_VENV_IN_PROJECT: 1

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v2
      
      - uses: szenius/set-timezone@v1.0
        with:
          timezoneLinux: "America/New York"
      
      - run: mkdir ./.venv
      
      - run: cp -r docs/** .
      
      - if: github.ref == 'refs/heads/main'
        run: |
          echo "URL_PREFIX=/" >> $GITHUB_ENV
          
      - name: Cache Dependencies
        uses: actions/cache@v2
        id: cache-dependencies
        with:
          path: ./.venv
          key: ${{ runner.os }}-pip-${{ hashFiles('**/Pipfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-pipenv

      - name: Install dependecies
        uses: VaultVulp/action-pipenv@v2.0.1
        with:
          command: install --dev
          
      - name: Copy Schemas
        run: cp -rf NewHorizons/Schemas content/pages/
          
      - name: Build Site
        uses: VaultVulp/action-pipenv@v2.0.1
        with:
          command: run menagerie generate
          
      - name: Upload Artifact
        uses: actions/upload-artifact@v2
        with:
          name: Built-Docs
          path: out/
          
      - name: Deploy To Pages
        if: success() && github.ref == 'refs/heads/main'
        uses: JamesIves/github-pages-deploy-action@4.1.5
        with:
          branch: gh-pages 
          folder: out/
          
