version: 2.1

# -------------------------
#        ORBS
# -------------------------
orbs:
  nx: nrwl/nx@1.6.1
  node: circleci/node@5.0.2
  rust: circleci/rust@1.6.0
  browser-tools: circleci/browser-tools@1.4.0

# -------------------------
#        DEFAULTS
# -------------------------
machine:
  pre:
    - mkdir ~/.cache/yarn
    - mkdir ~/.cache/Cypress
    - mkdir ~/Library/Caches/Homebrew
    - mkdir /usr/local/Homebrew

dependencies:
  cache_directories:
    - ~/.cache/yarn
    - ~/.cache/Cypress
    - /usr/local/Homebrew
    - ~/Library/Caches/Homebrew
  override:
    - yarn install
    - brew install

defaults: &defaults
  working_directory: ~/repo

# -------------------------
#        EXECUTORS
# -------------------------
executors:
  linux:
    <<: *defaults
    docker:
      - image: cimg/rust:1.66.1-browsers
    resource_class: medium+

  macos:
    <<: *defaults
    macos:
      xcode: &_XCODE_VERSION '13.0.0'

# -------------------------
#        COMMANDS
# -------------------------
commands:
  run-yarn-install:
    parameters:
      os:
        type: string
    steps:
      - restore_cache:
          name: Restore Yarn Package Cache
          keys:
            - node-deps-{{ arch }}-v1-{{ checksum "package.json" }}-{{ checksum "yarn.lock" }}
      - run:
          name: Install dependencies
          command: yarn install --prefer-offline --frozen-lockfile --non-interactive --cache-folder ~/.cache/yarn
      - save_cache:
          name: Save Yarn Package Cache
          key: node-deps-{{ arch }}-v1-{{ checksum "package.json" }}-{{ checksum "yarn.lock" }}
          paths:
            - node_modules
            - ~/.cache/yarn
            - ~/.cache/Cypress
  install-pnpm:
    steps:
      - run:
          name: Install PNPM
          command: npm install --prefix=$HOME/.local -g pnpm@6.32.4

  setup:
    parameters:
      os:
        type: string
    steps:
      - checkout
      - when:
          condition:
            equal: [<< parameters.os >>, macos]
          steps:
            - restore_cache:
                name: Restore Homebrew packages
                keys:
                  - nrwl-nx-homebrew-packages
            - run:
                name: Configure Detox Environment, Install applesimutils
                command: |
                  HOMEBREW_NO_AUTO_UPDATE=1 brew tap wix/brew >/dev/null
                  HOMEBREW_NO_AUTO_UPDATE=1 brew install applesimutils >/dev/null
                  xcrun simctl shutdown all && xcrun simctl erase all
                no_output_timeout: 20m
            - save_cache:
                name: Save Homebrew Cache
                key: nrwl-nx-homebrew-packages
                paths:
                  - /usr/local/Homebrew
                  - ~/Library/Caches/Homebrew
      - when:
          condition:
            equal: [<< parameters.os >>, linux]
          steps:
            - run:
                command: sudo apt-get install -y ca-certificates
            - browser-tools/install-chrome
            - browser-tools/install-chromedriver
      - node/install:
          # Use LTS version
          node-version: ''
      - run-yarn-install:
          os: << parameters.os >>
      - when:
          condition:
            equal: [<< parameters.os >>, linux]
          steps:
            - install-pnpm

