version: 2
jobs:
  build:
    docker: &base-image
      - image: ocaml/opam:ubuntu-16.04-ocaml-4.11
    environment:
      - TERM: "xterm"
    steps:
      - checkout

      - run:
          name: Check if build should be skipped on CircleCI only
          command: |
            if [[ $(git log --format=oneline -n 1 $CIRCLE_SHA1) == *"[circle-skip]"* ]]; then echo 1 > skip; fi

      - run: &check-version-update
          name: Check if version update is needed
          command: |
            [[ -e skip ]] || ./scripts/makam-version.sh check-if-updated

      - run: &install-node
          name: Install Node.js and other system dependencies
          command: |
            [[ -e skip ]] || curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
            [[ -e skip ]] || sudo apt-get install -y nodejs m4
            [[ -e skip ]] || sudo npm install -g yarn
            [[ -e skip ]] || sudo chown -R $(whoami):$(id -gn $(whoami)) ~/.config

      - run: &install-dependencies
          name: Install OCaml dependencies
          command: |
            [[ -e skip ]] || opam switch create --yes ./

      - run: &compile
          name: Compile Makam
          command: |
            [[ -e skip ]] || opam config exec make

      - run: &prepare-npm-package
          name: Prepare npm package, test and install it
          command: |
            [[ -e skip ]] || opam config exec make prepare-test-npm-package
            [[ -e skip ]] || (cd npm; opam config exec yarn jest)
            [[ -e skip ]] || npm install makam-$(./scripts/makam-version.sh npm-test-version).tgz

      - run: &tests
          name: Run Tests
          command: |
            [[ -e skip ]] || make MAKAM=./node_modules/.bin/makam makam-timing-tests
            [[ -e skip ]] || make MAKAM=./node_modules/.bin/makam makam-snapshot-tests

      - store_artifacts:
          path: /tmp/artifacts

      - run: &compile-js
          name: Compile Makam.js
          command: |
            [[ -e skip ]] || opam config exec make js

      - run: &tests-js
          name: Run Tests for Makam.js
          command: |
            [[ -e skip ]] || if [[ $(git log --format=oneline -n 1 $CIRCLE_SHA1) == *"[ci-test-js]"* ]]; then make makam-js-tests; fi

      - run: &publish-npm-package
          name: Prepare production npm package and publish it
          command: |
            if [[ -e skip || $(git rev-parse --abbrev-ref HEAD) != master ]]; then echo 1 > skip-deploy; fi
            [[ -e skip-deploy ]] || if [[ $(./scripts/makam-version.sh has-update) == false ]]; then echo 1 > skip-deploy; fi
            [[ -e skip-deploy ]] || (set -u; echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc; set +u)
            [[ -e skip-deploy ]] || opam config exec make npm-prod-publish

      - run: &deploy-lambda
          name: Deploy Makam Lambda
          command: |
            [[ -e skip-deploy ]] || opam config exec make webservice-prod-deploy

      - run: &publish-webui-package
          name: Publish Makam WebUI package
          command: |
            [[ -e skip-deploy ]] || opam config exec make webui-prod-publish
  check-other-repos:
    docker: *base-image
    steps:
      - run:
          name: Force CircleCI build of website-source
          command: |
            curl -u ${CIRCLECI_TOKEN_WEBSITE_SOURCE}: \
              -d build_parameters[CIRCLE_JOB]=test \
              https://circleci.com/api/v1.1/project/github/astampoulis/website-source/tree/master

workflows:
  version: 2
  test-and-deploy:
    jobs:
      - build
      - check-other-repos:
          requires:
            - build
          filters:
            branches:
              only:
                - master
