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.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +382 -2
  3. package/bin/uni-pack.js +7 -0
  4. package/package.json +66 -4
  5. package/src/cli.js +242 -0
  6. package/src/commands/build.js +46 -0
  7. package/src/commands/debug.js +183 -0
  8. package/src/commands/device.js +309 -0
  9. package/src/commands/devices.js +23 -0
  10. package/src/commands/doctor.js +170 -0
  11. package/src/commands/init.js +488 -0
  12. package/src/commands/projects.js +31 -0
  13. package/src/commands/wgt.js +90 -0
  14. package/src/core/adb.js +292 -0
  15. package/src/core/build.js +438 -0
  16. package/src/core/config.js +208 -0
  17. package/src/core/debugPreset.js +56 -0
  18. package/src/core/devicePairing.js +153 -0
  19. package/src/core/errors.js +36 -0
  20. package/src/core/fsx.js +183 -0
  21. package/src/core/gradle.js +221 -0
  22. package/src/core/gradleLog.js +188 -0
  23. package/src/core/hash.js +18 -0
  24. package/src/core/hbuilderx.js +260 -0
  25. package/src/core/hbuilderxCli.js +168 -0
  26. package/src/core/initConfig.js +214 -0
  27. package/src/core/initEnv.js +140 -0
  28. package/src/core/interactive.js +93 -0
  29. package/src/core/logger.js +82 -0
  30. package/src/core/output.js +181 -0
  31. package/src/core/patch/assets.js +154 -0
  32. package/src/core/patch/gradle.js +166 -0
  33. package/src/core/patch/manifest.js +81 -0
  34. package/src/core/patch/nativePlugins.js +137 -0
  35. package/src/core/patch/projectManifest.js +66 -0
  36. package/src/core/patch/template.js +75 -0
  37. package/src/core/patch/utsPlugins.js +425 -0
  38. package/src/core/process.js +120 -0
  39. package/src/core/project.js +263 -0
  40. package/src/core/qrcode.js +13 -0
  41. package/src/core/resourceVersion.js +87 -0
  42. package/src/core/sdk.js +403 -0
  43. package/src/core/signing.js +102 -0
  44. package/src/core/templateCache.js +81 -0
  45. package/src/core/version.js +128 -0
  46. package/src/core/webBuild.js +77 -0
  47. package/src/core/wgt.js +98 -0
  48. package/src/core/workdir.js +44 -0
