修改了保存的BUG
This commit is contained in:
@@ -927,16 +927,35 @@ export class ConfigPanel {
|
||||
|
||||
// 创建新项目
|
||||
private async createProject(name: string) {
|
||||
const newId = 'p' + (this.projects.length + 1);
|
||||
const newProject: Project = {
|
||||
id: newId,
|
||||
name: name
|
||||
};
|
||||
this.projects.push(newProject);
|
||||
|
||||
vscode.window.showInformationMessage(`新建项目: ${name}`);
|
||||
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) {
|
||||
@@ -987,11 +1006,34 @@ export class ConfigPanel {
|
||||
};
|
||||
this.aircrafts.push(newAircraft);
|
||||
|
||||
// 新增:创建飞行器目录
|
||||
await this.createAircraftDirectory(newAircraft);
|
||||
|
||||
vscode.window.showInformationMessage(`新建飞行器: ${name}`);
|
||||
await this.saveCurrentProjectData();
|
||||
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) {
|
||||
const aircraft = this.aircrafts.find(a => a.id === aircraftId);
|
||||
@@ -1022,45 +1064,83 @@ export class ConfigPanel {
|
||||
|
||||
// 创建新容器
|
||||
private async createContainer(name: string) {
|
||||
if (!this.currentAircraftId) {
|
||||
vscode.window.showErrorMessage('无法创建容器:未找到当前飞行器');
|
||||
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<void> {
|
||||
try {
|
||||
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
|
||||
if (!aircraft) {
|
||||
console.warn('未找到对应的飞行器,跳过创建容器目录');
|
||||
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,
|
||||
name: name,
|
||||
aircraftId: this.currentAircraftId
|
||||
};
|
||||
this.containers.push(newContainer);
|
||||
|
||||
// 创建两个默认配置文件
|
||||
const configCount = this.configs.length;
|
||||
// 构建路径:项目路径/飞行器名/容器名
|
||||
const aircraftDir = vscode.Uri.joinPath(vscode.Uri.file(projectPath), aircraft.name);
|
||||
const containerDir = vscode.Uri.joinPath(aircraftDir, container.name);
|
||||
|
||||
// 第一个配置文件
|
||||
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();
|
||||
// 确保飞行器目录存在
|
||||
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) {
|
||||
@@ -1102,20 +1182,47 @@ export class ConfigPanel {
|
||||
|
||||
// 创建新配置文件
|
||||
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);
|
||||
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);
|
||||
|
||||
vscode.window.showInformationMessage(`新建配置: ${name}`);
|
||||
await this.saveCurrentProjectData();
|
||||
this.updateWebview();
|
||||
// 新增:确保容器目录存在
|
||||
await this.ensureContainerDirectoryExists(this.currentContainerId);
|
||||
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user