version: 2
build:
  machine:
    java: oraclejdk8
jobs:
  build-kotlin-sdk:
    docker:
      - image: circleci/android:api-28-alpha
    environment:
      JAVA_TOOL_OPTIONS: -Xmx5024m
    steps:
      - checkout
      - run:
          name: checkout Rocket.Chat.Kotlin.SDK
          command: git clone https://github.com/RocketChat/Rocket.Chat.Kotlin.SDK.git ../Rocket.Chat.Kotlin.SDK
      - run:
          name: ANDROID_HOME
          command: echo "sdk.dir="$ANDROID_HOME > local.properties 
      - run:
          name: Build Kotlin.SDK
          command: pushd app/ ; ./build-sdk.sh ; popd
      - save_cache:
          paths:
            - ~/.gradle/caches
            - ~/.gradle/wrapper
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}-{{ checksum "player/build.gradle" }}-{{ checksum "core/build.gradle" }}-{{ checksum "util/build.gradle" }}-{{ checksum "draw/build.gradle" }}-{{ checksum "emoji/build.gradle" }}-{{ checksum "suggestions/build.gradle" }}
      - save_cache:
          paths:
            - app/libs/
            - ../Rocket.Chat.Kotlin.SDK/.last_commit_hash
          key: kotlin-sdk-{{ .Revision }}
      - store_artifacts:
          path: app/libs/
          destination: libs
  code-analysis:
    docker:
      - image: circleci/android:api-28-alpha
    environment:
      JAVA_TOOL_OPTIONS: -Xmx5024m
    steps:
      - checkout
      - run:
          name: ANDROID_HOME
          command: echo "sdk.dir="$ANDROID_HOME > local.properties
      - run:
          name: checkout Rocket.Chat.Kotlin.SDK
          command: git clone https://github.com/RocketChat/Rocket.Chat.Kotlin.SDK.git ../Rocket.Chat.Kotlin.SDK
      - restore_cache:
          key: kotlin-sdk-{{ .Revision }}
      - restore_cache:
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}-{{ checksum "player/build.gradle" }}-{{ checksum "core/build.gradle" }}-{{ checksum "util/build.gradle" }}-{{ checksum "draw/build.gradle" }}-{{ checksum "emoji/build.gradle" }}-{{ checksum "suggestions/build.gradle" }}
      - run:
          name: Download Dependencies
          command: ./gradlew --no-daemon androidDependencies --quiet --console=plain
      - save_cache:
          paths:
            - ~/.gradle/caches
            - ~/.gradle/wrapper
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}-{{ checksum "player/build.gradle" }}-{{ checksum "core/build.gradle" }}-{{ checksum "util/build.gradle" }}-{{ checksum "draw/build.gradle" }}-{{ checksum "emoji/build.gradle" }}-{{ checksum "suggestions/build.gradle" }}
      - run:
          name: Run Lint #, Checkstyles, PMD, Findbugs...
          command: ./gradlew --no-daemon lint
      - run:
          name: Run Unit test
          command: ./gradlew --no-daemon test
      - run:
          name: Compile Instrumentation test
          command: ./gradlew --no-daemon assembleAndroidTest
      - store_artifacts:
          path: app/build/reports/
          destination: reports
  build-play-apk:
    docker:
      - image: circleci/android:api-28-alpha
    environment:
      JAVA_TOOL_OPTIONS: -Xmx5024m
    steps:
      - checkout
      - run:
          name: ANDROID_HOME
          command: echo "sdk.dir="$ANDROID_HOME > local.properties
      - run:
          name: restore files from ENV
          command: |
            echo $ROCKET_JKS_BASE64 | base64 --decode > Rocket.jks
            echo $ROCKET_PLAY_JSON | base64 --decode > app/rocket-chat.json
      - run:
          name: checkout Rocket.Chat.Kotlin.SDK
          command: git clone https://github.com/RocketChat/Rocket.Chat.Kotlin.SDK.git ../Rocket.Chat.Kotlin.SDK
      - restore_cache:
          key: kotlin-sdk-{{ .Revision }}
      - restore_cache:
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}-{{ checksum "player/build.gradle" }}-{{ checksum "core/build.gradle" }}-{{ checksum "util/build.gradle" }}-{{ checksum "draw/build.gradle" }}-{{ checksum "emoji/build.gradle" }}-{{ checksum "suggestions/build.gradle" }}
      - run:
          name: Download Dependencies
          command: ./gradlew --no-daemon androidDependencies --quiet --console=plain
      - save_cache:
          paths:
            - ~/.gradle/caches
            - ~/.gradle/wrapper
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}-{{ checksum "player/build.gradle" }}-{{ checksum "core/build.gradle" }}-{{ checksum "util/build.gradle" }}-{{ checksum "draw/build.gradle" }}-{{ checksum "emoji/build.gradle" }}-{{ checksum "suggestions/build.gradle" }}
      - run:
          name: Build APK
          command: |
            ./gradlew --no-daemon assemblePlayRelease --info --console=plain --stacktrace
      - store_artifacts:
          path: app/build/outputs/apk
          destination: apks
  build-foss-apk:
    docker:
      - image: circleci/android:api-28-alpha
    environment:
      JAVA_TOOL_OPTIONS: -Xmx5024m
    steps:
      - checkout
      - run:
          name: ANDROID_HOME
          command: echo "sdk.dir="$ANDROID_HOME > local.properties
      - run:
          name: restore files from ENV
          command: |
            echo $ROCKET_JKS_BASE64 | base64 --decode > Rocket.jks
            echo $ROCKET_PLAY_JSON | base64 --decode > app/rocket-chat.json
      - run:
          name: checkout Rocket.Chat.Kotlin.SDK
          command: git clone https://github.com/RocketChat/Rocket.Chat.Kotlin.SDK.git ../Rocket.Chat.Kotlin.SDK
      - restore_cache:
          key: kotlin-sdk-{{ .Revision }}
      - restore_cache:
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}-{{ checksum "player/build.gradle" }}-{{ checksum "core/build.gradle" }}-{{ checksum "util/build.gradle" }}-{{ checksum "draw/build.gradle" }}-{{ checksum "emoji/build.gradle" }}-{{ checksum "suggestions/build.gradle" }}
      - run:
          name: Download Dependencies
          command: ./gradlew --no-daemon androidDependencies --quiet --console=plain
      - save_cache:
          paths:
            - ~/.gradle/caches
            - ~/.gradle/wrapper
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}-{{ checksum "player/build.gradle" }}-{{ checksum "core/build.gradle" }}-{{ checksum "util/build.gradle" }}-{{ checksum "draw/build.gradle" }}-{{ checksum "emoji/build.gradle" }}-{{ checksum "suggestions/build.gradle" }}
      - run:
          name: Build APK
          command: |
            ./gradlew --no-daemon assembleFossRelease --info --console=plain --stacktrace
      - store_artifacts:
          path: app/build/outputs/apk
          destination: apks

workflows:
  version: 2
  build-deploy:
    jobs:
      - build-kotlin-sdk
      - code-analysis:
          requires:
            - build-kotlin-sdk
          filters:
            branches:
              ignore: # skip on merge commits.
                - develop
                - develop-2.x
                - master
      - build-play-apk:
          requires:
            - build-kotlin-sdk
      - build-foss-apk:
          requires:
            - build-kotlin-sdk
