0
0

暂时添加了合并功能,但是存在很多BUG

This commit is contained in:
xubing
2025-11-27 20:10:09 +08:00
parent 9f44664b0b
commit 97c897962f
6 changed files with 1151 additions and 199 deletions

View File

@@ -52,14 +52,6 @@ interface GitFileTree {
children?: GitFileTree[];
}
interface ProjectData {
projects: Project[];
aircrafts: Aircraft[];
containers: Container[];
configs: Config[];
gitRepos: GitRepo[]; // Git 仓库数据整合到项目数据中
}
// Git 分支接口
interface GitBranch {
name: string;
@@ -67,6 +59,27 @@ interface GitBranch {
selected?: boolean;
}
interface MergedFolder {
id: string;
displayName: string; // 配置栏显示的名称
folderName: string; // 实际文件夹名称
path: string;
fileCount: number;
containerId: string;
originalConfigIds: string[];
createdAt: string;
}
// 修改 ProjectData 接口,添加 mergedFolders
interface ProjectData {
projects: Project[];
aircrafts: Aircraft[];
containers: Container[];
configs: Config[];
gitRepos: GitRepo[];
mergedFolders: MergedFolder[]; // 新增合并文件夹数据
}
export class ConfigPanel {
public static currentPanel: ConfigPanel | undefined;
public readonly panel: vscode.WebviewPanel;
@@ -84,6 +97,7 @@ export class ConfigPanel {
private containers: Container[] = [];
private configs: Config[] = [];
private gitRepos: GitRepo[] = []; // Git 仓库数据
private mergedFolders: MergedFolder[] = [];
// Git 文件树
private currentRepoFileTree: GitFileTree[] = [];
@@ -300,6 +314,22 @@ export class ConfigPanel {
case 'openConfigFileInVSCode':
await this.openConfigFileInVSCode(data.configId);
break;
case 'mergeConfigs':
await this.mergeConfigs(data.configIds, data.displayName, data.folderName);
break;
case 'deleteMergedFolder':
await this.deleteMergedFolder(data.folderId);
break;
case 'openMergedFolderInVSCode':
await this.openMergedFolderInVSCode(data.folderId);
break;
case 'loadMergedFolder':
await this.loadMergedFolder(data.folderId);
break;
}
} catch (error) {
console.error('处理 Webview 消息时出错:', error);
@@ -813,108 +843,116 @@ export class ConfigPanel {
* 保存当前项目数据到项目路径
*/
private async saveCurrentProjectData(): Promise<void> {
try {
if (!this.currentProjectId) {
console.warn('未找到当前项目,数据将不会保存');
return;
}
const projectPath = this.projectPaths.get(this.currentProjectId);
if (!projectPath) {
console.warn('未找到项目存储路径,数据将不会保存');
return;
}
const dataUri = vscode.Uri.joinPath(vscode.Uri.file(projectPath), '.dcsp-data.json');
// 只保存与当前项目相关的数据
const currentProjectAircrafts = this.aircrafts.filter(a => a.projectId === this.currentProjectId);
const currentAircraftIds = currentProjectAircrafts.map(a => a.id);
const currentProjectContainers = this.containers.filter(c => currentAircraftIds.includes(c.aircraftId));
const currentContainerIds = currentProjectContainers.map(c => c.id);
const currentProjectConfigs = this.configs.filter(cfg => currentContainerIds.includes(cfg.containerId));
// 只保存与当前项目容器相关的 Git 仓库
const currentProjectGitRepos = this.gitRepos.filter(repo =>
currentContainerIds.includes(repo.containerId)
);
const data: ProjectData = {
projects: this.projects.filter(p => p.id === this.currentProjectId),
aircrafts: currentProjectAircrafts,
containers: currentProjectContainers,
configs: currentProjectConfigs,
gitRepos: currentProjectGitRepos // 保存 Git 仓库数据
};
const uint8Array = new TextEncoder().encode(JSON.stringify(data, null, 2));
await vscode.workspace.fs.writeFile(dataUri, uint8Array);
console.log('✅ 当前项目数据已保存,包含', currentProjectGitRepos.length, '个 Git 仓库');
} catch (error) {
vscode.window.showErrorMessage(`保存项目数据失败: ${error}`);
try {
if (!this.currentProjectId) {
console.warn('未找到当前项目,数据将不会保存');
return;
}
const projectPath = this.projectPaths.get(this.currentProjectId);
if (!projectPath) {
console.warn('未找到项目存储路径,数据将不会保存');
return;
}
const dataUri = vscode.Uri.joinPath(vscode.Uri.file(projectPath), '.dcsp-data.json');
// 只保存与当前项目相关的数据
const currentProjectAircrafts = this.aircrafts.filter(a => a.projectId === this.currentProjectId);
const currentAircraftIds = currentProjectAircrafts.map(a => a.id);
const currentProjectContainers = this.containers.filter(c => currentAircraftIds.includes(c.aircraftId));
const currentContainerIds = currentProjectContainers.map(c => c.id);
const currentProjectConfigs = this.configs.filter(cfg => currentContainerIds.includes(cfg.containerId));
// 只保存与当前项目容器相关的 Git 仓库和合并文件夹
const currentProjectGitRepos = this.gitRepos.filter(repo =>
currentContainerIds.includes(repo.containerId)
);
const currentProjectMergedFolders = this.mergedFolders.filter(folder =>
currentContainerIds.includes(folder.containerId)
);
const data: ProjectData = {
projects: this.projects.filter(p => p.id === this.currentProjectId),
aircrafts: currentProjectAircrafts,
containers: currentProjectContainers,
configs: currentProjectConfigs,
gitRepos: currentProjectGitRepos,
mergedFolders: currentProjectMergedFolders // 保存合并文件夹数据
};
const uint8Array = new TextEncoder().encode(JSON.stringify(data, null, 2));
await vscode.workspace.fs.writeFile(dataUri, uint8Array);
console.log('✅ 当前项目数据已保存,包含', currentProjectGitRepos.length, '个 Git 仓库和', currentProjectMergedFolders.length, '个合并文件夹');
} catch (error) {
vscode.window.showErrorMessage(`保存项目数据失败: ${error}`);
}
}
/**
* 从项目路径加载数据
*/
private async loadProjectData(projectPath: string): Promise<boolean> {
try {
const dataUri = vscode.Uri.joinPath(vscode.Uri.file(projectPath), '.dcsp-data.json');
// 检查数据文件是否存在
try {
const dataUri = vscode.Uri.joinPath(vscode.Uri.file(projectPath), '.dcsp-data.json');
// 检查数据文件是否存在
try {
await vscode.workspace.fs.stat(dataUri);
} catch {
vscode.window.showErrorMessage('选择的文件夹中没有找到项目数据文件 (.dcsp-data.json)');
return false;
}
// 读取数据文件
const fileData = await vscode.workspace.fs.readFile(dataUri);
const dataStr = new TextDecoder().decode(fileData);
const data: ProjectData = JSON.parse(dataStr);
// 清空现有数据
this.projects = [];
this.aircrafts = [];
this.containers = [];
this.configs = [];
this.gitRepos = []; // 清空 Git 仓库数据
// 验证数据格式并加载
if (data.projects && Array.isArray(data.projects)) {
this.projects = data.projects;
}
if (data.aircrafts && Array.isArray(data.aircrafts)) {
this.aircrafts = data.aircrafts;
}
if (data.containers && Array.isArray(data.containers)) {
this.containers = data.containers;
}
if (data.configs && Array.isArray(data.configs)) {
this.configs = data.configs;
}
if (data.gitRepos && Array.isArray(data.gitRepos)) {
this.gitRepos = data.gitRepos; // 加载 Git 仓库数据
}
// 设置当前项目为第一个项目(如果有的话)
if (this.projects.length > 0) {
this.currentProjectId = this.projects[0].id;
this.projectPaths.set(this.currentProjectId, projectPath);
this.currentView = 'aircrafts';
}
vscode.window.showInformationMessage(`项目数据已从 ${projectPath} 加载,包含 ${this.gitRepos.length} 个 Git 仓库`);
this.updateWebview();
return true;
} catch (error) {
vscode.window.showErrorMessage(`加载项目数据失败: ${error}`);
await vscode.workspace.fs.stat(dataUri);
} catch {
vscode.window.showErrorMessage('选择的文件夹中没有找到项目数据文件 (.dcsp-data.json)');
return false;
}
// 读取数据文件
const fileData = await vscode.workspace.fs.readFile(dataUri);
const dataStr = new TextDecoder().decode(fileData);
const data: ProjectData = JSON.parse(dataStr);
// 清空现有数据
this.projects = [];
this.aircrafts = [];
this.containers = [];
this.configs = [];
this.gitRepos = [];
this.mergedFolders = []; // 清空合并文件夹数据
// 验证数据格式并加载
if (data.projects && Array.isArray(data.projects)) {
this.projects = data.projects;
}
if (data.aircrafts && Array.isArray(data.aircrafts)) {
this.aircrafts = data.aircrafts;
}
if (data.containers && Array.isArray(data.containers)) {
this.containers = data.containers;
}
if (data.configs && Array.isArray(data.configs)) {
this.configs = data.configs;
}
if (data.gitRepos && Array.isArray(data.gitRepos)) {
this.gitRepos = data.gitRepos;
}
if (data.mergedFolders && Array.isArray(data.mergedFolders)) {
this.mergedFolders = data.mergedFolders; // 加载合并文件夹数据
}
// 设置当前项目为第一个项目(如果有的话)
if (this.projects.length > 0) {
this.currentProjectId = this.projects[0].id;
this.projectPaths.set(this.currentProjectId, projectPath);
this.currentView = 'aircrafts';
}
vscode.window.showInformationMessage(`项目数据已从 ${projectPath} 加载,包含 ${this.gitRepos.length} 个 Git 仓库和 ${this.mergedFolders.length} 个合并文件夹`);
this.updateWebview();
return true;
} catch (error) {
vscode.window.showErrorMessage(`加载项目数据失败: ${error}`);
return false;
}
}
/**
* 检查项目路径是否已存在数据
@@ -1480,50 +1518,52 @@ private buildBranchTree(branches: GitBranch[]): any[] {
}
private getWebviewContent(): string {
switch (this.currentView) {
case 'projects':
return this.projectView.render({
projects: this.projects,
projectPaths: this.projectPaths
});
case 'aircrafts':
const projectAircrafts = this.aircrafts.filter(a => a.projectId === this.currentProjectId);
return this.aircraftView.render({
aircrafts: projectAircrafts
});
case 'containers':
const currentProject = this.projects.find(p => p.id === this.currentProjectId);
const currentAircraft = this.aircrafts.find(a => a.id === this.currentAircraftId);
const projectContainers = this.containers.filter(c => c.aircraftId === this.currentAircraftId);
return this.containerView.render({
project: currentProject,
aircraft: currentAircraft,
containers: projectContainers
});
case 'configs':
const currentContainer = this.containers.find(c => c.id === this.currentContainerId);
const containerConfigs = this.configs.filter(cfg => cfg.containerId === this.currentContainerId);
const currentRepo = this.gitRepos.find(r => r.id === this.currentRepoId);
// 获取当前容器的 Git 仓库
const containerGitRepos = this.gitRepos.filter(repo => repo.containerId === this.currentContainerId);
return this.configView.render({
container: currentContainer,
configs: containerConfigs,
gitRepos: containerGitRepos, // 只显示当前容器的 Git 仓库
currentGitRepo: currentRepo,
gitFileTree: this.currentRepoFileTree,
gitLoading: false
});
default:
return this.projectView.render({
projects: this.projects,
projectPaths: this.projectPaths
});
}
switch (this.currentView) {
case 'projects':
return this.projectView.render({
projects: this.projects,
projectPaths: this.projectPaths
});
case 'aircrafts':
const projectAircrafts = this.aircrafts.filter(a => a.projectId === this.currentProjectId);
return this.aircraftView.render({
aircrafts: projectAircrafts
});
case 'containers':
const currentProject = this.projects.find(p => p.id === this.currentProjectId);
const currentAircraft = this.aircrafts.find(a => a.id === this.currentAircraftId);
const projectContainers = this.containers.filter(c => c.aircraftId === this.currentAircraftId);
return this.containerView.render({
project: currentProject,
aircraft: currentAircraft,
containers: projectContainers
});
case 'configs':
const currentContainer = this.containers.find(c => c.id === this.currentContainerId);
const containerConfigs = this.configs.filter(cfg => cfg.containerId === this.currentContainerId);
const currentRepo = this.gitRepos.find(r => r.id === this.currentRepoId);
// 获取当前容器的 Git 仓库和合并文件夹
const containerGitRepos = this.gitRepos.filter(repo => repo.containerId === this.currentContainerId);
const containerMergedFolders = this.mergedFolders.filter(folder => folder.containerId === this.currentContainerId);
return this.configView.render({
container: currentContainer,
configs: containerConfigs,
gitRepos: containerGitRepos,
currentGitRepo: currentRepo,
gitFileTree: this.currentRepoFileTree,
gitLoading: false,
mergedFolders: containerMergedFolders // 传递合并文件夹数据
});
default:
return this.projectView.render({
projects: this.projects,
projectPaths: this.projectPaths
});
}
}
private async openGitRepoInVSCode(repoId: string): Promise<void> {
const repo = this.gitRepos.find(r => r.id === repoId);
@@ -1608,4 +1648,226 @@ private buildBranchTree(branches: GitBranch[]): any[] {
vscode.window.showErrorMessage(`打开配置文件失败: ${error}`);
}
}
private async mergeConfigs(configIds: string[], displayName: string, folderName: string): Promise<void> {
try {
if (!this.currentContainerId) {
vscode.window.showErrorMessage('未找到当前容器');
return;
}
if (configIds.length < 2) {
vscode.window.showErrorMessage('请至少选择两个配置文件进行合并');
return;
}
const container = this.containers.find(c => c.id === this.currentContainerId);
if (!container) {
vscode.window.showErrorMessage('未找到容器');
return;
}
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
if (!aircraft) {
vscode.window.showErrorMessage('未找到飞行器');
return;
}
const projectPath = this.projectPaths.get(aircraft.projectId);
if (!projectPath) {
vscode.window.showErrorMessage('未设置项目存储路径');
return;
}
// 获取选中的配置文件
const selectedConfigs = this.configs.filter(config => configIds.includes(config.id));
if (selectedConfigs.length !== configIds.length) {
vscode.window.showErrorMessage('部分配置文件未找到');
return;
}
// 创建合并文件夹
const mergeFolderPath = path.join(projectPath, aircraft.name, container.name, folderName);
await fs.promises.mkdir(mergeFolderPath, { recursive: true });
// 将选中的配置文件复制到合并文件夹中
const copiedFiles: string[] = [];
for (const config of selectedConfigs) {
const sourcePath = path.join(projectPath, aircraft.name, container.name, config.fileName);
const targetPath = path.join(mergeFolderPath, config.fileName);
if (fs.existsSync(sourcePath)) {
await fs.promises.copyFile(sourcePath, targetPath);
copiedFiles.push(config.fileName);
console.log(`✅ 已复制配置文件: ${config.fileName}`);
} else {
// 如果源文件不存在,创建新文件
await fs.promises.writeFile(targetPath, config.content || '');
copiedFiles.push(config.fileName);
console.log(`✅ 已创建配置文件: ${config.fileName}`);
}
}
// 创建合并文件夹记录
const newFolder: MergedFolder = {
id: 'merged-' + Date.now(),
displayName: displayName, // 使用传入的显示名称
folderName: folderName, // 使用传入的文件夹名称
path: mergeFolderPath,
fileCount: copiedFiles.length,
containerId: this.currentContainerId,
originalConfigIds: configIds,
createdAt: new Date().toLocaleString()
};
// 添加到合并文件夹列表
this.mergedFolders.push(newFolder);
// 删除原始配置文件
for (const configId of configIds) {
await this.deleteConfigInternal(configId);
}
// 保存数据
await this.saveCurrentProjectData();
vscode.window.showInformationMessage(`成功合并 ${selectedConfigs.length} 个配置文件到文件夹: ${folderName}`);
// 更新UI不自动打开文件夹
this.updateWebview();
} catch (error) {
console.error('❌ 合并配置文件失败:', error);
vscode.window.showErrorMessage(`合并配置文件失败: ${error}`);
}
}
/**
* 内部删除配置文件方法(不显示确认对话框)
*/
private async deleteConfigInternal(configId: string): Promise<void> {
try {
const config = this.configs.find(c => c.id === configId);
if (!config) return;
// 从内存中删除配置
this.configs = this.configs.filter(c => c.id !== configId);
// 删除磁盘上的配置文件
const container = this.containers.find(c => c.id === config.containerId);
if (container) {
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
if (aircraft) {
const projectPath = this.projectPaths.get(aircraft.projectId);
if (projectPath) {
const filePath = path.join(projectPath, aircraft.name, container.name, config.fileName);
// 检查文件是否存在,如果存在则删除
if (fs.existsSync(filePath)) {
await fs.promises.unlink(filePath);
console.log(`✅ 已删除配置文件: ${filePath}`);
}
}
}
}
console.log(`✅ 内部删除配置: ${config.name}`);
} catch (error) {
console.error(`删除配置文件失败: ${error}`);
}
}
/**
* 删除合并文件夹
*/
private async deleteMergedFolder(folderId: string): Promise<void> {
const folder = this.mergedFolders.find(f => f.id === folderId);
if (!folder) return;
const confirm = await vscode.window.showWarningMessage(
`确定要删除合并文件夹 "${folder.displayName}" 吗?这将删除文件夹及其所有内容。`,
{ modal: true },
'确定删除',
'取消'
);
if (confirm === '确定删除') {
try {
// 删除文件夹及其所有内容
await fs.promises.rm(folder.path, { recursive: true, force: true });
// 从列表中移除
this.mergedFolders = this.mergedFolders.filter(f => f.id !== folderId);
await this.saveCurrentProjectData();
vscode.window.showInformationMessage(`合并文件夹已删除: ${folder.displayName}`);
this.updateWebview();
} catch (error) {
vscode.window.showErrorMessage(`删除合并文件夹失败: ${error}`);
}
}
}
/**
* 在 VSCode 中打开合并文件夹
*/
private async openMergedFolderInVSCode(folderId: string): Promise<void> {
const folder = this.mergedFolders.find(f => f.id === folderId);
if (!folder) {
vscode.window.showErrorMessage('未找到指定的合并文件夹');
return;
}
try {
// 检查文件夹是否存在
if (!fs.existsSync(folder.path)) {
vscode.window.showErrorMessage('合并文件夹不存在');
return;
}
// 使用 VSCode 打开文件夹
const folderUri = vscode.Uri.file(folder.path);
vscode.commands.executeCommand('vscode.openFolder', folderUri, { forceNewWindow: false });
} catch (error) {
vscode.window.showErrorMessage(`打开合并文件夹失败: ${error}`);
}
}
private async loadMergedFolder(folderId: string): Promise<void> {
const folder = this.mergedFolders.find(f => f.id === folderId);
if (!folder) {
vscode.window.showErrorMessage('未找到指定的合并文件夹');
return;
}
try {
// 检查文件夹是否存在
if (!fs.existsSync(folder.path)) {
vscode.window.showErrorMessage('合并文件夹目录不存在');
return;
}
// 使用 VSCode 的文件选择器让用户选择要打开的文件
const fileUri = await vscode.window.showOpenDialog({
defaultUri: vscode.Uri.file(folder.path),
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: false,
openLabel: '选择要打开的文件',
title: `在合并文件夹 ${folder.displayName} 中选择文件`
});
if (fileUri && fileUri.length > 0) {
// 打开选中的文件
const document = await vscode.workspace.openTextDocument(fileUri[0]);
await vscode.window.showTextDocument(document);
vscode.window.showInformationMessage(`已打开文件: ${path.basename(fileUri[0].fsPath)}`);
}
} catch (error) {
vscode.window.showErrorMessage(`打开合并文件夹文件失败: ${error}`);
}
}
}