@@ -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