cc-pets 2.3.0 → 2.4.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/CC_BRIDGE.md +19 -0
- package/CC_BRIDGE.zh-CN.md +15 -0
- package/CHANGELOG.md +51 -0
- package/CHANGELOG.zh-CN.md +51 -0
- package/CONTRIBUTING.md +22 -0
- package/CONTRIBUTING.zh-CN.md +17 -0
- package/README.md +117 -26
- package/README.zh-CN.md +92 -22
- package/Resources/AgentIcons/claude.png +0 -0
- package/Resources/AgentIcons/codex.icns +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.vsix +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.zip +0 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +155 -27
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentStatus.m +12 -6
- package/Sources/CCPets/App/CCPetsAppDelegate+Private.h +1 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +3 -6
- package/Sources/CCPets/App/CCPetsAppDelegate+Speech.m +113 -47
- package/Sources/CCPets/App/CCPetsAppDelegate.h +2 -2
- package/Sources/CCPets/App/main.m +6 -0
- package/Sources/CCPets/Bridge/CCPetsBridge.m +11 -6
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +78 -38
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +2 -3
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +50 -32
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +46 -40
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +9 -6
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +171 -53
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.h +21 -0
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.m +503 -0
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.m +367 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.h +2 -1
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.m +107 -25
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.h +17 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.m +64 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.h +23 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.m +475 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +2 -2
- package/Sources/CCPets/Support/CCPetsDoctor.m +26 -12
- package/Sources/CCPets/Support/CCPetsPreferences.h +2 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +34 -2
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +2 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +18 -7
- package/Sources/CCPets/UI/CCPetsGlassView.h +26 -4
- package/Sources/CCPets/UI/CCPetsGlassView.m +40 -9
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +2 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +561 -66
- package/Sources/CCPets/UI/CCPetsUIStyle.h +5 -2
- package/Sources/CCPets/UI/CCPetsUIStyle.m +57 -8
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +4 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +23 -28
- package/Sources/CCPets/UI/CCPetsUsageStore.m +2 -2
- package/Sources/CCPets/Usage/CCPetsUsage+CodexBackground.m +193 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +1 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +49 -11
- package/Sources/CCPets/Usage/CCPetsUsage.h +3 -2
- package/Sources/CCPets/Usage/CCPetsUsageMonitor.m +15 -17
- package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +11 -29
- package/Sources/CCPets/Usage/QuotaDashboardView.m +128 -2
- package/THIRD_PARTY_NOTICES.md +7 -3
- package/THIRD_PARTY_NOTICES.zh-CN.md +5 -1
- package/bin/claude-with-pet +2 -0
- package/bin/codex-with-pet +2 -0
- package/package.json +5 -1
- package/plugins/jetbrains/README.md +62 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalBridge.java +379 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalIdentity.java +24 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalStartup.java +12 -0
- package/plugins/jetbrains/src/main/resources/META-INF/plugin.xml +23 -0
- package/plugins/jetbrains/src/main/resources/META-INF/pluginIcon.svg +380 -0
- package/plugins/vscode/README.md +63 -0
- package/plugins/vscode/extension.js +210 -0
- package/plugins/vscode/package.json +18 -0
- package/scripts/build-idea-plugin.sh +101 -0
- package/scripts/build-vscode-extension.sh +112 -0
- package/scripts/build.sh +24 -2
- package/scripts/compile-cached.zsh +133 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// CC Pets Terminal for VS Code and its forks (Cursor, Windsurf, VSCodium, …).
|
|
2
|
+
//
|
|
3
|
+
// 每个窗口都有自己的 extension host,所以每个窗口各注册一个本机端点。Pet 按 TTY 问遍
|
|
4
|
+
// 同一应用的所有窗口:拥有该 TTY 的窗口选中终端,并回报自己的工作区路径;扩展 API 没有
|
|
5
|
+
// 前置系统窗口的接口,由 Pet 再"打开"一次这个路径,VS Code 会聚焦已打开它的那个窗口。
|
|
6
|
+
//
|
|
7
|
+
// 协议与 JetBrains 插件一致:注册文件在 ~/Library/Application Support/CC Pets/ide-instances/,
|
|
8
|
+
// 仅监听 127.0.0.1,要求 Bearer 令牌的 POST,拒绝带 Origin 的请求;只提供状态查询和
|
|
9
|
+
// 聚焦,不读取终端输出,也不向终端输入。
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
const vscode = require('vscode');
|
|
13
|
+
const http = require('http');
|
|
14
|
+
const crypto = require('crypto');
|
|
15
|
+
const fs = require('fs');
|
|
16
|
+
const os = require('os');
|
|
17
|
+
const path = require('path');
|
|
18
|
+
const { execFile } = require('child_process');
|
|
19
|
+
|
|
20
|
+
const REGISTRY_DIR = path.join(os.homedir(), 'Library', 'Application Support', 'CC Pets', 'ide-instances');
|
|
21
|
+
const VERSION = require('./package.json').version;
|
|
22
|
+
|
|
23
|
+
let server;
|
|
24
|
+
let registration;
|
|
25
|
+
let registryFile;
|
|
26
|
+
|
|
27
|
+
function run(file, args) {
|
|
28
|
+
return new Promise((resolve) => {
|
|
29
|
+
execFile(file, args, { timeout: 2000 }, (error, stdout) => resolve(error ? '' : String(stdout).trim()));
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function ttyForPid(pid) {
|
|
34
|
+
if (!Number.isInteger(pid) || pid <= 0) return '';
|
|
35
|
+
const tty = path.basename(await run('/bin/ps', ['-p', String(pid), '-o', 'tty=']));
|
|
36
|
+
return /^ttys\d{1,8}$/.test(tty) ? tty : '';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// extension host 是 Helper 进程,Pet 的 NSRunningApplication 认不出来。沿父进程链找到
|
|
40
|
+
// .app/Contents/MacOS/ 下的主程序,Pet 用它校验注册文件属于一个正在运行的应用。
|
|
41
|
+
async function hostApplication() {
|
|
42
|
+
let pid = process.ppid;
|
|
43
|
+
for (let depth = 0; depth < 12 && pid > 1; depth++) {
|
|
44
|
+
const comm = await run('/bin/ps', ['-p', String(pid), '-o', 'comm=']);
|
|
45
|
+
const match = comm.match(/^(\/.*?\.app)\/Contents\/MacOS\/[^/]+$/);
|
|
46
|
+
if (match && !match[1].includes('/Contents/Frameworks/')) {
|
|
47
|
+
const bundleID = await run('/usr/bin/plutil',
|
|
48
|
+
['-extract', 'CFBundleIdentifier', 'raw', '-o', '-', path.join(match[1], 'Contents', 'Info.plist')]);
|
|
49
|
+
return { pid, appPath: match[1], bundleID };
|
|
50
|
+
}
|
|
51
|
+
const parent = parseInt(await run('/bin/ps', ['-p', String(pid), '-o', 'ppid=']), 10);
|
|
52
|
+
if (!Number.isInteger(parent)) break;
|
|
53
|
+
pid = parent;
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 能被"再次打开"从而前置窗口的路径;空窗口、未保存的多根工作区和远程窗口没有。
|
|
59
|
+
function workspacePath() {
|
|
60
|
+
if (vscode.env.remoteName) return '';
|
|
61
|
+
const file = vscode.workspace.workspaceFile;
|
|
62
|
+
if (file) return file.scheme === 'file' ? file.fsPath : '';
|
|
63
|
+
const folders = vscode.workspace.workspaceFolders || [];
|
|
64
|
+
return folders.length === 1 && folders[0].uri.scheme === 'file' ? folders[0].uri.fsPath : '';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function status() {
|
|
68
|
+
return {
|
|
69
|
+
schemaVersion: 1,
|
|
70
|
+
kind: 'vscode',
|
|
71
|
+
pluginVersion: VERSION,
|
|
72
|
+
instanceID: registration.instanceID,
|
|
73
|
+
pid: registration.pid,
|
|
74
|
+
extensionHostPid: process.pid,
|
|
75
|
+
ideVersion: vscode.version,
|
|
76
|
+
workspace: workspacePath(),
|
|
77
|
+
windowFocused: vscode.window.state.focused,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function withTimeout(promise, ms) {
|
|
82
|
+
return Promise.race([promise, new Promise((resolve) => setTimeout(() => resolve(undefined), ms))]);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function terminalsForTTY(tty) {
|
|
86
|
+
// 远程窗口里终端的 PID 属于远端机器,在本机查 TTY 可能撞上无关进程。
|
|
87
|
+
if (vscode.env.remoteName) return [];
|
|
88
|
+
const matches = [];
|
|
89
|
+
for (const terminal of vscode.window.terminals) {
|
|
90
|
+
const pid = await withTimeout(terminal.processId, 1000);
|
|
91
|
+
if ((await ttyForPid(pid)) === tty) matches.push(terminal);
|
|
92
|
+
}
|
|
93
|
+
return matches;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function reply(res, code, body) {
|
|
97
|
+
const data = Buffer.from(JSON.stringify(body));
|
|
98
|
+
res.writeHead(code, { 'Content-Type': 'application/json', 'Content-Length': data.length });
|
|
99
|
+
res.end(data);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function readBody(req) {
|
|
103
|
+
return new Promise((resolve, reject) => {
|
|
104
|
+
const chunks = [];
|
|
105
|
+
let size = 0;
|
|
106
|
+
req.on('data', (chunk) => {
|
|
107
|
+
size += chunk.length;
|
|
108
|
+
if (size > 4096) { reject(new Error('too_large')); req.destroy(); } else chunks.push(chunk);
|
|
109
|
+
});
|
|
110
|
+
req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
|
111
|
+
req.on('error', reject);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function authorized(req) {
|
|
116
|
+
const expected = Buffer.from(`Bearer ${registration.token}`);
|
|
117
|
+
const given = Buffer.from(String(req.headers.authorization || ''));
|
|
118
|
+
return req.method === 'POST' && !req.headers.origin &&
|
|
119
|
+
given.length === expected.length && crypto.timingSafeEqual(given, expected);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function handle(req, res) {
|
|
123
|
+
if (!authorized(req)) return reply(res, 403, { error: 'unauthorized' });
|
|
124
|
+
if (req.url === '/status') return reply(res, 200, status());
|
|
125
|
+
if (req.url !== '/focus') return reply(res, 404, { error: 'unknown_route' });
|
|
126
|
+
|
|
127
|
+
let request;
|
|
128
|
+
try {
|
|
129
|
+
request = JSON.parse(await readBody(req));
|
|
130
|
+
} catch {
|
|
131
|
+
return reply(res, 400, { error: 'invalid_request' });
|
|
132
|
+
}
|
|
133
|
+
const tty = String((request && request.tty) || '').replace(/^\/dev\//, '');
|
|
134
|
+
if (!/^ttys\d{1,8}$/.test(tty)) return reply(res, 400, { error: 'invalid_target' });
|
|
135
|
+
|
|
136
|
+
const matches = await terminalsForTTY(tty);
|
|
137
|
+
if (matches.length !== 1) {
|
|
138
|
+
return reply(res, matches.length ? 409 : 404,
|
|
139
|
+
{ focused: false, error: matches.length ? 'ambiguous_target' : 'terminal_not_found' });
|
|
140
|
+
}
|
|
141
|
+
// 面板、分屏、编辑器区里的终端都由 show 负责显示并把键盘焦点交给它。
|
|
142
|
+
matches[0].show(false);
|
|
143
|
+
return reply(res, 200, { focused: true, workspace: workspacePath() });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function writeRegistration() {
|
|
147
|
+
fs.mkdirSync(REGISTRY_DIR, { recursive: true, mode: 0o700 });
|
|
148
|
+
const info = fs.lstatSync(REGISTRY_DIR);
|
|
149
|
+
if (!info.isDirectory() || info.isSymbolicLink() || info.uid !== process.getuid()) {
|
|
150
|
+
throw new Error('Invalid bridge directory');
|
|
151
|
+
}
|
|
152
|
+
fs.chmodSync(REGISTRY_DIR, 0o700);
|
|
153
|
+
const record = { ...status(), port: registration.port, token: registration.token,
|
|
154
|
+
appPath: registration.appPath, bundleID: registration.bundleID,
|
|
155
|
+
extensionsPath: registration.extensionsPath };
|
|
156
|
+
const temporary = path.join(REGISTRY_DIR, `.registration-${registration.instanceID}.tmp`);
|
|
157
|
+
fs.writeFileSync(temporary, JSON.stringify(record), { mode: 0o600, flag: 'w' });
|
|
158
|
+
fs.renameSync(temporary, registryFile);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function stop() {
|
|
162
|
+
if (server) server.close();
|
|
163
|
+
server = undefined;
|
|
164
|
+
if (registryFile) {
|
|
165
|
+
try { fs.unlinkSync(registryFile); } catch {}
|
|
166
|
+
}
|
|
167
|
+
registryFile = undefined;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function activate(context) {
|
|
171
|
+
if (process.platform !== 'darwin') return;
|
|
172
|
+
const host = await hostApplication();
|
|
173
|
+
if (!host) {
|
|
174
|
+
console.warn('[CC Pets Terminal] host application not found; bridge disabled');
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
// 不用 environmentVariableCollection:集合一变,VS Code 会自动重启还没交互过的终端
|
|
178
|
+
// (terminal.integrated.environmentChangesRelaunch),已交互的终端挂上"需要重启"提示。
|
|
179
|
+
// 从 JetBrains 终端里用 `code` 启动时继承来的 CC_PETS_IDE_*,由 Pet 按 bundle ID 忽略。
|
|
180
|
+
const instanceID = crypto.randomUUID();
|
|
181
|
+
registration = { instanceID, token: crypto.randomBytes(32).toString('hex'), ...host,
|
|
182
|
+
// 实际的扩展目录(可能被 --extensions-dir 或便携模式改过),Pet 用它判断安装版本。
|
|
183
|
+
extensionsPath: path.dirname(context.extensionPath) };
|
|
184
|
+
registryFile = path.join(REGISTRY_DIR, `${instanceID}.json`);
|
|
185
|
+
server = http.createServer((req, res) => {
|
|
186
|
+
handle(req, res).catch(() => reply(res, 500, { error: 'focus_failed' }));
|
|
187
|
+
});
|
|
188
|
+
server.maxConnections = 8;
|
|
189
|
+
try {
|
|
190
|
+
await new Promise((resolve, reject) => {
|
|
191
|
+
server.once('error', reject);
|
|
192
|
+
server.listen(0, '127.0.0.1', resolve);
|
|
193
|
+
});
|
|
194
|
+
registration.port = server.address().port;
|
|
195
|
+
writeRegistration();
|
|
196
|
+
} catch (error) {
|
|
197
|
+
console.warn('[CC Pets Terminal] could not start bridge', error);
|
|
198
|
+
stop();
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// 工作区变化后,注册文件里的路径只用于展示;聚焦时以 /focus 的实时回报为准。
|
|
203
|
+
context.subscriptions.push(
|
|
204
|
+
vscode.workspace.onDidChangeWorkspaceFolders(() => { try { writeRegistration(); } catch {} }),
|
|
205
|
+
{ dispose: stop },
|
|
206
|
+
);
|
|
207
|
+
process.once('exit', stop);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
module.exports = { activate, deactivate: stop };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cc-pets-terminal",
|
|
3
|
+
"displayName": "CC Pets Terminal",
|
|
4
|
+
"description": "Lets CC Pets jump back to the exact integrated terminal that hosts an agent session (macOS).",
|
|
5
|
+
"version": "1.0.0",
|
|
6
|
+
"publisher": "ccpets",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"private": true,
|
|
9
|
+
"engines": { "vscode": "^1.80.0" },
|
|
10
|
+
"categories": ["Other"],
|
|
11
|
+
"extensionKind": ["ui"],
|
|
12
|
+
"main": "./extension.js",
|
|
13
|
+
"activationEvents": ["onStartupFinished"],
|
|
14
|
+
"capabilities": {
|
|
15
|
+
"untrustedWorkspaces": { "supported": true },
|
|
16
|
+
"virtualWorkspaces": true
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
#!/bin/zsh
|
|
2
|
+
# Build the offline IDEA plugin using the installed SDK and macOS tools.
|
|
3
|
+
set -euo pipefail
|
|
4
|
+
|
|
5
|
+
PLUGIN_SCRIPT_DIR="${0:A:h}"
|
|
6
|
+
PLUGIN_PROJECT_DIR="${PLUGIN_SCRIPT_DIR:h}"
|
|
7
|
+
PLUGIN_IDE_APP="/Applications/IntelliJ IDEA.app"
|
|
8
|
+
PLUGIN_OUTPUT="${PLUGIN_PROJECT_DIR}/Resources/IDEPlugins/cc-pets-terminal.zip"
|
|
9
|
+
|
|
10
|
+
usage() {
|
|
11
|
+
print -r -- "Usage: ${0:t} [--ide <IDEA.app>] [--output <plugin.zip>]
|
|
12
|
+
|
|
13
|
+
Build against an installed IDEA 2026.2 / 2026.3 SDK without downloading dependencies.
|
|
14
|
+
Uses the IDE's bundled Java 25 compiler, or JAVA_HOME when explicitly set.
|
|
15
|
+
Defaults:
|
|
16
|
+
--ide /Applications/IntelliJ IDEA.app
|
|
17
|
+
--output Resources/IDEPlugins/cc-pets-terminal.zip (in this repository)"
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
while (( $# > 0 )); do
|
|
21
|
+
case "$1" in
|
|
22
|
+
--ide|--output)
|
|
23
|
+
if (( $# < 2 )) || [[ -z "$2" || "$2" == --* ]]; then
|
|
24
|
+
print -u2 -r -- "Missing value for $1"
|
|
25
|
+
exit 2
|
|
26
|
+
fi
|
|
27
|
+
if [[ "$1" == --ide ]]; then PLUGIN_IDE_APP="$2"; else PLUGIN_OUTPUT="$2"; fi
|
|
28
|
+
shift 2
|
|
29
|
+
;;
|
|
30
|
+
--ide=*) PLUGIN_IDE_APP="${1#--ide=}"; shift ;;
|
|
31
|
+
--output=*) PLUGIN_OUTPUT="${1#--output=}"; shift ;;
|
|
32
|
+
-h|--help) usage; exit 0 ;;
|
|
33
|
+
*) print -u2 -r -- "Unknown argument: $1"; usage >&2; exit 2 ;;
|
|
34
|
+
esac
|
|
35
|
+
done
|
|
36
|
+
|
|
37
|
+
if [[ -z "$PLUGIN_IDE_APP" || -z "$PLUGIN_OUTPUT" ]]; then
|
|
38
|
+
print -u2 -- "IDEA app and output paths must not be empty."
|
|
39
|
+
exit 2
|
|
40
|
+
fi
|
|
41
|
+
PLUGIN_SDK_DIR="${PLUGIN_IDE_APP:A}/Contents"
|
|
42
|
+
PLUGIN_OUTPUT="${PLUGIN_OUTPUT:a}"
|
|
43
|
+
PLUGIN_SOURCE_DIR="${PLUGIN_PROJECT_DIR}/plugins/jetbrains/src/main"
|
|
44
|
+
PLUGIN_JAVA_HOME="${JAVA_HOME:-${PLUGIN_SDK_DIR}/jbr/Contents/Home}"
|
|
45
|
+
PLUGIN_JAVA="${PLUGIN_JAVA_HOME}/bin/java"
|
|
46
|
+
PLUGIN_JARS=("${PLUGIN_SDK_DIR}"/lib/*.jar(N) "${PLUGIN_SDK_DIR}"/plugins/terminal/lib/**/*.jar(N))
|
|
47
|
+
PLUGIN_SOURCES=("${PLUGIN_SOURCE_DIR}"/java/**/*.java(N))
|
|
48
|
+
if (( ${#PLUGIN_JARS[@]} == 0 )) || [[ ! -f "${PLUGIN_SDK_DIR}/plugins/terminal/lib/terminal.jar" ]]; then
|
|
49
|
+
print -u2 -r -- "IDEA SDK with the bundled Terminal plugin was not found: ${PLUGIN_SDK_DIR}"
|
|
50
|
+
exit 1
|
|
51
|
+
fi
|
|
52
|
+
if [[ ! -x "$PLUGIN_JAVA" ]] || (( ${#PLUGIN_SOURCES[@]} == 0 )); then
|
|
53
|
+
print -u2 -- "The Java runtime or plugin Java sources are missing."
|
|
54
|
+
exit 1
|
|
55
|
+
fi
|
|
56
|
+
/usr/bin/xmllint --nonet --noout "${PLUGIN_SOURCE_DIR}/resources/META-INF/plugin.xml"
|
|
57
|
+
|
|
58
|
+
PLUGIN_BUILD_DIR="$(mktemp -d "${TMPDIR:-/tmp}/cc-pets-idea-build.XXXXXX")"
|
|
59
|
+
PLUGIN_STAGED_OUTPUT=""
|
|
60
|
+
cleanup() {
|
|
61
|
+
[[ -z "$PLUGIN_STAGED_OUTPUT" ]] || rm -f -- "$PLUGIN_STAGED_OUTPUT"
|
|
62
|
+
rm -rf -- "$PLUGIN_BUILD_DIR"
|
|
63
|
+
}
|
|
64
|
+
trap cleanup EXIT
|
|
65
|
+
trap 'exit 130' INT
|
|
66
|
+
trap 'exit 143' TERM
|
|
67
|
+
mkdir -p "${PLUGIN_BUILD_DIR}/classes/META-INF" "${PLUGIN_BUILD_DIR}/distribution/cc-pets-terminal/lib"
|
|
68
|
+
|
|
69
|
+
# The bundled runtime contains jdk.compiler even when it has no javac launcher.
|
|
70
|
+
"$PLUGIN_JAVA" -m jdk.compiler/com.sun.tools.javac.Main \
|
|
71
|
+
--release 25 -proc:none -Xlint:deprecation \
|
|
72
|
+
-d "${PLUGIN_BUILD_DIR}/classes" \
|
|
73
|
+
-classpath "${(j.:.)PLUGIN_JARS}" \
|
|
74
|
+
"${PLUGIN_SOURCES[@]}"
|
|
75
|
+
cp -R "${PLUGIN_SOURCE_DIR}/resources/." "${PLUGIN_BUILD_DIR}/classes/"
|
|
76
|
+
|
|
77
|
+
# Only archive files, in sorted order, with fixed timestamps and permissions.
|
|
78
|
+
# This keeps repeated builds reproducible without Python or a jar launcher.
|
|
79
|
+
(
|
|
80
|
+
cd "${PLUGIN_BUILD_DIR}/classes"
|
|
81
|
+
PLUGIN_CLASS_FILES=(**/*(.N))
|
|
82
|
+
chmod 644 "${PLUGIN_CLASS_FILES[@]}"
|
|
83
|
+
TZ=UTC /usr/bin/touch -t 202601010000.00 "${PLUGIN_CLASS_FILES[@]}"
|
|
84
|
+
TZ=UTC /usr/bin/zip -X -q -9 "${PLUGIN_BUILD_DIR}/distribution/cc-pets-terminal/lib/cc-pets-terminal.jar" \
|
|
85
|
+
"${PLUGIN_CLASS_FILES[@]}"
|
|
86
|
+
)
|
|
87
|
+
(
|
|
88
|
+
cd "${PLUGIN_BUILD_DIR}/distribution"
|
|
89
|
+
chmod 644 cc-pets-terminal/lib/cc-pets-terminal.jar
|
|
90
|
+
TZ=UTC /usr/bin/touch -t 202601010000.00 cc-pets-terminal/lib/cc-pets-terminal.jar
|
|
91
|
+
TZ=UTC /usr/bin/zip -X -q -9 "${PLUGIN_BUILD_DIR}/cc-pets-terminal.zip" cc-pets-terminal/lib/cc-pets-terminal.jar
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
# Publish only after compilation and both archives have completed successfully.
|
|
95
|
+
mkdir -p "${PLUGIN_OUTPUT:h}"
|
|
96
|
+
PLUGIN_STAGED_OUTPUT="$(mktemp "${PLUGIN_OUTPUT:h}/.cc-pets-plugin.XXXXXX")"
|
|
97
|
+
cp "${PLUGIN_BUILD_DIR}/cc-pets-terminal.zip" "$PLUGIN_STAGED_OUTPUT"
|
|
98
|
+
chmod 644 "$PLUGIN_STAGED_OUTPUT"
|
|
99
|
+
mv -f -- "$PLUGIN_STAGED_OUTPUT" "$PLUGIN_OUTPUT"
|
|
100
|
+
PLUGIN_STAGED_OUTPUT=""
|
|
101
|
+
print -r -- "Built IDEA plugin: ${PLUGIN_OUTPUT}"
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
#!/bin/zsh
|
|
2
|
+
# Package the VS Code extension as a VSIX with macOS tools only (no Node, npm or vsce).
|
|
3
|
+
# A VSIX is a ZIP holding [Content_Types].xml, extension.vsixmanifest and extension/.
|
|
4
|
+
set -euo pipefail
|
|
5
|
+
|
|
6
|
+
EXT_SCRIPT_DIR="${0:A:h}"
|
|
7
|
+
EXT_PROJECT_DIR="${EXT_SCRIPT_DIR:h}"
|
|
8
|
+
EXT_SOURCE_DIR="${EXT_PROJECT_DIR}/plugins/vscode"
|
|
9
|
+
EXT_OUTPUT="${EXT_PROJECT_DIR}/Resources/IDEPlugins/cc-pets-terminal.vsix"
|
|
10
|
+
|
|
11
|
+
usage() {
|
|
12
|
+
print -r -- "Usage: ${0:t} [--output <extension.vsix>]
|
|
13
|
+
|
|
14
|
+
Defaults:
|
|
15
|
+
--output Resources/IDEPlugins/cc-pets-terminal.vsix (in this repository)"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
while (( $# > 0 )); do
|
|
19
|
+
case "$1" in
|
|
20
|
+
--output)
|
|
21
|
+
if (( $# < 2 )) || [[ -z "$2" || "$2" == --* ]]; then
|
|
22
|
+
print -u2 -r -- "Missing value for $1"
|
|
23
|
+
exit 2
|
|
24
|
+
fi
|
|
25
|
+
EXT_OUTPUT="$2"; shift 2 ;;
|
|
26
|
+
--output=*) EXT_OUTPUT="${1#--output=}"; shift ;;
|
|
27
|
+
-h|--help) usage; exit 0 ;;
|
|
28
|
+
*) print -u2 -r -- "Unknown argument: $1"; usage >&2; exit 2 ;;
|
|
29
|
+
esac
|
|
30
|
+
done
|
|
31
|
+
EXT_OUTPUT="${EXT_OUTPUT:a}"
|
|
32
|
+
|
|
33
|
+
EXT_FILES=(package.json extension.js)
|
|
34
|
+
for file in "${EXT_FILES[@]}"; do
|
|
35
|
+
[[ -f "${EXT_SOURCE_DIR}/${file}" ]] || { print -u2 -r -- "Missing ${EXT_SOURCE_DIR}/${file}"; exit 1; }
|
|
36
|
+
done
|
|
37
|
+
manifest_value() { /usr/bin/plutil -extract "$1" raw -o - "${EXT_SOURCE_DIR}/package.json"; }
|
|
38
|
+
EXT_VERSION="$(manifest_value version)"
|
|
39
|
+
EXT_PUBLISHER="$(manifest_value publisher)"
|
|
40
|
+
EXT_NAME="$(manifest_value name)"
|
|
41
|
+
EXT_DISPLAY="$(manifest_value displayName)"
|
|
42
|
+
EXT_DESCRIPTION="$(manifest_value description)"
|
|
43
|
+
EXT_ENGINE="$(manifest_value engines.vscode)"
|
|
44
|
+
if [[ ! "$EXT_VERSION" =~ '^[0-9]+\.[0-9]+\.[0-9]+$' ]]; then
|
|
45
|
+
print -u2 -r -- "Invalid extension version: ${EXT_VERSION}"
|
|
46
|
+
exit 1
|
|
47
|
+
fi
|
|
48
|
+
|
|
49
|
+
EXT_BUILD_DIR="$(mktemp -d "${TMPDIR:-/tmp}/cc-pets-vscode-build.XXXXXX")"
|
|
50
|
+
EXT_STAGED_OUTPUT=""
|
|
51
|
+
cleanup() {
|
|
52
|
+
[[ -z "$EXT_STAGED_OUTPUT" ]] || rm -f -- "$EXT_STAGED_OUTPUT"
|
|
53
|
+
rm -rf -- "$EXT_BUILD_DIR"
|
|
54
|
+
}
|
|
55
|
+
trap cleanup EXIT
|
|
56
|
+
trap 'exit 130' INT
|
|
57
|
+
trap 'exit 143' TERM
|
|
58
|
+
|
|
59
|
+
mkdir -p "${EXT_BUILD_DIR}/extension"
|
|
60
|
+
for file in "${EXT_FILES[@]}"; do cp "${EXT_SOURCE_DIR}/${file}" "${EXT_BUILD_DIR}/extension/"; done
|
|
61
|
+
[[ ! -f "${EXT_PROJECT_DIR}/LICENSE" ]] || cp "${EXT_PROJECT_DIR}/LICENSE" "${EXT_BUILD_DIR}/extension/LICENSE.txt"
|
|
62
|
+
|
|
63
|
+
cat > "${EXT_BUILD_DIR}/extension.vsixmanifest" <<EOF
|
|
64
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
65
|
+
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
|
|
66
|
+
<Metadata>
|
|
67
|
+
<Identity Language="en-US" Id="${EXT_NAME}" Version="${EXT_VERSION}" Publisher="${EXT_PUBLISHER}" />
|
|
68
|
+
<DisplayName>${EXT_DISPLAY}</DisplayName>
|
|
69
|
+
<Description xml:space="preserve">${EXT_DESCRIPTION}</Description>
|
|
70
|
+
<Categories>Other</Categories>
|
|
71
|
+
<Properties>
|
|
72
|
+
<Property Id="Microsoft.VisualStudio.Code.Engine" Value="${EXT_ENGINE}" />
|
|
73
|
+
<Property Id="Microsoft.VisualStudio.Code.ExtensionKind" Value="ui" />
|
|
74
|
+
</Properties>
|
|
75
|
+
</Metadata>
|
|
76
|
+
<Installation>
|
|
77
|
+
<InstallationTarget Id="Microsoft.VisualStudio.Code" />
|
|
78
|
+
</Installation>
|
|
79
|
+
<Dependencies />
|
|
80
|
+
<Assets>
|
|
81
|
+
<Asset Type="Microsoft.VisualStudio.Code.Manifest" Path="extension/package.json" Addressable="true" />
|
|
82
|
+
</Assets>
|
|
83
|
+
</PackageManifest>
|
|
84
|
+
EOF
|
|
85
|
+
|
|
86
|
+
cat > "${EXT_BUILD_DIR}/[Content_Types].xml" <<'EOF'
|
|
87
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
88
|
+
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
|
|
89
|
+
<Default Extension=".json" ContentType="application/json" />
|
|
90
|
+
<Default Extension=".js" ContentType="application/javascript" />
|
|
91
|
+
<Default Extension=".txt" ContentType="text/plain" />
|
|
92
|
+
<Default Extension=".vsixmanifest" ContentType="text/xml" />
|
|
93
|
+
</Types>
|
|
94
|
+
EOF
|
|
95
|
+
/usr/bin/xmllint --nonet --noout "${EXT_BUILD_DIR}/extension.vsixmanifest" "${EXT_BUILD_DIR}/[Content_Types].xml"
|
|
96
|
+
|
|
97
|
+
# Sorted entries, fixed timestamps and permissions keep repeated builds reproducible.
|
|
98
|
+
(
|
|
99
|
+
cd "$EXT_BUILD_DIR"
|
|
100
|
+
EXT_ENTRIES=("[Content_Types].xml" extension.vsixmanifest extension/**/*(.N))
|
|
101
|
+
chmod 644 "${EXT_ENTRIES[@]}"
|
|
102
|
+
TZ=UTC /usr/bin/touch -t 202601010000.00 "${EXT_ENTRIES[@]}"
|
|
103
|
+
TZ=UTC /usr/bin/zip -X -q -9 -D "${EXT_BUILD_DIR}/cc-pets-terminal.vsix" "${EXT_ENTRIES[@]}"
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
mkdir -p "${EXT_OUTPUT:h}"
|
|
107
|
+
EXT_STAGED_OUTPUT="$(mktemp "${EXT_OUTPUT:h}/.cc-pets-vsix.XXXXXX")"
|
|
108
|
+
cp "${EXT_BUILD_DIR}/cc-pets-terminal.vsix" "$EXT_STAGED_OUTPUT"
|
|
109
|
+
chmod 644 "$EXT_STAGED_OUTPUT"
|
|
110
|
+
mv -f -- "$EXT_STAGED_OUTPUT" "$EXT_OUTPUT"
|
|
111
|
+
EXT_STAGED_OUTPUT=""
|
|
112
|
+
print -r -- "Built VS Code extension ${EXT_VERSION}: ${EXT_OUTPUT}"
|
package/scripts/build.sh
CHANGED
|
@@ -4,6 +4,7 @@ set -eu
|
|
|
4
4
|
SCRIPT_DIR="${0:A:h}"
|
|
5
5
|
PROJECT_DIR="${SCRIPT_DIR:h}"
|
|
6
6
|
source "${PROJECT_DIR}/scripts/i18n.zsh"
|
|
7
|
+
source "${PROJECT_DIR}/scripts/compile-cached.zsh"
|
|
7
8
|
BUILD_DIR="${PROJECT_DIR}/.build/release"
|
|
8
9
|
CACHE_DIR="${PROJECT_DIR}/.build/clang-cache"
|
|
9
10
|
APP_DIR="${BUILD_DIR}/CC Pets.app"
|
|
@@ -31,7 +32,7 @@ if (( ${#SOURCES[@]} == 0 )); then
|
|
|
31
32
|
exit 1
|
|
32
33
|
fi
|
|
33
34
|
|
|
34
|
-
CLANG_MODULE_CACHE_PATH="${CACHE_DIR}"
|
|
35
|
+
CLANG_MODULE_CACHE_PATH="${CACHE_DIR}" cc_pets_compile \
|
|
35
36
|
-fobjc-arc \
|
|
36
37
|
-fmodules-cache-path="${CACHE_DIR}" \
|
|
37
38
|
-mmacosx-version-min=13.0 \
|
|
@@ -45,6 +46,7 @@ CLANG_MODULE_CACHE_PATH="${CACHE_DIR}" clang \
|
|
|
45
46
|
-framework IOKit \
|
|
46
47
|
-framework QuartzCore \
|
|
47
48
|
-framework UserNotifications \
|
|
49
|
+
-lsqlite3 \
|
|
48
50
|
"${SOURCES[@]}" \
|
|
49
51
|
-o "${BUILD_DIR}/cc-pets"
|
|
50
52
|
|
|
@@ -56,6 +58,16 @@ if (( ${#SPRITES[@]} > 0 )); then
|
|
|
56
58
|
cp "${SPRITES[@]}" "${BUILD_DIR}/"
|
|
57
59
|
cp "${SPRITES[@]}" "${APP_RESOURCES_DIR}/"
|
|
58
60
|
fi
|
|
61
|
+
# Agent 图标放独立目录,避免被当作宠物精灵图扫描;同时支持裸二进制与 App。
|
|
62
|
+
mkdir -p "${BUILD_DIR}/AgentIcons" "${APP_RESOURCES_DIR}/AgentIcons"
|
|
63
|
+
cp "${PROJECT_DIR}"/Resources/AgentIcons/* "${BUILD_DIR}/AgentIcons/"
|
|
64
|
+
cp "${PROJECT_DIR}"/Resources/AgentIcons/* "${APP_RESOURCES_DIR}/AgentIcons/"
|
|
65
|
+
# 离线 IDEA 插件和 VS Code 扩展随 App 和裸二进制分发;普通安装不需要 Java、Node 或 SDK。
|
|
66
|
+
mkdir -p "${BUILD_DIR}/IDEPlugins" "${APP_RESOURCES_DIR}/IDEPlugins"
|
|
67
|
+
for ide_plugin in cc-pets-terminal.zip cc-pets-terminal.vsix; do
|
|
68
|
+
cp "${PROJECT_DIR}/Resources/IDEPlugins/${ide_plugin}" "${BUILD_DIR}/IDEPlugins/"
|
|
69
|
+
cp "${PROJECT_DIR}/Resources/IDEPlugins/${ide_plugin}" "${APP_RESOURCES_DIR}/IDEPlugins/"
|
|
70
|
+
done
|
|
59
71
|
# 默认台词。代码里不再留内置词库,这个文件就是默认台词的唯一来源,
|
|
60
72
|
# 首次启动时被拷到 ~/.cc-pets/phrases.txt。
|
|
61
73
|
# 裸二进制旁边也放一份:直接跑 .build/release/cc-pets 时没有 app bundle。
|
|
@@ -72,7 +84,7 @@ for LPROJ_DIR in "${PROJECT_DIR}"/Resources/*.lproj(N/); do
|
|
|
72
84
|
cp -R "${LPROJ_DIR}" "${APP_RESOURCES_DIR}/${LPROJ_DIR:t}"
|
|
73
85
|
cp -R "${LPROJ_DIR}" "${BUILD_DIR}/${LPROJ_DIR:t}"
|
|
74
86
|
done
|
|
75
|
-
|
|
87
|
+
cc_pets_publish_binary "${BUILD_DIR}/cc-pets" "${APP_MACOS_DIR}/cc-pets"
|
|
76
88
|
cp "${PROJECT_DIR}/Resources/Info.plist" "${APP_DIR}/Contents/Info.plist"
|
|
77
89
|
cp "${PROJECT_DIR}/Resources/AppIcon.icns" "${APP_RESOURCES_DIR}/AppIcon.icns"
|
|
78
90
|
/usr/libexec/PlistBuddy -c "Add :CFBundleShortVersionString string ${PACKAGE_VERSION}" "${APP_DIR}/Contents/Info.plist"
|
|
@@ -85,5 +97,15 @@ for LOCALIZATION in ${(u)LOCALIZATIONS}; do
|
|
|
85
97
|
"${APP_DIR}/Contents/Info.plist"
|
|
86
98
|
done
|
|
87
99
|
/usr/bin/codesign --force --sign - "${APP_DIR}" >/dev/null
|
|
100
|
+
/usr/bin/codesign --verify --strict "${APP_DIR}"
|
|
101
|
+
# 磁盘签名可验证不等于能执行;被系统以 SIGKILL 拒绝时不能仍然报告构建成功。
|
|
102
|
+
BUILT_VERSION="$("${BUILD_DIR}/cc-pets" --version)" || {
|
|
103
|
+
print -u2 -- '[build] cc-pets could not execute --version after signing.'
|
|
104
|
+
exit 1
|
|
105
|
+
}
|
|
106
|
+
if [[ "${BUILT_VERSION}" != "cc-pets ${PACKAGE_VERSION}" ]]; then
|
|
107
|
+
print -u2 -- "[build] Unexpected executable version: ${BUILT_VERSION}"
|
|
108
|
+
exit 1
|
|
109
|
+
fi
|
|
88
110
|
print "$(cc_pets_t "Built: {buildDir}/cc-pets" buildDir="${BUILD_DIR}")"
|
|
89
111
|
print "$(cc_pets_t "App bundle ready: {appDir}" appDir="${APP_DIR}")"
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# 内部构建助手:仅接受 build.sh / tests/test.sh 使用的 clang 编译并链接参数。
|
|
2
|
+
# 不缓存测试结果;缓存放在现有 clean 命令管理的 clang-cache 目录内。
|
|
3
|
+
# Mach-O 不能用 cp 原地覆盖:旧 inode 可能仍关联着内核缓存的签名信息。
|
|
4
|
+
# 同目录暂存、签名和校验完成后再 rename,让裸 CLI、App 和 harness 都拿到新 inode。
|
|
5
|
+
cc_pets_publish_binary() (
|
|
6
|
+
emulate -L zsh
|
|
7
|
+
set -eu
|
|
8
|
+
local source_binary="$1" target_binary="$2"
|
|
9
|
+
local publish_dir="$(mktemp -d "${target_binary:h}/.${target_binary:t}.publish.XXXXXX")"
|
|
10
|
+
trap 'rm -rf -- "$publish_dir"' EXIT
|
|
11
|
+
trap 'exit 130' INT
|
|
12
|
+
trap 'exit 143' TERM
|
|
13
|
+
local staged_binary="${publish_dir}/${target_binary:t}"
|
|
14
|
+
cp -p "$source_binary" "$staged_binary"
|
|
15
|
+
/usr/bin/codesign --force --sign - "$staged_binary" >/dev/null
|
|
16
|
+
/usr/bin/codesign --verify --strict "$staged_binary"
|
|
17
|
+
mv -f "$staged_binary" "$target_binary"
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
cc_pets_compile() (
|
|
21
|
+
emulate -L zsh
|
|
22
|
+
set -eu
|
|
23
|
+
setopt PIPE_FAIL
|
|
24
|
+
local started=$SECONDS
|
|
25
|
+
local compiler="${commands[clang]:?clang not found}"
|
|
26
|
+
local root="${PROJECT_DIR:?}"
|
|
27
|
+
local cache="${root}/.build/clang-cache/cc-pets"
|
|
28
|
+
local output='' arg source_file key object_file fingerprint sdk
|
|
29
|
+
local -a compile_flags=() link_args=() sources=() object_keys=()
|
|
30
|
+
# framework / library 参数只影响链接;这样不同 harness 能共用相同的目标文件。
|
|
31
|
+
while (( $# )); do
|
|
32
|
+
arg="$1"
|
|
33
|
+
shift
|
|
34
|
+
case "$arg" in
|
|
35
|
+
-o) output="$1"; shift ;;
|
|
36
|
+
-framework) link_args+=("$arg" "$1"); shift ;;
|
|
37
|
+
-l*) link_args+=("$arg") ;;
|
|
38
|
+
*.m) sources+=("$arg"); link_args+=("$arg") ;;
|
|
39
|
+
*) compile_flags+=("$arg"); link_args+=("$arg") ;;
|
|
40
|
+
esac
|
|
41
|
+
done
|
|
42
|
+
[[ -n "$output" && ${#sources} -gt 0 ]] || {
|
|
43
|
+
print -u2 -- 'cc_pets_compile: expected .m inputs and -o output'
|
|
44
|
+
return 1
|
|
45
|
+
}
|
|
46
|
+
if [[ "${CC_PETS_NO_BUILD_CACHE:-0}" == 1 ]]; then
|
|
47
|
+
print -u2 -- '[compile] cache disabled'
|
|
48
|
+
local uncached_work="$(mktemp -d "${output:h}/.${output:t}.compile.XXXXXX")"
|
|
49
|
+
trap 'rm -rf -- "$uncached_work"' EXIT
|
|
50
|
+
trap 'exit 130' INT
|
|
51
|
+
trap 'exit 143' TERM
|
|
52
|
+
"$compiler" "${link_args[@]}" -o "${uncached_work}/binary"
|
|
53
|
+
cc_pets_publish_binary "${uncached_work}/binary" "$output"
|
|
54
|
+
return
|
|
55
|
+
fi
|
|
56
|
+
print -u2 -- "[compile] ${output:t}: checking ${#sources} sources"
|
|
57
|
+
|
|
58
|
+
# 内容摘要避免 checkout、复制或时间戳保留造成的误命中。头文件保守地整体失效,
|
|
59
|
+
# 同时覆盖增删和重命名。工具链、SDK、架构、参数与编译环境变化也必须失效。
|
|
60
|
+
sdk="$(xcrun --show-sdk-path)"
|
|
61
|
+
local -a headers=("${root}"/Sources/CCPets/**/*.(h|inc|def)(N)
|
|
62
|
+
"${root}"/tests/**/*.(h|inc|def)(N))
|
|
63
|
+
fingerprint="$(
|
|
64
|
+
{
|
|
65
|
+
print -r -- 'cc-pets-compile-cache-v1'
|
|
66
|
+
print -r -- "$root" "$PWD" "$compiler" "$sdk"
|
|
67
|
+
"$compiler" --version
|
|
68
|
+
xcrun --show-sdk-version
|
|
69
|
+
/usr/bin/sw_vers -buildVersion
|
|
70
|
+
/usr/bin/uname -m
|
|
71
|
+
# 只记录会影响编译/链接的环境;HOME、测试隔离目录不影响生成的代码。
|
|
72
|
+
local name
|
|
73
|
+
for name in DEVELOPER_DIR SDKROOT MACOSX_DEPLOYMENT_TARGET CPATH \
|
|
74
|
+
C_INCLUDE_PATH OBJC_INCLUDE_PATH CPLUS_INCLUDE_PATH LIBRARY_PATH \
|
|
75
|
+
COMPILER_PATH GCC_EXEC_PREFIX CCC_OVERRIDE_OPTIONS SOURCE_DATE_EPOCH \
|
|
76
|
+
ZERO_AR_DATE RC_ARCHS; do
|
|
77
|
+
print -r -- "$name=${(P)name-}"
|
|
78
|
+
done
|
|
79
|
+
/usr/bin/shasum -a 256 "${root}/scripts/compile-cached.zsh" "${headers[@]}"
|
|
80
|
+
[[ ! -f "${sdk}/SDKSettings.json" ]] || /usr/bin/shasum -a 256 "${sdk}/SDKSettings.json"
|
|
81
|
+
} | /usr/bin/shasum -a 256
|
|
82
|
+
)"
|
|
83
|
+
mkdir -p "${cache}/objects" "${cache}/binaries"
|
|
84
|
+
local work="$(mktemp -d "${cache}/work.XXXXXX")"
|
|
85
|
+
# 只删除本次调用创建的工作目录;失败和中断不会留下可命中的半成品。
|
|
86
|
+
trap 'rm -rf -- "$work"' EXIT
|
|
87
|
+
trap 'exit 130' INT
|
|
88
|
+
trap 'exit 143' TERM
|
|
89
|
+
local flags_key="$(print -rn -- "${(pj:\0:)compile_flags}" | /usr/bin/shasum -a 256)"
|
|
90
|
+
local built=0 reused=0 index=0
|
|
91
|
+
typeset -A objects=()
|
|
92
|
+
for source_file in "${sources[@]}"; do
|
|
93
|
+
key="$(
|
|
94
|
+
{ print -r -- "$fingerprint" "$flags_key" "$source_file"
|
|
95
|
+
/usr/bin/shasum -a 256 "$source_file"
|
|
96
|
+
} | /usr/bin/shasum -a 256
|
|
97
|
+
)"
|
|
98
|
+
key="${key%% *}"
|
|
99
|
+
object_file="${cache}/objects/${key}.o"
|
|
100
|
+
if [[ ! -f "$object_file" ]]; then
|
|
101
|
+
print -u2 -- "[compile] ${output:t}: ${source_file:t}"
|
|
102
|
+
"$compiler" "${compile_flags[@]}" -c "$source_file" -o "${work}/object.o"
|
|
103
|
+
mv -f "${work}/object.o" "$object_file"
|
|
104
|
+
(( ++built ))
|
|
105
|
+
else
|
|
106
|
+
(( ++reused ))
|
|
107
|
+
fi
|
|
108
|
+
objects[$source_file]="$object_file"
|
|
109
|
+
object_keys+=("$key")
|
|
110
|
+
done
|
|
111
|
+
for (( index=1; index<=${#link_args}; ++index )); do
|
|
112
|
+
arg="${link_args[$index]}"
|
|
113
|
+
if (( ${+objects[$arg]} )); then
|
|
114
|
+
link_args[$index]="${objects[$arg]}"
|
|
115
|
+
fi
|
|
116
|
+
done
|
|
117
|
+
key="$(
|
|
118
|
+
{ print -r -- "$fingerprint"
|
|
119
|
+
print -rn -- "${(pj:\0:)link_args}" "${(pj:\0:)object_keys}"
|
|
120
|
+
} | /usr/bin/shasum -a 256
|
|
121
|
+
)"
|
|
122
|
+
local binary="${cache}/binaries/${key%% *}"
|
|
123
|
+
local linked='cached'
|
|
124
|
+
if [[ ! -x "$binary" ]]; then
|
|
125
|
+
print -u2 -- "[compile] ${output:t}: linking"
|
|
126
|
+
"$compiler" "${link_args[@]}" -o "${work}/binary"
|
|
127
|
+
mv -f "${work}/binary" "$binary"
|
|
128
|
+
linked='linked'
|
|
129
|
+
fi
|
|
130
|
+
# 不改动缓存原件,也不原地覆盖可能正在被 Hook / CLI 使用的可执行文件。
|
|
131
|
+
cc_pets_publish_binary "$binary" "$output"
|
|
132
|
+
print -u2 -- "[compile] ${output:t}: compiled=${built}, reused=${reused}, ${linked}, $(( SECONDS - started ))s"
|
|
133
|
+
)
|