# -------------------------
#          JOBS
# -------------------------
jobs:
  # -------------------------
  #     JOBS: Agent
  # -------------------------
  agent:
    parameters:
      os:
        type: string
        default: 'linux'
      pm:
        type: string
        default: 'pnpm'
    executor: << parameters.os >>
    environment:
      GIT_AUTHOR_EMAIL: test@test.com
      GIT_AUTHOR_NAME: Test
      GIT_COMMITTER_EMAIL: test@test.com
      GIT_COMMITTER_NAME: Test
      NX_E2E_CI_CACHE_KEY: e2e-circleci-<< parameters.os >>
      SELECTED_PM: << parameters.pm >>
      NX_E2E_RUN_CYPRESS: 'false'
      NX_VERBOSE_LOGGING: 'false'
      NX_PERF_LOGGING: 'false'
      NX_NATIVE_HASHER: 'true'
    steps:
      - run:
          name: Configure git metadata (needed for lerna smoke tests)
          command: |
            git config --global user.email test@test.com
            git config --global user.name "Test Test"
      - run:
          name: Set dynamic nx run variable
          command: |
            echo "export NX_RUN_GROUP=\"run-group-<< parameters.os >>-$CIRCLE_WORKFLOW_ID\";" >> $BASH_ENV
      - setup:
          os: << parameters.os >>
      - run:
          name: Agent
          command: npx nx-cloud start-agent
          no_output_timeout: 60m

  # -------------------------
  #     JOBS: Main Linux
  # -------------------------
  main-linux:
    executor: linux
    environment:
      NX_E2E_CI_CACHE_KEY: e2e-circleci-linux
      NX_VERBOSE_LOGGING: 'false'
      NX_DAEMON: 'true'
      NX_PERF_LOGGING: 'false'
      NX_NATIVE_HASHER: 'true'
    steps:
      - run:
          name: Set dynamic nx run variable
          command: |
            echo "export NX_RUN_GROUP=\"run-group-linux-$CIRCLE_WORKFLOW_ID\";" >> $BASH_ENV
      - setup:
          os: linux
      - nx/set-shas:
          main-branch-name: 'master'
      - run: npx nx-cloud start-ci-run --stop-agents-after="e2e"
      - run:
          name: Check Documentation
          command: npx nx documentation --no-dte
          no_output_timeout: 20m
      - run:
          name: Run Checks/Lint/Test/Build
          no_output_timeout: 60m
          command: |
            pids=()

            npx nx-cloud record -- nx format:check --base=$NX_BASE --head=$NX_HEAD &
            pids+=($!)

            npx nx run-many -t root-lint check-imports check-commit check-lock-files depcheck --parallel=1 --no-dte &
            pids+=($!)

            yarn nx affected --target=lint --base=$NX_BASE --head=$NX_HEAD --parallel=3 &
            pids+=($!)
            yarn nx affected --target=test --base=$NX_BASE --head=$NX_HEAD --parallel=1 &
            pids+=($!)
            (yarn nx affected --target=build --base=$NX_BASE --head=$NX_HEAD --parallel=3 &&
            npx nx affected --target=e2e --base=$NX_BASE --head=$NX_HEAD --exclude=e2e-storybook,e2e-storybook-angular --parallel=1) &
            pids+=($!)

            for pid in "${pids[@]}"; do
              wait "$pid"
            done
  # -------------------------
  #     JOBS: Main-MacOS
  # -------------------------
  mainmacos:
    executor: macos
    environment:
      NX_E2E_CI_CACHE_KEY: e2e-circleci-macos
      NX_DAEMON: 'true'
      NX_PERF_LOGGING: 'false'
      NX_NATIVE_HASHER: 'true'
    steps:
      - run:
          name: Set dynamic nx run variable
          command: |
            echo "export NX_RUN_GROUP=\"run-group-macos-$CIRCLE_WORKFLOW_ID\";" >> $BASH_ENV
      - setup:
          os: macos
      - rust/install
      - run:
          name: Run E2E Tests for macOS
          # FIXME: remove --exclude=e2e-detox once we have a fix for the detox tests
          command: |
            npx nx run-many -t e2e-macos --parallel=1 --exclude=e2e-detox
          no_output_timeout: 45m

# -------------------------
#        WORKFLOWS(JOBS)
# -------------------------
workflows:
  version: 2

  build:
    jobs:
      - agent:
          name: 'agent1'
      - agent:
          name: 'agent2'
      - agent:
          name: 'agent3'
      - agent:
          name: 'agent4'
      - agent:
          name: 'agent5'
      - agent:
          name: 'agent6'
      - agent:
          name: 'agent7'
      - agent:
          name: 'agent8'
      - main-linux
      - mainmacos:
          name: main-macos-e2e
