version: 3
jobs:
  build:
    docker:
      - image: circleci/android:api-29
    working_directory: ~/ultrasonic
    environment:
      JVM_OPTS: -Xmx3200m
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-ultrasonic-{{ .Branch }}-{{ checksum "dependencies.gradle" }}
            - v1-ultrasonic-{{ .Branch }}
            - v1-ultrasonic
      - run:
          name: configure gradle.properties for CI building
          command: |
            sed -i '/^org.gradle.jvmargs/d' gradle.properties
            sed -i 's/^org.gradle.daemon=true/org.gradle.daemon=false/g' gradle.properties
      - run:
          name: checkstyle
          command: ./gradlew -Pqc ktlintCheck
      - run:
          name: static analysis
          command: ./gradlew -Pqc detekt
      - run:
          name: build debug
          command: ./gradlew assembleDebug
      - run:
          name: unit-tests
          command: |
            ./gradlew ciTest testDebugUnitTest
            ./gradlew jacocoFullReport
      - run:
          name: lint
          command: ./gradlew :ultrasonic:lintRelease
      - run:
          name: build
          command: ./gradlew buildRelease
      - run:
          name: assemble release
          command: ./gradlew assembleRelease
      - save_cache:
          paths:
            - ~/.gradle
          key:  v1-ultrasonic-{{ .Branch }}-{{ checksum "dependencies.gradle" }}
      - store_artifacts:
          path: ultrasonic/build/reports
          destination: reports
      - store_artifacts:
          path: subsonic-api/build/reports
          destination: reports
      - store_artifacts:
          path: build/reports/jacoco/jacocoFullReport/
  push_translations:
    docker:
      - image: circleci/python:3.6
    working_directory: ~/ultrasonic
    steps:
      - checkout
      - run:
          name: install transifex client
          command: |
            python -m venv ~/venv
            . ~/venv/bin/activate
            pip install transifex-client
      - run:
          name: configure transifex client
          command: echo $'[https://www.transifex.com]\nhostname = https://www.transifex.com\nusername = api\npassword = '"${TRANSIFEX_PASSWORD}"$'\n' > ~/.transifexrc
      - run:
          name: push changes in translation files
          command: |
            . ~/venv/bin/activate
            tx push -s
  generate_signed_apk:
    docker:
      - image: circleci/android:api-28
    working_directory: ~/ultrasonic
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-ultrasonic-{{ .Branch }}-{{ checksum "dependencies.gradle" }}
            - v1-ultrasonic-{{ .Branch }}
            - v1-ultrasonic
      - run:
          name: decrypt ultrasonic-keystore
          command: openssl aes-256-cbc -K ${ULTRASONIC_KEYSTORE_KEY} -iv ${ULTRASONIC_KEYSTORE_IV} -in ultrasonic-keystore.enc -out ultrasonic-keystore -d
      - run:
          name: build release apk
          command: ./gradlew build assembleRelease
      - run:
          name: sign release apk
          command: |
            mkdir -p /tmp/ultrasonic-release
            jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore ~/ultrasonic/ultrasonic-keystore -storepass ${ULTRASONIC_KEYSTORE_STOREPASS} -keypass ${ULTRASONIC_KEYSTORE_KEYPASS} ultrasonic/build/outputs/apk/release/ultrasonic-release-unsigned.apk ultrasonic
            jarsigner -verify ultrasonic/build/outputs/apk/release/ultrasonic-release-unsigned.apk
            ${ANDROID_HOME}/build-tools/27.0.0/zipalign -v 4 ultrasonic/build/outputs/apk/release/ultrasonic-release-unsigned.apk /tmp/ultrasonic-release/ultrasonic-${CIRCLE_TAG}.apk
      - persist_to_workspace:
          root: /tmp/ultrasonic-release
          paths:
            - ultrasonic-*.apk
  publish_github_signed_apk:
    docker:
      - image: circleci/golang
    steps:
      - attach_workspace:
          at: /tmp/ultrasonic-release
      - run:
          name: install ghr
          command: go get -v github.com/tcnksm/ghr
      - run:
          name: publish release on github tag
          command: ghr -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} ${CIRCLE_TAG} /tmp/ultrasonic-release
workflows:
  version: 2
  build_and_push_translations:
    jobs:
      - build
      - push_translations:
          requires:
            - build
          filters:
            branches:
              only:
                - develop
                - master
      - generate_signed_apk:
          filters:
            tags:
              only: /^[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
      - publish_github_signed_apk:
          requires:
            - generate_signed_apk
          filters:
            tags:
              only: /^[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/

