name: Build

on: [push, pull_request]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    name: build (Linux, GCC ${{ matrix.gcc }}, ${{ matrix.plugins }} plugins)
    strategy:
      fail-fast: false
      matrix:
        os:
        - ubuntu-18.04
        gcc:
        - 4.8
        - 7
        plugins:
        - default
        include:
        - os: ubuntu-20.04
          gcc: 10
          plugins: all
    steps:
    - name: Set up Python 3
      uses: actions/setup-python@v2
      with:
        python-version: 3
    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install \
            libgtk2.0-0 \
            libsdl-image1.2-dev \
            libsdl-ttf2.0-dev \
            libsdl1.2-dev \
            libxml-libxml-perl \
            libxml-libxslt-perl \
            lua5.3 \
            ninja-build \
            zlib1g-dev
        pip install sphinx
    - name: Install GCC
      run: |
        sudo apt-get install gcc-${{ matrix.gcc }} g++-${{ matrix.gcc }}
    - name: Clone DFHack
      uses: actions/checkout@v1
      with:
        fetch-depth: 0  # unlimited - we need past tags
        submodules: true
    - name: Set up environment
      id: env_setup
      run: |
        DF_VERSION="$(sh travis/get-df-version.sh)"
        echo "::set-output name=df_version::${DF_VERSION}"
        echo "DF_VERSION=${DF_VERSION}" >> $GITHUB_ENV
        echo "DF_FOLDER=${HOME}/DF/${DF_VERSION}/df_linux" >> $GITHUB_ENV
    - name: Fetch DF cache
      uses: actions/cache@v2
      with:
        path: ~/DF
        key: ${{ steps.env_setup.outputs.df_version }}
    - name: Download DF
      run: |
        sh travis/download-df.sh
    - name: Build DFHack
      env:
        CC: gcc-${{ matrix.gcc }}
        CXX: g++-${{ matrix.gcc }}
      run: |
        cmake \
          -S . \
          -B build-ci \
          -G Ninja \
          -DDFHACK_BUILD_ARCH=64 \
          -DBUILD_TESTS:BOOL=ON \
          -DBUILD_DEV_PLUGINS:BOOL=${{ matrix.plugins == 'all' }} \
          -DBUILD_SIZECHECK:BOOL=${{ matrix.plugins == 'all' }} \
          -DBUILD_STONESENSE:BOOL=${{ matrix.plugins == 'all' }} \
          -DBUILD_SUPPORTED:BOOL=1 \
          -DCMAKE_INSTALL_PREFIX="$DF_FOLDER"
        ninja -C build-ci install
    - name: Run tests
      id: run_tests
      run: |
        export TERM=dumb
        mv "$DF_FOLDER"/dfhack.init-example "$DF_FOLDER"/dfhack.init
        script -qe -c "python travis/run-tests.py --headless --keep-status \"$DF_FOLDER\""
        python travis/check-rpc.py "$DF_FOLDER/dfhack-rpc.txt"
        mkdir -p artifacts
        cp "$DF_FOLDER/test_status.json" "$DF_FOLDER"/*.log artifacts
    - name: Upload test artifacts
      uses: actions/upload-artifact@v1
      if: (success() || failure()) && steps.run_tests.outcome != 'skipped'
      continue-on-error: true
      with:
        name: test-artifacts-${{ matrix.gcc }}
        path: artifacts
    - name: Clean up DF folder
      # prevent DFHack-generated files from ending up in the cache
      # (download-df.sh also removes them, this is just to save cache space)
      if: success() || failure()
      run: |
        rm -rf "$DF_FOLDER"

  docs:
    runs-on: ubuntu-18.04
    steps:
    - name: Set up Python 3
      uses: actions/setup-python@v2
      with:
        python-version: 3
    - name: Install dependencies
      run: |
        pip install sphinx
    - name: Clone DFHack
      uses: actions/checkout@v1
      with:
        submodules: true
    - name: Build docs
      run: |
        sphinx-build -W --keep-going -j3 . docs/html
    - name: Upload docs
      uses: actions/upload-artifact@v1
      with:
        name: docs
        path: docs/html

  lint:
    runs-on: ubuntu-18.04
    steps:
    - name: Set up Python 3
      uses: actions/setup-python@v2
      with:
        python-version: 3
    - name: Set up Ruby 2.7
      uses: actions/setup-ruby@v1
      with:
        ruby-version: 2.7
    - name: Install Lua
      run: |
        sudo apt-get update
        sudo apt-get install lua5.3
    - name: Clone DFHack
      uses: actions/checkout@v1
      with:
        submodules: true
        # don't need tags here
    - name: Check whitespace
      run: |
        python travis/lint.py
    - name: Check Authors.rst
      if: success() || failure()
      run: |
        python travis/authors-rst.py
    - name: Check for missing documentation
      if: success() || failure()
      run: |
        python travis/script-docs.py
    - name: Check Lua syntax
      if: success() || failure()
      run: |
        python travis/script-syntax.py --ext=lua --cmd="luac5.3 -p" --github-actions
    - name: Check Ruby syntax
      if: success() || failure()
      run: |
        python travis/script-syntax.py --ext=rb --cmd="ruby -c" --github-actions

  check-pr:
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    steps:
    - name: Check that PR is based on develop branch
      env:
        BASE_BRANCH: ${{ github.base_ref }}
      run: |
        echo "PR base branch: $BASE_BRANCH"
        test "$BASE_BRANCH" = develop
