version: 2.1

defaults: &defaults
  working_directory: ~/truemail
  docker:
    - image: cimg/ruby:<< parameters.ruby-version >>

orbs:
  ruby: circleci/ruby@1.4.0

references:
  restore_bundle_cache: &restore_bundle_cache
    restore_cache:
      keys:
        - truemail-{{ checksum "truemail.gemspec" }}

  bundle_install: &bundle_install
    run:
      name: Installing gems
      command: |
            bundle config set --local path 'vendor/bundle'
            bundle install

  save_bundle_cache: &save_bundle_cache
    save_cache:
      key: truemail-{{ checksum "truemail.gemspec" }}
      paths:
        - vendor/bundle

  system_dependencies: &system_dependencies
    run:
      name: Installing system requirements
      command: |
            bundle exec smtp_mock -s -i ~

  install_codeclimate_reporter: &install_codeclimate_reporter
    run:
      name: Installing CodeClimate test reporter
      command: |
            curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
            chmod +x ./cc-test-reporter

  use_compatible_gemspec: &use_compatible_gemspec
    run:
      name: Use compatible gemspec
      command: |
            cp .circleci/gemspec_compatible truemail.gemspec

jobs:
  linters-ruby:
    parameters:
      ruby-version:
        type: string

    <<: *defaults

    steps:
      - checkout

      - <<: *restore_bundle_cache
      - <<: *bundle_install
      - <<: *save_bundle_cache

      - run:
          name: Running Overcommit
          command: |
            bundle exec overcommit -s
            SKIP=AuthorEmail,AuthorName bundle exec overcommit -r

  tests-ruby:
    parameters:
      ruby-version:
        type: string

    <<: *defaults

    steps:
      - checkout

      - <<: *restore_bundle_cache
      - <<: *bundle_install
      - <<: *save_bundle_cache
      - <<: *system_dependencies
      - <<: *install_codeclimate_reporter

      - run:
          name: Running RSpec
          command: |
            ./cc-test-reporter before-build
            bundle exec rspec

      - run:
          name: Creating CodeClimate test coverage report
          command: |
            ./cc-test-reporter format-coverage -t simplecov -o "coverage/codeclimate.$CIRCLE_NODE_INDEX.json"

      - store_artifacts:
          name: Saving Simplecov coverage artifacts
          path: ~/truemail/coverage
          destination: coverage

      - deploy:
          name: Uploading CodeClimate test coverage report
          command: |
            ./cc-test-reporter sum-coverage --output - --parts $CIRCLE_NODE_TOTAL coverage/codeclimate.*.json | ./cc-test-reporter upload-coverage --debug --input -

  compatibility-ruby:
    parameters:
      ruby-version:
        type: string
    docker:
      - image: cimg/ruby:<< parameters.ruby-version >>
    steps:
      - checkout
      - <<: *use_compatible_gemspec
      - ruby/install-deps:
          bundler-version: "2.3.5"
          with-cache: false
          path: './vendor/custom_bundle'
      - <<: *system_dependencies
      - run:
          name: Running compatibility tests
          command: bundle exec rspec

workflows:
  build_and_test:
    jobs:
      - linters-ruby:
          matrix:
            parameters:
              ruby-version: ["3.1"]
      - tests-ruby:
          matrix:
            parameters:
              ruby-version: ["3.1"]
      - compatibility-ruby:
          matrix:
            parameters:
              ruby-version: ["2.5", "2.6", "2.7", "3.0"]