forked from tutao/tutanota
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandroid.js
More file actions
203 lines (173 loc) · 6.4 KB
/
Copy pathandroid.js
File metadata and controls
203 lines (173 loc) · 6.4 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Build script for android app.
*
* Besides options below this script may require signing parameters passed as environment variables:
* 'APK_SIGN_ALIAS'
* 'APK_SIGN_STORE_PASS'
* 'APK_SIGN_KEY_PASS'
* 'APK_SIGN_STORE'
* 'ANDROID_HOME'
*/
import { Argument, Option, program } from "commander"
import { buildDirForApp, runDevBuild } from "./buildSrc/DevBuild.js"
import { prepareMobileBuild } from "./buildSrc/prepareMobileBuild.js"
import { getTutanotaAppVersion, measure } from "./buildSrc/buildUtils.js"
import path from "node:path"
import { $ } from "zx"
import fs from "node:fs/promises"
import { runPreflightCheck } from "./buildSrc/runPreflightCheck.js"
// chalk is in scope because of zx
const log = (...messages) => console.log(chalk.green("\nBUILD:"), ...messages, "\n")
$.verbose = true
await program
.usage("[options] [test|prod|local|host <url>] ")
.addArgument(
new Argument("stage", "the server to connect to. test/local/prod are shorthands for using host <url> of the corresponding staging level server")
.choices(["test", "prod", "local", "host"])
.default("prod")
.argOptional(),
)
.addArgument(new Argument("host").argOptional())
.addOption(new Option("-a, --app <type>", "app to build").choices(["mail", "calendar", "drive"]).default("mail"))
.addOption(
new Option(
"-b, --buildtype <type>",
"gradle build type. use debug if you need to debug the app with android studio. release and releaseTest build the same app with different appIds for side-by-side installation",
)
.choices(["debug", "release", "releaseTest"])
.default("release"),
)
.addOption(new Option("-i, --install", "call adb install after build to deploy the app to a device/emulator"))
.addOption(
new Option(
"-w --webclient <client>",
"choose web client build. make is faster and easier to debug, but dist is what would be running in production. There's usually no reason to use dist during development.",
)
.choices(["make", "dist"])
.default("dist"),
)
.option("-e, --existing", "Use existing prebuilt web client files to skip the lengthy web client build. Use if you're developing the Kotlin code.")
.action(async (stage, host, { webclient, buildtype, install, existing, app }) => {
if ((stage === "host" && host == null) || (stage !== "host" && host != null)) {
program.outputHelp()
process.exit(1)
}
runPreflightCheck("android")
const apk = await buildAndroid({
stage: stage ?? "prod",
host: host,
webClient: webclient,
existing,
buildType: buildtype,
app,
})
if (install) {
await $`adb install ${apk}`
// would be cool to auto-start the app, but needs to figure out the correct app to start:
// await $`adb shell am start -n de.tutao.tutanota/de.tutao.tutanota.MainActivity`
}
})
.parseAsync(process.argv)
async function buildAndroid({ stage, host, buildType, existing, webClient, app }) {
log(`Starting ${stage} build with build type: ${buildType}, webclient: ${webClient}, host: ${host}`)
if (!existing) {
if (webClient === "make") {
const { runDevBuild } = await import("./buildSrc/DevBuild.js")
await runDevBuild({
stage,
host,
desktop: false,
clean: false,
watch: false,
serve: false,
networkDebugging: false,
app,
})
} else {
const version = await getTutanotaAppVersion()
const { buildWebapp } = await import("./buildSrc/buildWebapp.js")
await buildWebapp({
version,
stage,
host,
minify: true,
projectDir: path.resolve("."),
measure,
app,
mobileBuild: true,
})
}
} else {
console.log("skipped webapp build")
}
await prepareMobileBuild({ app })
const buildDir = buildDirForApp(app)
try {
await $`rm -r ${buildDir}/app-android`
} catch (e) {
// Ignoring the error if the folder is not there
}
switch (app) {
case "mail":
return await buildMailApk({ buildType })
case "calendar": {
await buildCalendarBundle({ buildType })
return await buildCalendarApk({ buildType })
}
case "drive": {
await buildDriveBundle({ buildType })
return await buildDriveApk({ buildType })
}
}
}
async function buildCalendarBundle({ buildType }) {
return await buildBundle({ baseBundleName: "calendar", gradleModule: "calendar", buildType })
}
async function buildDriveBundle({ buildType }) {
return await buildBundle({ baseBundleName: "drive", gradleModule: "drive", buildType })
}
async function buildBundle({ baseBundleName, gradleModule, buildType }) {
const version = await getTutanotaAppVersion()
const aabFileName = `${baseBundleName}-tutao-${buildType}-${version}.aab`
const aabPath = `app-android/${gradleModule}/build/outputs/bundle/tutao${capitalize(buildType)}/${aabFileName}`
const outDirPath = `./artifacts/app-android`
const outAabPath = `${outDirPath}/${aabFileName}`
await fs.rm(outAabPath, { force: true })
const $inAppDir = $({ cwd: "app-android" })
await $inAppDir`./gradlew :${gradleModule}:bundleTutao${buildType}`
await fs.mkdir(outDirPath, { recursive: true })
await fs.rename(aabPath, outAabPath)
log(`Build complete. The AAB is located at ${outAabPath}`)
return outAabPath
}
async function buildCalendarApk({ buildType }) {
return await buildApk({ buildType, gradleModule: "calendar", baseBundleName: "calendar" })
}
async function buildMailApk({ buildType }) {
return await buildApk({ app: "mail", buildType, gradleModule: "app", baseBundleName: "tutanota-app" })
}
async function buildDriveApk({ buildType }) {
return await buildApk({ app: "drive", buildType, gradleModule: "drive", baseBundleName: "drive" })
}
/**
* @param baseBundleName {string}
* @param gradleModule {string}
* @param buildType {"debug"|"release"|"releaseTest"}
*/
async function buildApk({ baseBundleName, gradleModule, buildType }) {
const version = await getTutanotaAppVersion()
const apkFileName = `${baseBundleName}-tutao-${buildType}-${version}.apk`
const apkPath = `app-android/${gradleModule}/build/outputs/apk/tutao/${buildType}/${apkFileName}`
const outDirPath = `./artifacts/app-android`
const outApkPath = `${outDirPath}/${apkFileName}`
await fs.rm(outApkPath, { force: true })
const $inAppDir = $({ cwd: "app-android" })
await $inAppDir`./gradlew :${gradleModule}:assembleTutao${buildType}`
await fs.mkdir(outDirPath, { recursive: true })
await fs.rename(apkPath, outApkPath)
log(`Build complete. The APK is located at ${outApkPath}`)
return outApkPath
}
function capitalize(buildType) {
return buildType.charAt(0).toUpperCase() + buildType.slice(1)
}