# Ruby CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
#
version: 2

spec_shared: &spec_shared
  working_directory: ~/sidekiq_simple_delay
  steps:
    - checkout

    - run:
        name: install dependencies
        command: |
          bundle check --path vendor/bundle || bundle install --jobs=4 --retry=3 --path vendor/bundle

    - run:
        name: Make test-results
        command: |
          mkdir /tmp/test-results

    - run:
        name: Run basic specs
        command: |
          bundle exec rspec --format progress \
                          --format RspecJunitFormatter \
                          --out /tmp/test-results/rspec-basic.xml \
                          --format progress \
                          spec --tag ~run_tag

    - run:
        name: Run enable_delay specs
        command: |
          bundle exec rspec --format progress \
                          --format RspecJunitFormatter \
                          --out /tmp/test-results/rspec-basic.xml \
                          --format progress \
                          spec --tag run_tag:enable_delay

    - run:
        name: Run active_record_base specs
        command: |
          bundle exec rspec --format progress \
                          --format RspecJunitFormatter \
                          --out /tmp/test-results/rspec-basic.xml \
                          --format progress \
                          spec --tag run_tag:active_record_base

    - run:
        name: Run application_record specs
        command: |
          bundle exec rspec --format progress \
                          --format RspecJunitFormatter \
                          --out /tmp/test-results/rspec-basic.xml \
                          --format progress \
                          spec --tag run_tag:application_record

    - run:
        name: Run active_record_single specs
        command: |
          bundle exec rspec --format progress \
                          --format RspecJunitFormatter \
                          --out /tmp/test-results/rspec-basic.xml \
                          --format progress \
                          spec --tag run_tag:active_record_single

    - run:
        name: Run action_mailer specs
        command: |
          bundle exec rspec --format progress \
                          --format RspecJunitFormatter \
                          --out /tmp/test-results/rspec-basic.xml \
                          --format progress \
                          spec --tag run_tag:action_mailer_base

    - run:
        name: Run application_mailer specs
        command: |
          bundle exec rspec --format progress \
                          --format RspecJunitFormatter \
                          --out /tmp/test-results/rspec-basic.xml \
                          --format progress \
                          spec --tag run_tag:application_mailer
    - run:
        name: Run action_mailer_single specs
        command: |
          bundle exec rspec --format progress \
                          --format RspecJunitFormatter \
                          --out /tmp/test-results/rspec-basic.xml \
                          --format progress \
                          spec --tag run_tag:action_mailer_single

    # collect reports
    - store_test_results:
        path: /tmp/test-results
    - store_artifacts:
        path: /tmp/test-results
        destination: test-results

jobs:
  deps:
    docker:
      - image: circleci/ruby:2.5
    working_directory: ~/sidekiq_simple_delay

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "sidekiq_simple_delay.gemspec" }}

      - run:
          name: install dependencies
          command: |
            bundle check --path vendor/bundle || bundle install --jobs=4 --retry=3 --path vendor/bundle

      - save_cache:
          paths:
            - vendor/bundle
          key: v1-dependencies-{{ checksum "sidekiq_simple_delay.gemspec" }}

      - run:
          name: dependencies security audit
          command: |
            bundle exec bundle-audit check --update

      - run:
          name: Rubocop
          command: bundle exec rubocop

  ruby-2.5:
    <<: *spec_shared
    docker:
      - image: circleci/ruby:2.5

  ruby-2.4:
    <<: *spec_shared
    docker:
      - image: circleci/ruby:2.4

  ruby-2.3:
    <<: *spec_shared
    docker:
      - image: circleci/ruby:2.3

  ruby-2.2:
    <<: *spec_shared
    docker:
      - image: circleci/ruby:2.2

  generate_docs:
    docker:
      - image: circleci/ruby:2.5
    working_directory: ~/sidekiq_simple_delay

    steps:
      - checkout

      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "sidekiq_simple_delay.gemspec" }}

      - run:
          name: Setup bundler path
          command: |
            bundle check --path vendor/bundle

      - run:
          name: Generate Yard docs
          command: |
            bundle exec yard --output-dir /tmp/workspace/docs/yard

      - run:
          name: Num Docs
          command: |
            ls -al /tmp/workspace/docs/yard | wc -l

      - persist_to_workspace:
          root: /tmp/workspace/docs
          paths:
            - yard

workflows:
  version: 2
  build:
    jobs:
      - deps
      - ruby-2.5:
          requires:
            - deps
      - ruby-2.4:
          requires:
            - deps
      - ruby-2.3:
          requires:
            - deps
      - ruby-2.2:
          requires:
            - deps
      - generate_docs:
          requires:
            - ruby-2.5
            - ruby-2.4
            - ruby-2.3
            - ruby-2.2