feat:将调用node.js的fs模块改为vscode官方的API
- 这样可以避免用户本地没有node环境导致插件无法运行的原因
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
import * as vscode from "vscode";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import { spawn } from "child_process";
|
||||
import { promisify } from "util";
|
||||
@ -107,13 +106,16 @@ export async function checkVerilogProject(
|
||||
|
||||
try {
|
||||
// 检查项目路径是否存在
|
||||
if (!fs.existsSync(projectPath)) {
|
||||
const projectUri = vscode.Uri.file(projectPath);
|
||||
try {
|
||||
await vscode.workspace.fs.stat(projectUri);
|
||||
} catch (error) {
|
||||
result.errors.push(`项目路径不存在: ${projectPath}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
// 查找所有 Verilog 文件 (.v, .sv)
|
||||
const verilogFiles = findVerilogFiles(projectPath);
|
||||
const verilogFiles = await findVerilogFiles(projectPath);
|
||||
result.allVerilogFiles = verilogFiles;
|
||||
|
||||
if (verilogFiles.length === 0) {
|
||||
@ -123,7 +125,9 @@ export async function checkVerilogProject(
|
||||
|
||||
// 分析文件内容,查找 top module 和 testbench
|
||||
for (const file of verilogFiles) {
|
||||
const content = fs.readFileSync(file, "utf-8");
|
||||
const fileUri = vscode.Uri.file(file);
|
||||
const contentBytes = await vscode.workspace.fs.readFile(fileUri);
|
||||
const content = Buffer.from(contentBytes).toString("utf-8");
|
||||
const fileName = path.basename(file).toLowerCase();
|
||||
|
||||
// 检查是否是 testbench 文件
|
||||
@ -169,23 +173,23 @@ export async function checkVerilogProject(
|
||||
/**
|
||||
* 递归查找目录下所有 Verilog 文件
|
||||
*/
|
||||
function findVerilogFiles(dir: string): string[] {
|
||||
async function findVerilogFiles(dir: string): Promise<string[]> {
|
||||
const verilogFiles: string[] = [];
|
||||
|
||||
function searchDir(currentDir: string) {
|
||||
const files = fs.readdirSync(currentDir);
|
||||
async function searchDir(currentDir: string) {
|
||||
const dirUri = vscode.Uri.file(currentDir);
|
||||
const entries = await vscode.workspace.fs.readDirectory(dirUri);
|
||||
|
||||
for (const file of files) {
|
||||
const filePath = path.join(currentDir, file);
|
||||
const stat = fs.statSync(filePath);
|
||||
for (const [fileName, fileType] of entries) {
|
||||
const filePath = path.join(currentDir, fileName);
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
if (fileType === vscode.FileType.Directory) {
|
||||
// 跳过 node_modules 等目录
|
||||
if (!file.startsWith(".") && file !== "node_modules") {
|
||||
searchDir(filePath);
|
||||
if (!fileName.startsWith(".") && fileName !== "node_modules") {
|
||||
await searchDir(filePath);
|
||||
}
|
||||
} else if (stat.isFile()) {
|
||||
const ext = path.extname(file).toLowerCase();
|
||||
} else if (fileType === vscode.FileType.File) {
|
||||
const ext = path.extname(fileName).toLowerCase();
|
||||
if (ext === ".v" || ext === ".sv") {
|
||||
verilogFiles.push(filePath);
|
||||
}
|
||||
@ -193,14 +197,14 @@ function findVerilogFiles(dir: string): string[] {
|
||||
}
|
||||
}
|
||||
|
||||
searchDir(dir);
|
||||
await searchDir(dir);
|
||||
return verilogFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 iverilog 可执行文件路径
|
||||
*/
|
||||
function getIverilogPath(extensionPath: string): string {
|
||||
async function getIverilogPath(extensionPath: string): Promise<string> {
|
||||
const platform = process.platform;
|
||||
let iverilogBin = "";
|
||||
|
||||
@ -214,17 +218,19 @@ function getIverilogPath(extensionPath: string): string {
|
||||
}
|
||||
|
||||
// 如果插件包中没有,尝试使用系统安装的 iverilog
|
||||
if (!fs.existsSync(iverilogBin)) {
|
||||
const iverilogUri = vscode.Uri.file(iverilogBin);
|
||||
try {
|
||||
await vscode.workspace.fs.stat(iverilogUri);
|
||||
return iverilogBin;
|
||||
} catch {
|
||||
return "iverilog"; // 使用系统 PATH 中的 iverilog
|
||||
}
|
||||
|
||||
return iverilogBin;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 vvp 可执行文件路径
|
||||
*/
|
||||
function getVvpPath(extensionPath: string): string {
|
||||
async function getVvpPath(extensionPath: string): Promise<string> {
|
||||
const platform = process.platform;
|
||||
let vvpBin = "";
|
||||
|
||||
@ -238,11 +244,13 @@ function getVvpPath(extensionPath: string): string {
|
||||
}
|
||||
|
||||
// 如果插件包中没有,尝试使用系统安装的 vvp
|
||||
if (!fs.existsSync(vvpBin)) {
|
||||
const vvpUri = vscode.Uri.file(vvpBin);
|
||||
try {
|
||||
await vscode.workspace.fs.stat(vvpUri);
|
||||
return vvpBin;
|
||||
} catch {
|
||||
return "vvp"; // 使用系统 PATH 中的 vvp
|
||||
}
|
||||
|
||||
return vvpBin;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -264,8 +272,8 @@ export async function generateVCD(
|
||||
}
|
||||
|
||||
// 2. 获取 iverilog 和 vvp 路径
|
||||
const iverilogPath = getIverilogPath(extensionPath);
|
||||
const vvpPath = getVvpPath(extensionPath);
|
||||
const iverilogPath = await getIverilogPath(extensionPath);
|
||||
const vvpPath = await getVvpPath(extensionPath);
|
||||
|
||||
// 3. 准备输出路径
|
||||
const outputDir = projectPath;
|
||||
@ -320,7 +328,11 @@ export async function generateVCD(
|
||||
}
|
||||
|
||||
// 8. 查找生成的 VCD 文件
|
||||
const vcdFiles = fs.readdirSync(projectPath).filter(file => file.endsWith('.vcd'));
|
||||
const projectUri = vscode.Uri.file(projectPath);
|
||||
const entries = await vscode.workspace.fs.readDirectory(projectUri);
|
||||
const vcdFiles = entries
|
||||
.filter(([fileName, fileType]) => fileType === vscode.FileType.File && fileName.endsWith('.vcd'))
|
||||
.map(([fileName]) => fileName);
|
||||
|
||||
if (vcdFiles.length === 0) {
|
||||
return {
|
||||
@ -335,9 +347,9 @@ export async function generateVCD(
|
||||
|
||||
// 9. 清理中间文件
|
||||
try {
|
||||
if (fs.existsSync(outputFile)) {
|
||||
fs.unlinkSync(outputFile);
|
||||
}
|
||||
const outputUri = vscode.Uri.file(outputFile);
|
||||
await vscode.workspace.fs.stat(outputUri);
|
||||
await vscode.workspace.fs.delete(outputUri);
|
||||
} catch (error) {
|
||||
// 忽略清理错误
|
||||
}
|
||||
@ -364,10 +376,13 @@ export async function checkIverilogAvailable(
|
||||
extensionPath: string
|
||||
): Promise<{ available: boolean; version?: string; message: string }> {
|
||||
try {
|
||||
const iverilogPath = getIverilogPath(extensionPath);
|
||||
const iverilogPath = await getIverilogPath(extensionPath);
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(iverilogPath)) {
|
||||
const iverilogUri = vscode.Uri.file(iverilogPath);
|
||||
try {
|
||||
await vscode.workspace.fs.stat(iverilogUri);
|
||||
} catch (error) {
|
||||
return {
|
||||
available: false,
|
||||
message: `iverilog 不可用。未找到文件: ${iverilogPath}`,
|
||||
|
||||
Reference in New Issue
Block a user