0
0

修改了重命名的bug

This commit is contained in:
xubing
2026-01-30 11:26:15 +08:00
parent ad34dca525
commit 10f6f34127
6 changed files with 264 additions and 905 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -34,13 +34,12 @@ export class ProjectService {
* 文件名/文件夹名净化:保留大小写,允许中文、字母、数字、-、_、.
*/
public sanitizeFileName(name: string): string {
let fileName = name.trim(); // 移除 .toLowerCase() 以保留大小写
let fileName = name.trim();
// 1. 替换所有空格为下划线
fileName = fileName.replace(/\s+/g, '_');
// 2. 移除特殊字符
// 在正则中增加了 A-Z 以及 \. (注意:\u4e00-\u9fa5 匹配中文)
fileName = fileName.replace(/[^\u4e00-\u9fa5a-zA-Z0-9_\-\.]/g, '');
// 3. 确保不为空
@@ -50,6 +49,39 @@ export class ProjectService {
return fileName;
}
// =============== 查重工具方法 (新增) ===============
/** 检查项目名是否重复 */
isProjectNameExists(name: string): boolean {
return this.projects.some(p => p.name === name);
}
/** 检查指定项目下的飞行器名是否重复 */
isAircraftNameExists(projectId: string, name: string): boolean {
return this.aircrafts.some(a => a.projectId === projectId && a.name === name);
}
/** 检查指定飞行器下的容器名是否重复 */
isContainerNameExists(aircraftId: string, name: string): boolean {
return this.containers.some(c => c.aircraftId === aircraftId && c.name === name);
}
/** 检查指定容器下的配置名或文件名是否重复 */
isConfigOrFolderConflict(containerId: string, name: string, fileName: string, excludeId?: string): { nameConflict: boolean, fileConflict: boolean } {
const configs = this.getConfigsByContainer(containerId);
const folders = this.getModuleFoldersByContainer(containerId);
// 检查显示名称冲突 (排除自身)
const nameConflict = configs.some(c => c.id !== excludeId && c.name === name) ||
folders.some(f => f.id !== excludeId && f.name === name);
// 检查磁盘文件名/文件夹名冲突 (排除自身)
const fileConflict = configs.some(c => c.id !== excludeId && c.fileName === fileName) ||
folders.some(f => f.id !== excludeId && f.localPath.split('/').pop() === fileName);
return { nameConflict, fileConflict };
}
// =============== 项目相关方法 ===============
getProjects(): Project[] {
@@ -91,14 +123,12 @@ export class ProjectService {
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));
@@ -129,7 +159,6 @@ export class ProjectService {
};
this.aircrafts.push(newAircraft);
// 创建飞行器目录
await this.createAircraftDirectory(newAircraft);
return newId;
}
@@ -147,27 +176,19 @@ export class ProjectService {
const aircraft = this.aircrafts.find(a => a.id === aircraftId);
if (!aircraft) return false;
// ⚠️ 先算出要删的 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;
}
/**
* 从已存在的磁盘目录导入飞行器
* 不会创建/删除任何文件,只在内存中补充 Aircraft / Container / ModuleFolder 数据
*/
async importAircraftFromExistingFolder(projectId: string, aircraftName: string): Promise<string> {
// 已存在同名飞行器直接返回
const existed = this.getAircraftsByProject(projectId).find(a => a.name === aircraftName);
if (existed) {
return existed.id;
@@ -188,11 +209,9 @@ export class ProjectService {
const aircraftDir = path.join(projectPath, aircraftName);
if (!fs.existsSync(aircraftDir)) {
// 目录不存在就不再解析子目录
return newId;
}
// 每个子目录视为一个容器(排除 .git
const entries = await fs.promises.readdir(aircraftDir, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory()) continue;
@@ -206,12 +225,6 @@ export class ProjectService {
// =============== 容器相关方法 ===============
/**
* ⚠️ 这里不再做 .git 过滤:
* - 我们只会通过 createContainer / importContainerFromExistingFolder 创建容器
* - importContainerFromExistingFolder 内部已经排除了 .git
* 所以不会出现名字叫 ".git" 的容器
*/
getContainersByAircraft(aircraftId: string): Container[] {
return this.containers.filter(c => c.aircraftId === aircraftId);
}
@@ -230,7 +243,6 @@ export class ProjectService {
this.containers.push(newContainer);
await this.createContainerDirectory(newContainer);
// UI 手动创建的容器,仍然保留“默认两个配置”的行为
await this.createDefaultConfigs(newContainer);
return newId;
@@ -256,15 +268,6 @@ export class ProjectService {
return true;
}
/**
* 从已存在的磁盘目录导入容器
*
* ✅ 你的最新需求:
* - 不创建“默认两个配置”
* - 自动扫描容器目录下的『子文件夹』
* - 每个子文件夹创建一个 ModuleFoldertype: 'local'
* - 排除 `.git` 子文件夹
*/
async importContainerFromExistingFolder(aircraftId: string, containerName: string): Promise<string> {
if (containerName === '.git') {
throw new Error('不能将 .git 导入为容器');
@@ -283,18 +286,11 @@ export class ProjectService {
};
this.containers.push(container);
// 🚩 关键逻辑:扫描容器目录下的子文件夹 -> 创建 ModuleFolder
await this.scanContainerModuleFolders(container);
// 不再创建默认两个配置(不调用 createDefaultConfigs
// 也不创建目录(目录是 Git 克隆出来的,本来就存在)
return newId;
}
/**
* 扫描容器目录中的子文件夹(不含 .git将其作为本地模块文件夹记录
*/
private async scanContainerModuleFolders(container: Container): Promise<void> {
try {
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
@@ -343,11 +339,8 @@ export class ProjectService {
return this.configs.find(c => c.id === configId);
}
// src/panels/services/ProjectService.ts
async createConfig(name: string, containerId: string): Promise<string> {
const newId = this.generateUniqueId('cfg', this.configs);
// 使用净化后的文件名
const newFileName = this.sanitizeFileName(name);
const newConfig: Config = {
@@ -358,16 +351,12 @@ export class ProjectService {
};
this.configs.push(newConfig);
// 1. 首先确保父级容器目录存在
await this.ensureContainerDirectoryExists(containerId);
// 2. 获取文件的完整磁盘路径
const filePath = this.getConfigFilePath(newId);
if (filePath) {
try {
const fileUri = vscode.Uri.file(filePath);
// 3. 真实创建文件:写入一个空的 Uint8Array 即可创建一个空文件
// 如果你希望有默认内容,可以在这里自定义 TextEncoder().encode("...")
await vscode.workspace.fs.writeFile(fileUri, new Uint8Array());
console.log(`✅ 磁盘文件已真实创建: ${filePath}`);
} catch (error) {
@@ -378,9 +367,6 @@ export class ProjectService {
return newId;
}
/**
* [修改] 同时更新配置显示名称和文件名称
*/
updateConfigName(configId: string, newName: string, newFileName: string): boolean {
const config = this.configs.find(c => c.id === configId);
if (config) {
@@ -399,19 +385,14 @@ export class ProjectService {
return true;
}
/**
* [新增] 重命名磁盘上的配置文件
*/
async renameConfigFileOnDisk(configId: string, newFileName: string): Promise<boolean> {
const config = this.configs.find(c => c.id === configId);
if (!config) return false;
const oldFilePath = this.getConfigFilePath(configId);
// 为了计算新路径,临时使用新的文件名
const oldFileName = config.fileName;
config.fileName = newFileName;
const newFilePath = this.getConfigFilePath(configId);
// 恢复旧的文件名,因为 ProjectService.updateConfigName 会在 ConfigPanel 中调用
config.fileName = oldFileName;
if (!oldFilePath || !newFilePath) {
@@ -467,11 +448,8 @@ export class ProjectService {
return true;
}
// =============== 文件系统操作 (新增/修改) ===============
// =============== 文件系统操作 ===============
/**
* 获取项目目录重命名所需的旧路径和新路径(新增)
*/
getProjectOldAndNewPaths(projectId: string, newName: string): { oldPath: string, newPath: string } | null {
const project = this.projects.find(p => p.id === projectId);
const oldPath = this.projectPaths.get(projectId);
@@ -479,15 +457,11 @@ export class ProjectService {
if (!project || !oldPath) return null;
const parentDir = path.dirname(oldPath);
// 新路径使用新的项目名称作为文件夹名
const newPath = path.join(parentDir, newName);
return { oldPath, newPath };
}
/**
* 获取飞行器目录的完整路径
*/
getAircraftDirectoryPath(aircraftId: string): string | null {
const aircraft = this.getAircraft(aircraftId);
if (!aircraft) return null;
@@ -498,10 +472,6 @@ export class ProjectService {
return path.join(projectPath, aircraft.name);
}
/**
* 获取飞行器目录重命名所需的旧路径和新路径(新增)
* 注意oldPath使用内存中当前的名称newPath使用将要更新的新名称
*/
getAircraftOldAndNewPaths(aircraftId: string, newName: string): { oldPath: string, newPath: string } | null {
const aircraft = this.aircrafts.find(a => a.id === aircraftId);
if (!aircraft) return null;
@@ -516,9 +486,6 @@ export class ProjectService {
}
/**
* 获取容器目录的完整路径
*/
getContainerDirectoryPath(containerId: string): string | null {
const container = this.getContainer(containerId);
if (!container) return null;
@@ -532,10 +499,6 @@ export class ProjectService {
return path.join(projectPath, aircraft.name, container.name);
}
/**
* 获取容器目录重命名所需的旧路径和新路径(新增)
* 注意oldPath使用内存中当前的名称newPath使用将要更新的新名称
*/
getContainerOldAndNewPaths(containerId: string, newName: string): { oldPath: string, newPath: string } | null {
const container = this.containers.find(c => c.id === containerId);
if (!container) return null;
@@ -554,9 +517,6 @@ export class ProjectService {
}
/**
* 递归删除目录
*/
async deleteDirectoryFromDisk(directoryPath: string): Promise<boolean> {
if (!directoryPath) return false;
@@ -569,13 +529,10 @@ export class ProjectService {
return false;
} catch (error) {
console.error(`删除目录失败: ${error}`);
throw error; // Re-throw to be handled by ConfigPanel
throw error;
}
}
/**
* 重命名磁盘上的目录(新增)
*/
async renameDirectoryOnDisk(oldPath: string, newPath: string): Promise<boolean> {
if (!oldPath || !newPath) return false;
@@ -589,7 +546,7 @@ export class ProjectService {
return false;
} catch (error) {
console.error(`重命名目录失败: ${error}`);
throw error; // Re-throw to be handled by ConfigPanel
throw error;
}
}
@@ -659,10 +616,6 @@ export class ProjectService {
}
}
/**
* 仅用于「新建容器」时的默认两个配置
* (导入容器时不会调用)
*/
private async createDefaultConfigs(container: Container): Promise<void> {
this.configs.push({
id: this.generateUniqueId('cfg', this.configs),
@@ -709,21 +662,18 @@ export class ProjectService {
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));
// ⭐ 载入新数据时顺便过滤掉名字为 ".git" 的容器(避免历史数据带进来)
const cleanedContainers = data.containers.filter(c => c.name !== '.git');
this.projects.push(...data.projects);
@@ -778,7 +728,6 @@ export class ProjectService {
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);
}