version: 2.1

# -------------------------
#        ORBS
# -------------------------
orbs:
  nx: nrwl/nx@1.6.2
  rust: circleci/rust@1.6.0
  browser-tools: circleci/browser-tools@1.4.8

# -------------------------
#        EXECUTORS
# -------------------------

defaults: &defaults
  working_directory: ~/repo

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

  macos:
    <<: *defaults
    resource_class: macos.m1.medium.gen1
    macos:
      xcode: '14.2.0'

# -------------------------
#        COMMANDS
# -------------------------
commands:
  run-pnpm-install:
    parameters:
      os:
        type: string
    steps:
      - restore_cache:
          name: Restore pnpm Package Cache
          keys:
            - node-deps-{{ arch }}-v3-{{ checksum "pnpm-lock.yaml" }}
      - when:
          condition:
            equal: [<< parameters.os >>, linux]
          steps:
            - run:
                name: Install pnpm package manager (linux)
                command: |
                  npm install --prefix=$HOME/.local -g @pnpm/exe@8
      - when:
          condition:
            equal: [<< parameters.os >>, macos]
          steps:
            - run:
                name: Install pnpm package manager (macos)
                command: |
                  npm install -g @pnpm/exe@8
      - run:
          name: Install Dependencies
          command: |
            pnpm install --frozen-lockfile
            pnpm playwright install --with-deps
      - save_cache:
          name: Save pnpm Package Cache
          key: node-deps-{{ arch }}-v3-{{ checksum "pnpm-lock.yaml" }}
          paths:
            - ~/.pnpm-store
            - ~/.cache/Cypress
            - node_modules
  install-sdkman:
    description: Install SDKMAN
    steps:
      - restore_cache:
          name: Restore SDKMAN executable and binaries from cache
          keys:
            - sdkman-cli-{{ arch }}-v2
      - run:
          name: Installing SDKMAN
          command: |
            if [ ! -d ~/.sdkman ]
            then
              curl -s "https://get.sdkman.io?rcupdate=false" | bash
              sed -i -e 's/sdkman_auto_answer=false/sdkman_auto_answer=true/g' ~/.sdkman/etc/config
            fi
            echo -e '\nsource "/home/circleci/.sdkman/bin/sdkman-init.sh"' >> $BASH_ENV
            source $BASH_ENV
            sdk version
      - save_cache:
          name: Save SDKMAN executable and binaries to cache
          key: sdkman-cli-{{ arch }}-v2
          paths:
            - ~/.sdkman
  install-gradle:
    description: Install gradle
    parameters:
      gradle-version:
        type: string
        default: ''
    steps:
      - restore_cache:
          name: Restore Gradle binary from cache
          keys:
            - gradle-cli-{{ arch }}-v1
      - run:
          name: Installing Gradle
          command: |
            sdk install gradle << parameters.gradle-version >>
            gradle --version
      - save_cache:
          name: Save Gradle binary to cache
          key: gradle-cli-{{ arch }}-v1
          paths:
            - ~/.sdkman/candidates/gradle/
# -------------------------
#          JOBS
# -------------------------
jobs:
  # -------------------------
  #     JOBS: Main Linux
  # -------------------------
  main-linux:
    executor: linux
    environment:
      NX_E2E_CI_CACHE_KEY: e2e-circleci-linux
      NX_DAEMON: 'true'
      NX_PERF_LOGGING: 'false'
      NX_NATIVE_LOGGING: 'false'
      NX_E2E_RUN_E2E: 'true'
      NX_CI_EXECUTION_ENV: 'linux'
      NX_CLOUD_DTE_V2: 'true'
    steps:
      - checkout
      - nx/set-shas:
          main-branch-name: 'master'
      - run: npx nx-cloud@next start-ci-run --distribute-on=".nx/workflows/dynamic-changesets.yaml" --stop-agents-after="e2e"
      - run:
          command: |
            sudo apt-get update
            sudo apt-get install -y ca-certificates lsof
      - browser-tools/install-chrome
      - browser-tools/install-chromedriver
      - install-sdkman
      - install-gradle:
          gradle-version: '8.5'
      - run-pnpm-install:
          os: linux
      - run:
          name: Check Documentation
          command: pnpm nx documentation --no-dte
          no_output_timeout: 20m
      - run:
          name: Run Checks/Lint/Test/Build
          no_output_timeout: 60m
          command: |
            pids=()

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

            pnpm nx run-many -t check-imports check-commit check-lock-files check-codeowners documentation --parallel=1 --no-dte &
            pids+=($!)

            (pnpm nx affected --targets=lint,test,build --base=$NX_BASE --head=$NX_HEAD --parallel=3 &&
            pnpm nx affected --targets=e2e,e2e-ci --base=$NX_BASE --head=$NX_HEAD --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_PERF_LOGGING: 'false'
      NX_CI_EXECUTION_ENV: 'macos'
      SELECTED_PM: 'npm' # explicitly define npm for macOS tests
    steps:
      - checkout
      - 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
      - run-pnpm-install:
          os: macos
      - rust/install
      - nx/set-shas:
          main-branch-name: 'master'
      - run:
          name: Run E2E Tests for macOS
          command: |
            pnpm nx affected -t e2e-macos --parallel=1 --base=$NX_BASE --head=$NX_HEAD
          no_output_timeout: 45m

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

  build:
    jobs:
      - main-linux
      - mainmacos:
          name: main-macos-e2e
