现在代码上传逻辑存在问题
This commit is contained in:
484
src/panels/services/ProjectService.ts
Normal file
484
src/panels/services/ProjectService.ts
Normal file
@@ -0,0 +1,484 @@
|
||||
// src/panels/services/ProjectService.ts
|
||||
import * as vscode from 'vscode';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import {
|
||||
Project,
|
||||
Aircraft,
|
||||
Container,
|
||||
Config,
|
||||
ModuleFolder
|
||||
} from '../types/CommonTypes';
|
||||
import { StorageService } from './StorageService';
|
||||
|
||||
/**
|
||||
* 项目数据管理服务
|
||||
*/
|
||||
export class ProjectService {
|
||||
private projects: Project[] = [];
|
||||
private aircrafts: Aircraft[] = [];
|
||||
private containers: Container[] = [];
|
||||
private configs: Config[] = [];
|
||||
private moduleFolders: ModuleFolder[] = [];
|
||||
private projectPaths: Map<string, string> = new Map();
|
||||
|
||||
/**
|
||||
* 生成唯一ID
|
||||
*/
|
||||
generateUniqueId(prefix: string, _existingItems: any[] = []): string {
|
||||
const randomPart = `${Date.now().toString(36)}${Math.random().toString(36).substring(2, 8)}`;
|
||||
return `${prefix}${randomPart}`;
|
||||
}
|
||||
|
||||
// =============== 项目相关方法 ===============
|
||||
|
||||
getProjects(): Project[] {
|
||||
return this.projects;
|
||||
}
|
||||
|
||||
getProjectPaths(): Map<string, string> {
|
||||
return this.projectPaths;
|
||||
}
|
||||
|
||||
getProjectPath(projectId: string): string | undefined {
|
||||
return this.projectPaths.get(projectId);
|
||||
}
|
||||
|
||||
setProjectPath(projectId: string, path: string): void {
|
||||
this.projectPaths.set(projectId, path);
|
||||
}
|
||||
|
||||
async createProject(name: string): Promise<string> {
|
||||
const newId = this.generateUniqueId('p', this.projects);
|
||||
const newProject: Project = {
|
||||
id: newId,
|
||||
name: name
|
||||
};
|
||||
this.projects.push(newProject);
|
||||
return newId;
|
||||
}
|
||||
|
||||
updateProjectName(projectId: string, newName: string): boolean {
|
||||
const project = this.projects.find(p => p.id === projectId);
|
||||
if (project) {
|
||||
project.name = newName;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
deleteProject(projectId: string): boolean {
|
||||
const project = this.projects.find(p => p.id === projectId);
|
||||
if (!project) return false;
|
||||
|
||||
// 先把要删的 id 都算出来,再统一过滤
|
||||
const relatedAircrafts = this.aircrafts.filter(a => a.projectId === projectId);
|
||||
const aircraftIds = relatedAircrafts.map(a => a.id);
|
||||
|
||||
const relatedContainers = this.containers.filter(c => aircraftIds.includes(c.aircraftId));
|
||||
const containerIds = relatedContainers.map(c => c.id);
|
||||
|
||||
// 真正删除数据
|
||||
this.projects = this.projects.filter(p => p.id !== projectId);
|
||||
this.aircrafts = this.aircrafts.filter(a => a.projectId !== projectId);
|
||||
this.containers = this.containers.filter(c => !aircraftIds.includes(c.aircraftId));
|
||||
this.configs = this.configs.filter(cfg => !containerIds.includes(cfg.containerId));
|
||||
this.moduleFolders = this.moduleFolders.filter(folder => !containerIds.includes(folder.containerId));
|
||||
|
||||
this.projectPaths.delete(projectId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// =============== 飞行器相关方法 ===============
|
||||
|
||||
getAircraftsByProject(projectId: string): Aircraft[] {
|
||||
return this.aircrafts.filter(a => a.projectId === projectId);
|
||||
}
|
||||
|
||||
async createAircraft(name: string, projectId: string): Promise<string> {
|
||||
const newId = this.generateUniqueId('a', this.aircrafts);
|
||||
const newAircraft: Aircraft = {
|
||||
id: newId,
|
||||
name: name,
|
||||
projectId: projectId
|
||||
};
|
||||
this.aircrafts.push(newAircraft);
|
||||
|
||||
// 创建飞行器目录
|
||||
await this.createAircraftDirectory(newAircraft);
|
||||
return newId;
|
||||
}
|
||||
|
||||
updateAircraftName(aircraftId: string, newName: string): boolean {
|
||||
const aircraft = this.aircrafts.find(a => a.id === aircraftId);
|
||||
if (aircraft) {
|
||||
aircraft.name = newName;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
deleteAircraft(aircraftId: string): boolean {
|
||||
const aircraft = this.aircrafts.find(a => a.id === aircraftId);
|
||||
if (!aircraft) return false;
|
||||
|
||||
// ⚠️ 修正点:先在删除 containers 之前,算出要删的 containerIds
|
||||
const relatedContainers = this.containers.filter(c => c.aircraftId === aircraftId);
|
||||
const containerIds = relatedContainers.map(c => c.id);
|
||||
|
||||
// 删除飞机自身和容器
|
||||
this.aircrafts = this.aircrafts.filter(a => a.id !== aircraftId);
|
||||
this.containers = this.containers.filter(c => c.aircraftId !== aircraftId);
|
||||
|
||||
// 再删关联的 config 和 moduleFolder
|
||||
this.configs = this.configs.filter(cfg => !containerIds.includes(cfg.containerId));
|
||||
this.moduleFolders = this.moduleFolders.filter(folder => !containerIds.includes(folder.containerId));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// =============== 容器相关方法 ===============
|
||||
|
||||
getContainersByAircraft(aircraftId: string): Container[] {
|
||||
return this.containers.filter(c => c.aircraftId === aircraftId);
|
||||
}
|
||||
|
||||
async createContainer(name: string, aircraftId: string): Promise<string> {
|
||||
const newId = this.generateUniqueId('c', this.containers);
|
||||
const newContainer: Container = {
|
||||
id: newId,
|
||||
name: name,
|
||||
aircraftId: aircraftId
|
||||
};
|
||||
this.containers.push(newContainer);
|
||||
|
||||
await this.createContainerDirectory(newContainer);
|
||||
await this.createDefaultConfigs(newContainer);
|
||||
|
||||
return newId;
|
||||
}
|
||||
|
||||
updateContainerName(containerId: string, newName: string): boolean {
|
||||
const container = this.containers.find(c => c.id === containerId);
|
||||
if (container) {
|
||||
container.name = newName;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
deleteContainer(containerId: string): boolean {
|
||||
const container = this.containers.find(c => c.id === containerId);
|
||||
if (!container) return false;
|
||||
|
||||
this.containers = this.containers.filter(c => c.id !== containerId);
|
||||
this.configs = this.configs.filter(cfg => cfg.containerId !== containerId);
|
||||
this.moduleFolders = this.moduleFolders.filter(folder => folder.containerId !== containerId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// =============== 配置相关方法 ===============
|
||||
|
||||
getConfigsByContainer(containerId: string): Config[] {
|
||||
return this.configs.filter(cfg => cfg.containerId === containerId);
|
||||
}
|
||||
|
||||
getConfig(configId: string): Config | undefined {
|
||||
return this.configs.find(c => c.id === configId);
|
||||
}
|
||||
|
||||
async createConfig(name: string, containerId: string): Promise<string> {
|
||||
const newId = this.generateUniqueId('cfg', this.configs);
|
||||
const newConfig: Config = {
|
||||
id: newId,
|
||||
name: name,
|
||||
fileName: name.toLowerCase().replace(/\s+/g, '_'),
|
||||
content: `# ${name} 配置文件\n# 创建时间: ${new Date().toLocaleString()}\n# 您可以在此编辑配置内容\n\n`,
|
||||
containerId: containerId
|
||||
};
|
||||
this.configs.push(newConfig);
|
||||
|
||||
await this.ensureContainerDirectoryExists(containerId);
|
||||
return newId;
|
||||
}
|
||||
|
||||
updateConfigName(configId: string, newName: string): boolean {
|
||||
const config = this.configs.find(c => c.id === configId);
|
||||
if (config) {
|
||||
config.name = newName;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
deleteConfig(configId: string): boolean {
|
||||
const config = this.configs.find(c => c.id === configId);
|
||||
if (!config) return false;
|
||||
|
||||
this.configs = this.configs.filter(c => c.id !== configId);
|
||||
return true;
|
||||
}
|
||||
|
||||
// =============== 模块文件夹相关方法 ===============
|
||||
|
||||
getModuleFoldersByContainer(containerId: string): ModuleFolder[] {
|
||||
return this.moduleFolders.filter(folder => folder.containerId === containerId);
|
||||
}
|
||||
|
||||
getModuleFolder(folderId: string): ModuleFolder | undefined {
|
||||
return this.moduleFolders.find(f => f.id === folderId);
|
||||
}
|
||||
|
||||
addModuleFolder(folder: ModuleFolder): void {
|
||||
this.moduleFolders.push(folder);
|
||||
}
|
||||
|
||||
updateModuleFolder(folderId: string, updates: Partial<ModuleFolder>): boolean {
|
||||
const folderIndex = this.moduleFolders.findIndex(f => f.id === folderId);
|
||||
if (folderIndex === -1) return false;
|
||||
|
||||
this.moduleFolders[folderIndex] = {
|
||||
...this.moduleFolders[folderIndex],
|
||||
...updates
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
deleteModuleFolder(folderId: string): boolean {
|
||||
const folder = this.moduleFolders.find(f => f.id === folderId);
|
||||
if (!folder) return false;
|
||||
|
||||
this.moduleFolders = this.moduleFolders.filter(f => f.id !== folderId);
|
||||
return true;
|
||||
}
|
||||
|
||||
renameModuleFolder(folderId: string, newName: string): boolean {
|
||||
const folder = this.moduleFolders.find(f => f.id === folderId);
|
||||
if (!folder) return false;
|
||||
|
||||
const oldName = folder.localPath.split('/').pop() || '';
|
||||
folder.localPath = folder.localPath.replace(/\/[^/]+$/, '/' + newName);
|
||||
return true;
|
||||
}
|
||||
|
||||
// =============== 文件系统操作 ===============
|
||||
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async createContainerDirectory(container: Container): Promise<void> {
|
||||
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);
|
||||
|
||||
await vscode.workspace.fs.createDirectory(aircraftDir);
|
||||
await vscode.workspace.fs.createDirectory(containerDir);
|
||||
console.log(`✅ 创建容器目录: ${containerDir.fsPath}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error(`创建容器目录失败: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
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 createDefaultConfigs(container: Container): Promise<void> {
|
||||
this.configs.push({
|
||||
id: this.generateUniqueId('cfg', this.configs),
|
||||
name: '配置1',
|
||||
fileName: 'dockerfile',
|
||||
content: `# ${container.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: container.id
|
||||
});
|
||||
|
||||
this.configs.push({
|
||||
id: this.generateUniqueId('cfg', this.configs),
|
||||
name: '配置2',
|
||||
fileName: 'docker-compose.yml',
|
||||
content: `# ${container.name} 的 Docker Compose 配置\nversion: '3.8'\n\nservices:\n ${container.name.toLowerCase().replace(/\s+/g, '-')}:\n build: .\n container_name: ${container.name}\n ports:\n - "8080:8080"\n environment:\n - NODE_ENV=production\n volumes:\n - ./data:/app/data\n restart: unless-stopped`,
|
||||
containerId: container.id
|
||||
});
|
||||
}
|
||||
|
||||
// =============== 数据持久化 ===============
|
||||
|
||||
async saveCurrentProjectData(projectId: string): Promise<boolean> {
|
||||
try {
|
||||
const projectPath = this.projectPaths.get(projectId);
|
||||
if (!projectPath) {
|
||||
console.warn('未找到项目存储路径,数据将不会保存');
|
||||
return false;
|
||||
}
|
||||
|
||||
const data = this.getProjectData(projectId);
|
||||
return await StorageService.saveProjectData(projectPath, data);
|
||||
} catch (error) {
|
||||
console.error('保存项目数据失败:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async loadProjectData(projectPath: string): Promise<string | null> {
|
||||
try {
|
||||
const data = await StorageService.loadProjectData(projectPath);
|
||||
if (!data) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const projectId = data.projects[0]?.id;
|
||||
if (!projectId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 先根据旧的 projectId 算出要清理的 aircraft / container / config / moduleFolder
|
||||
const existingAircrafts = this.aircrafts.filter(a => a.projectId === projectId);
|
||||
const aircraftIds = existingAircrafts.map(a => a.id);
|
||||
|
||||
const existingContainers = this.containers.filter(c => aircraftIds.includes(c.aircraftId));
|
||||
const containerIds = existingContainers.map(c => c.id);
|
||||
|
||||
// 清理旧数据(同一个 projectId 的)
|
||||
this.projects = this.projects.filter(p => p.id !== projectId);
|
||||
this.aircrafts = this.aircrafts.filter(a => a.projectId !== projectId);
|
||||
this.containers = this.containers.filter(c => !aircraftIds.includes(c.aircraftId));
|
||||
this.configs = this.configs.filter(cfg => !containerIds.includes(cfg.containerId));
|
||||
this.moduleFolders = this.moduleFolders.filter(folder => !containerIds.includes(folder.containerId));
|
||||
|
||||
// 加载新数据
|
||||
this.projects.push(...data.projects);
|
||||
this.aircrafts.push(...data.aircrafts);
|
||||
this.containers.push(...data.containers);
|
||||
this.configs.push(...data.configs);
|
||||
this.moduleFolders.push(...data.moduleFolders);
|
||||
|
||||
this.projectPaths.set(projectId, projectPath);
|
||||
return projectId;
|
||||
} catch (error) {
|
||||
console.error('加载项目数据失败:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private getProjectData(projectId: string): any {
|
||||
return {
|
||||
projects: [this.projects.find(p => p.id === projectId)!],
|
||||
aircrafts: this.aircrafts.filter(a => a.projectId === projectId),
|
||||
containers: this.containers.filter(c => {
|
||||
const aircraft = this.aircrafts.find(a => a.id === c.aircraftId);
|
||||
return aircraft && aircraft.projectId === projectId;
|
||||
}),
|
||||
configs: this.configs.filter(cfg => {
|
||||
const container = this.containers.find(c => c.id === cfg.containerId);
|
||||
if (!container) return false;
|
||||
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
|
||||
return aircraft && aircraft.projectId === projectId;
|
||||
}),
|
||||
moduleFolders: this.moduleFolders.filter(folder => {
|
||||
const container = this.containers.find(c => c.id === folder.containerId);
|
||||
if (!container) return false;
|
||||
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
|
||||
return aircraft && aircraft.projectId === projectId;
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
// =============== 工具方法 ===============
|
||||
|
||||
getModuleFolderFullPath(folder: ModuleFolder): string | null {
|
||||
const container = this.containers.find(c => c.id === folder.containerId);
|
||||
if (!container) return null;
|
||||
|
||||
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
|
||||
if (!aircraft) return null;
|
||||
|
||||
const projectPath = this.projectPaths.get(aircraft.projectId);
|
||||
if (!projectPath) return null;
|
||||
|
||||
const pathParts = folder.localPath.split('/').filter(part => part);
|
||||
if (pathParts.length < 4) return null;
|
||||
|
||||
const folderName = pathParts[pathParts.length - 1];
|
||||
return path.join(projectPath, aircraft.name, container.name, folderName);
|
||||
}
|
||||
|
||||
getConfigFilePath(configId: string): string | null {
|
||||
const config = this.configs.find(c => c.id === configId);
|
||||
if (!config) return null;
|
||||
|
||||
const container = this.containers.find(c => c.id === config.containerId);
|
||||
if (!container) return null;
|
||||
|
||||
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
|
||||
if (!aircraft) return null;
|
||||
|
||||
const projectPath = this.projectPaths.get(aircraft.projectId);
|
||||
if (!projectPath) return null;
|
||||
|
||||
return path.join(projectPath, aircraft.name, container.name, config.fileName);
|
||||
}
|
||||
|
||||
async deleteConfigFileFromDisk(configId: string): Promise<boolean> {
|
||||
const filePath = this.getConfigFilePath(configId);
|
||||
if (!filePath) return false;
|
||||
|
||||
try {
|
||||
if (fs.existsSync(filePath)) {
|
||||
await fs.promises.unlink(filePath);
|
||||
console.log(`✅ 已删除配置文件: ${filePath}`);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.error(`删除配置文件失败: ${error}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user