name: Publish @notesnook/android-internal

on: workflow_dispatch

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 40
    env:
      CMAKE_C_COMPILER_LAUNCHER: ccache
      CMAKE_CXX_COMPILER_LAUNCHER: ccache
      NDK_CCACHE: ccache
      USE_CCACHE: 1

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup Node
        uses: ./.github/actions/setup-node-with-cache

      - name: Use specific Java version for the builds
        uses: joschi/setup-jdk@v2
        with:
          java-version: "11"
          architecture: "x64"

      - name: Install node modules
        run: |
          npm ci --ignore-scripts --prefer-offline --no-audit
          npm run bootstrap -- --scope=mobile

      - name: Make Gradlew Executable
        run: cd apps/mobile/native/android && chmod +x ./gradlew

      - name: Install CCache
        uses: hendrikmuhs/ccache-action@v1.2.11
        with:
          key: ${{ runner.os }}-ccache-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
          max-size: 1500M
          restore-keys: |
            ${{ runner.os }}-ccache-

      - name: Setup CCache
        run: |
          ln -s $(which ccache) /usr/local/bin/gcc
          ln -s $(which ccache) /usr/local/bin/g++
          ln -s $(which ccache) /usr/local/bin/cc
          ln -s $(which ccache) /usr/local/bin/c++
          ln -s $(which ccache) /usr/local/bin/clang
          ln -s $(which ccache) /usr/local/bin/clang++
          ccache --set-config=base_dir=$PWD
          ccache --set-config=inode_cache=true
          ccache --set-config=compiler_check=content
          ccache --set-config=depend_mode=true
          ccache --set-config=file_clone=true
          ccache --set-config=sloppiness=clang_index_store,file_stat_matches,include_file_ctime,include_file_mtime,ivfsoverlay,pch_defines,modules,system_headers,time_macros
          ccache -p

      - name: Cache gradle
        uses: actions/cache@v2
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}

      - name: Patch CPP Projects to use CCache
        run: |
          sed -i -e 's/arguments/arguments "-DCMAKE_CXX_COMPILER_LAUNCHER=ccache",/g' apps/mobile/node_modules/react-native-mmkv-storage/android/build.gradle
          sed -i -e 's/arguments/arguments "-DCMAKE_CXX_COMPILER_LAUNCHER=ccache",/g' apps/mobile/node_modules/react-native-reanimated/android/build.gradle
          sed -i -e 's/defaultConfig {/ndkVersion safeExtGet('ndkVersion', "25.2.9519653")\n  defaultConfig {/g' apps/mobile/node_modules/react-native-reanimated//android/build.gradle
          sed -i -e 's/defaultConfig {/ndkVersion safeExtGet('ndkVersion', "25.2.9519653")\n  defaultConfig {/g' apps/mobile/node_modules/react-native-mmkv-storage/android/build.gradle

      - name: CCache Stats Before Build
        run: ccache -sv

      - name: Build unsigned app bundle
        run: yarn release:android:bundle

      - name: CCache Stats after build
        run: ccache -sv

      - name: Sign app bundle for Playstore release
        id: sign_app
        uses: r0adkll/sign-android-release@master
        with:
          releaseDirectory: apps/mobile/native/android/app/build/outputs/bundle/release
          signingKeyBase64: ${{ secrets.SIGNING_KEY }}
          alias: ${{ secrets.ALIAS }}
          keyStorePassword: ${{ secrets.KEY_PASSWORD }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}
        env:
          BUILD_TOOLS_VERSION: "33.0.0"

      - name: Build apks for Github release
        run: yarn release:android

      - name: Sign apk files
        id: sign_apk
        uses: r0adkll/sign-android-release@master
        with:
          releaseDirectory: apps/mobile/native/android/app/build/outputs/apk/release
          signingKeyBase64: ${{ secrets.PUBLIC_SIGNING_KEY }}
          alias: ${{ secrets.PUBLIC_ALIAS }}
          keyStorePassword: ${{ secrets.PUBLIC_KEY_PASSWORD }}
          keyPassword: ${{ secrets.PUBLIC_KEY_PASSWORD }}
        env:
          BUILD_TOOLS_VERSION: "33.0.0"

      - name: Rename apk files
        run: |
          cd apps/mobile/native/android/app/build/outputs/apk/release/
          mv app-arm64-v8a-release-unsigned-signed.apk notesnook-arm64-v8a.apk
          mv app-armeabi-v7a-release-unsigned-signed.apk notesnook-armeabi-v7a.apk
          mv app-x86-release-unsigned-signed.apk notesnook-x86.apk
          mv app-x86_64-release-unsigned-signed.apk notesnook-x86_64.apk

      - name: Get app version
        id: package-version
        uses: martinbeentjes/npm-get-version-action@master
        with:
          path: apps/mobile

      - name: Publish to Playstore
        id: deploy
        uses: r0adkll/upload-google-play@v1.1.1
        with:
          serviceAccountJsonPlainText: ${{ secrets.SERVICE_ACCOUNT_JSON }}
          packageName: com.streetwriters.notesnook
          releaseFiles: ${{steps.sign_app.outputs.signedReleaseFile}}
          track: alpha
          status: completed
          whatsNewDirectory: apps/mobile/native/android/releasenotes/

      - name: Create release draft on Github
        uses: softprops/action-gh-release@v1
        with:
          draft: true
          tag_name: ${{ steps.package-version.outputs.current-version}}-beta-android
          name: Notesnook Android v${{ steps.package-version.outputs.current-version}} Beta
          repository: streetwriters/notesnook
          token: ${{ secrets.GITHUB_TOKEN }}
          files: |
            apps/mobile/native/android/app/build/outputs/apk/release/notesnook-arm64-v8a.apk
            apps/mobile/native/android/app/build/outputs/apk/release/notesnook-armeabi-v7a.apk
            apps/mobile/native/android/app/build/outputs/apk/release/notesnook-x86.apk
            apps/mobile/native/android/app/build/outputs/apk/release/notesnook-x86_64.apk

      - name: Upload sourcemaps
        uses: actions/upload-artifact@v4
        with:
          name: sourcemaps
          path: |
            apps/mobile/native/android/app/build/**/*.map
            packages/editor-mobile/sourcemaps/*.map
