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
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import { copyFile, readdir, rm } from 'node:fs/promises'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { PackError, EXIT } from './errors.js'
|
|
4
|
+
import { emptyDir, ensureDir, exists } from './fsx.js'
|
|
5
|
+
import { runGradle } from './gradle.js'
|
|
6
|
+
import { resolveHbuilderx } from './hbuilderx.js'
|
|
7
|
+
import { patchAndroidManifest } from './patch/manifest.js'
|
|
8
|
+
import { patchAppGradle, patchRootGradle } from './patch/gradle.js'
|
|
9
|
+
import { applyAssets } from './patch/assets.js'
|
|
10
|
+
import { applyNativePlugins } from './patch/nativePlugins.js'
|
|
11
|
+
import { mergeProjectManifest } from './patch/projectManifest.js'
|
|
12
|
+
import { prepareTemplate, refreshTemplateFiles } from './patch/template.js'
|
|
13
|
+
import { cleanIncrementalInputs, readTemplateState, templateFingerprint, writeTemplateState } from './templateCache.js'
|
|
14
|
+
import { UTS_AAR, integrateUtsPlugins, scanUtsPlugins } from './patch/utsPlugins.js'
|
|
15
|
+
import {
|
|
16
|
+
detectVueVersion,
|
|
17
|
+
missingAppKeyHint,
|
|
18
|
+
normalizeManifest,
|
|
19
|
+
readProjectManifest,
|
|
20
|
+
readProjectPackage,
|
|
21
|
+
} from './project.js'
|
|
22
|
+
import {
|
|
23
|
+
BASE_AARS,
|
|
24
|
+
DEBUG_AAR,
|
|
25
|
+
MODULE_AARS,
|
|
26
|
+
findSdkForVersion,
|
|
27
|
+
readTemplateDefaults,
|
|
28
|
+
resolveAndroidTools,
|
|
29
|
+
resolveJavaHome,
|
|
30
|
+
resolveSdk,
|
|
31
|
+
} from './sdk.js'
|
|
32
|
+
import { resolveSigning } from './signing.js'
|
|
33
|
+
import { assertVersionMatch, readCompilerVersion, readSdkVersion, versionMismatchError } from './version.js'
|
|
34
|
+
import { runWebBuild } from './webBuild.js'
|
|
35
|
+
import { assertResourceVersion } from './resourceVersion.js'
|
|
36
|
+
import { resolveBuildTempDir, resolveWorkRoot } from './workdir.js'
|
|
37
|
+
import { apkBaseName, findApk, publishApk, resolveApkPrefix } from './output.js'
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {Object} input
|
|
41
|
+
* @param {any} input.config
|
|
42
|
+
* @param {any} input.project
|
|
43
|
+
* @param {'release' | 'debug'} input.mode
|
|
44
|
+
* @param {any} input.options
|
|
45
|
+
* @param {import('./logger.js').Logger} input.logger
|
|
46
|
+
*/
|
|
47
|
+
export async function buildProject(input) {
|
|
48
|
+
const { config, project, mode, options, logger } = input
|
|
49
|
+
const debug = mode === 'debug'
|
|
50
|
+
const prune = options.prune === undefined ? undefined : Number(options.prune)
|
|
51
|
+
if (prune !== undefined && (!Number.isInteger(prune) || prune < 0)) {
|
|
52
|
+
throw new PackError(`--prune 需要一个非负整数,收到:${String(options.prune)}`, EXIT.CONFIG, {
|
|
53
|
+
hint: '例如 --prune 5 表示每个 mode 只保留最近 5 个时间戳 APK,latest 永远保留',
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const { manifest, file, type, inputDir } = await readProjectManifest(project.projectDir)
|
|
58
|
+
project.type = type
|
|
59
|
+
project.inputDir = inputDir
|
|
60
|
+
|
|
61
|
+
const resolved = normalizeManifest(manifest, {
|
|
62
|
+
appid: project.appid,
|
|
63
|
+
name: project.name,
|
|
64
|
+
package: project.package || (await readProjectPackage(project.projectDir)),
|
|
65
|
+
versionName: project.versionName,
|
|
66
|
+
versionCode: project.versionCode,
|
|
67
|
+
minSdkVersion: project.minSdkVersion,
|
|
68
|
+
targetSdkVersion: project.targetSdkVersion,
|
|
69
|
+
abiFilters: project.abiFilters,
|
|
70
|
+
})
|
|
71
|
+
const vueVersion = await detectVueVersion(project.projectDir, manifest, type)
|
|
72
|
+
logger.info(`项目 ${resolved.name}(${resolved.appid})v${resolved.versionName}(${resolved.versionCode})`)
|
|
73
|
+
logger.detail(
|
|
74
|
+
`manifest: ${file}(${type === 'hbuilderx' ? 'HBuilderX' : 'CLI'} 项目,Vue ${vueVersion})`,
|
|
75
|
+
)
|
|
76
|
+
const appKeyHint = missingAppKeyHint(project.appKey)
|
|
77
|
+
if (appKeyHint) {
|
|
78
|
+
logger.warn(appKeyHint)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const hbuilderx = type === 'hbuilderx' ? await resolveHbuilderx(config, { project }) : null
|
|
82
|
+
if (type === 'hbuilderx' && !hbuilderx && !options.skipWeb) {
|
|
83
|
+
throw new PackError('找不到 HBuilderX,无法编译 HBuilderX 项目的源码', EXIT.PREFLIGHT, {
|
|
84
|
+
hint: [
|
|
85
|
+
'任选一种方式指定:',
|
|
86
|
+
' 1) uni-pack init -p <项目> --hbuilderx <HBuilderX 目录>',
|
|
87
|
+
' 2) 在配置 hbuilderx.root 里填写 HBuilderX 安装目录',
|
|
88
|
+
' 3) 设置环境变量 HBUILDERX_HOME',
|
|
89
|
+
].join('\n'),
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const compiler = await readCompilerVersion(project, { hbuilderxRoot: hbuilderx?.root })
|
|
94
|
+
if (compiler.version) {
|
|
95
|
+
logger.info(`编译器版本:${compiler.version}(${compiler.source})`)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
let sdk = await resolveSdk(config, { project, version: compiler.version ?? undefined })
|
|
99
|
+
let sdkVersion = await readSdkVersion(sdk.root)
|
|
100
|
+
// 全局 sdk.root 可能是别的项目留下的;项目没单独指定时按编译器版本自动切到匹配的 SDK。
|
|
101
|
+
if (compiler.version && sdkVersion && compiler.version !== sdkVersion && !project.sdkRoot) {
|
|
102
|
+
const matched = await findSdkForVersion(config, compiler.version)
|
|
103
|
+
if (matched) {
|
|
104
|
+
sdk = await resolveSdk(config, { project: { ...project, sdkRoot: matched } })
|
|
105
|
+
sdkVersion = await readSdkVersion(sdk.root)
|
|
106
|
+
logger.info(`按项目版本切换离线 SDK:${sdk.root}`)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
logger.detail(`离线 SDK:${sdk.root}`)
|
|
110
|
+
|
|
111
|
+
const versions = { compiler: compiler.version, sdk: sdkVersion }
|
|
112
|
+
const versionCheck = assertVersionMatch(versions)
|
|
113
|
+
if (!versionCheck.ok && !options.skipVersionCheck) {
|
|
114
|
+
throw versionMismatchError(versions, { type })
|
|
115
|
+
}
|
|
116
|
+
if (versionCheck.ok) {
|
|
117
|
+
logger.info(`版本一致:uni-app ${versions.compiler} / SDK ${versions.sdk}`)
|
|
118
|
+
} else {
|
|
119
|
+
logger.warn(`版本不一致:uni-app ${versions.compiler ?? '未知'} / SDK ${versions.sdk ?? '未知'}(已跳过检查)`)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const workRoot = resolveWorkRoot(config, project)
|
|
123
|
+
const buildTemp = resolveBuildTempDir(workRoot)
|
|
124
|
+
let resourcesDir = path.resolve(
|
|
125
|
+
project.projectDir,
|
|
126
|
+
String(project.webBuild?.outputDir ?? (type === 'hbuilderx' ? 'unpackage/dist/build/app-plus' : 'dist/build/app')),
|
|
127
|
+
)
|
|
128
|
+
if (options.skipWeb) {
|
|
129
|
+
if (!(await exists(path.join(resourcesDir, 'app-service.js')))) {
|
|
130
|
+
throw new PackError('--skip-web 但前端产物不存在,请先构建', EXIT.WEB_BUILD)
|
|
131
|
+
}
|
|
132
|
+
logger.info(`跳过前端构建,使用 ${resourcesDir}`)
|
|
133
|
+
} else {
|
|
134
|
+
resourcesDir = (
|
|
135
|
+
await runWebBuild(project, logger, {
|
|
136
|
+
hbuilderx: hbuilderx ?? undefined,
|
|
137
|
+
vueVersion,
|
|
138
|
+
tempDir: buildTemp.tempDir,
|
|
139
|
+
})
|
|
140
|
+
).dir
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const resourceVersion = await assertResourceVersion({
|
|
144
|
+
resourcesDir,
|
|
145
|
+
expected: resolved,
|
|
146
|
+
skip: Boolean(options.skipResourceCheck),
|
|
147
|
+
logger,
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
const utsPlugins = await scanUtsPlugins({ projectDir: project.projectDir, resourcesDir })
|
|
151
|
+
const hasUts = utsPlugins.some((plugin) => plugin.usable)
|
|
152
|
+
if (utsPlugins.length > 0) {
|
|
153
|
+
logger.info(`UTS 插件:${utsPlugins.map((plugin) => plugin.name).join(', ')}`)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const signing = await resolveSigning(project, options)
|
|
157
|
+
const templateDefaults = await readTemplateDefaults(sdk.templateRoot)
|
|
158
|
+
const compileSdkVersion = Number(project.compileSdkVersion ?? templateDefaults.compileSdkVersion ?? 35)
|
|
159
|
+
const buildToolsVersion = firstNonEmpty(
|
|
160
|
+
project.buildToolsVersion,
|
|
161
|
+
config.android?.buildTools,
|
|
162
|
+
templateDefaults.buildToolsVersion,
|
|
163
|
+
)
|
|
164
|
+
const tools = await resolveAndroidTools(config, { preferredBuildTools: buildToolsVersion })
|
|
165
|
+
const javaHome = await resolveJavaHome(config)
|
|
166
|
+
const gradleRoot = path.join(workRoot, 'gradle')
|
|
167
|
+
|
|
168
|
+
logger.step('准备构建工程')
|
|
169
|
+
const template = await templateFingerprint({ root: sdk.root, templateRoot: sdk.templateRoot, version: sdkVersion })
|
|
170
|
+
const previous = await readTemplateState(gradleRoot)
|
|
171
|
+
if (
|
|
172
|
+
previous?.fingerprint === template.fingerprint &&
|
|
173
|
+
(await exists(path.join(gradleRoot, 'simpleDemo/build.gradle')))
|
|
174
|
+
) {
|
|
175
|
+
await refreshTemplateFiles(sdk.templateRoot, gradleRoot)
|
|
176
|
+
const cleaned = await cleanIncrementalInputs({ gradleRoot, appid: resolved.appid })
|
|
177
|
+
logger.info(`复用上次构建工程(模板未变化,清理 ${cleaned.length} 项增量输入)`)
|
|
178
|
+
} else {
|
|
179
|
+
await emptyDir(gradleRoot)
|
|
180
|
+
await prepareTemplate(sdk.templateRoot, gradleRoot)
|
|
181
|
+
await writeTemplateState(gradleRoot, {
|
|
182
|
+
fingerprint: template.fingerprint,
|
|
183
|
+
sdkRoot: sdk.root,
|
|
184
|
+
templateRoot: sdk.templateRoot,
|
|
185
|
+
version: sdkVersion ?? undefined,
|
|
186
|
+
builtAt: new Date().toISOString(),
|
|
187
|
+
})
|
|
188
|
+
logger.info('已复制离线 SDK 模板到工作目录')
|
|
189
|
+
}
|
|
190
|
+
const aars = await copyAars({
|
|
191
|
+
sdk,
|
|
192
|
+
targetDir: path.join(gradleRoot, 'simpleDemo/libs'),
|
|
193
|
+
debug,
|
|
194
|
+
modules: resolved.modules,
|
|
195
|
+
uts: hasUts,
|
|
196
|
+
vueVersion,
|
|
197
|
+
logger,
|
|
198
|
+
})
|
|
199
|
+
logger.success(`模板与 AAR 就绪(${aars.count} 个)`)
|
|
200
|
+
|
|
201
|
+
logger.step('写入项目配置')
|
|
202
|
+
await patchRootGradle(path.join(gradleRoot, 'build.gradle'), config.android.sdkDir)
|
|
203
|
+
await patchAppGradle(path.join(gradleRoot, 'simpleDemo/build.gradle'), {
|
|
204
|
+
packageName: resolved.packageName,
|
|
205
|
+
versionName: resolved.versionName,
|
|
206
|
+
versionCode: resolved.versionCode,
|
|
207
|
+
minSdkVersion: resolved.minSdkVersion,
|
|
208
|
+
targetSdkVersion: resolved.targetSdkVersion,
|
|
209
|
+
compileSdkVersion,
|
|
210
|
+
buildToolsVersion: tools.buildTools,
|
|
211
|
+
abiFilters: resolved.abiFilters,
|
|
212
|
+
signing,
|
|
213
|
+
})
|
|
214
|
+
const androidManifest = path.join(gradleRoot, 'simpleDemo/src/main/AndroidManifest.xml')
|
|
215
|
+
await patchAndroidManifest(androidManifest, {
|
|
216
|
+
packageName: resolved.packageName,
|
|
217
|
+
appKey: String(project.appKey ?? ''),
|
|
218
|
+
permissions: resolved.permissions,
|
|
219
|
+
features: resolved.features.map((entry) => {
|
|
220
|
+
const [name, required] = entry.split('|')
|
|
221
|
+
return { name, required }
|
|
222
|
+
}),
|
|
223
|
+
debug,
|
|
224
|
+
})
|
|
225
|
+
await mergeProjectManifest({ targetFile: androidManifest, projectDir: project.projectDir, logger })
|
|
226
|
+
// 密钥库只复制到工作目录供 Gradle 本次构建使用,构建结束后立即删除
|
|
227
|
+
const keystoreCopy = path.join(gradleRoot, 'simpleDemo/aw-pack.jks')
|
|
228
|
+
await copyFile(signing.keystore, keystoreCopy)
|
|
229
|
+
await applyAssets({
|
|
230
|
+
moduleDir: path.join(gradleRoot, 'simpleDemo'),
|
|
231
|
+
dataDir: sdk.dataDir,
|
|
232
|
+
resourcesDir,
|
|
233
|
+
appid: resolved.appid,
|
|
234
|
+
appName: resolved.name,
|
|
235
|
+
versionName: resolved.versionName,
|
|
236
|
+
debug,
|
|
237
|
+
projectDir: project.projectDir,
|
|
238
|
+
manifest,
|
|
239
|
+
})
|
|
240
|
+
await applyNativePlugins({
|
|
241
|
+
projectDir: project.projectDir,
|
|
242
|
+
moduleDir: path.join(gradleRoot, 'simpleDemo'),
|
|
243
|
+
libsDir: path.join(gradleRoot, 'simpleDemo/libs'),
|
|
244
|
+
logger,
|
|
245
|
+
})
|
|
246
|
+
await integrateUtsPlugins({
|
|
247
|
+
plugins: utsPlugins,
|
|
248
|
+
gradleRoot,
|
|
249
|
+
mainModule: 'simpleDemo',
|
|
250
|
+
compileSdkVersion,
|
|
251
|
+
kotlinVersion: templateDefaults.kotlinVersion,
|
|
252
|
+
logger,
|
|
253
|
+
})
|
|
254
|
+
logger.success('配置写入完成')
|
|
255
|
+
|
|
256
|
+
logger.step(`Gradle ${debug ? 'assembleDebug' : 'assembleRelease'}`)
|
|
257
|
+
try {
|
|
258
|
+
await runGradle({
|
|
259
|
+
gradlew: sdk.gradlew,
|
|
260
|
+
cwd: gradleRoot,
|
|
261
|
+
task: debug ? 'assembleDebug' : 'assembleRelease',
|
|
262
|
+
javaHome,
|
|
263
|
+
androidSdkDir: config.android.sdkDir,
|
|
264
|
+
offline: Boolean(options.offline),
|
|
265
|
+
daemon: options.noDaemon !== true,
|
|
266
|
+
signing,
|
|
267
|
+
logger,
|
|
268
|
+
})
|
|
269
|
+
} finally {
|
|
270
|
+
await rm(keystoreCopy, { force: true }).catch((error) => {
|
|
271
|
+
logger.warn(`临时签名文件删除失败:${error instanceof Error ? error.message : String(error)}`)
|
|
272
|
+
})
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const apkDir = path.join(gradleRoot, 'simpleDemo/build/outputs/apk', mode)
|
|
276
|
+
const apk = await findApk(apkDir, mode)
|
|
277
|
+
await verifySignature(tools.apksigner, apk, logger)
|
|
278
|
+
|
|
279
|
+
const baseName = apkBaseName(resolveApkPrefix(project, resolved), resolved.versionName, resolved.versionCode)
|
|
280
|
+
const published = await publishApk({
|
|
281
|
+
apk,
|
|
282
|
+
outputDir: path.resolve(String(options.out ?? project.outputDir)),
|
|
283
|
+
baseName,
|
|
284
|
+
mode,
|
|
285
|
+
workDir: workRoot,
|
|
286
|
+
logger,
|
|
287
|
+
prune,
|
|
288
|
+
record: {
|
|
289
|
+
appid: resolved.appid,
|
|
290
|
+
packageName: resolved.packageName,
|
|
291
|
+
versionName: resolved.versionName,
|
|
292
|
+
versionCode: resolved.versionCode,
|
|
293
|
+
projectType: type,
|
|
294
|
+
vueVersion,
|
|
295
|
+
compilerVersion: versions.compiler,
|
|
296
|
+
sdkVersion: versions.sdk,
|
|
297
|
+
hbuilderx: hbuilderx?.root ?? '',
|
|
298
|
+
template: sdk.templateRoot,
|
|
299
|
+
javaHome,
|
|
300
|
+
},
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
logger.success(`APK:${published.target}`)
|
|
304
|
+
logger.info(`最新副本:${published.latest}`)
|
|
305
|
+
logger.info(`产物清单:${published.recordFile}`)
|
|
306
|
+
return {
|
|
307
|
+
apk: published.target,
|
|
308
|
+
latest: published.latest,
|
|
309
|
+
sha256: published.sha256,
|
|
310
|
+
apkRecord: published.recordFile,
|
|
311
|
+
latestLinked: published.latestLinked,
|
|
312
|
+
resourceVersion: resourceVersion.versionName,
|
|
313
|
+
resourceVersionCode: resourceVersion.versionCode,
|
|
314
|
+
resolved,
|
|
315
|
+
workRoot,
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* @param {string} apksigner
|
|
321
|
+
* @param {string} apk
|
|
322
|
+
* @param {import('./logger.js').Logger} logger
|
|
323
|
+
*/
|
|
324
|
+
async function verifySignature(apksigner, apk, logger) {
|
|
325
|
+
try {
|
|
326
|
+
const { run } = await import('./process.js')
|
|
327
|
+
const { stdout } = await run(apksigner, ['verify', '--print-certs', apk], { allowFailure: false })
|
|
328
|
+
const sha1 = /SHA-1 digest:\s*([0-9a-f:]+)/i.exec(stdout)?.[1] ?? ''
|
|
329
|
+
const sha256 = /SHA-256 digest:\s*([0-9a-f:]+)/i.exec(stdout)?.[1] ?? ''
|
|
330
|
+
logger.info(`签名 SHA1:${sha1}`)
|
|
331
|
+
logger.detail(`签名 SHA256:${sha256}`)
|
|
332
|
+
} catch (error) {
|
|
333
|
+
throw new PackError('APK 签名校验失败', EXIT.SIGN, { cause: error })
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* 模板自带的 libs 是该 SDK 版本的基线;BASE_AARS 里新旧版本都有的再补上,
|
|
339
|
+
* 老 SDK 缺少的 AAR 只提示不阻断。
|
|
340
|
+
* @param {Object} options
|
|
341
|
+
* @param {{ templateRoot: string, libsDir: string }} options.sdk
|
|
342
|
+
* @param {string} options.targetDir
|
|
343
|
+
* @param {boolean} options.debug
|
|
344
|
+
* @param {string[]} options.modules
|
|
345
|
+
* @param {boolean} [options.uts]
|
|
346
|
+
* @param {2 | 3} [options.vueVersion]
|
|
347
|
+
* @param {import('./logger.js').Logger} options.logger
|
|
348
|
+
*/
|
|
349
|
+
async function copyAars(options) {
|
|
350
|
+
const { sdk, targetDir, debug, modules, uts, vueVersion, logger } = options
|
|
351
|
+
await ensureDir(targetDir)
|
|
352
|
+
|
|
353
|
+
/** @type {Map<string, string>} */
|
|
354
|
+
const sources = new Map()
|
|
355
|
+
const templateLibs = path.join(sdk.templateRoot, 'simpleDemo/libs')
|
|
356
|
+
for (const name of await listLibs(templateLibs)) {
|
|
357
|
+
if (name === DEBUG_AAR && !debug) {
|
|
358
|
+
continue
|
|
359
|
+
}
|
|
360
|
+
sources.set(name, path.join(templateLibs, name))
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const available = new Set(await listLibs(sdk.libsDir))
|
|
364
|
+
// 模板里的 uniapp-v8-release.aar 可能只带 Vue3 运行时;Vue2 必须用 SDK/libs 的完整版。
|
|
365
|
+
if (vueVersion === 2 && available.has('uniapp-v8-release.aar')) {
|
|
366
|
+
sources.set('uniapp-v8-release.aar', path.join(sdk.libsDir, 'uniapp-v8-release.aar'))
|
|
367
|
+
}
|
|
368
|
+
const missing = []
|
|
369
|
+
for (const name of debug ? [...BASE_AARS, DEBUG_AAR] : BASE_AARS) {
|
|
370
|
+
if (sources.has(name)) {
|
|
371
|
+
continue
|
|
372
|
+
}
|
|
373
|
+
if (available.has(name)) {
|
|
374
|
+
sources.set(name, path.join(sdk.libsDir, name))
|
|
375
|
+
} else if (name !== DEBUG_AAR) {
|
|
376
|
+
missing.push(name)
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (missing.length > 0) {
|
|
380
|
+
logger.warn(`当前 SDK 没有这些 AAR,已跳过:${missing.join(', ')}`)
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** @type {string[]} */
|
|
384
|
+
const unresolved = []
|
|
385
|
+
for (const module of modules) {
|
|
386
|
+
const names = MODULE_AARS[module]
|
|
387
|
+
if (!names) {
|
|
388
|
+
unresolved.push(module)
|
|
389
|
+
continue
|
|
390
|
+
}
|
|
391
|
+
for (const name of names) {
|
|
392
|
+
if (!available.has(name)) {
|
|
393
|
+
continue
|
|
394
|
+
}
|
|
395
|
+
sources.set(name, path.join(sdk.libsDir, name))
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (unresolved.length > 0) {
|
|
399
|
+
logger.warn(`未识别的模块 AAR:${unresolved.join(', ')}(若运行时报模块缺失,请在配置 modules 里补充)`)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (uts) {
|
|
403
|
+
if (!available.has(UTS_AAR)) {
|
|
404
|
+
throw new PackError(`离线 SDK 缺少 ${UTS_AAR},无法编译 UTS 插件`, EXIT.PREFLIGHT, {
|
|
405
|
+
hint: '请换用包含 utsplugin-release.aar 的 Android 离线 SDK',
|
|
406
|
+
})
|
|
407
|
+
}
|
|
408
|
+
sources.set(UTS_AAR, path.join(sdk.libsDir, UTS_AAR))
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
for (const [name, source] of sources) {
|
|
412
|
+
await copyFile(source, path.join(targetDir, name))
|
|
413
|
+
}
|
|
414
|
+
return { count: sources.size }
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* @param {string} dir
|
|
419
|
+
* @returns {Promise<string[]>}
|
|
420
|
+
*/
|
|
421
|
+
async function listLibs(dir) {
|
|
422
|
+
const entries = await readdir(dir, { withFileTypes: true }).catch(() => [])
|
|
423
|
+
return entries.filter((entry) => entry.isFile() && /\.(aar|jar)$/i.test(entry.name)).map((entry) => entry.name)
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* @param {...unknown} values
|
|
428
|
+
* @returns {string}
|
|
429
|
+
*/
|
|
430
|
+
function firstNonEmpty(...values) {
|
|
431
|
+
for (const value of values) {
|
|
432
|
+
const text = String(value ?? '').trim()
|
|
433
|
+
if (text) {
|
|
434
|
+
return text
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return ''
|
|
438
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import os from 'node:os'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { PackError, EXIT } from './errors.js'
|
|
4
|
+
import { exists, readJson, writeJson } from './fsx.js'
|
|
5
|
+
import { detectProject, isUniappProject } from './project.js'
|
|
6
|
+
|
|
7
|
+
export const CONFIG_DIR = path.join(os.homedir(), '.uniapp-android-cli')
|
|
8
|
+
export const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json')
|
|
9
|
+
export const PROJECT_CONFIG_FILE = 'uni-pack.config.json'
|
|
10
|
+
|
|
11
|
+
/** @returns {any} */
|
|
12
|
+
export function defaultConfig() {
|
|
13
|
+
return {
|
|
14
|
+
version: 1,
|
|
15
|
+
sdk: {
|
|
16
|
+
root: '',
|
|
17
|
+
searchDirs: [],
|
|
18
|
+
template: 'HBuilder-Integrate-AS',
|
|
19
|
+
libsDir: 'SDK/libs',
|
|
20
|
+
dataDir: 'SDK/assets/data',
|
|
21
|
+
},
|
|
22
|
+
hbuilderx: {
|
|
23
|
+
root: '',
|
|
24
|
+
searchDirs: [],
|
|
25
|
+
},
|
|
26
|
+
android: {
|
|
27
|
+
sdkDir: '',
|
|
28
|
+
buildTools: '',
|
|
29
|
+
jdk: '',
|
|
30
|
+
},
|
|
31
|
+
workDir: path.join(CONFIG_DIR, 'work'),
|
|
32
|
+
projects: {},
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** @returns {Promise<any>} */
|
|
37
|
+
export async function loadConfig() {
|
|
38
|
+
const base = defaultConfig()
|
|
39
|
+
if (!(await exists(CONFIG_FILE))) {
|
|
40
|
+
return base
|
|
41
|
+
}
|
|
42
|
+
const user = await readJson(CONFIG_FILE)
|
|
43
|
+
return mergeConfig(base, user)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {any} base
|
|
48
|
+
* @param {any} override
|
|
49
|
+
*/
|
|
50
|
+
export function mergeConfig(base, override) {
|
|
51
|
+
return {
|
|
52
|
+
...base,
|
|
53
|
+
...override,
|
|
54
|
+
sdk: { ...base.sdk, ...(override?.sdk ?? {}) },
|
|
55
|
+
hbuilderx: { ...base.hbuilderx, ...(override?.hbuilderx ?? {}) },
|
|
56
|
+
android: { ...base.android, ...(override?.android ?? {}) },
|
|
57
|
+
projects: { ...(base.projects ?? {}), ...(override?.projects ?? {}) },
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @param {any} config */
|
|
62
|
+
export async function saveConfig(config) {
|
|
63
|
+
await writeJson(CONFIG_FILE, config)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param {any} config
|
|
68
|
+
* @param {string} [nameOrPath]
|
|
69
|
+
* @param {string} [cwd]
|
|
70
|
+
*/
|
|
71
|
+
export async function resolveProject(config, nameOrPath, cwd = process.cwd()) {
|
|
72
|
+
const projects = config.projects ?? {}
|
|
73
|
+
const target = nameOrPath ?? cwd
|
|
74
|
+
|
|
75
|
+
if (projects[target]) {
|
|
76
|
+
return finalizeProject(config, target, projects[target])
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const absolute = path.resolve(target)
|
|
80
|
+
const byPath = Object.entries(projects).find(([, entry]) => {
|
|
81
|
+
const dir = path.resolve(String(entry.projectDir ?? ''))
|
|
82
|
+
return dir.toLowerCase() === absolute.toLowerCase()
|
|
83
|
+
})
|
|
84
|
+
if (byPath) {
|
|
85
|
+
return finalizeProject(config, byPath[0], byPath[1])
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 支持用项目名/目录名/唯一子串代替长路径,例如 -p PRO_PHONE 或 -p phone
|
|
89
|
+
const wanted = normalizeProjectName(target)
|
|
90
|
+
if (wanted && !target.includes('/') && !target.includes('\\')) {
|
|
91
|
+
const entries = Object.entries(projects)
|
|
92
|
+
const exact = entries.filter(
|
|
93
|
+
([key, entry]) =>
|
|
94
|
+
normalizeProjectName(key) === wanted || normalizeProjectName(path.basename(String(entry.projectDir ?? ''))) === wanted,
|
|
95
|
+
)
|
|
96
|
+
if (exact.length === 1) {
|
|
97
|
+
return finalizeProject(config, exact[0][0], exact[0][1])
|
|
98
|
+
}
|
|
99
|
+
if (exact.length > 1) {
|
|
100
|
+
throw projectAmbiguous(target, exact)
|
|
101
|
+
}
|
|
102
|
+
const partial = entries.filter(
|
|
103
|
+
([key, entry]) =>
|
|
104
|
+
normalizeProjectName(key).includes(wanted) ||
|
|
105
|
+
normalizeProjectName(path.basename(String(entry.projectDir ?? ''))).includes(wanted),
|
|
106
|
+
)
|
|
107
|
+
if (partial.length === 1) {
|
|
108
|
+
return finalizeProject(config, partial[0][0], partial[0][1])
|
|
109
|
+
}
|
|
110
|
+
if (partial.length > 1) {
|
|
111
|
+
throw projectAmbiguous(target, partial)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (!nameOrPath) {
|
|
116
|
+
if (!(await isUniappProject(absolute))) {
|
|
117
|
+
throw new PackError('当前目录不是 uni-app 项目,请用 --project 指定项目目录或先在配置里登记', EXIT.CONFIG)
|
|
118
|
+
}
|
|
119
|
+
return finalizeProject(config, path.basename(absolute), { projectDir: absolute })
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (!(await isUniappProject(absolute))) {
|
|
123
|
+
throw new PackError(`找不到项目:${nameOrPath}(既不是已登记的项目名,也不是 uni-app 项目目录)`, EXIT.CONFIG, {
|
|
124
|
+
hint: '用 uni-pack projects 查看已登记项目,或 --project 传完整路径',
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
return finalizeProject(config, path.basename(absolute), { projectDir: absolute })
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {string} value
|
|
132
|
+
* @returns {string}
|
|
133
|
+
*/
|
|
134
|
+
function normalizeProjectName(value) {
|
|
135
|
+
return String(value ?? '')
|
|
136
|
+
.toLowerCase()
|
|
137
|
+
.replace(/[^\w]+/g, '-')
|
|
138
|
+
.replace(/^-+|-+$/g, '')
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @param {string} target
|
|
143
|
+
* @param {Array<[string, any]>} entries
|
|
144
|
+
* @returns {PackError}
|
|
145
|
+
*/
|
|
146
|
+
function projectAmbiguous(target, entries) {
|
|
147
|
+
return new PackError(`项目名不唯一:${target}`, EXIT.CONFIG, {
|
|
148
|
+
hint: `候选:${entries.map(([key, entry]) => `${key}(${entry.projectDir})`).join(';')}`,
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* CLI 的 Vue 2 项目通常用 build:app-plus(产物 dist/build/app-plus),
|
|
154
|
+
* Vue 3 项目用 build:app-android(产物 dist/build/app)。
|
|
155
|
+
* @param {string} type
|
|
156
|
+
* @param {string} projectDir
|
|
157
|
+
* @returns {Promise<any>}
|
|
158
|
+
*/
|
|
159
|
+
export async function resolveDefaultWebBuild(type, projectDir) {
|
|
160
|
+
if (type === 'hbuilderx') {
|
|
161
|
+
return { mode: 'hbuilderx', outputDir: 'unpackage/dist/build/app-plus' }
|
|
162
|
+
}
|
|
163
|
+
const packageFile = path.join(projectDir, 'package.json')
|
|
164
|
+
const pkg = (await exists(packageFile)) ? await readJson(packageFile).catch(() => ({})) : {}
|
|
165
|
+
const scripts = pkg.scripts ?? {}
|
|
166
|
+
if (!scripts['build:app-android'] && scripts['build:app-plus']) {
|
|
167
|
+
return { command: 'pnpm', args: ['run', 'build:app-plus'], outputDir: 'dist/build/app-plus' }
|
|
168
|
+
}
|
|
169
|
+
return { command: 'pnpm', args: ['run', 'build:app-android'], outputDir: 'dist/build/app' }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* @param {any} config
|
|
174
|
+
* @param {string} name
|
|
175
|
+
* @param {any} entry
|
|
176
|
+
*/
|
|
177
|
+
async function finalizeProject(config, name, entry) {
|
|
178
|
+
const projectDir = path.resolve(String(entry.projectDir ?? process.cwd()))
|
|
179
|
+
const localFile = path.join(projectDir, PROJECT_CONFIG_FILE)
|
|
180
|
+
const local = (await exists(localFile)) ? await readJson(localFile) : {}
|
|
181
|
+
const layout = await detectProject(projectDir)
|
|
182
|
+
const detectedType = layout.type === 'unknown' ? 'cli' : layout.type
|
|
183
|
+
const type = String(local.type ?? entry.type ?? detectedType)
|
|
184
|
+
|
|
185
|
+
// 输出目录跟着项目走;全局 outputDir 不再参与,避免多项目互相覆盖。
|
|
186
|
+
const outputDir = String(local.outputDir ?? entry.outputDir ?? path.join(projectDir, 'apk'))
|
|
187
|
+
|
|
188
|
+
const defaultWebBuild = await resolveDefaultWebBuild(type, projectDir)
|
|
189
|
+
|
|
190
|
+
const merged = {
|
|
191
|
+
name,
|
|
192
|
+
projectDir,
|
|
193
|
+
inputDir: layout.inputDir,
|
|
194
|
+
...entry,
|
|
195
|
+
...local,
|
|
196
|
+
type,
|
|
197
|
+
outputDir: path.isAbsolute(outputDir) ? outputDir : path.resolve(projectDir, outputDir),
|
|
198
|
+
signing: { ...(entry.signing ?? {}), ...(local.signing ?? {}) },
|
|
199
|
+
debug: { ...(entry.debug ?? {}), ...(local.debug ?? {}) },
|
|
200
|
+
webBuild: {
|
|
201
|
+
...defaultWebBuild,
|
|
202
|
+
...(entry.webBuild ?? {}),
|
|
203
|
+
...(local.webBuild ?? {}),
|
|
204
|
+
},
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return { config, project: merged }
|
|
208
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} DebugPreset
|
|
3
|
+
* @property {'hbuilderx' | 'legacy'} [strategy]
|
|
4
|
+
* @property {'standard' | 'custom'} [playground]
|
|
5
|
+
* @property {'local' | 'device'} [customType]
|
|
6
|
+
* @property {string} [device]
|
|
7
|
+
* @property {boolean} [logs]
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 项目预设只作为默认值;显式调用 HBuilderX 参数优先,基座相关参数强制走 uni-pack 自己的流程。
|
|
12
|
+
* @param {{ hbuilderx?: boolean, playground?: string, customType?: string, push?: boolean, build?: boolean,
|
|
13
|
+
* base?: string, install?: boolean }} options
|
|
14
|
+
* @param {DebugPreset} [preset]
|
|
15
|
+
* @returns {'hbuilderx' | 'legacy'}
|
|
16
|
+
*/
|
|
17
|
+
export function resolveDebugStrategy(options, preset = {}) {
|
|
18
|
+
const forceLegacy = Boolean(options.push || options.build || options.base || options.install)
|
|
19
|
+
const wantsHbuilderx = Boolean(options.hbuilderx || options.playground || options.customType)
|
|
20
|
+
if (wantsHbuilderx) {
|
|
21
|
+
return 'hbuilderx'
|
|
22
|
+
}
|
|
23
|
+
return !forceLegacy && preset.strategy === 'hbuilderx' ? 'hbuilderx' : 'legacy'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {{ playground?: string, customType?: string }} options
|
|
28
|
+
* @param {DebugPreset} [preset]
|
|
29
|
+
* @returns {{ playground: 'standard' | 'custom', customType: 'local' | 'device' }}
|
|
30
|
+
*/
|
|
31
|
+
export function resolveDebugBase(options, preset = {}) {
|
|
32
|
+
const playground = String(options.playground ?? preset.playground ?? 'standard').toLowerCase()
|
|
33
|
+
const customType = String(options.customType ?? preset.customType ?? 'local').toLowerCase()
|
|
34
|
+
return {
|
|
35
|
+
playground: /** @type {'standard' | 'custom'} */ (playground === 'custom' ? 'custom' : 'standard'),
|
|
36
|
+
customType: /** @type {'local' | 'device'} */ (customType === 'device' ? 'device' : 'local'),
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {{ device?: string }} options
|
|
42
|
+
* @param {DebugPreset} [preset]
|
|
43
|
+
* @returns {string}
|
|
44
|
+
*/
|
|
45
|
+
export function resolveDebugDevice(options, preset = {}) {
|
|
46
|
+
return String(options.device ?? preset.device ?? '').trim()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {{ logs?: boolean }} options
|
|
51
|
+
* @param {DebugPreset} [preset]
|
|
52
|
+
* @returns {boolean}
|
|
53
|
+
*/
|
|
54
|
+
export function resolveDebugLogs(options, preset = {}) {
|
|
55
|
+
return Boolean(options.logs ?? preset.logs)
|
|
56
|
+
}
|