common_cache_key: &common_cache_key
  key: dependency-cache-{{ .Branch }}-{{ checksum "../ocaml-version" }}-{{ checksum ".circleci/config.yml" }}-{{ checksum "reason.opam" }}-{{ checksum "rtop.opam" }}

esy_cache_key: &esy_cache_key
  key: esy-cache-{{ checksum "esy.json" }}-{{ checksum "esy.lock/index.json" }}-{{ checksum ".circleci/config.yml" }}

esy_post_build_cache_key: &esy_post_build_cache_key
  key: esy-post-build-cache-{{ checksum "esy.json" }}-{{ checksum "esy.lock/index.json" }}-{{ checksum ".circleci/config.yml" }}

common_steps: &common_steps
  steps:
    - run:
        name: "Install npm"
        command: |
          sudo apt-get install -y nodejs npm
          mkdir -p ~/.npm-global
          npm config set prefix $NPM_CONFIG_PREFIX
    - checkout
    - run:
        name: "Note OCAML_VERSION for cache"
        command: echo $OCAML_VERSION > ../ocaml-version
    - restore_cache:
        <<: *common_cache_key
    - run:
        name: "Initialize opam"
        command: |
          sudo apt-get install -y m4
          opam remote set-url default https://opam.ocaml.org
    - run:
        name: "Install deps"
        command: |
          sudo apt-get install -y pkg-config libncurses5-dev
          opam update
          opam install -y dune menhir utop
    - run:
        name: 'Clean'
        command: make clean-for-ci
    - save_cache:
        <<: *common_cache_key
        paths:
          - ~/.opam
          - ~/.npm-global
    - run:
        name: 'opam pin'
        command: |
          opam pin add -y reason .
          opam pin add -y rtop .
    - run:
        name: 'Test'
        command: |
          eval `opam config env`
          make test-ci
    - run:
        name: 'Check git is clean'
        command: git diff --exit-code

version: 2
jobs:
  4.03.0:
    docker:
      - image: ocaml/opam:debian-ocaml-4.03
    environment:
      - TERM: dumb
      - OCAML_VERSION: "4.03.x"
      - NPM_CONFIG_PREFIX: "~/.npm-global"
    <<: *common_steps
  4.04.0:
    docker:
      - image: ocaml/opam:debian-ocaml-4.04
    environment:
      - TERM: dumb
      - OCAML_VERSION: "4.04.x"
      - NPM_CONFIG_PREFIX: "~/.npm-global"
    <<: *common_steps
  4.06.1:
    docker:
      - image: ocaml/opam:debian-ocaml-4.06
    environment:
      - TERM: dumb
      - OCAML_VERSION: "4.06.x"
      - NPM_CONFIG_PREFIX: "~/.npm-global"
    <<: *common_steps
  4.07.0:
    docker:
      - image: ocaml/opam:debian-ocaml-4.07
    environment:
      - TERM: dumb
      - OCAML_VERSION: "4.07.x"
      - NPM_CONFIG_PREFIX: "~/.npm-global"
    <<: *common_steps
  4.08.0:
    docker:
      - image: ocaml/opam:debian-ocaml-4.08
    environment:
      - TERM: dumb
      - OCAML_VERSION: "4.08.x"
      - NPM_CONFIG_PREFIX: "~/.npm-global"
    <<: *common_steps
  4.09.0:
    docker:
      - image: ocaml/opam:debian-ocaml-4.09
    environment:
      - TERM: dumb
      - OCAML_VERSION: "4.09.x"
      - NPM_CONFIG_PREFIX: "~/.npm-global"
    <<: *common_steps
  4.10.0:
    docker:
      - image: ocaml/opam:debian-ocaml-4.10
    environment:
      - TERM: dumb
      - OCAML_VERSION: "4.10.x"
      - NPM_CONFIG_PREFIX: "~/.npm-global"
    <<: *common_steps
  4.12.0:
    docker:
      - image: ocaml/opam:debian-ocaml-4.12
    environment:
      - TERM: dumb
      - OCAML_VERSION: "4.12.x"
      - NPM_CONFIG_PREFIX: "~/.npm-global"
    <<: *common_steps
  esy_build:
    docker:
      - image: ocaml/opam:debian-ocaml-4.12
    environment:
      - TERM: dumb
      - NPM_CONFIG_PREFIX: "~/.npm-global"
    steps:
      - checkout
      - run:
          name: "Install npm"
          command: |
            curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
            sudo apt-get install -y nodejs
            mkdir -p ~/.npm-global
            npm config set prefix $NPM_CONFIG_PREFIX
      - run:
          name: "Install m4"
          command: |
            sudo apt-get install -y m4
      - restore_cache:
          <<: *esy_cache_key
      - restore_cache:
          <<: *esy_post_build_cache_key
      - run:
          name: "Install esy"
          command: npm install -g esy@latest
      - run:
          name: 'Clean'
          command: make clean-for-ci
      - run:
          name: 'esy install'
          command: ~/.npm-global/bin/esy install
      - save_cache:
          <<: *esy_cache_key
          paths:
            - ~/.npm-global
            - ~/.esy
      - run:
          name: 'esy build'
          command: |
            ~/.npm-global/bin/esy build
            ~/.npm-global/bin/esy x refmt --version
      # Use both pre and post build caches so that we're more likely
      # to have dependencies cached. `esy build` is more likely to fail,
      # so this will make the push that fixes the build faster
      - save_cache:
          <<: *esy_post_build_cache_key
          paths:
            - ~/.esy

workflows:
  version: 2
  build-deploy:
    jobs:
      - 4.03.0
      - 4.04.0
      - 4.06.1
      - 4.07.0
      - 4.08.0
      - 4.09.0
      - 4.10.0
      - 4.12.0
      - esy_build
