0
0

修改了保存的BUG

This commit is contained in:
xubing
2025-11-25 09:24:31 +08:00
parent fa1e291bed
commit d3ef13de42
3 changed files with 256 additions and 55 deletions

View File

@@ -760,8 +760,25 @@ class ConfigPanel {
name: name name: name
}; };
this.projects.push(newProject); this.projects.push(newProject);
// 关键修复设置当前项目ID
this.currentProjectId = newId;
vscode.window.showInformationMessage(`新建项目: ${name}`); vscode.window.showInformationMessage(`新建项目: ${name}`);
this.updateWebview(); // 关键修复:立即要求用户选择项目存储路径
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();
}
} }
// 删除项目 // 删除项目
async deleteProject(projectId) { async deleteProject(projectId) {
@@ -807,10 +824,29 @@ class ConfigPanel {
projectId: this.currentProjectId projectId: this.currentProjectId
}; };
this.aircrafts.push(newAircraft); this.aircrafts.push(newAircraft);
// 新增:创建飞行器目录
await this.createAircraftDirectory(newAircraft);
vscode.window.showInformationMessage(`新建飞行器: ${name}`); vscode.window.showInformationMessage(`新建飞行器: ${name}`);
await this.saveCurrentProjectData(); await this.saveCurrentProjectData();
this.updateWebview(); this.updateWebview();
} }
async createAircraftDirectory(aircraft) {
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}`);
}
}
// 删除飞行器 // 删除飞行器
async deleteAircraft(aircraftId) { async deleteAircraft(aircraftId) {
const aircraft = this.aircrafts.find(a => a.id === aircraftId); const aircraft = this.aircrafts.find(a => a.id === aircraftId);
@@ -849,6 +885,8 @@ class ConfigPanel {
aircraftId: this.currentAircraftId aircraftId: this.currentAircraftId
}; };
this.containers.push(newContainer); this.containers.push(newContainer);
// 新增:创建容器目录
await this.createContainerDirectory(newContainer);
// 创建两个默认配置文件 // 创建两个默认配置文件
const configCount = this.configs.length; const configCount = this.configs.length;
// 第一个配置文件 // 第一个配置文件
@@ -871,6 +909,38 @@ class ConfigPanel {
await this.saveCurrentProjectData(); await this.saveCurrentProjectData();
this.updateWebview(); this.updateWebview();
} }
// 新增方法:创建容器目录
async createContainerDirectory(container) {
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}`);
}
}
// 删除容器 // 删除容器
async deleteContainer(containerId) { async deleteContainer(containerId) {
const container = this.containers.find(c => c.id === containerId); const container = this.containers.find(c => c.id === containerId);
@@ -915,10 +985,34 @@ class ConfigPanel {
containerId: this.currentContainerId containerId: this.currentContainerId
}; };
this.configs.push(newConfig); this.configs.push(newConfig);
// 新增:确保容器目录存在
await this.ensureContainerDirectoryExists(this.currentContainerId);
vscode.window.showInformationMessage(`新建配置: ${name}`); vscode.window.showInformationMessage(`新建配置: ${name}`);
await this.saveCurrentProjectData(); await this.saveCurrentProjectData();
this.updateWebview(); this.updateWebview();
} }
// 新增方法:确保容器目录存在
async ensureContainerDirectoryExists(containerId) {
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}`);
}
}
// 删除配置文件 // 删除配置文件
async deleteConfig(configId) { async deleteConfig(configId) {
const config = this.configs.find(c => c.id === configId); const config = this.configs.find(c => c.id === configId);

File diff suppressed because one or more lines are too long

View File

@@ -927,16 +927,35 @@ export class ConfigPanel {
// 创建新项目 // 创建新项目
private async createProject(name: string) { private async createProject(name: string) {
const newId = 'p' + (this.projects.length + 1); const newId = 'p' + (this.projects.length + 1);
const newProject: Project = { const newProject: Project = {
id: newId, id: newId,
name: name name: name
}; };
this.projects.push(newProject); this.projects.push(newProject);
vscode.window.showInformationMessage(`新建项目: ${name}`); // 关键修复设置当前项目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(); this.updateWebview();
} }
}
// 删除项目 // 删除项目
private async deleteProject(projectId: string) { private async deleteProject(projectId: string) {
@@ -987,11 +1006,34 @@ export class ConfigPanel {
}; };
this.aircrafts.push(newAircraft); this.aircrafts.push(newAircraft);
// 新增:创建飞行器目录
await this.createAircraftDirectory(newAircraft);
vscode.window.showInformationMessage(`新建飞行器: ${name}`); vscode.window.showInformationMessage(`新建飞行器: ${name}`);
await this.saveCurrentProjectData(); await this.saveCurrentProjectData();
this.updateWebview(); this.updateWebview();
} }
private async createAircraftDirectory(aircraft: Aircraft): Promise<void> {
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) { private async deleteAircraft(aircraftId: string) {
const aircraft = this.aircrafts.find(a => a.id === aircraftId); const aircraft = this.aircrafts.find(a => a.id === aircraftId);
@@ -1022,45 +1064,83 @@ export class ConfigPanel {
// 创建新容器 // 创建新容器
private async createContainer(name: string) { private async createContainer(name: string) {
if (!this.currentAircraftId) { if (!this.currentAircraftId) {
vscode.window.showErrorMessage('无法创建容器:未找到当前飞行器'); 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<void> {
try {
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
if (!aircraft) {
console.warn('未找到对应的飞行器,跳过创建容器目录');
return; return;
} }
const newId = 'c' + (this.containers.length + 1); const projectPath = this.projectPaths.get(aircraft.projectId);
if (!projectPath) {
console.warn('未找到项目路径,跳过创建容器目录');
return;
}
const newContainer: Container = { // 构建路径:项目路径/飞行器名/容器名
id: newId, const aircraftDir = vscode.Uri.joinPath(vscode.Uri.file(projectPath), aircraft.name);
name: name, const containerDir = vscode.Uri.joinPath(aircraftDir, container.name);
aircraftId: this.currentAircraftId
};
this.containers.push(newContainer);
// 创建两个默认配置文件 // 确保飞行器目录存在
const configCount = this.configs.length; try {
await vscode.workspace.fs.createDirectory(aircraftDir);
} catch (error) {
// 目录可能已存在,忽略错误
}
// 第一个配置文件 // 创建容器目录
this.configs.push({ await vscode.workspace.fs.createDirectory(containerDir);
id: 'cfg' + (configCount + 1), console.log(`✅ 创建容器目录: ${containerDir.fsPath}`);
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
});
// 第二个配置文件 } catch (error) {
this.configs.push({ console.error(`创建容器目录失败: ${error}`);
id: 'cfg' + (configCount + 2), vscode.window.showWarningMessage(`创建容器目录失败: ${error}`);
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 deleteContainer(containerId: string) { private async deleteContainer(containerId: string) {
@@ -1102,20 +1182,47 @@ export class ConfigPanel {
// 创建新配置文件 // 创建新配置文件
private async createConfig(name: string) { private async createConfig(name: string) {
const newId = 'cfg' + (this.configs.length + 1); const newId = 'cfg' + (this.configs.length + 1);
const newConfig: Config = { const newConfig: Config = {
id: newId, id: newId,
name: name, name: name,
fileName: name.toLowerCase().replace(/\s+/g, '_'), fileName: name.toLowerCase().replace(/\s+/g, '_'),
content: `# ${name} 配置文件\n# 创建时间: ${new Date().toLocaleString()}\n# 您可以在此编辑配置内容\n\n`, content: `# ${name} 配置文件\n# 创建时间: ${new Date().toLocaleString()}\n# 您可以在此编辑配置内容\n\n`,
containerId: this.currentContainerId containerId: this.currentContainerId
}; };
this.configs.push(newConfig); this.configs.push(newConfig);
vscode.window.showInformationMessage(`新建配置: ${name}`); // 新增:确保容器目录存在
await this.saveCurrentProjectData(); await this.ensureContainerDirectoryExists(this.currentContainerId);
this.updateWebview();
vscode.window.showInformationMessage(`新建配置: ${name}`);
await this.saveCurrentProjectData();
this.updateWebview();
}
// 新增方法:确保容器目录存在
private async ensureContainerDirectoryExists(containerId: string): Promise<void> {
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) { private async deleteConfig(configId: string) {