version: 2
jobs:
  build-kotlin-sdk:
    docker:
      - image: circleci/android:api-27-alpha
    environment:
      JVM_OPTS: -Xmx3200m
    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
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}-{{ checksum "player/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-27-alpha
    environment:
      JVM_OPTS: -Xmx3200m
    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" }}
      - run:
          name: Download Dependencies
          command: ./gradlew androidDependencies --quiet --console=plain
      - save_cache:
          paths:
            - ~/.gradle
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}-{{ checksum "player/build.gradle" }}
      - run:
          name: Run Lint #, Checkstyles, PMD, Findbugs...
          command: ./gradlew lint
      - run:
          name: Run Unit test
          command: ./gradlew test
      - run:
          name: Compile Instrumentation test
          command: ./gradlew assembleAndroidTest
      - store_artifacts:
          path: app/build/reports/
          destination: reports
  build-play-apk:
    docker:
      - image: circleci/android:api-27-alpha
    environment:
      JVM_OPTS: -Xmx3200m
    steps:
      - checkout
      - 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" }}
      - run:
          name: Download Dependencies
          command: ./gradlew androidDependencies --quiet --console=plain
      - save_cache:
          paths:
            - ~/.gradle
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}-{{ checksum "player/build.gradle" }}
      - run:
          name: Build APK
          command: |
            ./gradlew assemblePlayRelease --info --console=plain --stacktrace
      - store_artifacts:
          path: app/build/outputs/apk
          destination: apks
  build-foss-apk:
    docker:
      - image: circleci/android:api-27-alpha
    environment:
      JVM_OPTS: -Xmx3200m
    steps:
      - checkout
      - 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" }}
      - run:
          name: Download Dependencies
          command: ./gradlew androidDependencies --quiet --console=plain
      - save_cache:
          paths:
            - ~/.gradle
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}-{{ checksum "player/build.gradle" }}
      - run:
          name: Build APK
          command: |
            ./gradlew 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
