rcompiler 0.0.0-stage → 2.2.0
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/CHANGELOG.md +31 -0
- package/CONTRIBUTING.md +38 -0
- package/LICENSE +15 -0
- package/README.md +170 -2
- package/SECURITY.md +27 -0
- package/assets/placeholder.png +0 -0
- package/docs/ARCHITECTURE.md +74 -0
- package/docs/CLI.md +67 -0
- package/docs/COMPATIBILITY.md +29 -0
- package/docs/CONFIG.md +73 -0
- package/docs/DEBUGGING.md +46 -0
- package/docs/NATIVE.md +110 -0
- package/docs/ROADMAP.md +12 -0
- package/docs/TESTING.md +55 -0
- package/exts/args.js +142 -0
- package/exts/freya.js +430 -0
- package/exts/fs.js +317 -0
- package/exts/io.js +396 -0
- package/exts/path.js +208 -0
- package/exts/process.js +416 -0
- package/exts/rcompilerExt.js +1396 -0
- package/exts/vartypeany.js +131 -0
- package/package.json +50 -4
- package/src/analysis.js +571 -0
- package/src/assets.js +335 -0
- package/src/cli.js +359 -0
- package/src/emit.js +353 -0
- package/src/mods.js +146 -0
- package/src/preludes.js +4174 -0
- package/src/rcompiler.js +2785 -0
- package/src/standard.js +1452 -0
- package/src/targets.js +129 -0
- package/src/web.js +1593 -0
package/src/cli.js
ADDED
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// rCompiler V2 — CLI
|
|
3
|
+
// 用法:
|
|
4
|
+
// rcompiler <path.sb3> 编译并运行(release;--dev 改用 debug 快速调试)
|
|
5
|
+
// rcompiler t <path.sb3> 仅生成代码(打印,--out 可写到目录)
|
|
6
|
+
// rcompiler b <path> <to> <name> <ver> 仅生成项目
|
|
7
|
+
// rcompiler o <path> <to> <name> <ver> 编译为可执行文件(release)
|
|
8
|
+
// 选项:--profile 打印 Profile;--name/--ver;--out
|
|
9
|
+
|
|
10
|
+
'use strict';
|
|
11
|
+
const yargs = require('yargs/yargs');
|
|
12
|
+
const { hideBin } = require('yargs/helpers');
|
|
13
|
+
const fs = require('fs');
|
|
14
|
+
const path = require('path');
|
|
15
|
+
const { spawn } = require('child_process');
|
|
16
|
+
const os = require('os');
|
|
17
|
+
const { rcompiler, readProject, readZipBuffer } = require('./rcompiler');
|
|
18
|
+
const { resolveTarget, artifactPath, describeTarget } = require('./targets');
|
|
19
|
+
const pkg = require('../package.json');
|
|
20
|
+
|
|
21
|
+
let hadError = false;
|
|
22
|
+
|
|
23
|
+
// Cargo 包名/二进制名只允许 字母数字_-;空格点号等(如 "My Project 1.0")替换为 _
|
|
24
|
+
function cargoSafeName(s) {
|
|
25
|
+
const out = String(s || '').replace(/[^A-Za-z0-9_-]+/g, '_').replace(/^_+|_+$/g, '');
|
|
26
|
+
return out || 'rcompiler_app';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Skia 预编译二进制本地缓存(file:// 方案):
|
|
30
|
+
// 每台设备按平台/架构一次性下载官方 skia-binaries-<key>.tar.gz 到 ~/.cache/skia-binaries/,
|
|
31
|
+
// 之后所有舞台项目构建均从本地复用,不再访问 GitHub;{key} 由 skia-bindings 构建脚本自动替换。
|
|
32
|
+
function skiaCacheUrl() {
|
|
33
|
+
return 'file://' + path.join(os.homedir(), '.cache', 'skia-binaries', 'skia-binaries-{key}.tar.gz');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 应用图标:面板存 dataURL;CLI 读文件(仅 PNG,运行时用 image crate 解码)
|
|
37
|
+
function iconDataUrl(file) {
|
|
38
|
+
if (!file) return null;
|
|
39
|
+
const ext = path.extname(file).toLowerCase();
|
|
40
|
+
if (ext !== '.png') throw new Error('应用图标目前仅支持 PNG:' + file);
|
|
41
|
+
const buf = fs.readFileSync(file);
|
|
42
|
+
return `data:image/png;base64,${buf.toString('base64')}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// CLI 打包选项 → 与 extensionStorage 同构的对象(仅传显式给出的项)
|
|
46
|
+
function appOptions(argv) {
|
|
47
|
+
const app = {};
|
|
48
|
+
if (argv.title !== undefined) app.Title = String(argv.title);
|
|
49
|
+
if (argv.icon !== undefined) app.Icon = iconDataUrl(String(argv.icon));
|
|
50
|
+
if (argv.resizable !== undefined) app.Resizable = argv.resizable ? 'on' : 'off';
|
|
51
|
+
if (argv.fullscreen !== undefined) app.Fullscreen = argv.fullscreen ? 'on' : 'off';
|
|
52
|
+
if (argv.decorations !== undefined) app.Decorations = argv.decorations ? 'on' : 'off';
|
|
53
|
+
if (argv.hideCursor !== undefined) app.HideCursor = argv.hideCursor ? 'on' : 'off';
|
|
54
|
+
if (argv.controls !== undefined) app.Controls = argv.controls ? 'on' : 'off';
|
|
55
|
+
if (argv.autoplay !== undefined) app.Autoplay = argv.autoplay ? 'on' : 'off';
|
|
56
|
+
if (argv.exitKey !== undefined) app.ExitKey = String(argv.exitKey);
|
|
57
|
+
if (argv.quitOnStop !== undefined) app.QuitOnStop = argv.quitOnStop ? 'on' : 'off';
|
|
58
|
+
if (argv.cloneLimit !== undefined) app.CloneLimit = Number(argv.cloneLimit);
|
|
59
|
+
if (argv.fencing !== undefined) app.fencing = argv.fencing;
|
|
60
|
+
if (argv.bg !== undefined) app.Bg = String(argv.bg);
|
|
61
|
+
if (argv.stageWidth !== undefined) app.StageWidth = Number(argv.stageWidth);
|
|
62
|
+
if (argv.stageHeight !== undefined) app.StageHeight = Number(argv.stageHeight);
|
|
63
|
+
return app;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 调试模式:把 rustc 的 src/main.rs:行:列 映射回原项目角色/积木
|
|
67
|
+
// rustc 输出形如「error[E0308]: ...\n --> src/main.rs:1788:30」;
|
|
68
|
+
// 标记行(//^RC:)已被替换为注释,rustc 报的是其后代码行,取"不晚于错误行"的最近块标记。
|
|
69
|
+
// 纯函数(返回命中列表),printMappedErrors 负责打印;单测见 test/errmap.js
|
|
70
|
+
function mapErrors(output, map) {
|
|
71
|
+
if (!map || !Array.isArray(map.lines) || map.lines.length === 0) return [];
|
|
72
|
+
const sorted = map.lines.slice().sort((a, b) => a.line - b.line);
|
|
73
|
+
const targets = map.targets || [];
|
|
74
|
+
const seen = new Set();
|
|
75
|
+
const hits = [];
|
|
76
|
+
const reLoc = /(?:^|\s)-->\s+(?:\.\/)?src[\\/](?:main|lib)\.rs:(\d+):(\d+)/;
|
|
77
|
+
const reLegacy = /^(?:\.\/)?src[\\/](?:main|lib)\.rs:(\d+):(\d+):\s*(.*)$/;
|
|
78
|
+
let msg = '';
|
|
79
|
+
const mapHit = (errLine, message) => {
|
|
80
|
+
let lo = 0, hi = sorted.length - 1, e = null;
|
|
81
|
+
while (lo <= hi) {
|
|
82
|
+
const mid = (lo + hi) >> 1;
|
|
83
|
+
if (sorted[mid].line <= errLine) { e = sorted[mid]; lo = mid + 1; }
|
|
84
|
+
else hi = mid - 1;
|
|
85
|
+
}
|
|
86
|
+
if (!e) return false;
|
|
87
|
+
const key = errLine + ':' + e.t + ':' + e.b + ':' + message;
|
|
88
|
+
if (seen.has(key)) return true;
|
|
89
|
+
seen.add(key);
|
|
90
|
+
const tname = targets[e.t] != null ? targets[e.t] : ('#' + e.t);
|
|
91
|
+
const pos = (e.x != null && e.y != null) ? `,编辑器坐标 (${e.x}, ${e.y})` : '';
|
|
92
|
+
hits.push(` ${message || '编译错误'}\n 位置: 角色「${tname}」 → 积木 ${e.op}${e.b ? `(块 ${e.b})` : ''}${pos}(main.rs:${errLine})`);
|
|
93
|
+
return true;
|
|
94
|
+
};
|
|
95
|
+
for (const line of String(output).split('\n')) {
|
|
96
|
+
const legacy = line.match(reLegacy);
|
|
97
|
+
if (legacy) { mapHit(Number(legacy[1]), legacy[3] || '编译错误'); continue; }
|
|
98
|
+
const loc = line.match(reLoc);
|
|
99
|
+
if (!loc) {
|
|
100
|
+
// rustc 的诊断分组:warning:/error[...]: 行后跟 --> 位置行;仅映射 error(warning 是运行时噪音)
|
|
101
|
+
if (/^\s*error/.test(line)) msg = line.trim().slice(0, 140);
|
|
102
|
+
else if (/^\s*warning:/.test(line)) msg = '';
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (msg) mapHit(Number(loc[1]), msg);
|
|
106
|
+
}
|
|
107
|
+
return hits;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function printMappedErrors(output, map) {
|
|
111
|
+
const hits = mapErrors(output, map);
|
|
112
|
+
if (hits.length) {
|
|
113
|
+
console.error('\n\x1b[31m———— 编译错误定位(调试模式)————\x1b[m');
|
|
114
|
+
hits.forEach(h => console.error(h));
|
|
115
|
+
console.error('');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function cargoBuild(projectDir, release, then, opts = {}) {
|
|
120
|
+
const target = opts.target || null;
|
|
121
|
+
const args = release ? ['build', '--release'] : ['build'];
|
|
122
|
+
if (target && target.explicit) args.push('--target', target.triple);
|
|
123
|
+
const map = opts.map || null;
|
|
124
|
+
// 预检:项目带 skia(舞台)而本地缓存为空时直接失败——
|
|
125
|
+
// 否则 skia-bindings 下载失败会静默回退到源码编译,转而从 GitHub 拉取源码并无限挂起
|
|
126
|
+
try {
|
|
127
|
+
if (process.env.RCOMPILER_SKIA_REMOTE !== '1' && !process.env.SKIA_BINARIES_URL) {
|
|
128
|
+
const manifest = fs.readFileSync(path.join(projectDir, 'Cargo.toml'), 'utf8');
|
|
129
|
+
if (manifest.includes('skia-safe')) {
|
|
130
|
+
const cacheDir = path.join(os.homedir(), '.cache', 'skia-binaries');
|
|
131
|
+
const cached = fs.existsSync(cacheDir) &&
|
|
132
|
+
fs.readdirSync(cacheDir).some(f => /^skia-binaries-.+\.tar\.gz$/.test(f));
|
|
133
|
+
if (!cached) {
|
|
134
|
+
console.error([
|
|
135
|
+
'\x1b[31m缺少 Skia 预编译二进制本地缓存\x1b[m',
|
|
136
|
+
'舞台项目依赖 skia-bindings 预编译包,请按当前平台一次性下载(保持 .tar.gz 原样,勿解压)并放入:',
|
|
137
|
+
` ${cacheDir}`,
|
|
138
|
+
'',
|
|
139
|
+
'下载地址(tag = skia-bindings 版本,当前 0.75.0):',
|
|
140
|
+
' https://github.com/rust-skia/skia-binaries/releases/download/0.75.0/skia-binaries-<key>.tar.gz',
|
|
141
|
+
' <key> 组成:仓库hash-目标三元组-feature组合,常见如:',
|
|
142
|
+
' Linux x64 skia-binaries-e7e538647512cab844c3-x86_64-unknown-linux-gnu-gl-svg-textlayout-x11.tar.gz',
|
|
143
|
+
' macOS skia-binaries-e7e538647512cab844c3-<x86_64|aarch64>-apple-darwin-gl-svg-textlayout.tar.gz',
|
|
144
|
+
' 精确 <key> 可在有网环境执行一次 cargo build,从输出 “FROM:” 行复制;或到 releases 页按平台选择。',
|
|
145
|
+
'',
|
|
146
|
+
'如有可用代理想直接联网下载:export RCOMPILER_SKIA_REMOTE=1 后重试。'
|
|
147
|
+
].join('\n'));
|
|
148
|
+
then(1);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
} catch (_) { /* Cargo.toml 读取失败时交给 cargo 自行报错 */ }
|
|
154
|
+
const env = Object.assign({}, process.env);
|
|
155
|
+
if (!env.SKIA_BINARIES_URL) env.SKIA_BINARIES_URL = skiaCacheUrl();
|
|
156
|
+
if (!map) {
|
|
157
|
+
const child = spawn('cargo', args, { cwd: projectDir, stdio: 'inherit', env });
|
|
158
|
+
child.on('close', (code) => then(code));
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
// 调试模式:收集输出(保留 rustc 原文),失败时追加块级定位
|
|
162
|
+
const child = spawn('cargo', args, { cwd: projectDir, stdio: ['inherit', 'pipe', 'pipe'], env });
|
|
163
|
+
let out = '';
|
|
164
|
+
child.stdout.on('data', (d) => { out += d.toString(); });
|
|
165
|
+
child.stderr.on('data', (d) => { out += d.toString(); });
|
|
166
|
+
child.on('close', (code) => {
|
|
167
|
+
process.stderr.write(out);
|
|
168
|
+
if (code !== 0) printMappedErrors(out, map);
|
|
169
|
+
then(code);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async function compileProject(argv, mode) {
|
|
174
|
+
const zipFilePath = argv.path;
|
|
175
|
+
try {
|
|
176
|
+
const jsonData = await readProject(zipFilePath);
|
|
177
|
+
const zipBuffer = await readZipBuffer(zipFilePath);
|
|
178
|
+
const pathto = path.resolve(argv.to);
|
|
179
|
+
|
|
180
|
+
if (mode === 'c' && fs.existsSync(pathto)) {
|
|
181
|
+
fs.rmSync(pathto, { recursive: true, force: true });
|
|
182
|
+
console.log('\x1b[32m正在构建项目\x1b[m');
|
|
183
|
+
}
|
|
184
|
+
fs.mkdirSync(path.join(pathto, 'src'), { recursive: true });
|
|
185
|
+
|
|
186
|
+
const compiler = new rcompiler();
|
|
187
|
+
const name = cargoSafeName(argv.name || path.parse(zipFilePath).name);
|
|
188
|
+
const ver = argv.ver || '1.0.0';
|
|
189
|
+
const target = resolveTarget(argv.target);
|
|
190
|
+
// 帧参数(位于项目配置之后、默认值之前;仅当给出时生效)
|
|
191
|
+
const frame = {};
|
|
192
|
+
if (argv.fps != null) frame.fps = Number(argv.fps);
|
|
193
|
+
if (argv['render-fps'] != null) frame.renderFps = Number(argv['render-fps']);
|
|
194
|
+
if (argv.interp === true) frame.interp = true;
|
|
195
|
+
if (argv['no-interp'] === true) frame.interp = false;
|
|
196
|
+
if (argv.turbo === true) frame.turbo = true;
|
|
197
|
+
const { deplist, profile, cargo, map } = await compiler.compile(jsonData, path.join(pathto, 'src'), name, {
|
|
198
|
+
log: mode === 't' && !argv.quiet,
|
|
199
|
+
write: true, name, ver, zipBuffer, outDir: pathto,
|
|
200
|
+
frame,
|
|
201
|
+
projectName: argv.name || path.parse(zipFilePath).name,
|
|
202
|
+
antiReverse: argv.antiReverse,
|
|
203
|
+
ignoreExt: argv.ignoreExt,
|
|
204
|
+
app: appOptions(argv),
|
|
205
|
+
target: argv.target
|
|
206
|
+
});
|
|
207
|
+
fs.writeFileSync(path.join(pathto, 'Cargo.toml'), cargo);
|
|
208
|
+
// 调试模式:行号 → 原项目角色/积木映射(cargo 报错定位用)
|
|
209
|
+
if (map) {
|
|
210
|
+
const mapName = profile.backend === 'web' ? 'lib.rs.rcmap.json' : 'main.rs.rcmap.json';
|
|
211
|
+
fs.writeFileSync(path.join(pathto, 'src', mapName), JSON.stringify(map, null, 2));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// 舞台项目(依赖 skia-safe)随项目发射 .cargo/config.toml,指向本地 Skia 预编译缓存。
|
|
215
|
+
// 项目每次重新生成都会重写此设置,同设备所有舞台项目共享同一份缓存目录
|
|
216
|
+
if (cargo.includes('skia-safe')) {
|
|
217
|
+
fs.mkdirSync(path.join(pathto, '.cargo'), { recursive: true });
|
|
218
|
+
fs.writeFileSync(path.join(pathto, '.cargo', 'config.toml'),
|
|
219
|
+
'# By rCompiler — Skia 预编译二进制本地缓存\n' +
|
|
220
|
+
'# 首次使用:将官方 skia-binaries-<key>.tar.gz 原样下载到 ~/.cache/skia-binaries/(勿解压),所有舞台项目共用\n' +
|
|
221
|
+
'[env]\n' +
|
|
222
|
+
`SKIA_BINARIES_URL = "${skiaCacheUrl()}"\n`);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// WASM:生成 scratch-render JS 宿主 + index.html(脚本核心在 wasm 内)
|
|
226
|
+
if (profile.backend === 'web') {
|
|
227
|
+
const { writeWebHost } = require('./web');
|
|
228
|
+
writeWebHost(pathto, profile, {
|
|
229
|
+
name,
|
|
230
|
+
title: argv.title || argv.name || path.parse(zipFilePath).name,
|
|
231
|
+
target
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (argv.profile) {
|
|
236
|
+
console.log('---- Profile ----');
|
|
237
|
+
console.log(JSON.stringify({
|
|
238
|
+
target: describeTarget(profile.target),
|
|
239
|
+
backend: profile.backend,
|
|
240
|
+
hasStage: profile.hasStage, hasSound: profile.hasSound,
|
|
241
|
+
concurrency: profile.concurrency, reasons: profile.forcedMultiReasons,
|
|
242
|
+
frame: profile.frame, stageSize: profile.stageSize, tw: profile.tw,
|
|
243
|
+
antiReverse: profile.antiReverse, app: profile.app,
|
|
244
|
+
ignoreExt: [...(profile.ignoreExt || [])], ignoredBlocks: profile.ignoredBlocks || 0,
|
|
245
|
+
typeDeclared: [...profile.typeDeclared.entries()],
|
|
246
|
+
autoNumVars: profile.autoNumVars || [],
|
|
247
|
+
listTypeDeclared: [...profile.listTypeDeclared.entries()],
|
|
248
|
+
targets: profile.targets.map(t => ({ name: t.name, isStage: t.isStage, entries: t.entryCount, writes: t.writes })),
|
|
249
|
+
opcodes: [...profile.opcodes].length,
|
|
250
|
+
deps: deplist
|
|
251
|
+
}, null, 2));
|
|
252
|
+
}
|
|
253
|
+
return { name, deplist, map, antiReverse: profile.antiReverse, target, profile };
|
|
254
|
+
} catch (error) {
|
|
255
|
+
console.error('\x1b[31m编译出错\x1b[m', error.message);
|
|
256
|
+
if (error.rcompilerCtx) {
|
|
257
|
+
const c = error.rcompilerCtx;
|
|
258
|
+
console.error(` 位置: 角色「${c.target}」 → 积木 ${c.opcode}${c.block ? `(块 ${c.block})` : ''}`);
|
|
259
|
+
}
|
|
260
|
+
hadError = true;
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// 供测试引用(test/errmap.js);直接执行时才进入 CLI 解析
|
|
266
|
+
if (require.main === module) {
|
|
267
|
+
yargs(hideBin(process.argv))
|
|
268
|
+
.locale('zh_CN')
|
|
269
|
+
.usage('用法:rcompiler <命令> [选项]')
|
|
270
|
+
.option('profile', { type: 'boolean', describe: '打印项目 Profile' })
|
|
271
|
+
.option('quiet', { type: 'boolean', describe: 't 模式不打印代码' })
|
|
272
|
+
.option('target', { type: 'string', describe: 'Rust 目标三元组(如 wasm32-unknown-unknown、aarch64-unknown-linux-gnu;默认宿主)' })
|
|
273
|
+
.option('fps', { type: 'number', describe: '逻辑帧率 TPS(1..250;0=舞台跟随屏幕;无舞台默认不锁帧,仅显式给出才锁)' })
|
|
274
|
+
.option('render-fps', { type: 'number', describe: '渲染帧率(0=跟随屏幕,需舞台)' })
|
|
275
|
+
.option('interp', { type: 'boolean', describe: '开启视觉插值(需舞台+帧门控+非turbo)' })
|
|
276
|
+
.option('no-interp', { type: 'boolean', describe: '强制关闭视觉插值' })
|
|
277
|
+
.option('turbo', { type: 'boolean', describe: '关闭帧门控(脚本全速运行)' })
|
|
278
|
+
.option('anti-reverse', { type: 'string', describe: '抗逆向:normal(默认)| obf(混淆)| debug(调试)' })
|
|
279
|
+
.option('ignore-ext', { type: 'string', describe: '自动忽略扩展 id(逗号分隔;其全部积木编译时跳过,优先级低于面板设置)' })
|
|
280
|
+
.option('title', { type: 'string', describe: '窗口标题(默认项目名)' })
|
|
281
|
+
.option('icon', { type: 'string', describe: '应用图标 PNG 路径(Windows 同时嵌入 EXE)' })
|
|
282
|
+
.option('resizable', { type: 'boolean', describe: '窗口可缩放(--no-resizable 关闭)' })
|
|
283
|
+
.option('fullscreen', { type: 'boolean', describe: '全屏启动' })
|
|
284
|
+
.option('decorations', { type: 'boolean', describe: '窗口边框(--no-decorations 隐藏)' })
|
|
285
|
+
.option('hide-cursor', { type: 'boolean', describe: '隐藏鼠标指针' })
|
|
286
|
+
.option('controls', { type: 'boolean', describe: '顶栏绿旗/暂停/停止' })
|
|
287
|
+
.option('fencing', { type: 'boolean', describe: '围栏(默认开,对照 TurboWarp fencing);--no-fencing 关闭坐标/尺寸钳制——模拟器与"走到边缘测量"类项目需要' })
|
|
288
|
+
.option('autoplay', { type: 'boolean', describe: '启动即运行绿旗(--no-autoplay 关闭)' })
|
|
289
|
+
.option('exit-key', { type: 'string', describe: '退出快捷键:none | escape | ctrlq' })
|
|
290
|
+
.option('quit-on-stop', { type: 'boolean', describe: '停止全部脚本后退出' })
|
|
291
|
+
.option('clone-limit', { type: 'number', describe: '克隆上限(默认 300)' })
|
|
292
|
+
.option('bg', { type: 'string', describe: '窗口背景色 #RRGGBB' })
|
|
293
|
+
.option('stage-width', { type: 'number', describe: '舞台宽(默认跟随 TurboWarp 设置;20..1920)' })
|
|
294
|
+
.option('stage-height', { type: 'number', describe: '舞台高(默认跟随 TurboWarp 设置;20..1600)' })
|
|
295
|
+
.option('dev', { type: 'boolean', describe: '运行模式改用 debug 构建(编译快,仅用于逻辑调试;性能须用默认 release 测量)' })
|
|
296
|
+
.command('$0 <path>', '编译并运行', (y) => y.positional('path', { type: 'string' }), async (argv) => {
|
|
297
|
+
const r = await compileProject({ ...argv, to: 'rmake', name: path.parse(argv.path).name, ver: '1.0.0' }, 'c');
|
|
298
|
+
if (!r || hadError) return;
|
|
299
|
+
const release = !argv.dev;
|
|
300
|
+
const projectDir = path.resolve('./rmake');
|
|
301
|
+
cargoBuild(projectDir, release, (code) => {
|
|
302
|
+
if (code !== 0) { console.error('\x1b[31m编译器错误\x1b[m', code); return; }
|
|
303
|
+
if (r.target.backend === 'web') {
|
|
304
|
+
const { finishWebBuild } = require('./web');
|
|
305
|
+
finishWebBuild(projectDir, r.profile, { name: r.name, release, run: true }).catch(e => {
|
|
306
|
+
console.error('\x1b[31mWASM 打包失败\x1b[m', e.message);
|
|
307
|
+
});
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
const bin = artifactPath(projectDir, r.target, release, r.name);
|
|
311
|
+
if (!r.target.canRun || (r.target.explicit && !r.target.isHost)) {
|
|
312
|
+
console.log(`\x1b[32m构建完成\x1b[m: ${bin}` +
|
|
313
|
+
(r.target.explicit && !r.target.isHost ? '(交叉目标不自动运行)' : ''));
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
spawn(bin, argv._.slice(1), { stdio: 'inherit' });
|
|
317
|
+
}, { map: r.map, target: r.target });
|
|
318
|
+
})
|
|
319
|
+
.command('o <path> <to> <name> <ver>', '编译为可执行文件(release)', (y) => y
|
|
320
|
+
.positional('path', { type: 'string' }).positional('to', { type: 'string' })
|
|
321
|
+
.positional('name', { type: 'string' }).positional('ver', { type: 'string' }), async (argv) => {
|
|
322
|
+
const r = await compileProject({ ...argv, to: 'rmake' }, 'c');
|
|
323
|
+
if (!r || hadError) return;
|
|
324
|
+
const projectDir = path.resolve('./rmake');
|
|
325
|
+
cargoBuild(projectDir, true, (code) => {
|
|
326
|
+
if (code !== 0) { console.error('\x1b[31m编译器错误\x1b[m', code); return; }
|
|
327
|
+
if (r.target.backend === 'web') {
|
|
328
|
+
const { finishWebBuild } = require('./web');
|
|
329
|
+
finishWebBuild(projectDir, r.profile, {
|
|
330
|
+
name: r.name, release: true, run: false, outDir: path.resolve(argv.to)
|
|
331
|
+
}).catch(e => console.error('\x1b[31mWASM 打包失败\x1b[m', e.message));
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
fs.mkdirSync(path.resolve(argv.to), { recursive: true });
|
|
335
|
+
const src = artifactPath(projectDir, r.target, true, r.name);
|
|
336
|
+
fs.copyFileSync(src, path.resolve(argv.to, path.basename(src)));
|
|
337
|
+
console.log(`\x1b[32m构建完成,至 ${path.resolve(argv.to)} 目录\x1b[m`);
|
|
338
|
+
}, { map: r.map, target: r.target });
|
|
339
|
+
})
|
|
340
|
+
.command('t <path>', '仅生成代码', (y) => y.positional('path', { type: 'string' })
|
|
341
|
+
.option('out', { type: 'string', describe: '输出目录(默认 rmake)' }), async (argv) => {
|
|
342
|
+
await compileProject({ ...argv, to: argv.out || 'rmake', name: path.parse(argv.path).name, ver: '1.0.0' }, 't');
|
|
343
|
+
if (hadError) process.exit(1);
|
|
344
|
+
})
|
|
345
|
+
.command('b <path> <to> <name> <ver>', '仅生成项目', (y) => y
|
|
346
|
+
.positional('path', { type: 'string' }).positional('to', { type: 'string' })
|
|
347
|
+
.positional('name', { type: 'string' }).positional('ver', { type: 'string' }), async (argv) => {
|
|
348
|
+
await compileProject(argv, 'c');
|
|
349
|
+
})
|
|
350
|
+
.version(pkg.version)
|
|
351
|
+
.alias('version', 'v')
|
|
352
|
+
.help()
|
|
353
|
+
.alias('help', 'h')
|
|
354
|
+
.demandCommand(1, '您必须至少使用一个命令')
|
|
355
|
+
.strictCommands()
|
|
356
|
+
.argv;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
module.exports = { cargoSafeName, mapErrors, printMappedErrors };
|