version: 2

jobs:
    build:
        resource_class: medium+
        docker:
            - image: circleci/node:9
        environment:
            CONTRACTS_COMMIT_HASH: '9ed05f5'
        working_directory: ~/repo
        steps:
            - checkout
            - run: echo 'export PATH=$HOME/CIRCLE_PROJECT_REPONAME/node_modules/.bin:$PATH' >> $BASH_ENV
            - run:
                  name: install-yarn
                  command: sudo npm install --global yarn@1.9.4
            - run:
                  name: yarn
                  command: yarn --frozen-lockfile install || yarn --frozen-lockfile install
            - run: yarn build:ci:no_website
            - run: yarn build:ts
            - save_cache:
                  key: repo-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo
    build-website:
        resource_class: medium+
        docker:
            - image: circleci/node:9
        working_directory: ~/repo
        steps:
            - restore_cache:
                  keys:
                      - repo-{{ .Environment.CIRCLE_SHA1 }}
            - run: cd packages/website && yarn build:prod
    test-contracts-ganache:
        docker:
            - image: circleci/node:9
        working_directory: ~/repo
        steps:
            - restore_cache:
                  keys:
                      - repo-{{ .Environment.CIRCLE_SHA1 }}
            - run: yarn wsrun test:circleci @0x/contracts-multisig
            - run: yarn wsrun test:circleci @0x/contracts-utils
            - run: yarn wsrun test:circleci @0x/contracts-libs
            - run: yarn wsrun test:circleci @0x/contracts-tokens
            - run: yarn wsrun test:circleci @0x/contracts-extensions
            - run: yarn wsrun test:circleci @0x/contracts-protocol
    test-contracts-geth:
        docker:
            - image: circleci/node:9
            - image: 0xorg/devnet
        working_directory: ~/repo
        steps:
            - restore_cache:
                  keys:
                      - repo-{{ .Environment.CIRCLE_SHA1 }}
            # HACK(albrow): we need to sleep 10 seconds to ensure the devnet is
            # initialized
            - run: sleep 10 && TEST_PROVIDER=geth yarn wsrun test @0x/contracts-multisig
            - run: TEST_PROVIDER=geth yarn wsrun test @0x/contracts-utils
            - run: TEST_PROVIDER=geth yarn wsrun test @0x/contracts-libs
            - run: TEST_PROVIDER=geth yarn wsrun test @0x/contracts-tokens
            - run: TEST_PROVIDER=geth yarn wsrun test @0x/contracts-extensions
            - run: TEST_PROVIDER=geth yarn wsrun test @0x/contracts-protocol
    test-publish:
        resource_class: medium+
        docker:
            - image: circleci/node:9
            - image: 0xorg/verdaccio
        working_directory: ~/repo
        steps:
            - restore_cache:
                  keys:
                      - repo-{{ .Environment.CIRCLE_SHA1 }}
            - run: yarn test:publish:circleci
    test-doc-generation:
        docker:
            - image: circleci/node:9
        working_directory: ~/repo
        steps:
            - restore_cache:
                  keys:
                      - repo-{{ .Environment.CIRCLE_SHA1 }}
            - run: yarn test:generate_docs:circleci
    test-pipeline:
        docker:
            - image: circleci/node:9
            - image: postgres:11-alpine
        working_directory: ~/repo
        steps:
            - restore_cache:
                  keys:
                      - repo-{{ .Environment.CIRCLE_SHA1 }}
            - run: ZEROEX_DATA_PIPELINE_TEST_DB_URL='postgresql://postgres@localhost/postgres' yarn wsrun test:circleci @0x/pipeline
            - save_cache:
                  key: coverage-pipeline-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/pipeline/coverage/lcov.info
    test-rest:
        docker:
            - image: circleci/node:9
        working_directory: ~/repo
        steps:
            - restore_cache:
                  keys:
                      - repo-{{ .Environment.CIRCLE_SHA1 }}
            - run: yarn wsrun test:circleci @0x/contracts-test-utils
            - run: yarn wsrun test:circleci @0x/abi-gen
            - run: yarn wsrun test:circleci @0x/assert
            - run: yarn wsrun test:circleci @0x/base-contract
            - run: yarn wsrun test:circleci @0x/connect
            - run: yarn wsrun test:circleci @0x/contract-wrappers
            - run: yarn wsrun test:circleci @0x/dev-utils
            - run: yarn wsrun test:circleci @0x/json-schemas
            - run: yarn wsrun test:circleci @0x/metacoin
            - run: yarn wsrun test:circleci @0x/order-utils
            - run: yarn wsrun test:circleci @0x/order-watcher
            - run: yarn wsrun test:circleci @0x/sol-compiler
            - run: yarn wsrun test:circleci @0x/sol-cov
            - run: yarn wsrun test:circleci @0x/sol-doc
            - run: yarn wsrun test:circleci @0x/subproviders
            - run: yarn wsrun test:circleci @0x/web3-wrapper
            - run: yarn wsrun test:circleci @0x/utils
            - run: yarn wsrun test:circleci @0x/instant
            - save_cache:
                  key: coverage-abi-gen-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/abi-gen/coverage/lcov.info
            - save_cache:
                  key: coverage-assert-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/assert/coverage/lcov.info
            - save_cache:
                  key: coverage-base-contract-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/base-contract/coverage/lcov.info
            - save_cache:
                  key: coverage-connect-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/connect/coverage/lcov.info
            - save_cache:
                  key: coverage-contract-wrappers-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/contract-wrappers/coverage/lcov.info
            - save_cache:
                  key: coverage-dev-utils-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/dev-utils/coverage/lcov.info
            - save_cache:
                  key: coverage-json-schemas-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/json-schemas/coverage/lcov.info
            - save_cache:
                  key: coverage-metacoin-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/metacoin/coverage/lcov.info
            - save_cache:
                  key: coverage-order-utils-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/order-utils/coverage/lcov.info
            - save_cache:
                  key: coverage-order-watcher-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/order-watcher/coverage/lcov.info
            - save_cache:
                  key: coverage-sol-compiler-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/sol-compiler/coverage/lcov.info
            - save_cache:
                  key: coverage-sol-cov-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/sol-cov/coverage/lcov.info
            - save_cache:
                  key: coverage-sol-doc-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/sol-doc/coverage/lcov.info
            - save_cache:
                  key: coverage-subproviders-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/subproviders/coverage/lcov.info
            - save_cache:
                  key: coverage-web3-wrapper-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/packages/web3-wrapper/coverage/lcov.info
    test-python:
        working_directory: ~/repo
        docker:
            - image: circleci/python
            - image: 0xorg/ganache-cli
              command: |
                  ganache-cli --gasLimit 10000000 --noVMErrorsOnRPCResponse --db /snapshot --noVMErrorsOnRPCResponse -p 8545 --networkId 50 -m "concert load couple harbor equip island argue ramp clarify fence smart topic"
            - image: 0xorg/launch-kit-ci
              command: |
                  yarn start:ts -p 3000:3000
        steps:
            - checkout
            - run: sudo chown -R circleci:circleci /usr/local/bin
            - run: sudo chown -R circleci:circleci /usr/local/lib/python3.7/site-packages
            - restore_cache:
                  key: deps9-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
            - run:
                  command: |
                      cd python-packages/order_utils
                      python -m ensurepip
                      python -m pip install -e .[dev]
            - run:
                  command: |
                      cd python-packages/sra_client
                      python -m ensurepip
                      python -m pip install -e .
            - save_cache:
                  key: deps9-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - '/usr/local/bin'
                      - '/usr/local/lib/python3.7/site-packages'
                      - '.eggs'
                      - '.mypy_cache'
                      - '.pytest_cache'
                      - '.tox'
            - run:
                  command: |
                      cd python-packages/order_utils
                      coverage run setup.py test
            - run:
                  command: |
                      cd python-packages/sra_client
                      coverage run setup.py test
            - save_cache:
                  key: coverage-python-order-utils-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/python-packages/order_utils/.coverage
            - save_cache:
                  key: coverage-python-sra-client-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - ~/repo/python-packages/sra_client/.coverage
    test-rest-python:
        working_directory: ~/repo
        docker:
            - image: circleci/python
        steps:
            - checkout
            - run: sudo chown -R circleci:circleci /usr/local/bin
            - run: sudo chown -R circleci:circleci /usr/local/lib/python3.7/site-packages
            - restore_cache:
                  key: deps9-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
            - run:
                  command: |
                      cd python-packages/order_utils
                      python -m ensurepip
                      python -m pip install -e .[dev]
            - save_cache:
                  key: deps9-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - '/usr/local/bin'
                      - '/usr/local/lib/python3.7/site-packages'
                      - '.eggs'
                      - '.mypy_cache'
                      - '.pytest_cache'
                      - '.tox'
            - run:
                  command: |
                      cd python-packages/order_utils
                      tox
    static-tests-python:
        working_directory: ~/repo
        docker:
            - image: circleci/python
        steps:
            - checkout
            - run: sudo chown -R circleci:circleci /usr/local/bin
            - run: sudo chown -R circleci:circleci /usr/local/lib/python3.7/site-packages
            - restore_cache:
                  key: deps9-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
            - run:
                  command: |
                      cd python-packages/order_utils
                      python -m ensurepip
                      python -m pip install -e .[dev]
            - save_cache:
                  key: deps9-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
                  paths:
                      - '/usr/local/bin'
                      - '/usr/local/lib/python3.7/site-packages'
            - run:
                  command: |
                      cd python-packages/order_utils
                      python setup.py lint
    static-tests:
        working_directory: ~/repo
        docker:
            - image: circleci/node:9
        steps:
            - restore_cache:
                  keys:
                      - repo-{{ .Environment.CIRCLE_SHA1 }}
            - run: yarn lerna run lint
            - run: yarn prettier:ci
            - run: cd packages/0x.js && yarn build:umd:prod
            - run: yarn bundlewatch
    submit-coverage:
        docker:
            - image: circleci/node:9
        working_directory: ~/repo
        steps:
            - restore_cache:
                  keys:
                      - repo-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-abi-gen-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-assert-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-base-contract-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-connect-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-contract-wrappers-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-dev-utils-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-json-schemas-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-metacoin-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-order-utils-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-order-watcher-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-sol-compiler-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-sol-cov-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-sol-doc-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-subproviders-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-web3-wrapper-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-contracts-{{ .Environment.CIRCLE_SHA1 }}
            - restore_cache:
                  keys:
                      - coverage-python-order-utils-{{ .Environment.CIRCLE_SHA1 }}
            - run: yarn report_coverage
workflows:
    version: 2
    main:
        jobs:
            - build
            - build-website:
                  requires:
                      - build
            - test-contracts-ganache:
                  requires:
                      - build
            - test-contracts-geth:
                  requires:
                      - build
            - test-pipeline:
                  requires:
                      - build
            - test-rest:
                  requires:
                      - build
            - static-tests:
                  requires:
                      - build
            - test-publish:
                  requires:
                      - build
            - test-doc-generation:
                  requires:
                      - build
            - submit-coverage:
                  requires:
                      - test-rest
                      - test-python
            - test-python
            - static-tests-python
            # skip python tox run for now, as we don't yet have multiple test environments to support.
            #- test-rest-python
