Skip to content

Commit c1a6f9b

Browse files
committed
✨ feat(inspect added):
1 parent ee898f8 commit c1a6f9b

3 files changed

Lines changed: 126 additions & 3 deletions

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,10 @@
8181

8282
- 将图标更换为透明背景
8383
- 完善文档format changelog 格式
84-
- 支持 TSX平台上的 .github workflow CICD
84+
- 支持 TSX平台上的 .github workflow
85+
86+
## [0.0.5] - 2025-09-05
87+
88+
### Added
89+
90+
- 支持通过右键单击合约文件 inspect 生成合约abi文件

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "forge commander",
44
"description": "deploy contract easily with forge",
55
"publisher": "MrCar",
6-
"version": "0.0.4",
6+
"version": "0.0.5",
77
"engines": {
88
"vscode": "^1.75.0"
99
},
@@ -14,7 +14,8 @@
1414
"activationEvents": [
1515
"onCommand:forge-deploy.helloWorld",
1616
"onCommand:forge-deploy.deployContract",
17-
"onCommand:forge-deploy.executeScript"
17+
"onCommand:forge-deploy.executeScript",
18+
"onCommand:forge-deploy.inspectContract"
1819
],
1920
"contributes": {
2021
"commands": [
@@ -27,6 +28,11 @@
2728
"title": "部署智能合约",
2829
"category": "Forge Deploy"
2930
},
31+
{
32+
"command": "forge-deploy.inspectContract",
33+
"title": "生成合约 ABI JSON",
34+
"category": "Forge Deploy"
35+
},
3036
{
3137
"command": "forge-deploy.clearTerminal",
3238
"title": "清理 Forge 终端",
@@ -39,6 +45,11 @@
3945
"command": "forge-deploy.deployContract",
4046
"when": "resourceExtname == .sol",
4147
"group": "navigation@1"
48+
},
49+
{
50+
"command": "forge-deploy.inspectContract",
51+
"when": "resourceExtname == .sol",
52+
"group": "navigation@2"
4253
}
4354
],
4455
"commandPalette": [

src/extension.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ export function activate(context: vscode.ExtensionContext) {
119119
const filePath = uri.fsPath;
120120
const fileName = path.basename(filePath);
121121

122+
// 检查是否为测试或脚本文件
123+
if (fileName.endsWith('.t.sol') || fileName.endsWith('.s.sol')) {
124+
vscode.window.showWarningMessage('测试文件(.t.sol)和脚本文件(.s.sol)不支持部署操作');
125+
return;
126+
}
127+
122128
try {
123129
// 1. 搜索文件中的合约
124130
vscode.window.showInformationMessage(`🔍 正在搜索文件 "${fileName}" 中的合约...`);
@@ -255,6 +261,105 @@ export function activate(context: vscode.ExtensionContext) {
255261
}
256262
});
257263

264+
// 注册生成合约ABI JSON命令
265+
const inspectContractDisposable = vscode.commands.registerCommand('forge-deploy.inspectContract', async (uri: vscode.Uri) => {
266+
console.log('生成合约ABI JSON命令被触发,URI:', uri);
267+
268+
if (!uri) {
269+
vscode.window.showErrorMessage('无法获取文件路径');
270+
return;
271+
}
272+
273+
const filePath = uri.fsPath;
274+
const fileName = path.basename(filePath);
275+
const fileDir = path.dirname(filePath);
276+
277+
// 检查是否为测试或脚本文件
278+
if (fileName.endsWith('.t.sol') || fileName.endsWith('.s.sol')) {
279+
vscode.window.showWarningMessage('测试文件(.t.sol)和脚本文件(.s.sol)不支持ABI生成操作');
280+
return;
281+
}
282+
283+
try {
284+
// 1. 搜索文件中的合约
285+
vscode.window.showInformationMessage(`🔍 正在搜索文件 "${fileName}" 中的合约...`);
286+
const contracts = await _findContractsInFile(filePath);
287+
288+
if (contracts.length === 0) {
289+
vscode.window.showErrorMessage('在文件中未找到任何合约');
290+
return;
291+
}
292+
293+
// 2. 选择合约
294+
const contractNames = contracts.map(c => `${c.name} (${c.file}:${c.line})`);
295+
const selectedContract = await vscode.window.showQuickPick(contractNames, {
296+
placeHolder: '选择要生成ABI的合约'
297+
});
298+
299+
if (!selectedContract) {
300+
return;
301+
}
302+
303+
const contractName = selectedContract.split(' ')[0];
304+
305+
// 3. 输入输出文件名
306+
const defaultFileName = `${contractName}_abi.json`;
307+
const outputFileName = await vscode.window.showInputBox({
308+
prompt: '输入ABI JSON文件名',
309+
value: defaultFileName,
310+
placeHolder: '例如: MyContract_abi.json'
311+
});
312+
313+
if (!outputFileName) {
314+
return;
315+
}
316+
317+
// 4. 构建inspect命令
318+
const outputPath = path.join(fileDir, outputFileName);
319+
320+
// 获取工作区根目录
321+
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
322+
if (!workspaceFolder) {
323+
vscode.window.showErrorMessage('无法获取工作区根目录');
324+
return;
325+
}
326+
327+
// 计算相对于工作区根目录的路径
328+
const relativePath = path.relative(workspaceFolder.uri.fsPath, filePath);
329+
330+
// 构建forge inspect命令,使用相对路径格式 src/ContractName
331+
const inspectCommand = `forge inspect ${relativePath}:${contractName} abi --json > "${outputFileName}"`;
332+
333+
// 5. 确认生成
334+
const confirmPick = await vscode.window.showQuickPick(
335+
[
336+
{ label: '确认生成', value: true },
337+
{ label: '取消', value: false }
338+
],
339+
{ placeHolder: `确认为合约 "${contractName}" 生成ABI JSON文件?` }
340+
);
341+
342+
if (confirmPick && confirmPick.value) {
343+
if (!forgeTerminal) {
344+
forgeTerminal = vscode.window.createTerminal({
345+
name: 'Forge Deploy Terminal',
346+
cwd: workspaceFolder.uri.fsPath
347+
});
348+
}
349+
350+
forgeTerminal.show();
351+
forgeTerminal.sendText(inspectCommand);
352+
353+
vscode.window.showInformationMessage(
354+
`📄 ABI JSON生成命令已发送,文件将保存为: ${outputFileName}`
355+
);
356+
}
357+
358+
} catch (error) {
359+
vscode.window.showErrorMessage(`生成ABI JSON过程中发生错误: ${error}`);
360+
}
361+
});
362+
258363
// 注册执行脚本命令
259364
const executeScriptDisposable = vscode.commands.registerCommand('forge-deploy.executeScript', async (uri: vscode.Uri) => {
260365
console.log('执行脚本命令被触发,URI:', uri);
@@ -327,6 +432,7 @@ export function activate(context: vscode.ExtensionContext) {
327432
context.subscriptions.push(
328433
helloWorldDisposable,
329434
deployContractDisposable,
435+
inspectContractDisposable,
330436
executeScriptDisposable,
331437
clearTerminalDisposable
332438
);

0 commit comments

Comments
 (0)