-
Notifications
You must be signed in to change notification settings - Fork 71
/
postbuild_copy_apk.cmd
153 lines (145 loc) · 6 KB
/
postbuild_copy_apk.cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
@echo off
REM
REM Purpose:
REM Copy built APK's to match this style:
REM [APPLICATION_ID]_v[VERSION_NAME]_[COMMIT_SHORT_HASH].apk
REM Example:
REM com.github.catfriend1.syncthingandroid_v1.0.0.1_7d59e75.apk
REM
title %~nx0
setlocal enabledelayedexpansion
REM
REM Runtime variables.
SET SCRIPT_PATH=%~dps0
SET PACKAGE_SOURCE_CODE=1
SET TEMP_OUTPUT_FOLDER=X:
REM
REM SET GIT_INSTALL_DIR=%ProgramFiles%\Git
REM SET GIT_BIN="%GIT_INSTALL_DIR%\bin\git.exe"
REM SET PATH=%PATH%;"%GIT_INSTALL_DIR%\bin"
REM
echo [INFO] *** postbuild_copy_apk BEGIN ***
REM
IF NOT DEFINED BUILD_FLAVOUR_GPLAY echo [ERROR] Env var BUILD_FLAVOUR_GPLAY not defined. & SET "BUILD_FLAVOUR_GPLAY=release"
REM
REM Get "applicationId"
SET APPLICATION_ID=
FOR /F "tokens=3 delims= " %%A IN ('type "%SCRIPT_PATH%app\build.gradle.kts" 2^>^&1 ^| findstr /c:"applicationId "') DO SET APPLICATION_ID=%%A
SET APPLICATION_ID=%APPLICATION_ID:"=%
echo [INFO] applicationId="%APPLICATION_ID%"
REM
REM Get "versionMajor"
SET VERSION_MAJOR=
FOR /F "tokens=2 delims==) " %%A IN ('type "%SCRIPT_PATH%build.gradle.kts" 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%build.gradle.kts" 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%build.gradle.kts" 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%build.gradle.kts" 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%
REM
REM Get short hash of last commit.
REM IF NOT EXIST %GIT_BIN% echo [ERROR] git.exe not found. & pause & goto :eof
pushd %SCRIPT_PATH%
SET COMMIT_LONG_HASH=
FOR /F "tokens=1" %%A IN ('git rev-parse --verify HEAD 2^>NUL:') DO SET COMMIT_LONG_HASH=%%A
REM
SET COMMIT_SHORT_HASH=
FOR /F "tokens=1" %%A IN ('git rev-parse --short --verify HEAD 2^>NUL:') DO SET COMMIT_SHORT_HASH=%%A
popd
echo [INFO] VERSION_NAME=[%VERSION_NAME%], commit=[%COMMIT_SHORT_HASH%]=[%COMMIT_LONG_HASH%].
echo [INFO] Copying APK to same directory ...
REM
REM Copy APK to be ready for upload to the GitHub release page.
SET APK_RELEASE_NEW_FILENAME=%APPLICATION_ID%_github_v%VERSION_NAME%_%COMMIT_SHORT_HASH%.apk
call :copyIfExist %SCRIPT_PATH%app\build\outputs\apk\release\app-release.apk %SCRIPT_PATH%app\build\outputs\apk\release\%APK_RELEASE_NEW_FILENAME%
REM
SET APK_GPLAY_NEW_FILENAME=%APPLICATION_ID%_gplay_v%VERSION_NAME%_%COMMIT_SHORT_HASH%.apk
IF NOT "%SKIP_RELEASE_BUILD%" == "1" call :copyIfExist %SCRIPT_PATH%app\build\outputs\apk\%BUILD_FLAVOUR_GPLAY%\app-%BUILD_FLAVOUR_GPLAY%.apk %SCRIPT_PATH%app\build\outputs\apk\%BUILD_FLAVOUR_GPLAY%\%APK_GPLAY_NEW_FILENAME%
REM
REM Copy both APK to temporary storage location if the storage is available.
IF EXIST %TEMP_OUTPUT_FOLDER%\ (
echo [INFO] Copying APK to [%TEMP_OUTPUT_FOLDER%] ...
copy /y %SCRIPT_PATH%app\build\outputs\apk\release\%APK_RELEASE_NEW_FILENAME% %TEMP_OUTPUT_FOLDER%\ 2> NUL:
IF NOT "%SKIP_RELEASE_BUILD%" == "1" copy /y %SCRIPT_PATH%app\build\outputs\apk\%BUILD_FLAVOUR_GPLAY%\%APK_GPLAY_NEW_FILENAME% %TEMP_OUTPUT_FOLDER%\ 2> NUL:
)
REM
IF "%PACKAGE_SOURCE_CODE%" == "1" call :packageSourceCode
REM
echo [INFO] *** postbuild_copy_apk END ***
REM 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
:getFileSize
REM
REM Get file size to variable defined in parameter #2.
SET %~2=%~z1
REM
goto :eof
:packageSourceCode
REM
REM Syntax:
REM call :packageSourceCode
REM
REM Global variables.
REM [IN] COMMIT_LONG_HASH
REM [IN] COMMIT_SHORT_HASH
REM [IN] TEMP_OUTPUT_FOLDER
REM [IN] VERSION_NAME
REM
REM Variables.
SET TMP_DSC_ZIPFILE_FULLFN="%TEMP_OUTPUT_FOLDER%\%DATE:~-4%-%DATE:~-7,-5%-%DATE:~-10,-8%_com.github.catfriend1.syncthingandroid_v%VERSION_NAME%_%COMMIT_SHORT_HASH%.zip"
SET TMP_DSC_SEVENZIP_EXE="%ProgramFiles%\7-Zip\7z.exe"
REM
REM Check prerequisites.
where curl 1> NUL: 2>&1 || (echo [ERROR] curl not found on PATH. & goto :eof)
IF NOT EXIST %TEMP_OUTPUT_FOLDER%\ echo [ERROR] TEMP_OUTPUT_FOLDER=[%TEMP_OUTPUT_FOLDER%] not found. & goto :eof
IF NOT EXIST %TMP_DSC_SEVENZIP_EXE% echo [ERROR] TMP_DSC_SEVENZIP_EXE=[%TMP_DSC_SEVENZIP_EXE%] not found. & goto :eof
REM
REM Download source code for current build commit as ZIP.
:packageSourceCodeRetry
echo [INFO] Downloading source code ZIP from GitHub ...
curl -s -k -L -o %TMP_DSC_ZIPFILE_FULLFN% "https://github.com/Catfriend1/syncthing-android/archive/%COMMIT_LONG_HASH%.zip"
IF NOT EXIST %TMP_DSC_ZIPFILE_FULLFN% echo [ERROR] Download source code FAILED #1. & pause & goto :eof
call :getFileSize %TMP_DSC_ZIPFILE_FULLFN% FILE_SIZE
IF "%FILE_SIZE%" == "" echo [ERROR] Download source code FAILED #2. & pause & goto :eof
IF %FILE_SIZE% LSS 23 echo [ERROR] Download source code FAILED #3. & DEL /F %TMP_DSC_ZIPFILE_FULLFN% & pause & goto :packageSourceCodeRetry
REM
REM Package built APKs into ZIP.
echo [INFO] Adding built APKs to source code ZIP ...
%TMP_DSC_SEVENZIP_EXE% -y -bso0 a %TMP_DSC_ZIPFILE_FULLFN% %TEMP_OUTPUT_FOLDER%\%APK_RELEASE_NEW_FILENAME%
IF NOT "%SKIP_RELEASE_BUILD%" == "1" %TMP_DSC_SEVENZIP_EXE% -y -bso0 a %TMP_DSC_ZIPFILE_FULLFN% %TEMP_OUTPUT_FOLDER%\%APK_GPLAY_NEW_FILENAME%
REM
goto :eof
:renIfExist
REM
REM Syntax:
REM call :renIfExist [FULL_FN_ORIGINAL] [FILENAME_RENAMED]
IF EXIST %1 REN %1 %2 & goto :eof
echo [INFO] File not found: %1
REM
goto :eof