From 36fb216dcf3459afd95622383907f121f308bafb Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Mon, 17 Jun 2019 06:18:11 +0200 Subject: [PATCH] Improve build scripts (#427) * Create tx_pull_all_translations.cmd * Add script: build_and_release.cmd * Workaround play-publisher issue 597 ref: https://github.com/Triple-T/gradle-play-publisher/issues/597 * Display generated APK versionCode after build * Create versions.gradle * postbuild_rename_apk: Get versionCode from "versions.gradle", copy instead of rename * Fix Workaround for GPP left-over process kill * Update build.gradle --- app/build.gradle | 8 +++- app/versions.gradle | 6 +++ build_and_release.cmd | 84 ++++++++++++++++++++++++++++++++++++ postbuild_rename_apk.cmd | 51 ++++++++++++++++++---- tx_pull_all_translations.cmd | 15 +++++++ 5 files changed, 154 insertions(+), 10 deletions(-) create mode 100644 app/versions.gradle create mode 100644 build_and_release.cmd create mode 100644 tx_pull_all_translations.cmd diff --git a/app/build.gradle b/app/build.gradle index 800473a1..e75d6ff6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,6 +20,10 @@ dependencies { androidTestImplementation 'com.android.support:support-annotations:28.0.0' } +apply from: 'versions.gradle' +def ourVersionCode = versionMajor * 1000000 + versionMinor * 10000 + versionPatch * 100 + versionWrapper +def ourVersionName = "${versionMajor}.${versionMinor}.${versionPatch}.${versionWrapper}" + android { // Changes to these values need to be reflected in `.travis.yml` compileSdkVersion 28 @@ -37,8 +41,8 @@ android { applicationId "com.github.catfriend1.syncthingandroid" minSdkVersion 16 targetSdkVersion 28 - versionCode 1020001 - versionName "1.2.0.1" + versionCode ourVersionCode + versionName ourVersionName testApplicationId 'com.github.catfriend1.syncthingandroid.test' testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' playAccountConfig = playAccountConfigs.defaultAccountConfig diff --git a/app/versions.gradle b/app/versions.gradle new file mode 100644 index 00000000..628db02b --- /dev/null +++ b/app/versions.gradle @@ -0,0 +1,6 @@ +ext { + versionMajor = 1 + versionMinor = 2 + versionPatch = 0 + versionWrapper = 2 +} diff --git a/build_and_release.cmd b/build_and_release.cmd new file mode 100644 index 00000000..09ba1971 --- /dev/null +++ b/build_and_release.cmd @@ -0,0 +1,84 @@ +@echo off +setlocal enabledelayedexpansion +SET SCRIPT_PATH=%~dps0 +cd /d "%SCRIPT_PATH%" +cls +REM +REM Script Consts. +SET CLEANUP_BEFORE_BUILD=1 +REM +REM Runtime Variables. +REM +REM SET SYNCTHING_RELEASE_PLAY_ACCOUNT_CONFIG_FILE=%userprofile%\.android\play_key.json" +REM SET SYNCTHING_RELEASE_STORE_FILE="%userprofile%\.android\signing_key.jks" +SET SYNCTHING_RELEASE_KEY_ALIAS=Syncthing-Fork +title %SYNCTHING_RELEASE_KEY_ALIAS% - Build and Release +REM +SET PATH=C:\Program Files\Android\Android Studio\jre\bin;%PATH% +REM +echo [INFO] Let's prepare a new "%SYNCTHING_RELEASE_KEY_ALIAS%" GPlay release. +REM +echo [INFO] Checking prerequisites ... +IF NOT EXIST "%SYNCTHING_RELEASE_PLAY_ACCOUNT_CONFIG_FILE%" echo [ERROR] SYNCTHING_RELEASE_PLAY_ACCOUNT_CONFIG_FILE env var not set or file does not exist. & goto :eos +REM +echo [INFO] Checking if SyncthingNative was built before starting this script ... +REM +SET LIBCOUNT= +for /f "tokens=*" %%A IN ('dir /s /a "%SCRIPT_PATH%app\src\main\jniLibs\*" 2^>NUL: ^| find /C "libsyncthing.so"') DO SET LIBCOUNT=%%A +IF NOT "%LIBCOUNT%" == "3" echo [ERROR] SyncthingNative[s] "libsyncthing.so" are missing. Please run "gradlew buildNative" first. & goto :eos +REM +REM User has to enter the signing password if it is not filled in here. +SET SIGNING_PASSWORD= +:enterSigningPassword +IF NOT DEFINED SIGNING_PASSWORD SET /p SIGNING_PASSWORD=Enter signing password: +IF NOT DEFINED SIGNING_PASSWORD echo [ERROR] Signing password is required. Please retry. & goto :enterSigningPassword +REM +echo [INFO] Running lint before building ... +call gradlew --quiet lint +SET RESULT=%ERRORLEVEL% +IF NOT "%RESULT%" == "0" echo [ERROR] "gradlew lint" exited with code #%RESULT%. & goto :eos +REM +echo [INFO] Building Android APK variant "debug" ... +IF "%CLEANUP_BEFORE_BUILD%" == "1" del /f "%SCRIPT_PATH%app\build\outputs\apk\debug\app-debug.apk" 2> NUL: +call gradlew --quiet assembleDebug +SET RESULT=%ERRORLEVEL% +IF NOT "%RESULT%" == "0" echo [ERROR] "gradlew assembleDebug" exited with code #%RESULT%. & goto :eos +type "app\build\intermediates\merged_manifests\debug\AndroidManifest.xml" | findstr /i "android:version" +REM +IF "%CLEANUP_BEFORE_BUILD%" == "1" del /f "%SCRIPT_PATH%app\build\outputs\apk\release\app-release.apk" 2> NUL: +echo [INFO] Building Android APK variant "release" ... +call gradlew --quiet assembleRelease +SET RESULT=%ERRORLEVEL% +IF NOT "%RESULT%" == "0" echo [ERROR] "gradlew assembleRelease" exited with code #%RESULT%. & goto :eos +type "app\build\intermediates\merged_manifests\release\AndroidManifest.xml" | findstr /i "android:version" +REM +echo [INFO] Deleting unsupported play translations ... +call gradlew --quiet deleteUnsupportedPlayTranslations +SET RESULT=%ERRORLEVEL% +IF NOT "%RESULT%" == "0" echo [ERROR] "gradlew deleteUnsupportedPlayTranslations" exited with code #%RESULT%. & goto :eos +REM +:askUserReadyToPublish +SET UI_ANSWER= +SET /p UI_ANSWER=Are you ready to publish this release to GPlay? [y/n] +IF NOT "%UI_ANSWER%" == "y" goto :askUserReadyToPublish +REM +REM Workaround for play-publisher issue, see https://github.com/Triple-T/gradle-play-publisher/issues/597 +:clearPlayPublisherCache +IF EXIST "app\build\generated\gpp" rd /s /q "app\build\generated\gpp" +IF EXIST "app\build\generated\gpp" TASKKILL /F /IM java.exe & sleep 1 & goto :clearPlayPublisherCache +REM +REM Publish APK to GPlay +echo [INFO] Publishing APK to GPlay ... +call gradlew --quiet publishRelease +SET RESULT=%ERRORLEVEL% +IF NOT "%RESULT%" == "0" echo [ERROR] "gradlew publishRelease" exited with code #%RESULT%. & goto :eos +REM +goto :eos +:eos +REM +echo [INFO] End of Script. +REM +pause +goto :eof + + diff --git a/postbuild_rename_apk.cmd b/postbuild_rename_apk.cmd index 49d2633b..2d7f1538 100644 --- a/postbuild_rename_apk.cmd +++ b/postbuild_rename_apk.cmd @@ -17,14 +17,37 @@ REM SET PATH=%PATH%;"%GIT_INSTALL_DIR%\bin" REM REM Get "applicationId" +SET APPLICATION_ID= FOR /F "tokens=2 delims= " %%A IN ('type "%SCRIPT_PATH%app\build.gradle" 2^>^&1 ^| findstr "applicationId"') DO SET APPLICATION_ID=%%A SET APPLICATION_ID=%APPLICATION_ID:"=% echo [INFO] applicationId="%APPLICATION_ID%" REM -REM Get "versionName" -FOR /F "tokens=2 delims= " %%A IN ('type "%SCRIPT_PATH%app\build.gradle" 2^>^&1 ^| findstr "versionName"') DO SET VERSION_NAME=%%A -SET VERSION_NAME=%VERSION_NAME:"=% -echo [INFO] versionName="%VERSION_NAME%" +REM Get "versionMajor" +SET VERSION_MAJOR= +FOR /F "tokens=2 delims== " %%A IN ('type "%SCRIPT_PATH%app\versions.gradle" 2^>^&1 ^| findstr "versionMajor"') DO SET VERSION_MAJOR=%%A +SET VERSION_MAJOR=%VERSION_MAJOR:"=% +REM echo [INFO] versionMajor="%VERSION_MAJOR%" +REM +REM Get "versionMinor" +SET VERSION_MINOR= +FOR /F "tokens=2 delims== " %%A IN ('type "%SCRIPT_PATH%app\versions.gradle" 2^>^&1 ^| findstr "versionMinor"') DO SET VERSION_MINOR=%%A +SET VERSION_MINOR=%VERSION_MINOR:"=% +REM echo [INFO] versionMinor="%VERSION_MINOR%" +REM +REM Get "versionPatch" +SET VERSION_PATCH= +FOR /F "tokens=2 delims== " %%A IN ('type "%SCRIPT_PATH%app\versions.gradle" 2^>^&1 ^| findstr "versionPatch"') DO SET VERSION_PATCH=%%A +SET VERSION_PATCH=%VERSION_PATCH:"=% +REM echo [INFO] versionPatch="%VERSION_PATCH%" +REM +REM Get "versionWrapper" +SET VERSION_WRAPPER= +FOR /F "tokens=2 delims== " %%A IN ('type "%SCRIPT_PATH%app\versions.gradle" 2^>^&1 ^| findstr "versionWrapper"') DO SET VERSION_WRAPPER=%%A +SET VERSION_WRAPPER=%VERSION_WRAPPER:"=% +REM echo [INFO] versionWrapper="%VERSION_WRAPPER%" +REM +SET VERSION_NAME=%VERSION_MAJOR%.%VERSION_MINOR%.%VERSION_PATCH%.%VERSION_WRAPPER% +echo [INFO] VERSION_NAME=[%VERSION_NAME%] REM REM Get short hash of last commit. IF NOT EXIST %GIT_BIN% echo [ERROR] git.exe not found. & pause & goto :eof @@ -33,13 +56,15 @@ FOR /F "tokens=1" %%A IN ('git rev-parse --short --verify HEAD 2^>NUL:') DO SET popd echo [INFO] commit="%COMMIT_SHORT_HASH%" REM -REM Rename APK to be ready for upload to the GitHub release page. +REM Copy APK to be ready for upload to the GitHub release page. SET APK_GITHUB_NEW_FILENAME=%APPLICATION_ID%_v%VERSION_NAME%_%COMMIT_SHORT_HASH%.apk -call :renIfExist %SCRIPT_PATH%app\build\outputs\apk\debug\app-debug.apk %APK_GITHUB_NEW_FILENAME% +REM call :renIfExist %SCRIPT_PATH%app\build\outputs\apk\debug\app-debug.apk %APK_GITHUB_NEW_FILENAME% +call :copyIfExist %SCRIPT_PATH%app\build\outputs\apk\debug\app-debug.apk %SCRIPT_PATH%app\build\outputs\apk\debug\%APK_GITHUB_NEW_FILENAME% REM SET APK_GPLAY_NEW_FILENAME=%APPLICATION_ID%_gplay_v%VERSION_NAME%_%COMMIT_SHORT_HASH%.apk -call :renIfExist %SCRIPT_PATH%app\build\outputs\apk\release\app-release.apk %APK_GPLAY_NEW_FILENAME% -echo [INFO] APK file rename step complete. +REM call :renIfExist %SCRIPT_PATH%app\build\outputs\apk\release\app-release.apk %APK_GPLAY_NEW_FILENAME% +call :copyIfExist %SCRIPT_PATH%app\build\outputs\apk\release\app-release.apk %SCRIPT_PATH%app\build\outputs\apk\release\%APK_GPLAY_NEW_FILENAME% +echo [INFO] APK file copy step complete. REM REM Copy both APK to temporary storage location if the storage is available. IF EXIST %TEMP_OUTPUT_FOLDER% copy /y %SCRIPT_PATH%app\build\outputs\apk\debug\%APK_GITHUB_NEW_FILENAME% %TEMP_OUTPUT_FOLDER% 2> NUL: @@ -49,6 +74,16 @@ echo [INFO] End of Script. timeout 3 goto :eof +:copyIfExist +REM +REM Syntax: +REM call :copyIfExist [FULL_FN_ORIGINAL] [FILENAME_COPY_TARGET] +REM IF EXIST %1 REN %1 %2 & goto :eof +IF EXIST %1 copy /y %1 %2 & goto :eof +echo [INFO] File not found: %1 +REM +goto :eof + :renIfExist REM REM Syntax: diff --git a/tx_pull_all_translations.cmd b/tx_pull_all_translations.cmd new file mode 100644 index 00000000..ba13a824 --- /dev/null +++ b/tx_pull_all_translations.cmd @@ -0,0 +1,15 @@ +@echo off +title %~nx0 +cls +cd /d "%~dps0" +REM +REM SET FORCE_FLAG=-f +SET FORCE_FLAG= +REM +echo Pulling all reviewed translations ... +tx pull -a --mode reviewed %FORCE_FLAG% -r "syncthing-android-1.stringsxml" +tx pull -a --mode reviewed %FORCE_FLAG% -r "syncthing-android-1.description_fulltxt" +tx pull -a --mode reviewed %FORCE_FLAG% -r "syncthing-android-1.description_shorttxt" +tx pull -a --mode reviewed %FORCE_FLAG% -r "syncthing-android-1.titletxt" +REM +pause