// src/panels/ConfigPanel.ts import * as vscode from 'vscode'; import * as path from 'path'; import * as fs from 'fs'; import git from 'isomorphic-git'; import http from 'isomorphic-git/http/node'; import { ProjectView } from './views/ProjectView'; import { AircraftView } from './views/AircraftView'; import { ContainerView } from './views/ContainerView'; import { ConfigView } from './views/ConfigView'; // 数据模型接口 interface Project { id: string; name: string; } interface Aircraft { id: string; name: string; projectId: string; } interface Container { id: string; name: string; aircraftId: string; } interface Config { id: string; name: string; fileName: string; content: string; containerId: string; } interface ProjectData { projects: Project[]; aircrafts: Aircraft[]; containers: Container[]; configs: Config[]; } // Git 仓库接口 interface GitRepo { id: string; name: string; url: string; localPath: string; branch: string; lastSync: string; } interface GitFileTree { name: string; type: 'file' | 'folder'; path: string; children?: GitFileTree[]; } // Git 分支接口 interface GitBranch { name: string; isCurrent: boolean; isRemote: boolean; selected?: boolean; } export class ConfigPanel { public static currentPanel: ConfigPanel | undefined; public readonly panel: vscode.WebviewPanel; private readonly extensionUri: vscode.Uri; private currentView: 'projects' | 'aircrafts' | 'containers' | 'configs' = 'projects'; private currentProjectId: string = ''; private currentAircraftId: string = ''; private currentContainerId: string = ''; private currentRepoId: string = ''; // 数据存储 private projects: Project[] = []; private aircrafts: Aircraft[] = []; private containers: Container[] = []; private configs: Config[] = []; // Git 仓库存储 private gitRepos: GitRepo[] = []; private currentRepoFileTree: GitFileTree[] = []; // 项目存储路径映射 private projectPaths: Map = new Map(); // 视图实例 private readonly projectView: ProjectView; private readonly aircraftView: AircraftView; private readonly containerView: ContainerView; private readonly configView: ConfigView; public static createOrShow(extensionUri: vscode.Uri) { const column = vscode.window.activeTextEditor?.viewColumn || vscode.ViewColumn.One; if (ConfigPanel.currentPanel) { ConfigPanel.currentPanel.panel.reveal(column); return; } const panel = vscode.window.createWebviewPanel( 'DCSP', '数字卫星构建平台', column, { enableScripts: true, localResourceRoots: [extensionUri], retainContextWhenHidden: true } ); ConfigPanel.currentPanel = new ConfigPanel(panel, extensionUri); } public constructor(panel: vscode.WebviewPanel, extensionUri: vscode.Uri) { this.panel = panel; this.extensionUri = extensionUri; // 初始化各个视图 this.projectView = new ProjectView(extensionUri); this.aircraftView = new AircraftView(extensionUri); this.containerView = new ContainerView(extensionUri); this.configView = new ConfigView(extensionUri); // 加载 Git 仓库数据 this.loadGitRepos(); this.updateWebview(); this.setupMessageListener(); this.panel.onDidDispose(() => { ConfigPanel.currentPanel = undefined; }); } private setupMessageListener() { this.panel.webview.onDidReceiveMessage(async (data) => { console.log('📨 收到Webview消息:', data); switch (data.type) { case 'openExistingProject': await this.openExistingProject(); break; case 'configureProject': const selectedPath = await this.selectProjectPath(data.projectId, data.projectName); if (selectedPath) { this.currentView = 'aircrafts'; this.currentProjectId = data.projectId; this.updateWebview(); } break; case 'openProject': // 已配置的项目直接打开 this.currentView = 'aircrafts'; this.currentProjectId = data.projectId; this.updateWebview(); break; case 'openAircraftConfig': this.currentView = 'containers'; this.currentProjectId = data.projectId; this.currentAircraftId = data.aircraftId; this.updateWebview(); break; case 'openContainerConfig': this.currentView = 'configs'; this.currentContainerId = data.containerId; this.updateWebview(); break; // 修复返回按钮的消息处理 case 'goBackToProjects': this.currentView = 'projects'; // 清空当前选择的ID this.currentProjectId = ''; this.currentAircraftId = ''; this.currentContainerId = ''; this.currentRepoId = ''; this.updateWebview(); break; case 'goBackToAircrafts': this.currentView = 'aircrafts'; // 保持 currentProjectId,清空其他ID this.currentAircraftId = ''; this.currentContainerId = ''; this.updateWebview(); break; case 'goBackToContainers': this.currentView = 'containers'; // 保持 currentProjectId 和 currentAircraftId,清空 currentContainerId this.currentContainerId = ''; this.updateWebview(); break; case 'updateProjectName': await this.updateProjectName(data.projectId, data.name); break; case 'createProject': await this.createProject(data.name); break; case 'updateAircraftName': await this.updateAircraftName(data.aircraftId, data.name); break; case 'createAircraft': await this.createAircraft(data.name); break; case 'updateContainerName': await this.updateContainerName(data.containerId, data.name); break; case 'createContainer': await this.createContainer(data.name); break; case 'updateConfigName': await this.updateConfigName(data.configId, data.name); break; case 'updateConfigFileName': await this.updateConfigFileName(data.configId, data.fileName); break; case 'createConfig': await this.createConfig(data.name); break; case 'saveConfigFile': await this.saveConfigFileToDisk(data.configId, data.content); break; case 'loadConfigFile': this.loadConfigFile(data.configId); break; case 'deleteProject': await this.deleteProject(data.projectId); break; case 'deleteAircraft': await this.deleteAircraft(data.aircraftId); break; case 'deleteContainer': await this.deleteContainer(data.containerId); break; case 'deleteConfig': await this.deleteConfig(data.configId); break; // Git 仓库管理功能 case 'fetchBranches': console.log('🌿 获取分支列表:', data.url); await this.fetchBranches(data.url); break; case 'cloneBranches': console.log('🚀 克隆选中的分支:', data); await this.cloneBranches(data.url, data.branches); break; case 'cancelBranchSelection': console.log('❌ 取消分支选择'); this.updateWebview(); break; case 'loadGitRepo': await this.loadGitRepo(data.repoId); break; case 'syncGitRepo': await this.syncGitRepo(data.repoId); break; case 'deleteGitRepo': await this.deleteGitRepo(data.repoId); break; case 'importGitFile': await this.importGitFile(data.filePath); break; } }); } // === Git 仓库管理方法 === /** * 加载 Git 仓库数据 */ private async loadGitRepos(): Promise { try { const globalStoragePath = this.extensionUri.fsPath; const reposFile = path.join(globalStoragePath, 'git-repos.json'); if (fs.existsSync(reposFile)) { const data = await fs.promises.readFile(reposFile, 'utf8'); this.gitRepos = JSON.parse(data); } } catch (error) { vscode.window.showErrorMessage(`加载 Git 仓库数据失败: ${error}`); } } /** * 保存 Git 仓库数据 */ private async saveGitRepos(): Promise { try { const globalStoragePath = this.extensionUri.fsPath; const reposFile = path.join(globalStoragePath, 'git-repos.json'); // 确保目录存在 await fs.promises.mkdir(path.dirname(reposFile), { recursive: true }); await fs.promises.writeFile(reposFile, JSON.stringify(this.gitRepos, null, 2)); } catch (error) { vscode.window.showErrorMessage(`保存 Git 仓库数据失败: ${error}`); } } /** * 添加 Git 仓库到配置目录 */ private async addGitRepo(url: string, name: string, branch?: string): Promise { try { // 验证 URL if (!url || !url.startsWith('http')) { vscode.window.showErrorMessage('请输入有效的 Git 仓库 URL'); return; } const repoId = 'git-' + Date.now(); // 构建本地路径 let localPath = ''; if (this.currentContainerId && this.currentProjectId) { const projectPath = this.projectPaths.get(this.currentProjectId); const container = this.containers.find(c => c.id === this.currentContainerId); const aircraft = this.aircrafts.find(a => a.id === container?.aircraftId); if (projectPath && container && aircraft) { localPath = path.join(projectPath, aircraft.name, container.name, name); console.log(`📁 Git仓库将保存到容器目录: ${localPath}`); } } if (!localPath) { localPath = path.join(this.extensionUri.fsPath, name); console.log(`📁 Git仓库将保存到扩展目录: ${localPath}`); } // 修改:检查目标目录是否已存在 if (fs.existsSync(localPath)) { vscode.window.showErrorMessage(`目标目录已存在: ${localPath},请选择不同的名称或删除现有目录`); return; } // 修改:放宽重复检查,只检查完全相同的路径 const existingRepo = this.gitRepos.find(repo => repo.localPath === localPath // 只检查路径完全相同的情况 ); if (existingRepo) { vscode.window.showWarningMessage('该路径已存在 Git 仓库'); return; } const newRepo: GitRepo = { id: repoId, name: name, url: url, localPath: localPath, branch: branch || 'main', lastSync: new Date().toLocaleString() }; console.log(`📁 准备克隆仓库: ${name}, 分支: ${newRepo.branch}, 路径: ${localPath}`); // 显示进度 await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, title: `正在克隆仓库: ${name} (${newRepo.branch})`, cancellable: false }, async (progress) => { progress.report({ increment: 0 }); try { // 确保目录存在 await fs.promises.mkdir(path.dirname(localPath), { recursive: true }); // 克隆仓库 await git.clone({ fs: fs, http: http, dir: localPath, url: url, singleBranch: true, depth: 1, ref: branch || 'main', onProgress: (event: any) => { if (event.total) { const percent = (event.loaded / event.total) * 100; progress.report({ increment: percent, message: `${event.phase}...` }); } } }); console.log('✅ Git克隆成功完成'); this.gitRepos.push(newRepo); await this.saveGitRepos(); console.log('✅ 仓库数据保存成功'); vscode.window.showInformationMessage(`Git 仓库克隆成功: ${name} (${newRepo.branch})`); console.log('🌳 开始加载仓库文件树...'); // 自动加载仓库文件树 this.currentRepoId = repoId; await this.loadGitRepoFileTree(repoId); console.log('✅ 仓库文件树加载完成'); // 更新 Webview 显示 this.updateWebview(); } catch (error) { console.error('❌ 在克隆过程中捕获错误:', error); vscode.window.showErrorMessage(`克隆仓库失败: ${error}`); // 清理失败的克隆目录 try { console.log('🧹 开始清理失败的克隆目录...'); await fs.promises.rm(localPath, { recursive: true, force: true }); console.log('✅ 失败目录清理完成'); } catch (cleanupError) { console.error('❌ 清理失败目录时出错:', cleanupError); } } }); } catch (error) { console.error('❌ 在addGitRepo外部捕获错误:', error); vscode.window.showErrorMessage(`添加 Git 仓库失败: ${error}`); } } /** * 加载 Git 仓库文件树 */ private async loadGitRepo(repoId: string): Promise { this.currentRepoId = repoId; await this.loadGitRepoFileTree(repoId); this.updateWebview(); } /** * 同步 Git 仓库 */ private async syncGitRepo(repoId: string): Promise { const repo = this.gitRepos.find(r => r.id === repoId); if (!repo) { vscode.window.showErrorMessage('未找到指定的 Git 仓库'); return; } await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, title: `正在同步仓库: ${repo.name}`, cancellable: false }, async (progress) => { try { progress.report({ increment: 0, message: '拉取最新更改...' }); // 拉取最新更改 await git.pull({ fs: fs, http: http, dir: repo.localPath, author: { name: 'DCSP User', email: 'user@dcsp.local' }, fastForward: true }); // 更新最后同步时间 repo.lastSync = new Date().toLocaleString(); await this.saveGitRepos(); // 重新加载文件树 await this.loadGitRepoFileTree(repoId); vscode.window.showInformationMessage(`Git 仓库同步成功: ${repo.name}`); this.updateWebview(); } catch (error) { vscode.window.showErrorMessage(`同步 Git 仓库失败: ${error}`); } }); } /** * 删除 Git 仓库 */ private async deleteGitRepo(repoId: string): Promise { const repo = this.gitRepos.find(r => r.id === repoId); if (!repo) return; const confirm = await vscode.window.showWarningMessage( `确定要删除 Git 仓库 "${repo.name}" 吗?这也会删除本地副本。`, { modal: true }, '确定删除' ); if (confirm === '确定删除') { try { // 删除本地目录 await fs.promises.rm(repo.localPath, { recursive: true, force: true }); // 从列表中移除 this.gitRepos = this.gitRepos.filter(r => r.id !== repoId); await this.saveGitRepos(); // 如果删除的是当前仓库,清空状态 if (this.currentRepoId === repoId) { this.currentRepoId = ''; this.currentRepoFileTree = []; } vscode.window.showInformationMessage(`Git 仓库已删除: ${repo.name}`); this.updateWebview(); } catch (error) { vscode.window.showErrorMessage(`删除 Git 仓库失败: ${error}`); } } } /** * 加载 Git 仓库文件树 */ private async loadGitRepoFileTree(repoId: string): Promise { const repo = this.gitRepos.find(r => r.id === repoId); if (!repo) return; // 通知前端开始加载 this.panel.webview.postMessage({ type: 'gitRepoLoading', loading: true }); try { const fileTree = await this.buildFileTree(repo.localPath); this.currentRepoFileTree = fileTree; // 更新最后访问时间 repo.lastSync = new Date().toLocaleString(); await this.saveGitRepos(); } catch (error) { vscode.window.showErrorMessage(`加载仓库文件树失败: ${error}`); this.currentRepoFileTree = []; } // 通知前端加载完成 this.panel.webview.postMessage({ type: 'gitRepoLoading', loading: false }); this.updateWebview(); } /** * 构建文件树 */ private async buildFileTree(dir: string, relativePath: string = ''): Promise { try { const files = await fs.promises.readdir(dir); const tree: GitFileTree[] = []; for (const file of files) { // 忽略 .git 文件夹和其他隐藏文件 if (file.startsWith('.')) continue; const filePath = path.join(dir, file); const stats = await fs.promises.stat(filePath); const currentRelativePath = path.join(relativePath, file); if (stats.isDirectory()) { const children = await this.buildFileTree(filePath, currentRelativePath); tree.push({ name: file, type: 'folder', path: currentRelativePath, children: children }); } else { tree.push({ name: file, type: 'file', path: currentRelativePath }); } } return tree; } catch (error) { console.error('构建文件树失败:', error); return []; } } /** * 导入 Git 文件到当前容器 */ private async importGitFile(filePath: string): Promise { if (!this.currentRepoId || !this.currentContainerId) { vscode.window.showErrorMessage('请先选择 Git 仓库和容器'); return; } const repo = this.gitRepos.find(r => r.id === this.currentRepoId); if (!repo) { vscode.window.showErrorMessage('未找到当前 Git 仓库'); return; } const container = this.containers.find(c => c.id === this.currentContainerId); if (!container) { vscode.window.showErrorMessage('未找到当前容器'); return; } try { const fullPath = path.join(repo.localPath, filePath); const content = await fs.promises.readFile(fullPath, 'utf8'); const fileName = path.basename(filePath); // 创建新配置 const newId = 'cfg' + (this.configs.length + 1); const newConfig: Config = { id: newId, name: fileName, fileName: fileName, content: content, containerId: this.currentContainerId }; this.configs.push(newConfig); await this.saveCurrentProjectData(); vscode.window.showInformationMessage(`文件已导入到容器 ${container.name}: ${fileName}`); this.updateWebview(); } catch (error) { vscode.window.showErrorMessage(`导入文件失败: ${error}`); } } // === 原有项目配置管理方法 === // === 打开现有项目功能 === private async openExistingProject(): Promise { try { const result = await vscode.window.showOpenDialog({ canSelectFiles: false, canSelectFolders: true, canSelectMany: false, openLabel: '选择项目文件夹', title: '选择包含项目数据的文件夹' }); if (result && result.length > 0) { const selectedPath = result[0].fsPath; await this.loadProjectData(selectedPath); } } catch (error) { vscode.window.showErrorMessage(`打开项目时出错: ${error}`); } } // === 数据持久化方法 === /** * 保存当前项目数据到项目路径 */ private async saveCurrentProjectData(): Promise { try { if (!this.currentProjectId) { vscode.window.showWarningMessage('未找到当前项目,数据将不会保存'); return; } const projectPath = this.projectPaths.get(this.currentProjectId); if (!projectPath) { vscode.window.showWarningMessage('未找到项目存储路径,数据将不会保存'); 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)); const data: ProjectData = { projects: this.projects.filter(p => p.id === this.currentProjectId), // 只保存当前项目 aircrafts: currentProjectAircrafts, containers: currentProjectContainers, configs: currentProjectConfigs }; const uint8Array = new TextEncoder().encode(JSON.stringify(data, null, 2)); await vscode.workspace.fs.writeFile(dataUri, uint8Array); console.log('✅ 当前项目数据已保存'); } catch (error) { vscode.window.showErrorMessage(`保存项目数据失败: ${error}`); } } /** * 从项目路径加载数据 */ private async loadProjectData(projectPath: string): Promise { 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 = []; // 验证数据格式并加载 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 (this.projects.length > 0) { this.currentProjectId = this.projects[0].id; this.projectPaths.set(this.currentProjectId, projectPath); this.currentView = 'aircrafts'; } vscode.window.showInformationMessage(`项目数据已从 ${projectPath} 加载`); this.updateWebview(); return true; } catch (error) { vscode.window.showErrorMessage(`加载项目数据失败: ${error}`); return false; } } /** * 检查项目路径是否已存在数据 */ private async checkProjectPathHasData(projectPath: string): Promise { try { const dataUri = vscode.Uri.joinPath(vscode.Uri.file(projectPath), '.dcsp-data.json'); await vscode.workspace.fs.stat(dataUri); return true; } catch { return false; } } // === 项目路径选择 === private async selectProjectPath(projectId: string, projectName: string): Promise { try { // 选择现有路径或输入新路径 const choice = await vscode.window.showQuickPick( [ { label: '$(folder) 选择现有文件夹', description: '从文件系统中选择已存在的文件夹', value: 'select' }, { label: '$(new-folder) 创建新文件夹', description: '输入新文件夹路径(将自动创建)', value: 'create' } ], { placeHolder: '选择项目存储方式' } ); if (!choice) { return null; } if (choice.value === 'select') { // 选择现有路径 const result = await vscode.window.showOpenDialog({ canSelectFiles: false, canSelectFolders: true, canSelectMany: false, openLabel: `选择 ${projectName} 的存储位置`, title: `为项目 "${projectName}" 选择存储文件夹` }); if (result && result.length > 0) { const selectedPath = result[0].fsPath; // 检查是否已存在数据 const hasExistingData = await this.checkProjectPathHasData(selectedPath); if (hasExistingData) { const loadChoice = await vscode.window.showWarningMessage( `在路径 ${selectedPath} 中检测到现有项目数据,是否加载?`, { modal: true }, '是,加载现有数据', '否,创建新项目' ); if (loadChoice === '是,加载现有数据') { // 加载现有数据 const success = await this.loadProjectData(selectedPath); if (success) { this.projectPaths.set(projectId, selectedPath); this.currentView = 'aircrafts'; this.currentProjectId = projectId; this.updateWebview(); vscode.window.showInformationMessage(`项目数据已从 ${selectedPath} 加载`); return selectedPath; } } // 如果选择不加载或加载失败,继续创建新项目 } this.projectPaths.set(projectId, selectedPath); vscode.window.showInformationMessage(`项目存储位置已设置: ${selectedPath}`); // 保存初始数据 await this.saveCurrentProjectData(); return selectedPath; } } else { // 创建新路径 const pathInput = await vscode.window.showInputBox({ prompt: '请输入项目存储路径(绝对路径)', placeHolder: `/path/to/your/project/${projectName}`, validateInput: (value) => { if (!value) { return '路径不能为空'; } return null; } }); if (pathInput) { try { // 尝试创建目录 const dirUri = vscode.Uri.file(pathInput); await vscode.workspace.fs.createDirectory(dirUri); this.projectPaths.set(projectId, pathInput); vscode.window.showInformationMessage(`项目存储位置已创建: ${pathInput}`); // 保存初始数据 await this.saveCurrentProjectData(); return pathInput; } catch (error) { vscode.window.showErrorMessage(`创建目录失败: ${error}`); return null; } } } return null; } catch (error) { vscode.window.showErrorMessage(`选择存储路径时出错: ${error}`); return null; } } // 更新项目名 private async updateProjectName(projectId: string, newName: string) { const project = this.projects.find(p => p.id === projectId); if (project) { project.name = newName; vscode.window.showInformationMessage(`项目名称更新: ${newName}`); await this.saveCurrentProjectData(); this.updateWebview(); } } // 创建新项目 private async createProject(name: string) { const newId = 'p' + (this.projects.length + 1); const newProject: Project = { id: newId, name: name }; this.projects.push(newProject); // 关键修复:设置当前项目ID this.currentProjectId = newId; vscode.window.showInformationMessage(`新建项目: ${name}`); // 关键修复:立即要求用户选择项目存储路径 const selectedPath = await this.selectProjectPath(newId, name); if (selectedPath) { // 保存初始项目数据 await this.saveCurrentProjectData(); // 自动切换到飞行器视图 this.currentView = 'aircrafts'; this.updateWebview(); } else { // 如果用户取消选择路径,移除刚创建的项目 this.projects = this.projects.filter(p => p.id !== newId); this.currentProjectId = ''; vscode.window.showWarningMessage('项目创建已取消'); this.updateWebview(); } } // 删除项目 private async deleteProject(projectId: string) { const project = this.projects.find(p => p.id === projectId); if (!project) return; this.projects = this.projects.filter(p => p.id !== projectId); // 删除相关的飞行器 const relatedAircrafts = this.aircrafts.filter(a => a.projectId === projectId); const aircraftIds = relatedAircrafts.map(a => a.id); this.aircrafts = this.aircrafts.filter(a => a.projectId !== projectId); // 删除相关的容器 this.containers = this.containers.filter(c => !aircraftIds.includes(c.aircraftId)); // 删除相关的配置 const containerIds = this.containers.filter(c => aircraftIds.includes(c.aircraftId)).map(c => c.id); this.configs = this.configs.filter(cfg => !containerIds.includes(cfg.containerId)); // 删除项目路径映射 this.projectPaths.delete(projectId); vscode.window.showInformationMessage(`删除项目: ${project.name}`); await this.saveCurrentProjectData(); this.updateWebview(); } // 更新飞行器名 private async updateAircraftName(aircraftId: string, newName: string) { const aircraft = this.aircrafts.find(a => a.id === aircraftId); if (aircraft) { aircraft.name = newName; vscode.window.showInformationMessage(`飞行器名称更新: ${newName}`); await this.saveCurrentProjectData(); this.updateWebview(); } } // 创建新飞行器 private async createAircraft(name: string) { if (!this.currentProjectId) { vscode.window.showErrorMessage('无法创建飞行器:未找到当前项目'); return; } const newId = 'a' + (this.aircrafts.length + 1); const newAircraft: Aircraft = { id: newId, name: name, projectId: this.currentProjectId }; this.aircrafts.push(newAircraft); // 新增:创建飞行器目录 await this.createAircraftDirectory(newAircraft); vscode.window.showInformationMessage(`新建飞行器: ${name}`); await this.saveCurrentProjectData(); this.updateWebview(); } private async createAircraftDirectory(aircraft: Aircraft): Promise { try { const projectPath = this.projectPaths.get(aircraft.projectId); if (!projectPath) { console.warn('未找到项目路径,跳过创建飞行器目录'); return; } const aircraftDir = vscode.Uri.joinPath(vscode.Uri.file(projectPath), aircraft.name); // 创建飞行器目录 await vscode.workspace.fs.createDirectory(aircraftDir); console.log(`✅ 创建飞行器目录: ${aircraftDir.fsPath}`); } catch (error) { console.error(`创建飞行器目录失败: ${error}`); vscode.window.showWarningMessage(`创建飞行器目录失败: ${error}`); } } // 删除飞行器 private async deleteAircraft(aircraftId: string) { const aircraft = this.aircrafts.find(a => a.id === aircraftId); if (!aircraft) return; this.aircrafts = this.aircrafts.filter(a => a.id !== aircraftId); // 删除相关的容器 this.containers = this.containers.filter(c => c.aircraftId !== aircraftId); // 删除相关的配置 const containerIds = this.containers.filter(c => c.aircraftId === aircraftId).map(c => c.id); this.configs = this.configs.filter(cfg => !containerIds.includes(cfg.containerId)); vscode.window.showInformationMessage(`删除飞行器: ${aircraft.name}`); await this.saveCurrentProjectData(); this.updateWebview(); } // 更新容器名 private async updateContainerName(containerId: string, newName: string) { const container = this.containers.find(c => c.id === containerId); if (container) { container.name = newName; vscode.window.showInformationMessage(`容器名称更新: ${newName}`); await this.saveCurrentProjectData(); this.updateWebview(); } } // 创建新容器 private async createContainer(name: string) { if (!this.currentAircraftId) { vscode.window.showErrorMessage('无法创建容器:未找到当前飞行器'); return; } const newId = 'c' + (this.containers.length + 1); const newContainer: Container = { id: newId, name: name, aircraftId: this.currentAircraftId }; this.containers.push(newContainer); // 新增:创建容器目录 await this.createContainerDirectory(newContainer); // 创建两个默认配置文件 const configCount = this.configs.length; // 第一个配置文件 this.configs.push({ id: 'cfg' + (configCount + 1), name: '配置1', fileName: 'dockerfile', content: `# ${name} 的 Dockerfile\nFROM ubuntu:20.04\n\n# 设置工作目录\nWORKDIR /app\n\n# 复制文件\nCOPY . .\n\n# 安装依赖\nRUN apt-get update && apt-get install -y \\\n python3 \\\n python3-pip\n\n# 暴露端口\nEXPOSE 8080\n\n# 启动命令\nCMD ["python3", "app.py"]`, containerId: newId }); // 第二个配置文件 this.configs.push({ id: 'cfg' + (configCount + 2), name: '配置2', fileName: 'docker-compose.yml', content: `# ${name} 的 Docker Compose 配置\nversion: '3.8'\n\nservices:\n ${name.toLowerCase().replace(/\\s+/g, '-')}:\n build: .\n container_name: ${name}\n ports:\n - "8080:8080"\n environment:\n - NODE_ENV=production\n volumes:\n - ./data:/app/data\n restart: unless-stopped`, containerId: newId }); vscode.window.showInformationMessage(`新建容器: ${name} (包含2个默认配置文件)`); await this.saveCurrentProjectData(); this.updateWebview(); } // 新增方法:创建容器目录 private async createContainerDirectory(container: Container): Promise { try { const aircraft = this.aircrafts.find(a => a.id === container.aircraftId); if (!aircraft) { console.warn('未找到对应的飞行器,跳过创建容器目录'); return; } const projectPath = this.projectPaths.get(aircraft.projectId); if (!projectPath) { console.warn('未找到项目路径,跳过创建容器目录'); return; } // 构建路径:项目路径/飞行器名/容器名 const aircraftDir = vscode.Uri.joinPath(vscode.Uri.file(projectPath), aircraft.name); const containerDir = vscode.Uri.joinPath(aircraftDir, container.name); // 确保飞行器目录存在 try { await vscode.workspace.fs.createDirectory(aircraftDir); } catch (error) { // 目录可能已存在,忽略错误 } // 创建容器目录 await vscode.workspace.fs.createDirectory(containerDir); console.log(`✅ 创建容器目录: ${containerDir.fsPath}`); } catch (error) { console.error(`创建容器目录失败: ${error}`); vscode.window.showWarningMessage(`创建容器目录失败: ${error}`); } } // 删除容器 private async deleteContainer(containerId: string) { const container = this.containers.find(c => c.id === containerId); if (!container) return; // 删除容器 this.containers = this.containers.filter(c => c.id !== containerId); // 删除相关的配置 this.configs = this.configs.filter(cfg => cfg.containerId !== containerId); vscode.window.showInformationMessage(`删除容器: ${container.name}`); await this.saveCurrentProjectData(); this.updateWebview(); } // 更新配置名 private async updateConfigName(configId: string, newName: string) { const config = this.configs.find(c => c.id === configId); if (config) { config.name = newName; vscode.window.showInformationMessage(`配置名称更新: ${newName}`); await this.saveCurrentProjectData(); this.updateWebview(); } } // 更新文件名 private async updateConfigFileName(configId: string, fileName: string): Promise { const config = this.configs.find(c => c.id === configId); if (config) { config.fileName = fileName; vscode.window.showInformationMessage(`文件名更新: ${fileName}`); await this.saveCurrentProjectData(); this.updateWebview(); } } // 创建新配置文件 private async createConfig(name: string) { const newId = 'cfg' + (this.configs.length + 1); const newConfig: Config = { id: newId, name: name, fileName: name.toLowerCase().replace(/\s+/g, '_'), content: `# ${name} 配置文件\n# 创建时间: ${new Date().toLocaleString()}\n# 您可以在此编辑配置内容\n\n`, containerId: this.currentContainerId }; this.configs.push(newConfig); // 新增:确保容器目录存在 await this.ensureContainerDirectoryExists(this.currentContainerId); vscode.window.showInformationMessage(`新建配置: ${name}`); await this.saveCurrentProjectData(); this.updateWebview(); } // 新增方法:确保容器目录存在 private async ensureContainerDirectoryExists(containerId: string): Promise { try { const container = this.containers.find(c => c.id === containerId); if (!container) return; const aircraft = this.aircrafts.find(a => a.id === container.aircraftId); if (!aircraft) return; const projectPath = this.projectPaths.get(aircraft.projectId); if (!projectPath) return; // 构建路径并创建目录 const aircraftDir = vscode.Uri.joinPath(vscode.Uri.file(projectPath), aircraft.name); const containerDir = vscode.Uri.joinPath(aircraftDir, container.name); await vscode.workspace.fs.createDirectory(aircraftDir); await vscode.workspace.fs.createDirectory(containerDir); } catch (error) { console.error(`确保容器目录存在失败: ${error}`); } } // 删除配置文件 private async deleteConfig(configId: string) { const config = this.configs.find(c => c.id === configId); if (config) { this.configs = this.configs.filter(c => c.id !== configId); vscode.window.showInformationMessage(`删除配置: ${config.name}`); await this.saveCurrentProjectData(); this.updateWebview(); } } // 保存配置文件到磁盘 private async saveConfigFileToDisk(configId: string, content: string): Promise { try { const config = this.configs.find(c => c.id === configId); if (!config) { vscode.window.showErrorMessage('未找到配置文件'); return; } const container = this.containers.find(c => c.id === config.containerId); if (!container) { vscode.window.showErrorMessage('未找到容器'); return; } const aircraft = this.aircrafts.find(a => a.id === container.aircraftId); if (!aircraft) { vscode.window.showErrorMessage('未找到飞行器'); return; } const project = this.projects.find(p => p.id === aircraft.projectId); if (!project) { vscode.window.showErrorMessage('未找到项目'); return; } const projectPath = this.projectPaths.get(aircraft.projectId); if (!projectPath) { vscode.window.showErrorMessage('未设置项目存储路径,请先配置项目'); return; } // 构建文件路径:项目路径/飞行器名/容器名/文件名 const aircraftDir = vscode.Uri.joinPath(vscode.Uri.file(projectPath), aircraft.name); const containerDir = vscode.Uri.joinPath(aircraftDir, container.name); const fileUri = vscode.Uri.joinPath(containerDir, config.fileName); // 确保飞行器目录存在 try { await vscode.workspace.fs.createDirectory(aircraftDir); } catch (error) { // 目录可能已存在,忽略错误 } // 确保容器目录存在 try { await vscode.workspace.fs.createDirectory(containerDir); } catch (error) { // 目录可能已存在,忽略错误 } // 写入文件 const uint8Array = new TextEncoder().encode(content); await vscode.workspace.fs.writeFile(fileUri, uint8Array); // 更新配置内容 config.content = content; vscode.window.showInformationMessage(`配置文件已保存: ${fileUri.fsPath}`); // 保存数据到JSON文件 await this.saveCurrentProjectData(); } catch (error) { vscode.window.showErrorMessage(`保存文件时出错: ${error}`); } } // 加载配置文件 private loadConfigFile(configId: string) { const config = this.configs.find(c => c.id === configId); if (config) { this.panel.webview.postMessage({ type: 'configFileLoaded', content: config.content || `# ${config.name} 的配置文件\n# 您可以在此编辑配置内容\n\n` }); } else { this.panel.webview.postMessage({ type: 'configFileLoaded', content: `# 这是 ${configId} 的配置文件\n# 您可以在此编辑配置内容\n\napp.name = "示例应用"\napp.port = 8080\napp.debug = true` }); } } // === Git 分支管理 === private async fetchBranches(url: string): Promise { try { console.log('🌿 开始获取分支列表:', url); await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, title: '正在获取分支信息', cancellable: false }, async (progress) => { progress.report({ increment: 0, message: '连接远程仓库...' }); try { // 使用 isomorphic-git 的 listServerRefs progress.report({ increment: 30, message: '获取远程引用...' }); console.log('🔍 使用 listServerRefs 获取分支信息...'); const refs = await git.listServerRefs({ http: http, url: url }); console.log('📋 获取到的引用:', refs); // 过滤出分支引用 (refs/heads/ 和 refs/remotes/origin/) const branchRefs = refs.filter(ref => ref.ref.startsWith('refs/heads/') || ref.ref.startsWith('refs/remotes/origin/') ); console.log('🌿 过滤后的分支引用:', branchRefs); // 构建分支数据 const branches: GitBranch[] = branchRefs.map(ref => { const isRemote = ref.ref.startsWith('refs/remotes/'); const branchName = isRemote ? ref.ref.replace('refs/remotes/origin/', '') : ref.ref.replace('refs/heads/', ''); return { name: branchName, isCurrent: branchName === 'main' || branchName === 'master', isRemote: isRemote, selected: branchName === 'main' || branchName === 'master' }; }); console.log('🎯 最终分支列表:', branches); if (branches.length === 0) { throw new Error('未找到任何分支'); } progress.report({ increment: 80, message: '处理分支数据...' }); // 发送分支数据到前端 this.panel.webview.postMessage({ type: 'branchesFetched', branches: branches, repoUrl: url }); progress.report({ increment: 100, message: '完成' }); } catch (error) { console.error('❌ 使用 listServerRefs 获取分支失败:', error); // 如果方法失败,使用模拟数据 console.log('🔄 使用模拟分支数据'); const mockBranches = [ { name: 'main', isCurrent: true, isRemote: false, selected: true }, { name: 'master', isCurrent: false, isRemote: false, selected: false }, { name: 'develop', isCurrent: false, isRemote: false, selected: false }, { name: 'feature/new-feature', isCurrent: false, isRemote: false, selected: false } ]; this.panel.webview.postMessage({ type: 'branchesFetched', branches: mockBranches, repoUrl: url }); vscode.window.showWarningMessage('使用模拟分支数据,实际分支可能不同'); } }); } catch (error) { console.error('❌ 获取分支失败:', error); vscode.window.showErrorMessage(`获取分支失败: ${error}`); } } private async cloneBranches(url: string, branches: string[]): Promise { try { console.log('🚀 开始克隆分支:', { url, branches }); // 显示总进度 await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, title: `正在克隆 ${branches.length} 个分支`, cancellable: false }, async (progress) => { for (let i = 0; i < branches.length; i++) { const branch = branches[i]; const progressPercent = (i / branches.length) * 100; progress.report({ increment: progressPercent, message: `克隆分支: ${branch} (${i + 1}/${branches.length})` }); console.log(`📥 克隆分支: ${branch}`); await this.addGitRepo(url, this.generateRepoName(url, branch), branch); } }); vscode.window.showInformationMessage(`成功克隆 ${branches.length} 个分支`); } catch (error) { console.error('❌ 克隆分支失败:', error); vscode.window.showErrorMessage(`克隆分支失败: ${error}`); } } private generateRepoName(url: string, branch: string): string { const repoName = url.split('/').pop()?.replace('.git', '') || 'unknown-repo'; return `${repoName}-${branch.replace(/\//g, '-')}`; } // 更新视图 private updateWebview() { this.panel.webview.html = this.getWebviewContent(); } 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); return this.configView.render({ container: currentContainer, configs: containerConfigs, gitRepos: this.gitRepos, currentGitRepo: currentRepo, gitFileTree: this.currentRepoFileTree, gitLoading: false }); default: return this.projectView.render({ projects: this.projects, projectPaths: this.projectPaths }); } } }