0
0
Files
vs-p/out/panels/services/ProjectService.js
2025-12-04 10:16:57 +08:00

438 lines
18 KiB
JavaScript

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProjectService = void 0;
// src/panels/services/ProjectService.ts
const vscode = __importStar(require("vscode"));
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const StorageService_1 = require("./StorageService");
/**
* 项目数据管理服务
*/
class ProjectService {
constructor() {
this.projects = [];
this.aircrafts = [];
this.containers = [];
this.configs = [];
this.moduleFolders = [];
this.projectPaths = new Map();
}
/**
* 生成唯一ID
*/
generateUniqueId(prefix, _existingItems = []) {
const randomPart = `${Date.now().toString(36)}${Math.random().toString(36).substring(2, 8)}`;
return `${prefix}${randomPart}`;
}
// =============== 项目相关方法 ===============
getProjects() {
return this.projects;
}
getProjectPaths() {
return this.projectPaths;
}
getProjectPath(projectId) {
return this.projectPaths.get(projectId);
}
setProjectPath(projectId, path) {
this.projectPaths.set(projectId, path);
}
async createProject(name) {
const newId = this.generateUniqueId('p', this.projects);
const newProject = {
id: newId,
name: name
};
this.projects.push(newProject);
return newId;
}
updateProjectName(projectId, newName) {
const project = this.projects.find(p => p.id === projectId);
if (project) {
project.name = newName;
return true;
}
return false;
}
deleteProject(projectId) {
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) {
return this.aircrafts.filter(a => a.projectId === projectId);
}
async createAircraft(name, projectId) {
const newId = this.generateUniqueId('a', this.aircrafts);
const newAircraft = {
id: newId,
name: name,
projectId: projectId
};
this.aircrafts.push(newAircraft);
// 创建飞行器目录
await this.createAircraftDirectory(newAircraft);
return newId;
}
updateAircraftName(aircraftId, newName) {
const aircraft = this.aircrafts.find(a => a.id === aircraftId);
if (aircraft) {
aircraft.name = newName;
return true;
}
return false;
}
deleteAircraft(aircraftId) {
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) {
return this.containers.filter(c => c.aircraftId === aircraftId);
}
async createContainer(name, aircraftId) {
const newId = this.generateUniqueId('c', this.containers);
const newContainer = {
id: newId,
name: name,
aircraftId: aircraftId
};
this.containers.push(newContainer);
await this.createContainerDirectory(newContainer);
await this.createDefaultConfigs(newContainer);
return newId;
}
updateContainerName(containerId, newName) {
const container = this.containers.find(c => c.id === containerId);
if (container) {
container.name = newName;
return true;
}
return false;
}
deleteContainer(containerId) {
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) {
return this.configs.filter(cfg => cfg.containerId === containerId);
}
getConfig(configId) {
return this.configs.find(c => c.id === configId);
}
async createConfig(name, containerId) {
const newId = this.generateUniqueId('cfg', this.configs);
const newConfig = {
id: newId,
name: name,
fileName: name.toLowerCase().replace(/\s+/g, '_'),
containerId: containerId
};
this.configs.push(newConfig);
await this.ensureContainerDirectoryExists(containerId);
return newId;
}
updateConfigName(configId, newName) {
const config = this.configs.find(c => c.id === configId);
if (config) {
config.name = newName;
return true;
}
return false;
}
deleteConfig(configId) {
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) {
return this.moduleFolders.filter(folder => folder.containerId === containerId);
}
getModuleFolder(folderId) {
return this.moduleFolders.find(f => f.id === folderId);
}
addModuleFolder(folder) {
this.moduleFolders.push(folder);
}
updateModuleFolder(folderId, updates) {
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) {
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, newName) {
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;
}
// =============== 文件系统操作 ===============
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}`);
}
}
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);
await vscode.workspace.fs.createDirectory(aircraftDir);
await vscode.workspace.fs.createDirectory(containerDir);
console.log(`✅ 创建容器目录: ${containerDir.fsPath}`);
}
catch (error) {
console.error(`创建容器目录失败: ${error}`);
}
}
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 createDefaultConfigs(container) {
this.configs.push({
id: this.generateUniqueId('cfg', this.configs),
name: '配置1',
fileName: 'dockerfile',
containerId: container.id
});
this.configs.push({
id: this.generateUniqueId('cfg', this.configs),
name: '配置2',
fileName: 'docker-compose.yml',
containerId: container.id
});
}
// =============== 数据持久化 ===============
async saveCurrentProjectData(projectId) {
try {
const projectPath = this.projectPaths.get(projectId);
if (!projectPath) {
console.warn('未找到项目存储路径,数据将不会保存');
return false;
}
const data = this.getProjectData(projectId);
return await StorageService_1.StorageService.saveProjectData(projectPath, data);
}
catch (error) {
console.error('保存项目数据失败:', error);
return false;
}
}
async loadProjectData(projectPath) {
try {
const data = await StorageService_1.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;
}
}
getProjectData(projectId) {
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) {
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) {
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) {
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;
}
}
}
exports.ProjectService = ProjectService;
//# sourceMappingURL=ProjectService.js.map