version: 2
jobs:
  build:
    docker:
      - image: quay.io/fenicsproject/dev-env:latest
    working_directory: /home/fenics/working
    steps:
      - run:
          name: Clone DOLFIN
          command: git lfs clone https://bitbucket.org/"${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}".git .
      - checkout
      - run:
          name: Install/update dependencies  # Install with sudo as tests not run as superuser in circleci/python
          command: sudo pip3 install flake8 pytest pytest-xdist sphinx sphinx_rtd_theme --upgrade
      - run:
          name: Install FEniCS dependencies
          command: |
            DEP_BRANCH_NAME="master"
            pip3 install git+https://github.com/FEniCS/fiat.git@"${DEP_BRANCH_NAME}"
            pip3 install git+https://github.com/FEniCS/ufl.git@"${DEP_BRANCH_NAME}"
            pip3 install git+https://bitbucket.org/fenics-project/dijitso.git@"${DEP_BRANCH_NAME}"
            pip3 install git+https://bitbucket.org/fenics-project/ffc.git@"${DEP_BRANCH_NAME}"
      - run:
          name: Flake8 checks on pybind11 Python code
          command: python3 -m flake8 --append-config=python/setup.cfg python/dolfin
      - run:
          name: Build DOLFIN C++ documentation
          command: cd doc && doxygen
      - run:
          name: Configure DOLFIN (C++)
          command: mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ../
      - run:
          name: Build DOLFIN and install (C++)
          command: cd build && make -j2 install
      - run:
          name: Build and run C++ unit tests (serial)
          command: cd build && make -j2 unittests && ctest -R unittests
      - run:
          name: Build and run C++ regressions tests (serial)
          command: cd build && make -j2 demos && ctest -j2 -R demo -R serial
      - run:
          name: Run C++ regression tests (MPI)
          command: cd build && make -j2 demos && ctest -R demo -R mpi
      - run:
          name: Build Python interface
          command: |
            cd python
            pip3 -v install . --user
      - run:
          name: Build DOLFIN Python documentation
          command: cd python/doc && make html
      - persist_to_workspace:
          root: /home/fenics/working
          paths:
            - python/doc/build/html/
      - run:
          name: Run Python unit tests (serial)
          command: python3 -m pytest -n 2 python/test/unit/
      - run:
          name: Run Python unit tests (MPI)
          command: mpirun -n 3 python3 -m pytest python/test/unit/
      - run:
          name: Generate Python demo (regression) programs
          command: python3 python/demo/generate-demo-files.py
      - run:
          name: Run Python regression tests (serial)
          command: python3 -m pytest -v -n2 python/demo/test.py
      - run:
          name: Run Python regression tests (MPI)
          command: python3 -m pytest -v python/demo/test.py --mpiexec=mpiexec --num-proc=3
  pushdoc:
    docker:
      - image: circleci/python
    working_directory: /tmp
    steps:
      - attach_workspace:
          at: /tmp
      - run:
          name: Establish authenticity of fenicsproject.org
          command: mkdir ~/.ssh && echo "fenicsproject.org ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFbD134YcxSNaHVyio0XPAvJYh8SF90xp/ARqfrbAwOmJaEPTl5CNXm6f3+Sy0k1QTp+j9WawiJON+4/FWQBTD4=" >> ~/.ssh/known_hosts
      - run:
          name: Push Python API doc to fenicsproject.org
          command: cd /tmp/python/doc/build/html && scp -r * docs@fenicsproject.org:/var/www/vhosts/fenicsproject.org/docs/dolfin/dev/python/
workflows:
  version: 2
  build-and-pushdoc:
    jobs:
      - build
      - pushdoc:
          requires:
            - build
          filters:
            branches:
              only:
                - master
