version: 2.1
executors:
  latest-xcode:
    macos:
      xcode: '10.2.0'
commands:
  pod-update:
    description: Optimized pod update by using S3 cache, see https://discuss.circleci.com/t/best-practice-for-use-of-cached-cocoapods-specs/18897
    steps:
      - run: curl https://cocoapods-specs.circleci.com/fetch-cocoapods-repo-from-s3.sh | bash -s cf
      - run: bundle exec pod update --project-directory=Example --no-repo-update
  pod-install:
    description: Checkout and Install Dependencies
    steps:
      - run: chruby
      - checkout
      - run: echo "2.5.5" > .ruby-version # Use preinstalled version of chruby
      - run: gem install bundler
      - restore_cache:
          key: 2-gems-{{ checksum "Gemfile.lock" }}
      - run: bundle check || bundle install --path vendor/bundle
      - save_cache:
          key: 2-gems-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle
      - restore_cache:
          key: 3-pods-{{ checksum "Example/Podfile.lock" }}
      - run:
          name: Install CocoaPods
          command: |
            ls -la Example/Pods/Manifest.lock && echo Skipping cocoapods repo update || curl https://cocoapods-specs.circleci.com/fetch-cocoapods-repo-from-s3.sh | bash -s cf
            cmp Example/Pods/Manifest.lock Example/Podfile.lock && echo Skipping pod install || bundle exec pod install --project-directory=Example
      - save_cache:
          key: 3-pods-{{ checksum "Example/Podfile.lock" }}
          paths:
            - Example/Pods
jobs:
  check-dependecies-up-to-date:
    description: Check if any dependecies have been updated
    executor: latest-xcode
    shell: /bin/bash --login -o pipefail
    steps:
      - pod-install
      - pod-update
      - run: bundle update
      - run: git diff --exit-code # Outdated Cocoapods or Bundle dependencies found, please run 'bundle update and pod update' and checkin changes
      - run: bundle exec pod lib lint || bundle exec pod lib lint --verbose --no-clean
  macos-tests:
    description: Run tests on Mac OS
    executor: latest-xcode
    shell: /bin/bash --login -o pipefail
    environment:
      HOMEBREW_NO_AUTO_UPDATE: 1
    steps:
      - pod-install
      - restore_cache:
          key: 10-opencv-{{ .Branch }}
      - run: ./Example/OpenCV-OSX-Tests/install-opencv.sh
      - save_cache:
          key: 10-opencv-{{ .Branch }}
          paths:
            - /usr/local/Cellar/opencv
            - /usr/local/Cellar/tbb
            - /usr/local/Cellar/openblas
            - /usr/local/Cellar/gcc
            - /usr/local/Cellar/ffmpeg
            - /usr/local/Cellar/libpng
            - /usr/local/Cellar/libtiff
            - /usr/local/Cellar/ilmbase
            - /usr/local/Cellar/openexr
            - /usr/local/Cellar/jpeg
            - /usr/local/Cellar/opencore-amr
            - /usr/local/Cellar/snappy
            - /usr/local/Cellar/lame
            - /usr/local/Cellar/openjpeg
            - /usr/local/Cellar/opus
            - /usr/local/Cellar/speex
            - /usr/local/Cellar/theora
            - /usr/local/Cellar/libogg
            - /usr/local/Cellar/libvorbis
            - /usr/local/Cellar/x264
            - /usr/local/Cellar/x265
            - /usr/local/Cellar/libsoxr
            - /usr/local/Cellar/libbluray
            - /usr/local/Cellar/gnutls
            - /usr/local/Cellar/rtmpdump
            - /usr/local/Cellar/openssl
            - /usr/local/Cellar/freetype
            - /usr/local/Cellar/fontconfig
            - /usr/local/Cellar/p11-kit
            - /usr/local/Cellar/libunistring
            - /usr/local/Cellar/libtasn1
            - /usr/local/Cellar/nettle
            - /usr/local/Cellar/gmp
            - /usr/local/Cellar/libffi
      - run:
          name: Build and run tests
          command: xcodebuild -workspace Example/PerspectiveTransform.xcworkspace -scheme "OpenCV Tests" test -destination "platform=macos" | xcpretty -r junit
  ios-tests:
    description: Run tests on parameterized xcode destination
    parameters:
      destination:
        description: Xcode destination to run tests
        type: string
    executor: latest-xcode
    shell: /bin/bash --login -o pipefail
    steps:
      - pod-install
      - run:
          name: Build and run tests
          command: xcodebuild -workspace Example/PerspectiveTransform.xcworkspace -scheme Example test -destination "<< parameters.destination >>" | xcpretty -r junit
      - store_test_results:
          path: build/reports
      - store_artifacts:
          path: build/reports
          destination: xcodebuild-test-results
skip-travis-only: &skip-travis-branches
  filters:
    branches:
      ignore: /.*travis.*/
workflows:
  monthly:
    jobs: [check-dependecies-up-to-date]
    triggers:
      - schedule:
          cron: "0 0 1 * *"
          filters:
            branches:
              only: master
  onpush:
    jobs:
      - macos-tests:
          <<: *skip-travis-branches
      - ios-tests:
          destination: platform=iOS Simulator,name=iPhone X
          post-steps:
            - run: bash <(curl -s 'https://codecov.io/bash') -Z -J '^PerspectiveTransform$' -X gcov -X fix
          <<: *skip-travis-branches
      - ios-tests:
          destination: platform=iOS Simulator,name=iPhone XS
          <<: *skip-travis-branches
      - ios-tests:
          destination: platform=iOS Simulator,name=iPhone XR
          <<: *skip-travis-branches
      - ios-tests:
          destination: platform=iOS Simulator,name=iPhone 8
          <<: *skip-travis-branches
      - ios-tests:
          destination: platform=iOS Simulator,name=iPhone 8 Plus
          <<: *skip-travis-branches
      - ios-tests:
          destination: platform=iOS Simulator,name=iPhone SE
          <<: *skip-travis-branches
