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,153 @@
|
|
|
1
|
+
import crypto from 'node:crypto'
|
|
2
|
+
import { PackError, EXIT } from './errors.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 手机端把二维码里的 S 当作 mDNS 实例名、P 当作配对码:
|
|
6
|
+
* 设置 → 开发者选项 → 无线调试 → 使用二维码配对设备。
|
|
7
|
+
* 实例名需满足 RFC6763:≤15 字符,字母数字与连字符。
|
|
8
|
+
* @returns {string}
|
|
9
|
+
*/
|
|
10
|
+
export function createServiceName() {
|
|
11
|
+
return `unipack-${crypto.randomBytes(3).toString('hex')}`
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} value
|
|
16
|
+
* @returns {string}
|
|
17
|
+
*/
|
|
18
|
+
export function validateServiceName(value) {
|
|
19
|
+
const text = String(value ?? '').trim()
|
|
20
|
+
if (!text) {
|
|
21
|
+
return createServiceName()
|
|
22
|
+
}
|
|
23
|
+
const valid =
|
|
24
|
+
text.length <= 15 &&
|
|
25
|
+
/^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$/.test(text) &&
|
|
26
|
+
!text.includes('--') &&
|
|
27
|
+
/[A-Za-z]/.test(text)
|
|
28
|
+
if (!valid) {
|
|
29
|
+
throw new PackError(`服务名不合法:${text}`, EXIT.DEVICE, {
|
|
30
|
+
hint: '长度不超过 15,仅字母数字和连字符,首尾为字母或数字,不能有连续连字符,且至少含一个字母',
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
return text
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @returns {string}
|
|
38
|
+
*/
|
|
39
|
+
export function createPairingCode() {
|
|
40
|
+
return String(crypto.randomInt(100000, 1000000))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @param {string} code
|
|
45
|
+
* @returns {string}
|
|
46
|
+
*/
|
|
47
|
+
export function validatePairingCode(code) {
|
|
48
|
+
const text = String(code ?? '').trim()
|
|
49
|
+
if (!text) {
|
|
50
|
+
return createPairingCode()
|
|
51
|
+
}
|
|
52
|
+
if (!/^\d{6}$/.test(text)) {
|
|
53
|
+
throw new PackError(`配对码不合法:${text}`, EXIT.DEVICE, { hint: '配对码为 6 位数字' })
|
|
54
|
+
}
|
|
55
|
+
return text
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param {string} service
|
|
60
|
+
* @param {string} code
|
|
61
|
+
* @returns {string}
|
|
62
|
+
*/
|
|
63
|
+
export function pairingPayload(service, code) {
|
|
64
|
+
return `WIFI:T:ADB;S:${service};P:${code};;`
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @typedef {{ name: string, type: string, host: string, port: number, address: string }} MdnsLike
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 手机扫码后广播的实例名不一定等于二维码里的 S:优先取新出现的配对服务,
|
|
73
|
+
* 名字匹配其次,最后才接受当前唯一的配对服务。
|
|
74
|
+
* @param {MdnsLike[]} services
|
|
75
|
+
* @param {Set<string>} seen 扫码前已存在的地址
|
|
76
|
+
* @param {string} service 二维码里的服务名
|
|
77
|
+
* @returns {MdnsLike | null}
|
|
78
|
+
*/
|
|
79
|
+
export function selectPairingService(services, seen, service) {
|
|
80
|
+
const pairing = services.filter((item) => item.type === 'pairing')
|
|
81
|
+
const fresh = pairing.filter((item) => !seen.has(item.address))
|
|
82
|
+
// 新版 adb 的实例名就是二维码里的 S;老版本会带 ._adb-tls-pairing._tcp 后缀
|
|
83
|
+
const named = fresh.find((item) => item.name === service || item.name.startsWith(`${service}.`))
|
|
84
|
+
if (named) {
|
|
85
|
+
return named
|
|
86
|
+
}
|
|
87
|
+
if (fresh.length === 1) {
|
|
88
|
+
return fresh[0]
|
|
89
|
+
}
|
|
90
|
+
if (pairing.length === 1) {
|
|
91
|
+
return pairing[0]
|
|
92
|
+
}
|
|
93
|
+
return null
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {string} value
|
|
98
|
+
* @returns {string}
|
|
99
|
+
*/
|
|
100
|
+
export function normalizePairingAddress(value) {
|
|
101
|
+
const text = String(value ?? '').trim()
|
|
102
|
+
if (!text) {
|
|
103
|
+
throw new PackError('请输入配对地址', EXIT.DEVICE, { hint: '手机无线调试页显示的 IP:端口,例如 192.168.1.20:37123' })
|
|
104
|
+
}
|
|
105
|
+
if (/^\[[^\]]+\]:\d+$/.test(text) || /^[^:]+:\d+$/.test(text)) {
|
|
106
|
+
return text
|
|
107
|
+
}
|
|
108
|
+
throw new PackError(`配对地址需要带端口:${text}`, EXIT.DEVICE, {
|
|
109
|
+
hint: '使用手机“使用配对码配对设备”弹窗里显示的 IP:端口,不是连接端口 5555',
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @param {string} address
|
|
115
|
+
* @returns {string}
|
|
116
|
+
*/
|
|
117
|
+
export function hostOf(address) {
|
|
118
|
+
const text = String(address ?? '')
|
|
119
|
+
const bracketed = /^\[([^\]]+)\]:\d+$/.exec(text)
|
|
120
|
+
if (bracketed) {
|
|
121
|
+
return bracketed[1]
|
|
122
|
+
}
|
|
123
|
+
return text.replace(/:\d+$/, '')
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @param {string} value
|
|
128
|
+
* @returns {string}
|
|
129
|
+
*/
|
|
130
|
+
export function normalizeAddress(value) {
|
|
131
|
+
const text = String(value ?? '').trim()
|
|
132
|
+
if (!text) {
|
|
133
|
+
throw new PackError('请输入设备地址,例如 192.168.1.20:5555', EXIT.DEVICE)
|
|
134
|
+
}
|
|
135
|
+
if (/^\[[^\]]+\]:\d+$/.test(text)) {
|
|
136
|
+
return text
|
|
137
|
+
}
|
|
138
|
+
if (/^\[[^\]]+\]$/.test(text)) {
|
|
139
|
+
return `${text}:5555`
|
|
140
|
+
}
|
|
141
|
+
const colons = (text.match(/:/g) ?? []).length
|
|
142
|
+
if (colons === 0) {
|
|
143
|
+
return `${text}:5555`
|
|
144
|
+
}
|
|
145
|
+
if (colons === 1) {
|
|
146
|
+
const [, port] = text.split(':')
|
|
147
|
+
if (!/^\d+$/.test(port)) {
|
|
148
|
+
throw new PackError(`端口不合法:${text}`, EXIT.DEVICE, { hint: '格式为 <ip>:<端口>,例如 192.168.1.20:5555' })
|
|
149
|
+
}
|
|
150
|
+
return text
|
|
151
|
+
}
|
|
152
|
+
return `[${text}]:5555`
|
|
153
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export const EXIT = {
|
|
2
|
+
OK: 0,
|
|
3
|
+
CONFIG: 2,
|
|
4
|
+
PREFLIGHT: 3,
|
|
5
|
+
WEB_BUILD: 4,
|
|
6
|
+
GRADLE: 5,
|
|
7
|
+
SIGN: 6,
|
|
8
|
+
DEVICE: 7,
|
|
9
|
+
WGT: 8,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class PackError extends Error {
|
|
13
|
+
/**
|
|
14
|
+
* @param {string} message
|
|
15
|
+
* @param {number} [code]
|
|
16
|
+
* @param {{ hint?: string, cause?: unknown }} [options]
|
|
17
|
+
*/
|
|
18
|
+
constructor(message, code = EXIT.CONFIG, options = {}) {
|
|
19
|
+
super(message, { cause: options.cause })
|
|
20
|
+
this.name = 'PackError'
|
|
21
|
+
this.code = code
|
|
22
|
+
this.hint = options.hint
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {unknown} error
|
|
28
|
+
* @returns {{ code: number, message: string, hint?: string }}
|
|
29
|
+
*/
|
|
30
|
+
export function toFailure(error) {
|
|
31
|
+
if (error instanceof PackError) {
|
|
32
|
+
return { code: error.code, message: error.message, hint: error.hint }
|
|
33
|
+
}
|
|
34
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
35
|
+
return { code: 1, message }
|
|
36
|
+
}
|
package/src/core/fsx.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { cp, link, mkdir, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} dir
|
|
6
|
+
*/
|
|
7
|
+
export async function ensureDir(dir) {
|
|
8
|
+
await mkdir(dir, { recursive: true })
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {string} dir
|
|
13
|
+
*/
|
|
14
|
+
export async function emptyDir(dir) {
|
|
15
|
+
await rm(dir, { recursive: true, force: true })
|
|
16
|
+
await mkdir(dir, { recursive: true })
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} file
|
|
21
|
+
*/
|
|
22
|
+
export async function readJson(file) {
|
|
23
|
+
return JSON.parse(await readFile(file, 'utf8'))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* HBuilderX 的 manifest.json 是 JSONC:带注释、末尾逗号。字符串里的 // 不能被当成注释。
|
|
28
|
+
* @param {string} text
|
|
29
|
+
* @returns {any}
|
|
30
|
+
*/
|
|
31
|
+
export function parseJsonc(text) {
|
|
32
|
+
const source = text.replace(/^/, '')
|
|
33
|
+
let output = ''
|
|
34
|
+
let inString = false
|
|
35
|
+
let escaped = false
|
|
36
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
37
|
+
const char = source[index]
|
|
38
|
+
const next = source[index + 1]
|
|
39
|
+
if (inString) {
|
|
40
|
+
output += char
|
|
41
|
+
if (escaped) {
|
|
42
|
+
escaped = false
|
|
43
|
+
} else if (char === '\\') {
|
|
44
|
+
escaped = true
|
|
45
|
+
} else if (char === '"') {
|
|
46
|
+
inString = false
|
|
47
|
+
}
|
|
48
|
+
continue
|
|
49
|
+
}
|
|
50
|
+
if (char === '"') {
|
|
51
|
+
inString = true
|
|
52
|
+
output += char
|
|
53
|
+
continue
|
|
54
|
+
}
|
|
55
|
+
if (char === '/' && next === '/') {
|
|
56
|
+
while (index < source.length && source[index] !== '\n') {
|
|
57
|
+
index += 1
|
|
58
|
+
}
|
|
59
|
+
output += '\n'
|
|
60
|
+
continue
|
|
61
|
+
}
|
|
62
|
+
if (char === '/' && next === '*') {
|
|
63
|
+
index += 2
|
|
64
|
+
while (index < source.length && !(source[index] === '*' && source[index + 1] === '/')) {
|
|
65
|
+
index += 1
|
|
66
|
+
}
|
|
67
|
+
index += 1
|
|
68
|
+
continue
|
|
69
|
+
}
|
|
70
|
+
if (char === ',') {
|
|
71
|
+
let lookahead = index + 1
|
|
72
|
+
while (lookahead < source.length) {
|
|
73
|
+
if (/\s/.test(source[lookahead])) {
|
|
74
|
+
lookahead += 1
|
|
75
|
+
continue
|
|
76
|
+
}
|
|
77
|
+
if (source[lookahead] === '/' && source[lookahead + 1] === '/') {
|
|
78
|
+
lookahead += 2
|
|
79
|
+
while (lookahead < source.length && source[lookahead] !== '\n') {
|
|
80
|
+
lookahead += 1
|
|
81
|
+
}
|
|
82
|
+
continue
|
|
83
|
+
}
|
|
84
|
+
if (source[lookahead] === '/' && source[lookahead + 1] === '*') {
|
|
85
|
+
lookahead += 2
|
|
86
|
+
while (lookahead < source.length && !(source[lookahead] === '*' && source[lookahead + 1] === '/')) {
|
|
87
|
+
lookahead += 1
|
|
88
|
+
}
|
|
89
|
+
lookahead += 2
|
|
90
|
+
continue
|
|
91
|
+
}
|
|
92
|
+
break
|
|
93
|
+
}
|
|
94
|
+
if (source[lookahead] === '}' || source[lookahead] === ']') {
|
|
95
|
+
continue
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
output += char
|
|
99
|
+
}
|
|
100
|
+
return JSON.parse(output)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @param {string} file
|
|
105
|
+
* @returns {Promise<any>}
|
|
106
|
+
*/
|
|
107
|
+
export async function readJsonc(file) {
|
|
108
|
+
return parseJsonc(await readFile(file, 'utf8'))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @param {string} file
|
|
113
|
+
* @param {unknown} value
|
|
114
|
+
*/
|
|
115
|
+
export async function writeJson(file, value) {
|
|
116
|
+
await ensureDir(path.dirname(file))
|
|
117
|
+
await writeFile(file, `${JSON.stringify(value, null, 2)}\n`, 'utf8')
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @param {string} file
|
|
122
|
+
*/
|
|
123
|
+
export async function exists(file) {
|
|
124
|
+
try {
|
|
125
|
+
await stat(file)
|
|
126
|
+
return true
|
|
127
|
+
} catch {
|
|
128
|
+
return false
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 递归复制,skip 返回 true 的相对路径会被跳过。
|
|
134
|
+
* @param {string} from
|
|
135
|
+
* @param {string} to
|
|
136
|
+
* @param {(relativePath: string, isDirectory: boolean) => boolean} [skip]
|
|
137
|
+
*/
|
|
138
|
+
export async function copyTree(from, to, skip) {
|
|
139
|
+
await cp(from, to, {
|
|
140
|
+
recursive: true,
|
|
141
|
+
filter: (source) => {
|
|
142
|
+
const relative = path.relative(from, source).split(path.sep).join('/')
|
|
143
|
+
if (!relative) {
|
|
144
|
+
return true
|
|
145
|
+
}
|
|
146
|
+
return !(skip?.(relative, false) ?? false)
|
|
147
|
+
},
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 用硬链接把 target 指向 source:latest 副本零复制,且 source 被覆盖时不会影响已发布文件。
|
|
153
|
+
* @param {string} source
|
|
154
|
+
* @param {string} target
|
|
155
|
+
*/
|
|
156
|
+
export async function replaceHardLink(source, target) {
|
|
157
|
+
await ensureDir(path.dirname(target))
|
|
158
|
+
await rm(target, { force: true })
|
|
159
|
+
await link(source, target)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @param {string} dir
|
|
164
|
+
*/
|
|
165
|
+
export async function listFiles(dir) {
|
|
166
|
+
return readdir(dir, { withFileTypes: true })
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @param {string} file
|
|
171
|
+
*/
|
|
172
|
+
export async function readText(file) {
|
|
173
|
+
return readFile(file, 'utf8')
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @param {string} file
|
|
178
|
+
* @param {string} text
|
|
179
|
+
*/
|
|
180
|
+
export async function writeText(file, text) {
|
|
181
|
+
await ensureDir(path.dirname(file))
|
|
182
|
+
await writeFile(file, text, 'utf8')
|
|
183
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { createWriteStream } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { PackError, EXIT } from './errors.js'
|
|
4
|
+
import { ensureDir, readJson, writeJson } from './fsx.js'
|
|
5
|
+
import { GradleLogAggregator, formatFailureHint } from './gradleLog.js'
|
|
6
|
+
import { run } from './process.js'
|
|
7
|
+
|
|
8
|
+
/** 配置缓存兼容性记录在工作目录根部,不兼容一次后不再重试。 */
|
|
9
|
+
export const GRADLE_FLAGS_FILE = 'gradle-flags.json'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {Object} RunGradleOptions
|
|
13
|
+
* @property {string} gradlew
|
|
14
|
+
* @property {string} cwd
|
|
15
|
+
* @property {'assembleRelease' | 'assembleDebug'} task
|
|
16
|
+
* @property {string} javaHome
|
|
17
|
+
* @property {string} androidSdkDir
|
|
18
|
+
* @property {boolean} [offline]
|
|
19
|
+
* @property {boolean} [daemon] 默认 true;CI 可用 --no-daemon 关闭
|
|
20
|
+
* @property {boolean} [buildCache] 默认 true
|
|
21
|
+
* @property {boolean} [configurationCache] 默认自动尝试
|
|
22
|
+
* @property {{ storePassword: string, keyPassword: string }} [signing] 只通过环境变量传给 Gradle,不落盘
|
|
23
|
+
* @property {import('./logger.js').Logger} logger
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {{ task: 'assembleRelease' | 'assembleDebug', offline?: boolean, daemon?: boolean,
|
|
28
|
+
* buildCache?: boolean, configurationCache?: boolean }} options
|
|
29
|
+
* @returns {string[]}
|
|
30
|
+
*/
|
|
31
|
+
export function buildGradleArgs(options) {
|
|
32
|
+
const args = [`:simpleDemo:${options.task}`, '--console=plain']
|
|
33
|
+
if (options.daemon === false) {
|
|
34
|
+
args.push('--no-daemon')
|
|
35
|
+
}
|
|
36
|
+
if (options.buildCache !== false) {
|
|
37
|
+
args.push('--build-cache')
|
|
38
|
+
}
|
|
39
|
+
args.push('--parallel')
|
|
40
|
+
if (options.offline) {
|
|
41
|
+
args.push('--offline')
|
|
42
|
+
}
|
|
43
|
+
if (options.configurationCache) {
|
|
44
|
+
args.push('--configuration-cache')
|
|
45
|
+
}
|
|
46
|
+
return args
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Gradle 8 / AGP 版本组合不总支持配置缓存;命中这些文案时回退普通构建。
|
|
51
|
+
* @param {string} output
|
|
52
|
+
* @returns {boolean}
|
|
53
|
+
*/
|
|
54
|
+
export function isConfigurationCacheFailure(output) {
|
|
55
|
+
if (!output) {
|
|
56
|
+
return false
|
|
57
|
+
}
|
|
58
|
+
return new RegExp(
|
|
59
|
+
[
|
|
60
|
+
'configuration cache problems found',
|
|
61
|
+
'not compatible with the configuration cache',
|
|
62
|
+
'problems were found storing the configuration cache',
|
|
63
|
+
'Configuration cache state could not be cached',
|
|
64
|
+
'Configuration cache entry discarded with',
|
|
65
|
+
'Invocation of Task\\.project',
|
|
66
|
+
].join('|'),
|
|
67
|
+
'i',
|
|
68
|
+
).test(output)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param {Pick<RunGradleOptions, 'javaHome' | 'androidSdkDir' | 'signing'>} options
|
|
73
|
+
* @returns {Record<string, string>}
|
|
74
|
+
*/
|
|
75
|
+
export function buildGradleEnv(options) {
|
|
76
|
+
return {
|
|
77
|
+
JAVA_HOME: options.javaHome,
|
|
78
|
+
ANDROID_HOME: options.androidSdkDir,
|
|
79
|
+
ANDROID_SDK_ROOT: options.androidSdkDir,
|
|
80
|
+
...(options.signing
|
|
81
|
+
? {
|
|
82
|
+
UNI_PACK_STORE_PASS: options.signing.storePassword,
|
|
83
|
+
UNI_PACK_KEY_PASS: options.signing.keyPassword,
|
|
84
|
+
}
|
|
85
|
+
: {}),
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @typedef {Object} GradleAttempt
|
|
91
|
+
* @property {string[]} lines
|
|
92
|
+
* @property {Array<{ label: string, count: number, sample: string[] }>} groups
|
|
93
|
+
* @property {unknown} failure
|
|
94
|
+
* @property {Error | null} logWriteError
|
|
95
|
+
* @property {string} logFile
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @param {RunGradleOptions & { args: string[], logFile: string }} options
|
|
100
|
+
* @returns {Promise<GradleAttempt>}
|
|
101
|
+
*/
|
|
102
|
+
async function executeGradleAttempt(options) {
|
|
103
|
+
const verbose = Boolean(options.logger.verbose)
|
|
104
|
+
const lines = []
|
|
105
|
+
|
|
106
|
+
const stream = createWriteStream(options.logFile)
|
|
107
|
+
/** @type {{ error: Error | null }} */
|
|
108
|
+
const logWrite = { error: null }
|
|
109
|
+
stream.on('error', (error) => {
|
|
110
|
+
logWrite.error = error
|
|
111
|
+
})
|
|
112
|
+
const aggregator = verbose ? null : new GradleLogAggregator({ onLine: (line) => stream.write(`${line}\n`) })
|
|
113
|
+
|
|
114
|
+
/** @type {unknown} */
|
|
115
|
+
let failure = null
|
|
116
|
+
try {
|
|
117
|
+
await run(options.gradlew, options.args, {
|
|
118
|
+
cwd: options.cwd,
|
|
119
|
+
env: buildGradleEnv(options),
|
|
120
|
+
// 非 verbose 时只写聚合后的日志;verbose 时原样流式落盘
|
|
121
|
+
onChunk: verbose ? (chunk) => stream.write(chunk) : undefined,
|
|
122
|
+
onLine: (line) => {
|
|
123
|
+
lines.push(line)
|
|
124
|
+
if (verbose) {
|
|
125
|
+
options.logger.detail(line)
|
|
126
|
+
} else {
|
|
127
|
+
aggregator?.push(line)
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
})
|
|
131
|
+
} catch (error) {
|
|
132
|
+
failure = error
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const groups = aggregator ? aggregator.finish() : []
|
|
136
|
+
await closeStream(stream)
|
|
137
|
+
return { lines, groups, failure, logWriteError: logWrite.error, logFile: options.logFile }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @param {RunGradleOptions} options
|
|
142
|
+
*/
|
|
143
|
+
export async function runGradle(options) {
|
|
144
|
+
const workRoot = path.dirname(options.cwd)
|
|
145
|
+
const logDir = path.join(workRoot, 'logs')
|
|
146
|
+
await ensureDir(logDir)
|
|
147
|
+
|
|
148
|
+
const flagsFile = path.join(workRoot, GRADLE_FLAGS_FILE)
|
|
149
|
+
const flags = await readJson(flagsFile).catch(() => ({}))
|
|
150
|
+
const tryConfigurationCache = options.configurationCache !== false && flags.configurationCache !== false
|
|
151
|
+
|
|
152
|
+
const baseArgs = buildGradleArgs({
|
|
153
|
+
task: options.task,
|
|
154
|
+
offline: options.offline,
|
|
155
|
+
daemon: options.daemon,
|
|
156
|
+
buildCache: options.buildCache,
|
|
157
|
+
configurationCache: false,
|
|
158
|
+
})
|
|
159
|
+
const stamp = Date.now()
|
|
160
|
+
let attempt = await executeGradleAttempt({
|
|
161
|
+
...options,
|
|
162
|
+
args: tryConfigurationCache ? [...baseArgs, '--configuration-cache'] : baseArgs,
|
|
163
|
+
logFile: path.join(logDir, `gradle-${stamp}.log`),
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
if (attempt.failure && tryConfigurationCache && isConfigurationCacheFailure(attempt.lines.join('\n'))) {
|
|
167
|
+
options.logger.warn('Gradle 配置缓存与该工程不兼容,已回退为普通构建(后续构建不再尝试)')
|
|
168
|
+
await writeJson(flagsFile, { ...flags, configurationCache: false })
|
|
169
|
+
attempt = await executeGradleAttempt({
|
|
170
|
+
...options,
|
|
171
|
+
args: baseArgs,
|
|
172
|
+
logFile: path.join(logDir, `gradle-${stamp}-no-cc.log`),
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (attempt.logWriteError) {
|
|
177
|
+
options.logger.warn(`Gradle 日志写入失败:${attempt.logWriteError.message}`)
|
|
178
|
+
}
|
|
179
|
+
if (!options.logger.verbose) {
|
|
180
|
+
printWarningSummary(attempt.groups, options.logger)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (attempt.failure) {
|
|
184
|
+
throw new PackError('Gradle 构建失败', EXIT.GRADLE, {
|
|
185
|
+
hint: formatFailureHint({ lines: attempt.lines, logFile: attempt.logFile }),
|
|
186
|
+
cause: attempt.failure,
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
options.logger.success(`Gradle ${options.task} 完成`)
|
|
191
|
+
options.logger.info(`完整日志:${attempt.logFile}`)
|
|
192
|
+
return attempt.logFile
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @param {Array<{ label: string, count: number, sample: string[] }>} groups
|
|
197
|
+
* @param {import('./logger.js').Logger} logger
|
|
198
|
+
*/
|
|
199
|
+
function printWarningSummary(groups, logger) {
|
|
200
|
+
if (groups.length === 0) {
|
|
201
|
+
return
|
|
202
|
+
}
|
|
203
|
+
const total = groups.reduce((sum, group) => sum + group.count, 0)
|
|
204
|
+
// 一次 warn 输出整段摘要:warn 走 stderr、info 走 stdout,分开输出会被缓冲打乱顺序
|
|
205
|
+
const lines = [`Gradle 告警:${total} 条同类告警已聚合为 ${groups.length} 类(看原始栈请用 --verbose 重跑)`]
|
|
206
|
+
for (const group of groups) {
|
|
207
|
+
lines.push(` ${group.label} ×${group.count}`)
|
|
208
|
+
if (group.sample[0]) {
|
|
209
|
+
lines.push(` 样例:${group.sample[0]}`)
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
logger.warn(lines.join('\n'))
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @param {import('node:fs').WriteStream} stream
|
|
217
|
+
* @returns {Promise<void>}
|
|
218
|
+
*/
|
|
219
|
+
function closeStream(stream) {
|
|
220
|
+
return new Promise((resolve) => stream.end(resolve))
|
|
221
|
+
}
|