version: 2.1

jobs:
  build:
    description: "Build the application into an Android APK executable"
    docker:
      - image: cirrusci/flutter:stable
    working_directory: ~/project
    steps:
      - checkout
      - run:
          name: Print 
          command: flutter doctor
        
      - run:
          name: Install Dependencies
          command: flutter pub get
        
      - run:
          name: Build
          command: flutter -v build apk
        
      - save_cache:
          paths:
            - .dart_tool
          key: dart-dependencies-{{ checksum "pubspec.lock" }}

  lint:
    description: "Run static analysis for the code"
    docker:
      - image: cirrusci/flutter:stable
    working_directory: ~/project
    steps:
      - checkout

      - restore_cache:
          keys:
            - dart-dependencies-{{ checksum "pubspec.lock" }}
            - dart-dependencies-

      - run:
          name: Make folder for analysis results
          command: mkdir lint_analysis_data

      - run:
          name: Analyze code
          command: flutter analyze --no-fatal-infos > lint_analysis_data/results.txt

      - store_artifacts:
          path: ~/project/lint_analysis_data

  test:
    description: "Run all unittests"
    docker:
      - image: cirrusci/flutter:stable
    working_directory: ~/project
    steps:
      - checkout
      - restore_cache:
          keys:
            - dart-dependencies-{{ checksum "pubspec.lock" }}
            - dart-dependencies-

      - run:
          name: Install test report converter tool
          command: pub global activate junitreport

      - run:
          name: Make folder for test results
          command: mkdir -p test_results/flutter

      - run:
          name: Run tests
          command: flutter test --machine | ~/.pub-cache/bin/tojunit --output test_results/flutter/results.xml || true 

      - store_test_results:
          path: test_results

workflows:
  build_and_test:
    jobs:
      - build
      - lint:
          requires:
            - build
      - test:
          requires:
            - build