# Use the latest 2.1 version of CircleCI pipeline processing engine, see https://circleci.com/docs/2.0/configuration-reference/
version: 2.1
executors:
  php-74:
    docker:
      - image: thecodingmachine/php:7.4-v4-cli
  php-80:
    docker:
      - image: thecodingmachine/php:8.0-v4-cli
  php-81:
    docker:
      - image: thecodingmachine/php:8.1-v4-cli
jobs:
  "Code Style Analysis":
    executor: php-74
    steps:
      - checkout

      - run: date "+%F" > /tmp/cachekey; cat composer.json >> /tmp/cachekey

      - restore_cache:
          keys:
            - composer-v4

      - run: COMPOSER_ROOT_VERSION=dev-master composer update

      - save_cache:
          key: composer-v4
          paths:
            - /home/docker/.composer/cache/files
            - /home/docker/.composer/cache/vcs

      - persist_to_workspace:
          root: /home/docker/project/
          paths:
            - .

      - run:
          name: Code Style Analysis with PHPCS
          command: vendor/bin/phpcs

  phar-build:
    executor: php-74
    steps:
      - attach_workspace:
          at: /home/docker/project/
      - run:
          name: Build Phar file
          command: bin/build-phar.sh
      - run:
          name: Smoke test Phar file
          command: build/psalm.phar --version
      - store_artifacts:
          path: build/psalm.phar
      - run:
          name: Display link to phar file
          command: |
            echo "Phar build available at:"
            echo https://circleci.com/api/v1.1/project/github/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/${CIRCLE_BUILD_NUM}/artifacts/0/home/docker/project/build/psalm.phar

      - persist_to_workspace:
          root: /home/docker/project/
          paths:
            - build/psalm.phar
    # The resource_class feature allows configuring CPU and RAM resources for each job. Different resource classes are available for different executors. https://circleci.com/docs/2.0/configuration-reference/#resourceclass
    resource_class: large
  test-with-real-projects:
    executor: php-81
    steps:
      - checkout # used here just for the side effect of loading the github public ssh key so we can clone other stuff
      - attach_workspace:
          at: /home/docker/project/
      - run:
          name: Analyse PHPUnit
          command: bin/test-with-real-projects.sh phpunit
      - run:
          name: Analyse Psl
          command: bin/test-with-real-projects.sh psl
      - run:
          name: Analyse Collections
          command: bin/test-with-real-projects.sh collections

# Orchestrate or schedule a set of jobs, see https://circleci.com/docs/2.0/workflows/
workflows:
  Welcome:
    jobs:
      - "Code Style Analysis"
      - phar-build:
          requires:
            - "Code Style Analysis"
      - test-with-real-projects:
          requires:
            - phar-build
