uniapp-android-cli 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +382 -2
- package/bin/uni-pack.js +7 -0
- package/package.json +66 -4
- package/src/cli.js +242 -0
- package/src/commands/build.js +46 -0
- package/src/commands/debug.js +183 -0
- package/src/commands/device.js +309 -0
- package/src/commands/devices.js +23 -0
- package/src/commands/doctor.js +170 -0
- package/src/commands/init.js +488 -0
- package/src/commands/projects.js +31 -0
- package/src/commands/wgt.js +90 -0
- package/src/core/adb.js +292 -0
- package/src/core/build.js +438 -0
- package/src/core/config.js +208 -0
- package/src/core/debugPreset.js +56 -0
- package/src/core/devicePairing.js +153 -0
- package/src/core/errors.js +36 -0
- package/src/core/fsx.js +183 -0
- package/src/core/gradle.js +221 -0
- package/src/core/gradleLog.js +188 -0
- package/src/core/hash.js +18 -0
- package/src/core/hbuilderx.js +260 -0
- package/src/core/hbuilderxCli.js +168 -0
- package/src/core/initConfig.js +214 -0
- package/src/core/initEnv.js +140 -0
- package/src/core/interactive.js +93 -0
- package/src/core/logger.js +82 -0
- package/src/core/output.js +181 -0
- package/src/core/patch/assets.js +154 -0
- package/src/core/patch/gradle.js +166 -0
- package/src/core/patch/manifest.js +81 -0
- package/src/core/patch/nativePlugins.js +137 -0
- package/src/core/patch/projectManifest.js +66 -0
- package/src/core/patch/template.js +75 -0
- package/src/core/patch/utsPlugins.js +425 -0
- package/src/core/process.js +120 -0
- package/src/core/project.js +263 -0
- package/src/core/qrcode.js +13 -0
- package/src/core/resourceVersion.js +87 -0
- package/src/core/sdk.js +403 -0
- package/src/core/signing.js +102 -0
- package/src/core/templateCache.js +81 -0
- package/src/core/version.js +128 -0
- package/src/core/webBuild.js +77 -0
- package/src/core/wgt.js +98 -0
- package/src/core/workdir.js +44 -0
package/src/core/sdk.js
ADDED
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import os from 'node:os'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { readdir } from 'node:fs/promises'
|
|
4
|
+
import { PackError, EXIT } from './errors.js'
|
|
5
|
+
import { exists, readText } from './fsx.js'
|
|
6
|
+
import { run } from './process.js'
|
|
7
|
+
|
|
8
|
+
/** 基础 AAR:与当前线上可运行的自定义基座保持一致 */
|
|
9
|
+
export const BASE_AARS = [
|
|
10
|
+
'android-gif-drawable-1.2.29.aar',
|
|
11
|
+
'audio-mp3aac-release.aar',
|
|
12
|
+
'breakpad-build-release.aar',
|
|
13
|
+
'lib.5plus.base-release.aar',
|
|
14
|
+
'msa_mdid_1.0.13.aar',
|
|
15
|
+
'uni-chooseMedia-release.aar',
|
|
16
|
+
'uni-createRequestPermissionListener-release.aar',
|
|
17
|
+
'uni-exit-release.aar',
|
|
18
|
+
'uni-getAccessibilityInfo-release.aar',
|
|
19
|
+
'uni-getAppAuthorizeSetting-release.aar',
|
|
20
|
+
'uni-getAppBaseInfo-release.aar',
|
|
21
|
+
'uni-getDeviceInfo-release.aar',
|
|
22
|
+
'uni-getNetworkType-release.aar',
|
|
23
|
+
'uni-getSystemInfo-release.aar',
|
|
24
|
+
'uni-getSystemSetting-release.aar',
|
|
25
|
+
'uni-installApk-release.aar',
|
|
26
|
+
'uni-network-release.aar',
|
|
27
|
+
'uni-openAppAuthorizeSetting-release.aar',
|
|
28
|
+
'uni-prompt-release.aar',
|
|
29
|
+
'uni-storage-release.aar',
|
|
30
|
+
'uniapp-v8-release.aar',
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
export const DEBUG_AAR = 'debug-server-release.aar'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* manifest.json 的 app-plus.modules 与 SDK/libs 里的 AAR 不是一一对应,常见的做映射,
|
|
37
|
+
* 认不出的模块给出提示而不是直接失败(很多模块已经并进核心 AAR)。
|
|
38
|
+
*/
|
|
39
|
+
export const MODULE_AARS = {
|
|
40
|
+
Barcode: ['barcode-release.aar'],
|
|
41
|
+
Bluetooth: ['Bluetooth-release.aar'],
|
|
42
|
+
Camera: ['media-release.aar'],
|
|
43
|
+
VideoPlayer: ['media-release.aar', 'weex_videoplayer-release.aar'],
|
|
44
|
+
Audio: ['audio-mp3aac-release.aar'],
|
|
45
|
+
Speech: ['speech-release.aar'],
|
|
46
|
+
Fingerprint: ['fingerprint-release.aar'],
|
|
47
|
+
Contacts: ['contacts-release.aar'],
|
|
48
|
+
Messaging: ['messaging-release.aar'],
|
|
49
|
+
iBeacon: ['iBeacon-release.aar'],
|
|
50
|
+
Share: ['share-weixin-release.aar', 'share-qq-release.aar', 'share-sina-release.aar'],
|
|
51
|
+
Payment: ['payment-weixin-release.aar', 'payment-alipay-release.aar'],
|
|
52
|
+
OAuth: ['oauth-weixin-release.aar', 'oauth-qq-release.aar', 'oauth-sina-release.aar'],
|
|
53
|
+
Statistic: ['statistic-release.aar'],
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 接受用户可能给的各种层级:SDK 根、解压出来的归档根,甚至 HBuilder-Integrate-AS 本身。
|
|
58
|
+
* @param {string} input
|
|
59
|
+
* @returns {Promise<string>} 含 HBuilder-Integrate-AS 的目录;找不到返回空串
|
|
60
|
+
*/
|
|
61
|
+
export async function resolveSdkRootInput(input) {
|
|
62
|
+
const value = String(input ?? '').trim()
|
|
63
|
+
if (!value) {
|
|
64
|
+
return ''
|
|
65
|
+
}
|
|
66
|
+
const root = path.resolve(value)
|
|
67
|
+
if (path.basename(root) === 'HBuilder-Integrate-AS') {
|
|
68
|
+
return path.dirname(root)
|
|
69
|
+
}
|
|
70
|
+
if (await exists(path.join(root, 'HBuilder-Integrate-AS'))) {
|
|
71
|
+
return root
|
|
72
|
+
}
|
|
73
|
+
const nested = path.join(root, path.basename(root))
|
|
74
|
+
if (await exists(path.join(nested, 'HBuilder-Integrate-AS'))) {
|
|
75
|
+
return nested
|
|
76
|
+
}
|
|
77
|
+
const entries = await readdir(root, { withFileTypes: true }).catch(() => [])
|
|
78
|
+
const candidates = entries
|
|
79
|
+
.filter((entry) => entry.isDirectory() && entry.name.startsWith('Android-SDK@'))
|
|
80
|
+
.map((entry) => path.join(root, entry.name))
|
|
81
|
+
.sort()
|
|
82
|
+
.reverse()
|
|
83
|
+
for (const candidate of candidates) {
|
|
84
|
+
if (await exists(path.join(candidate, 'HBuilder-Integrate-AS'))) {
|
|
85
|
+
return candidate
|
|
86
|
+
}
|
|
87
|
+
const deep = path.join(candidate, path.basename(candidate))
|
|
88
|
+
if (await exists(path.join(deep, 'HBuilder-Integrate-AS'))) {
|
|
89
|
+
return deep
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return ''
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 解析离线 SDK。显式配置优先;没有显式配置时按项目的编译器版本在已安装的 Android-SDK@x.yy 里找。
|
|
97
|
+
* @param {any} config
|
|
98
|
+
* @param {{ project?: any, version?: string, searchDirs?: string[] }} [options]
|
|
99
|
+
*/
|
|
100
|
+
export async function resolveSdk(config, options = {}) {
|
|
101
|
+
const explicit = [options.project?.sdkRoot, config.sdk?.root, process.env.UNI_SDK_ROOT]
|
|
102
|
+
.map((item) => String(item ?? '').trim())
|
|
103
|
+
.filter(Boolean)
|
|
104
|
+
|
|
105
|
+
let invalid = ''
|
|
106
|
+
for (const candidate of explicit) {
|
|
107
|
+
const root = await resolveSdkRootInput(candidate)
|
|
108
|
+
if (root) {
|
|
109
|
+
return validateSdkRoot(root, config)
|
|
110
|
+
}
|
|
111
|
+
invalid = candidate
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const version = String(options.version ?? '').trim()
|
|
115
|
+
if (version) {
|
|
116
|
+
const matched = await findSdkForVersion(config, version, options.searchDirs)
|
|
117
|
+
if (matched) {
|
|
118
|
+
return validateSdkRoot(matched, config)
|
|
119
|
+
}
|
|
120
|
+
if (invalid) {
|
|
121
|
+
throw new PackError(`离线 SDK 路径无效:${invalid}`, EXIT.PREFLIGHT, {
|
|
122
|
+
hint: `未找到与 uni-app ${version} 匹配的其他 SDK;--sdk / sdk.root 应指向包含 HBuilder-Integrate-AS 的 Android-SDK@x.yy 目录`,
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
const installed = await listInstalledSdks(config, options.searchDirs)
|
|
126
|
+
throw new PackError(`未找到与 uni-app ${version} 匹配的离线 SDK`, EXIT.PREFLIGHT, {
|
|
127
|
+
hint: [
|
|
128
|
+
`已安装的 SDK:${installed.map((item) => item.version).join(', ') || '(未扫描到)'}`,
|
|
129
|
+
`请下载 Android-SDK@${version} 放到 ${defaultSdkDirs(config).join(' 或 ')},`,
|
|
130
|
+
'或在配置里指定该项目的 sdkRoot / sdk.root',
|
|
131
|
+
].join('\n'),
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (invalid) {
|
|
136
|
+
throw new PackError(`离线 SDK 路径无效:${invalid}`, EXIT.PREFLIGHT, {
|
|
137
|
+
hint: 'UNI_SDK_ROOT / --sdk 应指向包含 HBuilder-Integrate-AS 的 Android-SDK@x.yy 目录',
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
throw new PackError('未配置离线 SDK 路径(sdk.root)', EXIT.PREFLIGHT, {
|
|
142
|
+
hint: '运行 uni-pack init --sdk <目录>,或设置环境变量 UNI_SDK_ROOT,或手工填写 ~/.uniapp-android-cli/config.json',
|
|
143
|
+
})
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @param {string} root
|
|
148
|
+
* @param {any} config
|
|
149
|
+
*/
|
|
150
|
+
async function validateSdkRoot(root, config) {
|
|
151
|
+
const templateRoot = path.join(root, String(config.sdk?.template ?? 'HBuilder-Integrate-AS'))
|
|
152
|
+
const moduleDir = path.join(templateRoot, 'simpleDemo')
|
|
153
|
+
const libsDir = path.join(root, String(config.sdk?.libsDir ?? 'SDK/libs'))
|
|
154
|
+
const dataDir = path.join(root, String(config.sdk?.dataDir ?? 'SDK/assets/data'))
|
|
155
|
+
|
|
156
|
+
const required = [
|
|
157
|
+
path.join(libsDir, 'lib.5plus.base-release.aar'),
|
|
158
|
+
path.join(moduleDir, 'build.gradle'),
|
|
159
|
+
path.join(moduleDir, 'src/main/AndroidManifest.xml'),
|
|
160
|
+
path.join(dataDir, 'dcloud_control.xml'),
|
|
161
|
+
]
|
|
162
|
+
const missing = []
|
|
163
|
+
for (const item of required) {
|
|
164
|
+
if (!(await exists(item))) {
|
|
165
|
+
missing.push(item)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (missing.length > 0) {
|
|
169
|
+
throw new PackError(`离线 SDK 不完整,缺少:\n${missing.join('\n')}`, EXIT.PREFLIGHT)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const gradlew = path.join(templateRoot, process.platform === 'win32' ? 'gradlew.bat' : 'gradlew')
|
|
173
|
+
if (!(await exists(gradlew))) {
|
|
174
|
+
throw new PackError(`找不到 ${gradlew}`, EXIT.PREFLIGHT)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return { root, templateRoot, moduleDir, libsDir, dataDir, gradlew }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* @param {any} config
|
|
182
|
+
* @param {string[]} [extraDirs]
|
|
183
|
+
* @returns {string[]}
|
|
184
|
+
*/
|
|
185
|
+
function defaultSdkDirs(config, extraDirs = []) {
|
|
186
|
+
return [
|
|
187
|
+
...extraDirs,
|
|
188
|
+
...(config?.sdk?.searchDirs ?? []),
|
|
189
|
+
config?.sdk?.root ? path.dirname(path.resolve(String(config.sdk.root))) : '',
|
|
190
|
+
...String(process.env.UNI_SDK_DIRS ?? '').split(path.delimiter),
|
|
191
|
+
'C:/develop/android-sdk/uniapp-Android',
|
|
192
|
+
path.join(os.homedir(), 'android-sdk', 'uniapp-Android'),
|
|
193
|
+
].filter((item) => typeof item === 'string' && item.trim())
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 扫描 Android-SDK@x.yy 目录。解压后常见一层同名嵌套,交给 resolveSdkRootInput 处理。
|
|
198
|
+
* @param {any} config
|
|
199
|
+
* @param {string[]} [extraDirs]
|
|
200
|
+
* @returns {Promise<Array<{ version: string, root: string, name: string }>>}
|
|
201
|
+
*/
|
|
202
|
+
export async function listInstalledSdks(config, extraDirs = []) {
|
|
203
|
+
/** @type {Array<{ version: string, root: string, name: string }>} */
|
|
204
|
+
const found = []
|
|
205
|
+
const seen = new Set()
|
|
206
|
+
for (const dir of defaultSdkDirs(config, extraDirs)) {
|
|
207
|
+
const entries = await readdir(path.resolve(dir), { withFileTypes: true }).catch(() => [])
|
|
208
|
+
for (const entry of entries) {
|
|
209
|
+
if (!entry.isDirectory()) {
|
|
210
|
+
continue
|
|
211
|
+
}
|
|
212
|
+
const match = /^Android-SDK@(\d+\.\d+)\./i.exec(entry.name)
|
|
213
|
+
if (!match) {
|
|
214
|
+
continue
|
|
215
|
+
}
|
|
216
|
+
const root = await resolveSdkRootInput(path.join(dir, entry.name))
|
|
217
|
+
if (!root) {
|
|
218
|
+
continue
|
|
219
|
+
}
|
|
220
|
+
const key = root.toLowerCase()
|
|
221
|
+
if (seen.has(key)) {
|
|
222
|
+
continue
|
|
223
|
+
}
|
|
224
|
+
seen.add(key)
|
|
225
|
+
found.push({ version: match[1], root, name: entry.name })
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return found.sort((a, b) => b.name.localeCompare(a.name))
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* @param {any} config
|
|
233
|
+
* @param {string} version
|
|
234
|
+
* @param {string[]} [extraDirs]
|
|
235
|
+
* @returns {Promise<string>}
|
|
236
|
+
*/
|
|
237
|
+
export async function findSdkForVersion(config, version, extraDirs = []) {
|
|
238
|
+
const installed = await listInstalledSdks(config, extraDirs)
|
|
239
|
+
return installed.find((item) => item.version === version)?.root ?? ''
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 老版本 SDK 的 compileSdk/buildTools 与新版不同,优先沿用 SDK 自带的模板值。
|
|
244
|
+
* Kotlin 版本同样以模板声明为准;模板没写 kotlin-gradle-plugin 时返回空,由调用方兜底。
|
|
245
|
+
* @param {string} templateRoot
|
|
246
|
+
* @returns {Promise<{ compileSdkVersion?: number, buildToolsVersion?: string, kotlinVersion?: string }>}
|
|
247
|
+
*/
|
|
248
|
+
export async function readTemplateDefaults(templateRoot) {
|
|
249
|
+
const file = path.join(templateRoot, 'simpleDemo', 'build.gradle')
|
|
250
|
+
if (!(await exists(file))) {
|
|
251
|
+
return {}
|
|
252
|
+
}
|
|
253
|
+
const text = await readText(file)
|
|
254
|
+
const compileSdk = Number(/compileSdkVersion\s+(\d+)/.exec(text)?.[1])
|
|
255
|
+
const buildTools = /buildToolsVersion\s+'([^']+)'/.exec(text)?.[1]
|
|
256
|
+
const rootFile = path.join(templateRoot, 'build.gradle')
|
|
257
|
+
const rootText = (await exists(rootFile)) ? await readText(rootFile) : ''
|
|
258
|
+
const kotlinVersion = /kotlin-gradle-plugin:([0-9][\w.-]*)/.exec(rootText)?.[1]
|
|
259
|
+
return {
|
|
260
|
+
compileSdkVersion: Number.isFinite(compileSdk) ? compileSdk : undefined,
|
|
261
|
+
buildToolsVersion: buildTools || undefined,
|
|
262
|
+
kotlinVersion: kotlinVersion || undefined,
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* @param {string} sdkDir
|
|
268
|
+
* @returns {Promise<string[]>}
|
|
269
|
+
*/
|
|
270
|
+
async function listBuildTools(sdkDir) {
|
|
271
|
+
const dir = path.join(sdkDir, 'build-tools')
|
|
272
|
+
const entries = await readdir(dir, { withFileTypes: true }).catch(() => [])
|
|
273
|
+
return entries
|
|
274
|
+
.filter((entry) => entry.isDirectory())
|
|
275
|
+
.map((entry) => entry.name)
|
|
276
|
+
.sort((a, b) => compareDotted(b, a))
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* @param {string} a
|
|
281
|
+
* @param {string} b
|
|
282
|
+
*/
|
|
283
|
+
function compareDotted(a, b) {
|
|
284
|
+
const left = a.split(/[.-]/).map(Number)
|
|
285
|
+
const right = b.split(/[.-]/).map(Number)
|
|
286
|
+
for (let index = 0; index < Math.max(left.length, right.length); index += 1) {
|
|
287
|
+
const diff = (left[index] || 0) - (right[index] || 0)
|
|
288
|
+
if (diff !== 0) {
|
|
289
|
+
return diff
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return a.localeCompare(b)
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* @param {any} config
|
|
297
|
+
* @returns {string}
|
|
298
|
+
*/
|
|
299
|
+
function resolveAndroidSdkDir(config) {
|
|
300
|
+
const configured = String(config.android?.sdkDir ?? '').trim()
|
|
301
|
+
const candidate = configured || String(process.env.ANDROID_HOME || process.env.ANDROID_SDK_ROOT || '').trim()
|
|
302
|
+
if (!candidate) {
|
|
303
|
+
throw new PackError('未配置 Android SDK 路径(android.sdkDir)', EXIT.PREFLIGHT, {
|
|
304
|
+
hint: '运行 uni-pack init --android-sdk <目录>,或设置环境变量 ANDROID_HOME',
|
|
305
|
+
})
|
|
306
|
+
}
|
|
307
|
+
return path.resolve(candidate)
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* 只依赖 adb 的命令(设备列表/配对)不需要 JDK 与 build-tools。
|
|
312
|
+
* @param {any} config
|
|
313
|
+
* @returns {Promise<string>}
|
|
314
|
+
*/
|
|
315
|
+
export async function resolveAdbPath(config) {
|
|
316
|
+
const adb = path.join(
|
|
317
|
+
resolveAndroidSdkDir(config),
|
|
318
|
+
'platform-tools',
|
|
319
|
+
`adb${process.platform === 'win32' ? '.exe' : ''}`,
|
|
320
|
+
)
|
|
321
|
+
if (!(await exists(adb))) {
|
|
322
|
+
throw new PackError(`找不到 adb:${adb}`, EXIT.PREFLIGHT, {
|
|
323
|
+
hint: '在 Android SDK Manager 里安装 Platform-Tools',
|
|
324
|
+
})
|
|
325
|
+
}
|
|
326
|
+
return adb
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* @param {any} config
|
|
331
|
+
* @param {{ preferredBuildTools?: string }} [options]
|
|
332
|
+
*/
|
|
333
|
+
export async function resolveAndroidTools(config, options = {}) {
|
|
334
|
+
const sdkDir = resolveAndroidSdkDir(config)
|
|
335
|
+
|
|
336
|
+
const installed = await listBuildTools(sdkDir)
|
|
337
|
+
const wanted = [
|
|
338
|
+
String(config.android?.buildTools ?? '').trim(),
|
|
339
|
+
String(options.preferredBuildTools ?? '').trim(),
|
|
340
|
+
].filter(Boolean)
|
|
341
|
+
const buildTools = wanted.find((item) => installed.includes(item)) ?? installed[0]
|
|
342
|
+
if (!buildTools) {
|
|
343
|
+
throw new PackError(`Android SDK 下没有 build-tools:${path.join(sdkDir, 'build-tools')}`, EXIT.PREFLIGHT)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const buildToolsDir = path.join(sdkDir, 'build-tools', buildTools)
|
|
347
|
+
const suffix = process.platform === 'win32' ? '.bat' : ''
|
|
348
|
+
const apksigner = path.join(buildToolsDir, `apksigner${suffix}`)
|
|
349
|
+
const zipalign = path.join(buildToolsDir, `zipalign${process.platform === 'win32' ? '.exe' : ''}`)
|
|
350
|
+
const adb = path.join(sdkDir, 'platform-tools', `adb${process.platform === 'win32' ? '.exe' : ''}`)
|
|
351
|
+
const keytool = await resolveKeytool(config)
|
|
352
|
+
|
|
353
|
+
if (!(await exists(apksigner))) {
|
|
354
|
+
throw new PackError(`找不到 apksigner:${apksigner}`, EXIT.PREFLIGHT)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return { sdkDir, buildTools, buildToolsDir, apksigner, zipalign, adb, keytool }
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* @param {any} config
|
|
362
|
+
* @returns {Promise<string>}
|
|
363
|
+
*/
|
|
364
|
+
export async function resolveJavaHome(config) {
|
|
365
|
+
const candidates = [
|
|
366
|
+
config.android?.jdk,
|
|
367
|
+
process.env.JAVA_HOME,
|
|
368
|
+
'C:/Program Files/Java/jdk-21',
|
|
369
|
+
'C:/Program Files/Java/jdk-17',
|
|
370
|
+
].filter((item) => typeof item === 'string' && item.length > 0)
|
|
371
|
+
|
|
372
|
+
for (const candidate of candidates) {
|
|
373
|
+
const java = path.join(String(candidate), 'bin', process.platform === 'win32' ? 'java.exe' : 'java')
|
|
374
|
+
if (await exists(java)) {
|
|
375
|
+
return String(candidate)
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
throw new PackError('找不到 JDK,请在配置里填写 android.jdk 或设置 JAVA_HOME', EXIT.PREFLIGHT)
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* @param {any} config
|
|
383
|
+
* @returns {Promise<string>}
|
|
384
|
+
*/
|
|
385
|
+
async function resolveKeytool(config) {
|
|
386
|
+
const javaHome = await resolveJavaHome(config)
|
|
387
|
+
return path.join(javaHome, 'bin', process.platform === 'win32' ? 'keytool.exe' : 'keytool')
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* @param {string} keytool
|
|
392
|
+
* @param {string} keystore
|
|
393
|
+
* @param {string} storePassword
|
|
394
|
+
* @param {string} alias
|
|
395
|
+
*/
|
|
396
|
+
export async function readKeystoreFingerprint(keytool, keystore, storePassword, alias) {
|
|
397
|
+
const { stdout } = await run(keytool, ['-list', '-v', '-keystore', keystore, '-storepass', storePassword, '-alias', alias], {
|
|
398
|
+
allowFailure: false,
|
|
399
|
+
})
|
|
400
|
+
const sha1 = /SHA1:\s*([0-9A-F:]+)/i.exec(stdout)?.[1] ?? ''
|
|
401
|
+
const sha256 = /SHA256:\s*([0-9A-F:]+)/i.exec(stdout)?.[1] ?? ''
|
|
402
|
+
return { sha1, sha256, raw: stdout }
|
|
403
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { PackError, EXIT } from './errors.js'
|
|
3
|
+
import { exists, readText } from './fsx.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} Signing
|
|
7
|
+
* @property {string} keystore
|
|
8
|
+
* @property {string} keyAlias
|
|
9
|
+
* @property {string} storePassword
|
|
10
|
+
* @property {string} keyPassword
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 密码来源优先级:命令行 → 环境变量 → keystore.properties。
|
|
15
|
+
* @param {any} project
|
|
16
|
+
* @param {{ storePassword?: string, keyPassword?: string }} options
|
|
17
|
+
* @returns {Promise<Signing>}
|
|
18
|
+
*/
|
|
19
|
+
export async function resolveSigning(project, options = {}) {
|
|
20
|
+
const signing = project.signing ?? {}
|
|
21
|
+
const keystore = resolveProjectPath(project.projectDir, signing.keystore)
|
|
22
|
+
const properties = await readProperties(signing.keystoreProperties, project.projectDir)
|
|
23
|
+
// 别名优先取配置;允许只给 keystore.properties(团队自定义布局时更省配置)
|
|
24
|
+
const keyAlias = String(signing.keyAlias ?? '') || String(properties.keyAlias ?? '')
|
|
25
|
+
if (!keystore || !(await exists(keystore))) {
|
|
26
|
+
throw new PackError(`找不到签名证书:${keystore || '(未配置)'}`, EXIT.SIGN, {
|
|
27
|
+
hint: '检查 signing.keystore;相对路径按项目目录解析,也可以写绝对路径',
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
if (!keyAlias) {
|
|
31
|
+
throw new PackError('未配置签名别名 keyAlias', EXIT.SIGN, {
|
|
32
|
+
hint: '在配置 signing.keyAlias 填写,或写进 keystoreProperties 的 keyAlias',
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const storePassword =
|
|
37
|
+
options.storePassword ??
|
|
38
|
+
envValue(signing.storePasswordEnv) ??
|
|
39
|
+
properties.storePassword ??
|
|
40
|
+
''
|
|
41
|
+
const keyPassword =
|
|
42
|
+
options.keyPassword ??
|
|
43
|
+
envValue(signing.keyPasswordEnv) ??
|
|
44
|
+
properties.keyPassword ??
|
|
45
|
+
storePassword
|
|
46
|
+
|
|
47
|
+
if (!storePassword || !keyPassword) {
|
|
48
|
+
throw new PackError('缺少签名密码', EXIT.SIGN, {
|
|
49
|
+
hint: '设置环境变量(如 PRODIS_STORE_PASS / PRODIS_KEY_PASS),或在配置里指定 keystoreProperties',
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return { keystore, keyAlias, storePassword, keyPassword }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 配置里的相对路径统一相对项目目录解析,避免从别的目录执行时找不到证书。
|
|
58
|
+
* @param {string | undefined} projectDir
|
|
59
|
+
* @param {unknown} value
|
|
60
|
+
* @returns {string}
|
|
61
|
+
*/
|
|
62
|
+
function resolveProjectPath(projectDir, value) {
|
|
63
|
+
const text = String(value ?? '').trim()
|
|
64
|
+
if (!text) {
|
|
65
|
+
return ''
|
|
66
|
+
}
|
|
67
|
+
return path.isAbsolute(text) ? text : path.join(String(projectDir ?? ''), text)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @param {string | undefined} name
|
|
72
|
+
*/
|
|
73
|
+
function envValue(name) {
|
|
74
|
+
if (!name) {
|
|
75
|
+
return undefined
|
|
76
|
+
}
|
|
77
|
+
return process.env[name] || undefined
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param {string | undefined} file
|
|
82
|
+
* @param {string} projectDir
|
|
83
|
+
*/
|
|
84
|
+
async function readProperties(file, projectDir) {
|
|
85
|
+
if (!file) {
|
|
86
|
+
return {}
|
|
87
|
+
}
|
|
88
|
+
const absolute = path.isAbsolute(file) ? file : path.join(projectDir, file)
|
|
89
|
+
if (!(await exists(absolute))) {
|
|
90
|
+
return {}
|
|
91
|
+
}
|
|
92
|
+
const text = await readText(absolute)
|
|
93
|
+
/** @type {Record<string, string>} */
|
|
94
|
+
const result = {}
|
|
95
|
+
for (const line of text.split(/\r?\n/)) {
|
|
96
|
+
const match = /^\s*([\w.]+)\s*=\s*(.*)\s*$/.exec(line)
|
|
97
|
+
if (match) {
|
|
98
|
+
result[match[1]] = match[2]
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return result
|
|
102
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { rm, stat } from 'node:fs/promises'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { exists, readJson, writeJson } from './fsx.js'
|
|
5
|
+
|
|
6
|
+
export const TEMPLATE_STATE_FILE = '.uni-pack-template.json'
|
|
7
|
+
|
|
8
|
+
const FINGERPRINT_FILES = [
|
|
9
|
+
'build.gradle',
|
|
10
|
+
'settings.gradle',
|
|
11
|
+
'gradle/wrapper/gradle-wrapper.properties',
|
|
12
|
+
'simpleDemo/build.gradle',
|
|
13
|
+
'simpleDemo/src/main/AndroidManifest.xml',
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* SDK 换版本、模板被手工改过(mtime/大小变化)时都要重新复制模板。
|
|
18
|
+
* @param {{ root: string, templateRoot: string, version?: string | null }} sdk
|
|
19
|
+
* @returns {Promise<{ fingerprint: string, files: Array<{ file: string, size: number, mtimeMs: number }> }>}
|
|
20
|
+
*/
|
|
21
|
+
export async function templateFingerprint(sdk) {
|
|
22
|
+
/** @type {Array<{ file: string, size: number, mtimeMs: number }>} */
|
|
23
|
+
const files = []
|
|
24
|
+
for (const relative of FINGERPRINT_FILES) {
|
|
25
|
+
const file = path.join(sdk.templateRoot, relative)
|
|
26
|
+
try {
|
|
27
|
+
const info = await stat(file)
|
|
28
|
+
files.push({ file: relative, size: info.size, mtimeMs: Math.round(info.mtimeMs) })
|
|
29
|
+
} catch {
|
|
30
|
+
files.push({ file: relative, size: -1, mtimeMs: -1 })
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const payload = JSON.stringify({
|
|
34
|
+
root: sdk.root,
|
|
35
|
+
templateRoot: sdk.templateRoot,
|
|
36
|
+
version: String(sdk.version ?? ''),
|
|
37
|
+
files,
|
|
38
|
+
})
|
|
39
|
+
return { fingerprint: createHash('sha256').update(payload).digest('hex'), files }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param {string} gradleRoot
|
|
44
|
+
* @returns {Promise<{ fingerprint?: string, templateRoot?: string, version?: string, builtAt?: string } | null>}
|
|
45
|
+
*/
|
|
46
|
+
export async function readTemplateState(gradleRoot) {
|
|
47
|
+
return readJson(path.join(gradleRoot, TEMPLATE_STATE_FILE)).catch(() => null)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {string} gradleRoot
|
|
52
|
+
* @param {Record<string, unknown>} state
|
|
53
|
+
*/
|
|
54
|
+
export async function writeTemplateState(gradleRoot, state) {
|
|
55
|
+
await writeJson(path.join(gradleRoot, TEMPLATE_STATE_FILE), state)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 增量构建前清掉会残留的输入:当前 appid 的 www、SDK 模板 assets 数据、上次的 AAR/so。
|
|
60
|
+
* build/、.gradle/、simpleDemo/build/ 保留,Gradle 才能命中 UP-TO-DATE。
|
|
61
|
+
* @param {{ gradleRoot: string, appid: string }} options
|
|
62
|
+
* @returns {Promise<string[]>} 已清理的路径
|
|
63
|
+
*/
|
|
64
|
+
export async function cleanIncrementalInputs(options) {
|
|
65
|
+
const relative = [
|
|
66
|
+
path.join('simpleDemo', 'src', 'main', 'assets', 'apps', options.appid),
|
|
67
|
+
path.join('simpleDemo', 'src', 'main', 'assets', 'data'),
|
|
68
|
+
path.join('simpleDemo', 'src', 'main', 'assets', 'dcloud_uniplugins.json'),
|
|
69
|
+
path.join('simpleDemo', 'libs'),
|
|
70
|
+
path.join('simpleDemo', 'src', 'main', 'jniLibs'),
|
|
71
|
+
]
|
|
72
|
+
const removed = []
|
|
73
|
+
for (const item of relative) {
|
|
74
|
+
const target = path.join(options.gradleRoot, item)
|
|
75
|
+
if (await exists(target)) {
|
|
76
|
+
await rm(target, { recursive: true, force: true })
|
|
77
|
+
removed.push(item)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return removed
|
|
81
|
+
}
|