@@ -0,0 +1,137 @@
1
+ import path from 'node:path'
2
+ import { copyFile, readdir } from 'node:fs/promises'
3
+ import { PackError, EXIT } from '../errors.js'
4
+ import { ensureDir, exists, readJson, readText, writeText } from '../fsx.js'
5
+
6
+ /**
7
+ * HBuilderX 的 nativeplugins 在离线打包时需要手工集成:AAR 放进 libs,
8
+ * module 类型的插件还要在 dcloud_properties.xml 里注册。
9
+ * @param {Object} options
10
+ * @param {string} options.projectDir
11
+ * @param {string} options.moduleDir simpleDemo 目录
12
+ * @param {string} options.libsDir 构建工程的 simpleDemo/libs
13
+ * @param {import('../logger.js').Logger} options.logger
14
+ */
15
+ export async function applyNativePlugins(options) {
16
+ const sourceDir = path.join(options.projectDir, 'nativeplugins')
17
+ if (!(await exists(sourceDir))) {
18
+ return { plugins: [] }
19
+ }
20
+
21
+ const entries = await readdir(sourceDir, { withFileTypes: true }).catch(() => [])
22
+ /** @type {Array<{ name: string, features: Array<{ name: string, value: string }>, dependencies: string[] }>} */
23
+ const plugins = []
24
+ for (const entry of entries) {
25
+ if (!entry.isDirectory()) {
26
+ continue
27
+ }
28
+ const pluginDir = path.join(sourceDir, entry.name)
29
+ const packageFile = path.join(pluginDir, 'package.json')
30
+ if (!(await exists(packageFile))) {
31
+ options.logger.warn(`原生插件 ${entry.name} 没有 package.json,离线打包需手工集成`)
32
+ continue
33
+ }
34
+ const pkg = await readJson(packageFile).catch(() => ({}))
35
+ const android = pkg?._dp_nativeplugin?.android
36
+ if (!android) {
37
+ options.logger.detail(`原生插件 ${entry.name} 不含 Android 集成信息`)
38
+ continue
39
+ }
40
+
41
+ const copied = await copyPluginLibs(path.join(pluginDir, 'android'), options.libsDir)
42
+ const features = (Array.isArray(android.plugins) ? android.plugins : [])
43
+ .filter((item) => item && item.class)
44
+ .map((item) => ({ name: String(item.name ?? pkg.name ?? entry.name), value: String(item.class) }))
45
+ const dependencies = (Array.isArray(android.dependencies) ? android.dependencies : []).map(String)
46
+ plugins.push({ name: String(pkg.id ?? pkg.name ?? entry.name), features, dependencies })
47
+ options.logger.detail(`原生插件 ${entry.name}:AAR ${copied} 个,feature ${features.length} 个`)
48
+ }
49
+
50
+ if (plugins.length === 0) {
51
+ return { plugins }
52
+ }
53
+
54
+ await registerFeatures(options.moduleDir, plugins, options.logger)
55
+ await injectDependencies(options.moduleDir, plugins, options.logger)
56
+ return { plugins }
57
+ }
58
+
59
+ /**
60
+ * @param {string} androidDir
61
+ * @param {string} libsDir
62
+ * @returns {Promise<number>}
63
+ */
64
+ async function copyPluginLibs(androidDir, libsDir) {
65
+ if (!(await exists(androidDir))) {
66
+ return 0
67
+ }
68
+ const files = await readdir(androidDir, { recursive: true, withFileTypes: true }).catch(() => [])
69
+ let copied = 0
70
+ for (const file of files) {
71
+ if (!file.isFile() || !/\.(aar|jar)$/i.test(file.name)) {
72
+ continue
73
+ }
74
+ const source = path.join(file.parentPath ?? file.path, file.name)
75
+ await ensureDir(libsDir)
76
+ await copyFile(source, path.join(libsDir, file.name))
77
+ copied += 1
78
+ }
79
+ return copied
80
+ }
81
+
82
+ /**
83
+ * @param {string} moduleDir
84
+ * @param {Array<{ features: Array<{ name: string, value: string }> }>} plugins
85
+ * @param {import('../logger.js').Logger} logger
86
+ */
87
+ async function registerFeatures(moduleDir, plugins, logger) {
88
+ const file = path.join(moduleDir, 'src/main/assets/data/dcloud_properties.xml')
89
+ if (!(await exists(file))) {
90
+ logger.warn('找不到 dcloud_properties.xml,原生插件未注册')
91
+ return
92
+ }
93
+ let text = await readText(file)
94
+ const additions = []
95
+ for (const plugin of plugins) {
96
+ for (const feature of plugin.features) {
97
+ if (text.includes(`name="${feature.name}"`)) {
98
+ continue
99
+ }
100
+ additions.push(`\t\t<feature name="${feature.name}" value="${feature.value}"/>`)
101
+ }
102
+ }
103
+ if (additions.length === 0) {
104
+ return
105
+ }
106
+ if (!text.includes('</features>')) {
107
+ throw new PackError('dcloud_properties.xml 结构异常,无法注册原生插件', EXIT.PREFLIGHT)
108
+ }
109
+ text = text.replace('</features>', `${additions.join('\n')}\n\t</features>`)
110
+ await writeText(file, text)
111
+ }
112
+
113
+ /**
114
+ * @param {string} moduleDir
115
+ * @param {Array<{ dependencies: string[] }>} plugins
116
+ * @param {import('../logger.js').Logger} logger
117
+ */
118
+ async function injectDependencies(moduleDir, plugins, logger) {
119
+ const dependencies = [...new Set(plugins.flatMap((plugin) => plugin.dependencies))]
120
+ if (dependencies.length === 0) {
121
+ return
122
+ }
123
+ const file = path.join(moduleDir, 'build.gradle')
124
+ let text = await readText(file)
125
+ const lines = dependencies
126
+ .filter((dependency) => !text.includes(dependency))
127
+ .map((dependency) => ` implementation '${dependency}'`)
128
+ if (lines.length === 0) {
129
+ return
130
+ }
131
+ if (!/dependencies\s*\{/.test(text)) {
132
+ logger.warn('模板 build.gradle 没有 dependencies 块,原生插件依赖未注入')
133
+ return
134
+ }
135
+ text = text.replace(/dependencies\s*\{/, (match) => `${match}\n${lines.join('\n')}`)
136
+ await writeText(file, text)
137
+ }
@@ -0,0 +1,66 @@
1
+ import path from 'node:path'
2
+ import { exists, readText, writeText } from '../fsx.js'
3
+
4
+ /**
5
+ * HBuilderX 项目根的 AndroidManifest.xml 是「原生配置」,云打包时会合并进最终清单。
6
+ * 离线打包至少要把其中的权限与 application 下的 meta-data 带过去(例如 ScopedStorage)。
7
+ * @param {Object} options
8
+ * @param {string} options.targetFile 构建工程的 AndroidManifest.xml
9
+ * @param {string} options.projectDir
10
+ * @param {import('../logger.js').Logger} options.logger
11
+ */
12
+ export async function mergeProjectManifest(options) {
13
+ const source = path.join(options.projectDir, 'AndroidManifest.xml')
14
+ if (!(await exists(source))) {
15
+ return { permissions: 0, metaData: 0 }
16
+ }
17
+
18
+ const sourceText = await readText(source)
19
+ let target = await readText(options.targetFile)
20
+
21
+ const permissions = matchAll(sourceText, /<uses-permission\b[^>]*\/>/g).filter(
22
+ (element) => !target.includes(permissionName(element)),
23
+ )
24
+ const metaData = matchAll(sourceText, /<meta-data\b[\s\S]*?\/>/g).filter(
25
+ (element) => !target.includes(metaDataName(element)),
26
+ )
27
+
28
+ if (permissions.length > 0) {
29
+ target = target.replace(/<application\b/, `${permissions.join('\n ')}\n\n <application`)
30
+ }
31
+ if (metaData.length > 0) {
32
+ target = target.replace(
33
+ /<\/application>/,
34
+ `${metaData.map((element) => ` ${element.trim()}`).join('\n')}\n </application>`,
35
+ )
36
+ }
37
+
38
+ if (permissions.length > 0 || metaData.length > 0) {
39
+ await writeText(options.targetFile, target)
40
+ }
41
+ options.logger.detail(`合并项目 AndroidManifest:权限 ${permissions.length},meta-data ${metaData.length}`)
42
+ return { permissions: permissions.length, metaData: metaData.length }
43
+ }
44
+
45
+ /**
46
+ * @param {string} text
47
+ * @param {RegExp} pattern
48
+ * @returns {string[]}
49
+ */
50
+ function matchAll(text, pattern) {
51
+ return [...text.matchAll(pattern)].map((match) => match[0])
52
+ }
53
+
54
+ /**
55
+ * @param {string} element
56
+ */
57
+ function permissionName(element) {
58
+ return (/android:name="([^"]+)"/.exec(element)?.[1]) ?? element
59
+ }
60
+
61
+ /**
62
+ * @param {string} element
63
+ */
64
+ function metaDataName(element) {
65
+ return (/android:name="([^"]+)"/.exec(element)?.[1]) ?? element
66
+ }
@@ -0,0 +1,75 @@
1
+ import path from 'node:path'
2
+ import { copyFile } from 'node:fs/promises'
3
+ import { copyTree, ensureDir, readText, writeText } from '../fsx.js'
4
+
5
+ const MARKERS = [
6
+ // CORE DEPS 是 SDK 运行时依赖(zip4j、okhttp 等),不是插件注入,必须保留
7
+ /\/\/ ===== SMARTPACK (?!CORE DEPS)[\s\S]*?END =====[^\n]*\n?/g,
8
+ /<!-- ===== SMARTPACK[\s\S]*?END ===== -->\r?\n?/g,
9
+ ]
10
+
11
+ const SKIP = new Set([
12
+ '.gradle',
13
+ '.idea',
14
+ 'build',
15
+ 'local.properties',
16
+ 'simpleDemo/build',
17
+ 'simpleDemo/libs',
18
+ 'simpleDemo/src/main/assets/apps',
19
+ 'simpleDemo/test.jks',
20
+ 'simpleDemo/aw-pack.jks',
21
+ ])
22
+
23
+ // 这些文件每次构建都会按项目改写,增量复用时也必须从干净模板重新覆盖
24
+ const PATCHED_FILES = [
25
+ 'build.gradle',
26
+ 'settings.gradle',
27
+ 'simpleDemo/build.gradle',
28
+ 'simpleDemo/src/main/AndroidManifest.xml',
29
+ ]
30
+
31
+ /**
32
+ * 从 SDK 复制一份干净模板到工作目录(首次构建或 SDK/模板变化时使用)。
33
+ * @param {string} templateRoot
34
+ * @param {string} targetDir
35
+ */
36
+ export async function prepareTemplate(templateRoot, targetDir) {
37
+ await ensureDir(targetDir)
38
+ await copyTree(
39
+ templateRoot,
40
+ targetDir,
41
+ (relative) => SKIP.has(relative) || relative.startsWith('simpleDemo/build/'),
42
+ )
43
+ await refreshTemplateFiles(templateRoot, targetDir)
44
+ return targetDir
45
+ }
46
+
47
+ /**
48
+ * 只把会被 patch 改写的几个文件恢复成模板原文,并清掉上一版插件留下的 SMARTPACK 注入块。
49
+ * 增量构建时用它代替整棵模板复制。
50
+ * @param {string} templateRoot
51
+ * @param {string} targetDir
52
+ */
53
+ export async function refreshTemplateFiles(templateRoot, targetDir) {
54
+ for (const relative of PATCHED_FILES) {
55
+ const source = path.join(templateRoot, relative)
56
+ const target = path.join(targetDir, relative)
57
+ await ensureDir(path.dirname(target))
58
+ await copyFile(source, target)
59
+ await stripMarkerBlocks(target)
60
+ }
61
+ }
62
+
63
+ /**
64
+ * @param {string} file
65
+ */
66
+ export async function stripMarkerBlocks(file) {
67
+ const text = await readText(file)
68
+ let stripped = text
69
+ for (const marker of MARKERS) {
70
+ stripped = stripped.replace(marker, '')
71
+ }
72
+ if (stripped !== text) {
73
+ await writeText(file, stripped)
74
+ }
75
+ }