0
0
Files
vs-p/out/panels/ConfigPanel.js

1949 lines
86 KiB
JavaScript
Raw Normal View History

2025-11-18 09:10:47 +08:00
"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
2025-11-18 09:10:47 +08:00
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigPanel = void 0;
const vscode = __importStar(require("vscode"));
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const isomorphic_git_1 = __importDefault(require("isomorphic-git"));
const node_1 = __importDefault(require("isomorphic-git/http/node"));
2025-11-18 18:45:30 +08:00
const ProjectView_1 = require("./views/ProjectView");
const AircraftView_1 = require("./views/AircraftView");
const ContainerView_1 = require("./views/ContainerView");
const ConfigView_1 = require("./views/ConfigView");
2025-11-18 09:10:47 +08:00
class ConfigPanel {
static createOrShow(extensionUri) {
const column = vscode.window.activeTextEditor?.viewColumn || vscode.ViewColumn.One;
if (ConfigPanel.currentPanel) {
ConfigPanel.currentPanel.panel.reveal(column);
return;
}
2025-11-18 18:45:30 +08:00
const panel = vscode.window.createWebviewPanel('DCSP', '数字卫星构建平台', column, {
2025-11-18 09:10:47 +08:00
enableScripts: true,
localResourceRoots: [extensionUri],
retainContextWhenHidden: true
});
ConfigPanel.currentPanel = new ConfigPanel(panel, extensionUri);
}
constructor(panel, extensionUri) {
2025-11-18 18:45:30 +08:00
this.currentView = 'projects';
2025-11-18 09:10:47 +08:00
this.currentProjectId = '';
this.currentAircraftId = '';
this.currentContainerId = '';
2025-11-27 22:04:32 +08:00
this.currentModuleFolderId = '';
2025-11-18 09:10:47 +08:00
// 数据存储
2025-11-18 18:45:30 +08:00
this.projects = [];
this.aircrafts = [];
this.containers = [];
this.configs = [];
2025-11-27 22:04:32 +08:00
this.moduleFolders = []; // 统一的模块文件夹数据
// Git 文件树
2025-11-27 22:04:32 +08:00
this.currentModuleFolderFileTree = [];
2025-11-18 14:03:22 +08:00
// 项目存储路径映射
this.projectPaths = new Map();
// Webview 状态跟踪
this.isWebviewDisposed = false;
2025-11-18 09:10:47 +08:00
this.panel = panel;
this.extensionUri = extensionUri;
this.isWebviewDisposed = false; // 初始化状态
2025-11-18 09:10:47 +08:00
// 初始化各个视图
2025-11-18 18:45:30 +08:00
this.projectView = new ProjectView_1.ProjectView(extensionUri);
this.aircraftView = new AircraftView_1.AircraftView(extensionUri);
this.containerView = new ContainerView_1.ContainerView(extensionUri);
this.configView = new ConfigView_1.ConfigView(extensionUri);
2025-11-18 09:10:47 +08:00
this.updateWebview();
this.setupMessageListener();
this.panel.onDidDispose(() => {
this.isWebviewDisposed = true; // 标记为已销毁
2025-11-18 09:10:47 +08:00
ConfigPanel.currentPanel = undefined;
});
}
setupMessageListener() {
this.panel.webview.onDidReceiveMessage(async (data) => {
console.log('📨 收到Webview消息:', data);
// 检查 Webview 是否仍然有效
if (this.isWebviewDisposed) {
console.log('⚠️ Webview 已被销毁,忽略消息');
return;
}
try {
switch (data.type) {
case 'openExistingProject':
await this.openExistingProject();
break;
case 'configureProject':
const selectedPath = await this.selectProjectPath(data.projectId, data.projectName);
if (selectedPath) {
this.currentView = 'aircrafts';
this.currentProjectId = data.projectId;
this.updateWebview();
}
break;
case 'openProject':
// 已配置的项目直接打开
2025-11-18 18:45:30 +08:00
this.currentView = 'aircrafts';
2025-11-18 14:03:22 +08:00
this.currentProjectId = data.projectId;
this.updateWebview();
break;
case 'openAircraftConfig':
this.currentView = 'containers';
this.currentProjectId = data.projectId;
this.currentAircraftId = data.aircraftId;
this.updateWebview();
break;
case 'openContainerConfig':
this.currentView = 'configs';
this.currentContainerId = data.containerId;
this.updateWebview();
break;
// 修复返回按钮的消息处理
case 'goBackToProjects':
this.currentView = 'projects';
// 清空当前选择的ID
this.currentProjectId = '';
this.currentAircraftId = '';
this.currentContainerId = '';
2025-11-27 22:04:32 +08:00
this.currentModuleFolderId = '';
this.updateWebview();
break;
case 'goBackToAircrafts':
this.currentView = 'aircrafts';
// 保持 currentProjectId清空其他ID
this.currentAircraftId = '';
this.currentContainerId = '';
this.updateWebview();
break;
case 'goBackToContainers':
this.currentView = 'containers';
// 保持 currentProjectId 和 currentAircraftId清空 currentContainerId
this.currentContainerId = '';
this.updateWebview();
break;
case 'updateProjectName':
await this.updateProjectName(data.projectId, data.name);
break;
case 'createProject':
await this.createProject(data.name);
break;
case 'updateAircraftName':
await this.updateAircraftName(data.aircraftId, data.name);
break;
case 'createAircraft':
await this.createAircraft(data.name);
break;
case 'updateContainerName':
await this.updateContainerName(data.containerId, data.name);
break;
case 'createContainer':
await this.createContainer(data.name);
break;
case 'updateConfigName':
await this.updateConfigName(data.configId, data.name);
break;
case 'createConfig':
await this.createConfig(data.name);
break;
case 'deleteProject':
await this.deleteProject(data.projectId);
break;
case 'deleteAircraft':
await this.deleteAircraft(data.aircraftId);
break;
case 'deleteContainer':
await this.deleteContainer(data.containerId);
break;
case 'deleteConfig':
await this.deleteConfig(data.configId);
break;
// Git 仓库管理功能
case 'fetchBranches':
console.log('🌿 获取分支列表:', data.url);
await this.fetchBranches(data.url);
break;
case 'cloneBranches':
console.log('🚀 克隆选中的分支:', data);
await this.cloneBranches(data.url, data.branches);
break;
case 'cancelBranchSelection':
console.log('❌ 取消分支选择');
this.updateWebview();
break;
2025-11-27 22:04:32 +08:00
case 'loadModuleFolder':
await this.loadModuleFolder(data.folderId);
break;
2025-11-27 22:04:32 +08:00
case 'syncGitModuleFolder':
await this.syncGitModuleFolder(data.folderId);
break;
2025-11-27 22:04:32 +08:00
case 'deleteModuleFolder':
await this.deleteModuleFolder(data.folderId);
break;
case 'importGitFile':
await this.importGitFile(data.filePath);
break;
2025-11-27 22:04:32 +08:00
case 'openModuleFolderInVSCode':
await this.openTheModuleFolder(data.moduleType, data.folderId);
2025-11-25 16:32:06 +08:00
break;
2025-11-25 21:13:41 +08:00
case 'openConfigFileInVSCode':
await this.openConfigFileInVSCode(data.configId);
break;
case 'mergeConfigs':
await this.mergeConfigs(data.configIds, data.displayName, data.folderName);
break;
case 'deleteMergedFolder':
2025-11-27 22:04:32 +08:00
await this.deleteModuleFolder(data.folderId);
break;
case 'openMergedFolderInVSCode':
2025-11-28 14:16:19 +08:00
await this.openTheModuleFolder('local', data.folderId);
break;
case 'loadMergedFolder':
2025-11-28 14:16:19 +08:00
await this.openTheModuleFolder('local', data.folderId);
break;
case 'openTheModuleFolder':
await this.openTheModuleFolder(data.moduleType, data.id);
break;
case 'uploadGitModuleFolder':
await this.uploadGitModuleFolder(data.folderId, data.username, data.password);
break;
case 'uploadLocalModuleFolder':
await this.uploadLocalModuleFolder(data.folderId, data.repoUrl, data.branchName);
break;
}
}
catch (error) {
console.error('处理 Webview 消息时出错:', error);
if (!this.isWebviewDisposed) {
vscode.window.showErrorMessage(`处理操作时出错: ${error}`);
}
}
});
}
// === 目录创建方法 ===
/**
* 创建飞行器目录
*/
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);
// 确保飞行器目录存在
try {
await vscode.workspace.fs.createDirectory(aircraftDir);
}
catch (error) {
// 目录可能已存在,忽略错误
}
// 创建容器目录
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}`);
}
}
// === Git 仓库管理方法 ===
/**
2025-11-27 22:04:32 +08:00
* 添加 Git 模块文件夹到容器目录
*/
2025-11-28 14:16:19 +08:00
async addGitModuleFolder(url, displayName, folderName, branch) {
try {
// 验证 URL
if (!url || !url.startsWith('http')) {
vscode.window.showErrorMessage('请输入有效的 Git 仓库 URL');
return;
}
if (!this.currentContainerId) {
vscode.window.showErrorMessage('请先选择容器');
return;
}
2025-11-27 22:04:32 +08:00
const folderId = 'git-' + Date.now();
// 构建本地路径
const container = this.containers.find(c => c.id === this.currentContainerId);
if (!container) {
vscode.window.showErrorMessage('未找到容器');
return;
}
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
if (!aircraft) {
vscode.window.showErrorMessage('未找到飞行器');
return;
}
const projectPath = this.projectPaths.get(aircraft.projectId);
if (!projectPath) {
vscode.window.showErrorMessage('未找到项目路径');
return;
}
2025-11-28 14:16:19 +08:00
// 构建相对路径(从项目路径开始)- 使用 folderName 作为实际文件夹名
const relativePath = `/${aircraft.projectId}/${aircraft.name}/${container.name}/${folderName}`;
// 完整路径用于实际操作 - 使用 folderName 作为实际文件夹名
const localPath = path.join(projectPath, aircraft.name, container.name, folderName);
console.log(`📁 准备克隆仓库: ${displayName}, 分支: ${branch}, 路径: ${localPath}`);
2025-11-27 22:04:32 +08:00
console.log(`📁 相对路径: ${relativePath}`);
2025-11-28 14:16:19 +08:00
// 修复:检查是否已存在相同实际路径的模块文件夹,而不是显示名称
const existingFolder = this.moduleFolders.find(folder => folder.localPath === relativePath && folder.containerId === this.currentContainerId);
2025-11-27 22:04:32 +08:00
if (existingFolder) {
2025-11-28 14:16:19 +08:00
vscode.window.showWarningMessage(`该路径的模块文件夹已存在: ${folderName}`);
return;
}
2025-11-27 22:04:32 +08:00
const newFolder = {
id: folderId,
2025-11-28 14:16:19 +08:00
name: displayName,
2025-11-27 22:04:32 +08:00
type: 'git',
localPath: relativePath,
containerId: this.currentContainerId
};
// 显示进度
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
2025-11-28 14:16:19 +08:00
title: `正在克隆仓库: ${displayName}`,
cancellable: false
}, async (progress) => {
progress.report({ increment: 0 });
try {
2025-11-28 14:16:19 +08:00
// 确保父目录存在
const parentDir = path.dirname(localPath);
await fs.promises.mkdir(parentDir, { recursive: true });
// 检查目标目录是否已存在
let dirExists = false;
try {
await fs.promises.access(localPath);
dirExists = true;
}
catch {
dirExists = false;
}
if (dirExists) {
const dirContents = await fs.promises.readdir(localPath);
if (dirContents.length > 0) {
const confirm = await vscode.window.showWarningMessage(`目标目录 "${folderName}" 不为空,确定要覆盖吗?`, { modal: true }, '确定覆盖', '取消');
if (confirm !== '确定覆盖') {
vscode.window.showInformationMessage('克隆操作已取消');
return;
}
// 清空目录(除了 .git 文件夹,如果存在的话)
for (const item of dirContents) {
const itemPath = path.join(localPath, item);
if (item !== '.git') {
await fs.promises.rm(itemPath, { recursive: true, force: true });
}
}
}
}
2025-11-28 14:16:19 +08:00
console.log(`🚀 开始克隆: ${url} -> ${localPath}, 分支: ${branch}`);
// 克隆仓库
await isomorphic_git_1.default.clone({
fs: fs,
http: node_1.default,
dir: localPath,
url: url,
singleBranch: true,
depth: 1,
2025-11-27 22:04:32 +08:00
ref: branch || 'main',
onProgress: (event) => {
2025-11-28 14:16:19 +08:00
console.log(`📊 克隆进度: ${event.phase} - ${event.loaded}/${event.total}`);
if (event.total) {
const percent = (event.loaded / event.total) * 100;
2025-11-28 14:16:19 +08:00
progress.report({
increment: percent,
message: `${event.phase}... (${Math.round(percent)}%)`
});
}
else {
progress.report({ message: `${event.phase}...` });
}
}
});
console.log('✅ Git克隆成功完成');
2025-11-28 14:16:19 +08:00
// 验证克隆是否真的成功
const clonedContents = await fs.promises.readdir(localPath);
console.log(`📁 克隆后的目录内容:`, clonedContents);
if (clonedContents.length === 0) {
throw new Error('克隆后目录为空,可能克隆失败');
}
// 只有在克隆成功后才添加到列表
2025-11-27 22:04:32 +08:00
this.moduleFolders.push(newFolder);
await this.saveCurrentProjectData();
2025-11-27 22:04:32 +08:00
console.log('✅ Git模块文件夹数据已保存到项目文件');
2025-11-28 14:16:19 +08:00
vscode.window.showInformationMessage(`Git 仓库克隆成功: ${displayName}`);
// 检查 Webview 状态后再加载文件树
if (!this.isWebviewDisposed) {
2025-11-27 22:04:32 +08:00
console.log('🌳 开始加载模块文件夹文件树...');
// 自动加载文件树
this.currentModuleFolderId = folderId;
await this.loadModuleFolderFileTree(folderId);
console.log('✅ 模块文件夹文件树加载完成');
}
else {
console.log('⚠️ Webview 已被销毁,跳过文件树加载');
}
}
catch (error) {
console.error('❌ 在克隆过程中捕获错误:', error);
2025-11-28 14:16:19 +08:00
// 清理:如果克隆失败,删除可能创建的不完整目录
try {
if (fs.existsSync(localPath)) {
await fs.promises.rm(localPath, { recursive: true, force: true });
console.log('🧹 已清理克隆失败的目录');
}
}
catch (cleanupError) {
console.error('清理失败目录时出错:', cleanupError);
}
vscode.window.showErrorMessage(`克隆仓库失败: ${error}`);
2025-11-28 14:16:19 +08:00
// 重新抛出错误,让外部知道克隆失败
throw error;
}
});
}
catch (error) {
2025-11-27 22:04:32 +08:00
console.error('❌ 在addGitModuleFolder外部捕获错误:', error);
vscode.window.showErrorMessage(`添加 Git 模块文件夹失败: ${error}`);
}
}
/**
2025-11-27 22:04:32 +08:00
* 加载模块文件夹
*/
2025-11-27 22:04:32 +08:00
async loadModuleFolder(folderId) {
this.currentModuleFolderId = folderId;
const folder = this.moduleFolders.find(f => f.id === folderId);
if (folder && folder.type === 'git') {
await this.loadModuleFolderFileTree(folderId);
}
this.updateWebview();
}
/**
2025-11-27 22:04:32 +08:00
* 同步 Git 模块文件夹
*/
2025-11-27 22:04:32 +08:00
async syncGitModuleFolder(folderId) {
const folder = this.moduleFolders.find(f => f.id === folderId);
if (!folder || folder.type !== 'git') {
vscode.window.showErrorMessage('未找到指定的 Git 模块文件夹');
return;
}
// 获取完整路径
const fullPath = this.getModuleFolderFullPath(folder);
if (!fullPath) {
vscode.window.showErrorMessage('无法获取模块文件夹的完整路径');
return;
}
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
2025-11-27 22:04:32 +08:00
title: `正在同步仓库: ${folder.name}`,
cancellable: false
}, async (progress) => {
try {
progress.report({ increment: 0, message: '拉取最新更改...' });
// 拉取最新更改
await isomorphic_git_1.default.pull({
fs: fs,
http: node_1.default,
2025-11-27 22:04:32 +08:00
dir: fullPath,
author: { name: 'DCSP User', email: 'user@dcsp.local' },
fastForward: true
});
// 重新加载文件树
2025-11-27 22:04:32 +08:00
await this.loadModuleFolderFileTree(folderId);
vscode.window.showInformationMessage(`Git 仓库同步成功: ${folder.name}`);
this.updateWebview();
}
catch (error) {
vscode.window.showErrorMessage(`同步 Git 仓库失败: ${error}`);
}
});
}
/**
2025-11-27 22:04:32 +08:00
* 删除模块文件夹
2025-11-25 21:13:41 +08:00
*/
2025-11-27 22:04:32 +08:00
async deleteModuleFolder(folderId) {
const folder = this.moduleFolders.find(f => f.id === folderId);
if (!folder)
return;
2025-11-27 22:04:32 +08:00
const confirm = await vscode.window.showWarningMessage(`确定要删除模块文件夹 "${folder.name}" 吗?这将删除本地文件。`, { modal: true }, '确定删除', '取消');
if (confirm === '确定删除') {
try {
2025-11-27 22:04:32 +08:00
// 获取完整路径并删除文件夹
const fullPath = this.getModuleFolderFullPath(folder);
if (fullPath) {
await fs.promises.rm(fullPath, { recursive: true, force: true });
}
// 从列表中移除
2025-11-27 22:04:32 +08:00
this.moduleFolders = this.moduleFolders.filter(f => f.id !== folderId);
await this.saveCurrentProjectData();
2025-11-27 22:04:32 +08:00
// 如果删除的是当前文件夹,清空状态
if (this.currentModuleFolderId === folderId) {
this.currentModuleFolderId = '';
this.currentModuleFolderFileTree = [];
}
2025-11-27 22:04:32 +08:00
vscode.window.showInformationMessage(`模块文件夹已删除: ${folder.name}`);
this.updateWebview();
}
catch (error) {
2025-11-27 22:04:32 +08:00
vscode.window.showErrorMessage(`删除模块文件夹失败: ${error}`);
2025-11-18 09:10:47 +08:00
}
}
}
/**
2025-11-27 22:04:32 +08:00
* 加载模块文件夹文件树
*/
2025-11-27 22:04:32 +08:00
async loadModuleFolderFileTree(folderId) {
// 检查 Webview 是否仍然有效
if (this.isWebviewDisposed) {
console.log('⚠️ Webview 已被销毁,跳过文件树加载');
return;
}
2025-11-27 22:04:32 +08:00
const folder = this.moduleFolders.find(f => f.id === folderId);
if (!folder)
return;
// 通知前端开始加载
try {
this.panel.webview.postMessage({
2025-11-27 22:04:32 +08:00
type: 'moduleFolderLoading',
loading: true
});
}
catch (error) {
console.log('⚠️ 无法发送加载消息Webview 可能已被销毁');
return;
}
try {
2025-11-27 22:04:32 +08:00
const fullPath = this.getModuleFolderFullPath(folder);
if (fullPath) {
const fileTree = await this.buildFileTree(fullPath);
this.currentModuleFolderFileTree = fileTree;
}
}
catch (error) {
2025-11-27 22:04:32 +08:00
console.error('加载模块文件夹文件树失败:', error);
this.currentModuleFolderFileTree = [];
}
// 再次检查 Webview 状态
if (this.isWebviewDisposed) {
console.log('⚠️ Webview 已被销毁,跳过完成通知');
return;
}
// 通知前端加载完成
try {
this.panel.webview.postMessage({
2025-11-27 22:04:32 +08:00
type: 'moduleFolderLoading',
loading: false
});
this.updateWebview();
}
catch (error) {
console.log('⚠️ 无法发送完成消息Webview 可能已被销毁');
}
}
/**
* 构建文件树
*/
async buildFileTree(dir, relativePath = '') {
try {
const files = await fs.promises.readdir(dir);
const tree = [];
for (const file of files) {
// 忽略 .git 文件夹和 .dcsp-data.json
if (file.startsWith('.') && file !== '.git')
continue;
if (file === '.dcsp-data.json')
continue;
const filePath = path.join(dir, file);
const stats = await fs.promises.stat(filePath);
const currentRelativePath = path.join(relativePath, file);
if (stats.isDirectory()) {
const children = await this.buildFileTree(filePath, currentRelativePath);
tree.push({
name: file,
type: 'folder',
path: currentRelativePath,
children: children
});
}
else {
tree.push({
name: file,
type: 'file',
path: currentRelativePath
});
}
}
return tree;
}
catch (error) {
console.error('构建文件树失败:', error);
return [];
}
}
/**
* 导入 Git 文件到当前容器
*/
async importGitFile(filePath) {
2025-11-27 22:04:32 +08:00
if (!this.currentModuleFolderId || !this.currentContainerId) {
vscode.window.showErrorMessage('请先选择模块文件夹和容器');
return;
}
2025-11-27 22:04:32 +08:00
const folder = this.moduleFolders.find(f => f.id === this.currentModuleFolderId);
if (!folder || folder.type !== 'git') {
vscode.window.showErrorMessage('未找到当前 Git 模块文件夹');
return;
}
const container = this.containers.find(c => c.id === this.currentContainerId);
if (!container) {
vscode.window.showErrorMessage('未找到当前容器');
return;
}
try {
2025-11-27 22:04:32 +08:00
const fullPath = this.getModuleFolderFullPath(folder);
if (!fullPath) {
vscode.window.showErrorMessage('无法获取模块文件夹路径');
return;
}
const fileFullPath = path.join(fullPath, filePath);
const content = await fs.promises.readFile(fileFullPath, 'utf8');
const fileName = path.basename(filePath);
// 创建新配置
const newId = 'cfg' + (this.configs.length + 1);
const newConfig = {
id: newId,
name: fileName,
fileName: fileName,
content: content,
containerId: this.currentContainerId
};
this.configs.push(newConfig);
await this.saveCurrentProjectData();
vscode.window.showInformationMessage(`文件已导入到容器 ${container.name}: ${fileName}`);
this.updateWebview();
}
catch (error) {
vscode.window.showErrorMessage(`导入文件失败: ${error}`);
}
2025-11-18 09:10:47 +08:00
}
// === 原有项目配置管理方法 ===
2025-11-18 21:14:10 +08:00
// === 打开现有项目功能 ===
async openExistingProject() {
try {
const result = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
openLabel: '选择项目文件夹',
title: '选择包含项目数据的文件夹'
});
if (result && result.length > 0) {
const selectedPath = result[0].fsPath;
await this.loadProjectData(selectedPath);
}
}
catch (error) {
vscode.window.showErrorMessage(`打开项目时出错: ${error}`);
}
}
// === 数据持久化方法 ===
/**
2025-11-21 16:07:48 +08:00
* 保存当前项目数据到项目路径
2025-11-18 21:14:10 +08:00
*/
2025-11-21 16:07:48 +08:00
async saveCurrentProjectData() {
2025-11-18 21:14:10 +08:00
try {
2025-11-21 16:07:48 +08:00
if (!this.currentProjectId) {
console.warn('未找到当前项目,数据将不会保存');
2025-11-21 16:07:48 +08:00
return;
2025-11-18 21:14:10 +08:00
}
2025-11-21 16:07:48 +08:00
const projectPath = this.projectPaths.get(this.currentProjectId);
2025-11-18 21:14:10 +08:00
if (!projectPath) {
console.warn('未找到项目存储路径,数据将不会保存');
2025-11-18 21:14:10 +08:00
return;
}
const dataUri = vscode.Uri.joinPath(vscode.Uri.file(projectPath), '.dcsp-data.json');
2025-11-21 16:07:48 +08:00
// 只保存与当前项目相关的数据
const currentProjectAircrafts = this.aircrafts.filter(a => a.projectId === this.currentProjectId);
const currentAircraftIds = currentProjectAircrafts.map(a => a.id);
const currentProjectContainers = this.containers.filter(c => currentAircraftIds.includes(c.aircraftId));
const currentContainerIds = currentProjectContainers.map(c => c.id);
const currentProjectConfigs = this.configs.filter(cfg => currentContainerIds.includes(cfg.containerId));
2025-11-27 22:04:32 +08:00
// 只保存与当前项目容器相关的模块文件夹
const currentProjectModuleFolders = this.moduleFolders.filter(folder => currentContainerIds.includes(folder.containerId));
2025-11-18 21:14:10 +08:00
const data = {
2025-11-21 16:07:48 +08:00
projects: this.projects.filter(p => p.id === this.currentProjectId),
aircrafts: currentProjectAircrafts,
containers: currentProjectContainers,
configs: currentProjectConfigs,
2025-11-27 22:04:32 +08:00
moduleFolders: currentProjectModuleFolders // 保存模块文件夹数据
2025-11-18 21:14:10 +08:00
};
const uint8Array = new TextEncoder().encode(JSON.stringify(data, null, 2));
await vscode.workspace.fs.writeFile(dataUri, uint8Array);
2025-11-27 22:04:32 +08:00
console.log('✅ 当前项目数据已保存,包含', currentProjectModuleFolders.length, '个模块文件夹');
2025-11-18 21:14:10 +08:00
}
catch (error) {
vscode.window.showErrorMessage(`保存项目数据失败: ${error}`);
}
}
/**
* 从项目路径加载数据
*/
async loadProjectData(projectPath) {
try {
const dataUri = vscode.Uri.joinPath(vscode.Uri.file(projectPath), '.dcsp-data.json');
// 检查数据文件是否存在
try {
await vscode.workspace.fs.stat(dataUri);
}
catch {
vscode.window.showErrorMessage('选择的文件夹中没有找到项目数据文件 (.dcsp-data.json)');
return false;
}
// 读取数据文件
const fileData = await vscode.workspace.fs.readFile(dataUri);
const dataStr = new TextDecoder().decode(fileData);
const data = JSON.parse(dataStr);
// 清空现有数据
this.projects = [];
this.aircrafts = [];
this.containers = [];
this.configs = [];
2025-11-27 22:04:32 +08:00
this.moduleFolders = []; // 清空模块文件夹数据
2025-11-18 21:14:10 +08:00
// 验证数据格式并加载
if (data.projects && Array.isArray(data.projects)) {
this.projects = data.projects;
}
if (data.aircrafts && Array.isArray(data.aircrafts)) {
this.aircrafts = data.aircrafts;
}
if (data.containers && Array.isArray(data.containers)) {
this.containers = data.containers;
}
if (data.configs && Array.isArray(data.configs)) {
this.configs = data.configs;
}
2025-11-27 22:04:32 +08:00
if (data.moduleFolders && Array.isArray(data.moduleFolders)) {
this.moduleFolders = data.moduleFolders; // 加载模块文件夹数据
}
2025-11-18 21:14:10 +08:00
// 设置当前项目为第一个项目(如果有的话)
if (this.projects.length > 0) {
this.currentProjectId = this.projects[0].id;
2025-11-21 16:07:48 +08:00
this.projectPaths.set(this.currentProjectId, projectPath);
2025-11-18 21:14:10 +08:00
this.currentView = 'aircrafts';
}
2025-11-27 22:04:32 +08:00
vscode.window.showInformationMessage(`项目数据已从 ${projectPath} 加载,包含 ${this.moduleFolders.length} 个模块文件夹`);
2025-11-18 21:14:10 +08:00
this.updateWebview();
return true;
}
catch (error) {
vscode.window.showErrorMessage(`加载项目数据失败: ${error}`);
return false;
}
}
/**
* 检查项目路径是否已存在数据
*/
async checkProjectPathHasData(projectPath) {
try {
const dataUri = vscode.Uri.joinPath(vscode.Uri.file(projectPath), '.dcsp-data.json');
await vscode.workspace.fs.stat(dataUri);
return true;
}
catch {
return false;
}
}
2025-11-18 14:03:22 +08:00
// === 项目路径选择 ===
async selectProjectPath(projectId, projectName) {
try {
2025-11-18 18:45:30 +08:00
// 选择现有路径或输入新路径
2025-11-18 14:03:22 +08:00
const choice = await vscode.window.showQuickPick([
{
label: '$(folder) 选择现有文件夹',
description: '从文件系统中选择已存在的文件夹',
value: 'select'
},
{
label: '$(new-folder) 创建新文件夹',
description: '输入新文件夹路径(将自动创建)',
value: 'create'
}
], {
placeHolder: '选择项目存储方式'
});
if (!choice) {
return null;
}
if (choice.value === 'select') {
// 选择现有路径
const result = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
openLabel: `选择 ${projectName} 的存储位置`,
title: `为项目 "${projectName}" 选择存储文件夹`
});
if (result && result.length > 0) {
const selectedPath = result[0].fsPath;
2025-11-18 21:14:10 +08:00
// 检查是否已存在数据
const hasExistingData = await this.checkProjectPathHasData(selectedPath);
if (hasExistingData) {
const loadChoice = await vscode.window.showWarningMessage(`在路径 ${selectedPath} 中检测到现有项目数据,是否加载?`, { modal: true }, '是,加载现有数据', '否,创建新项目');
if (loadChoice === '是,加载现有数据') {
// 加载现有数据
const success = await this.loadProjectData(selectedPath);
if (success) {
this.projectPaths.set(projectId, selectedPath);
this.currentView = 'aircrafts';
this.currentProjectId = projectId;
this.updateWebview();
vscode.window.showInformationMessage(`项目数据已从 ${selectedPath} 加载`);
return selectedPath;
}
}
// 如果选择不加载或加载失败,继续创建新项目
}
2025-11-18 14:03:22 +08:00
this.projectPaths.set(projectId, selectedPath);
vscode.window.showInformationMessage(`项目存储位置已设置: ${selectedPath}`);
2025-11-18 21:14:10 +08:00
// 保存初始数据
2025-11-21 16:07:48 +08:00
await this.saveCurrentProjectData();
2025-11-18 14:03:22 +08:00
return selectedPath;
}
}
else {
// 创建新路径
const pathInput = await vscode.window.showInputBox({
prompt: '请输入项目存储路径(绝对路径)',
placeHolder: `/path/to/your/project/${projectName}`,
validateInput: (value) => {
if (!value) {
return '路径不能为空';
}
return null;
}
});
if (pathInput) {
try {
// 尝试创建目录
const dirUri = vscode.Uri.file(pathInput);
await vscode.workspace.fs.createDirectory(dirUri);
this.projectPaths.set(projectId, pathInput);
vscode.window.showInformationMessage(`项目存储位置已创建: ${pathInput}`);
2025-11-18 21:14:10 +08:00
// 保存初始数据
2025-11-21 16:07:48 +08:00
await this.saveCurrentProjectData();
2025-11-18 14:03:22 +08:00
return pathInput;
}
catch (error) {
vscode.window.showErrorMessage(`创建目录失败: ${error}`);
return null;
}
}
}
return null;
}
catch (error) {
vscode.window.showErrorMessage(`选择存储路径时出错: ${error}`);
return null;
}
}
2025-11-18 18:45:30 +08:00
// 更新项目名
2025-11-18 21:14:10 +08:00
async updateProjectName(projectId, newName) {
2025-11-18 09:10:47 +08:00
const project = this.projects.find(p => p.id === projectId);
if (project) {
project.name = newName;
vscode.window.showInformationMessage(`项目名称更新: ${newName}`);
2025-11-21 16:07:48 +08:00
await this.saveCurrentProjectData();
2025-11-18 09:10:47 +08:00
this.updateWebview();
}
}
2025-11-18 18:45:30 +08:00
// 创建新项目
2025-11-18 21:14:10 +08:00
async createProject(name) {
2025-11-18 09:10:47 +08:00
const newId = 'p' + (this.projects.length + 1);
const newProject = {
id: newId,
name: name
};
this.projects.push(newProject);
vscode.window.showInformationMessage(`新建项目: ${name}`);
this.updateWebview();
2025-11-18 09:10:47 +08:00
}
2025-11-18 18:45:30 +08:00
// 删除项目
2025-11-18 21:14:10 +08:00
async deleteProject(projectId) {
2025-11-18 18:45:30 +08:00
const project = this.projects.find(p => p.id === projectId);
if (!project)
return;
2025-11-18 09:10:47 +08:00
this.projects = this.projects.filter(p => p.id !== projectId);
// 删除相关的飞行器
const relatedAircrafts = this.aircrafts.filter(a => a.projectId === projectId);
const aircraftIds = relatedAircrafts.map(a => a.id);
this.aircrafts = this.aircrafts.filter(a => a.projectId !== projectId);
// 删除相关的容器
this.containers = this.containers.filter(c => !aircraftIds.includes(c.aircraftId));
// 删除相关的配置
const containerIds = this.containers.filter(c => aircraftIds.includes(c.aircraftId)).map(c => c.id);
this.configs = this.configs.filter(cfg => !containerIds.includes(cfg.containerId));
2025-11-27 22:04:32 +08:00
// 删除相关的模块文件夹
this.moduleFolders = this.moduleFolders.filter(folder => !containerIds.includes(folder.containerId));
2025-11-18 14:03:22 +08:00
// 删除项目路径映射
this.projectPaths.delete(projectId);
2025-11-18 18:45:30 +08:00
vscode.window.showInformationMessage(`删除项目: ${project.name}`);
2025-11-21 16:07:48 +08:00
await this.saveCurrentProjectData();
2025-11-18 09:10:47 +08:00
this.updateWebview();
}
2025-11-18 18:45:30 +08:00
// 更新飞行器名
2025-11-18 21:14:10 +08:00
async updateAircraftName(aircraftId, newName) {
2025-11-18 18:45:30 +08:00
const aircraft = this.aircrafts.find(a => a.id === aircraftId);
if (aircraft) {
aircraft.name = newName;
vscode.window.showInformationMessage(`飞行器名称更新: ${newName}`);
2025-11-21 16:07:48 +08:00
await this.saveCurrentProjectData();
2025-11-18 18:45:30 +08:00
this.updateWebview();
}
}
// 创建新飞行器
2025-11-18 21:14:10 +08:00
async createAircraft(name) {
2025-11-18 18:45:30 +08:00
if (!this.currentProjectId) {
vscode.window.showErrorMessage('无法创建飞行器:未找到当前项目');
return;
}
const newId = 'a' + (this.aircrafts.length + 1);
const newAircraft = {
id: newId,
name: name,
projectId: this.currentProjectId
};
this.aircrafts.push(newAircraft);
// 创建飞行器目录
2025-11-25 09:24:31 +08:00
await this.createAircraftDirectory(newAircraft);
2025-11-18 18:45:30 +08:00
vscode.window.showInformationMessage(`新建飞行器: ${name}`);
2025-11-21 16:07:48 +08:00
await this.saveCurrentProjectData();
2025-11-18 18:45:30 +08:00
this.updateWebview();
}
// 删除飞行器
2025-11-18 21:14:10 +08:00
async deleteAircraft(aircraftId) {
2025-11-18 18:45:30 +08:00
const aircraft = this.aircrafts.find(a => a.id === aircraftId);
if (!aircraft)
return;
this.aircrafts = this.aircrafts.filter(a => a.id !== aircraftId);
// 删除相关的容器
this.containers = this.containers.filter(c => c.aircraftId !== aircraftId);
// 删除相关的配置
const containerIds = this.containers.filter(c => c.aircraftId === aircraftId).map(c => c.id);
this.configs = this.configs.filter(cfg => !containerIds.includes(cfg.containerId));
2025-11-27 22:04:32 +08:00
// 删除相关的模块文件夹
this.moduleFolders = this.moduleFolders.filter(folder => !containerIds.includes(folder.containerId));
2025-11-18 18:45:30 +08:00
vscode.window.showInformationMessage(`删除飞行器: ${aircraft.name}`);
2025-11-21 16:07:48 +08:00
await this.saveCurrentProjectData();
2025-11-18 18:45:30 +08:00
this.updateWebview();
}
// 更新容器名
2025-11-18 21:14:10 +08:00
async updateContainerName(containerId, newName) {
2025-11-18 09:10:47 +08:00
const container = this.containers.find(c => c.id === containerId);
if (container) {
container.name = newName;
vscode.window.showInformationMessage(`容器名称更新: ${newName}`);
2025-11-21 16:07:48 +08:00
await this.saveCurrentProjectData();
2025-11-18 09:10:47 +08:00
this.updateWebview();
}
}
2025-11-21 16:07:48 +08:00
// 创建新容器
2025-11-18 21:14:10 +08:00
async createContainer(name) {
2025-11-18 09:10:47 +08:00
if (!this.currentAircraftId) {
vscode.window.showErrorMessage('无法创建容器:未找到当前飞行器');
return;
}
const newId = 'c' + (this.containers.length + 1);
const newContainer = {
id: newId,
name: name,
aircraftId: this.currentAircraftId
};
this.containers.push(newContainer);
// 创建容器目录
2025-11-25 09:24:31 +08:00
await this.createContainerDirectory(newContainer);
2025-11-18 09:10:47 +08:00
// 创建两个默认配置文件
const configCount = this.configs.length;
2025-11-18 18:45:30 +08:00
// 第一个配置文件
2025-11-18 09:10:47 +08:00
this.configs.push({
id: 'cfg' + (configCount + 1),
2025-11-18 18:45:30 +08:00
name: '配置1',
2025-11-18 21:14:10 +08:00
fileName: 'dockerfile',
2025-11-18 09:10:47 +08:00
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
});
2025-11-18 18:45:30 +08:00
// 第二个配置文件
2025-11-18 09:10:47 +08:00
this.configs.push({
id: 'cfg' + (configCount + 2),
2025-11-18 18:45:30 +08:00
name: '配置2',
2025-11-18 21:14:10 +08:00
fileName: 'docker-compose.yml',
2025-11-18 09:10:47 +08:00
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个默认配置文件)`);
2025-11-21 16:07:48 +08:00
await this.saveCurrentProjectData();
2025-11-18 09:10:47 +08:00
this.updateWebview();
}
2025-11-18 18:45:30 +08:00
// 删除容器
2025-11-18 21:14:10 +08:00
async deleteContainer(containerId) {
2025-11-18 18:45:30 +08:00
const container = this.containers.find(c => c.id === containerId);
if (!container)
return;
2025-11-18 09:10:47 +08:00
// 删除容器
this.containers = this.containers.filter(c => c.id !== containerId);
// 删除相关的配置
this.configs = this.configs.filter(cfg => cfg.containerId !== containerId);
2025-11-27 22:04:32 +08:00
// 删除相关的模块文件夹
this.moduleFolders = this.moduleFolders.filter(folder => folder.containerId !== containerId);
2025-11-18 18:45:30 +08:00
vscode.window.showInformationMessage(`删除容器: ${container.name}`);
2025-11-21 16:07:48 +08:00
await this.saveCurrentProjectData();
2025-11-18 09:10:47 +08:00
this.updateWebview();
}
2025-11-18 18:45:30 +08:00
// 更新配置名
2025-11-18 21:14:10 +08:00
async updateConfigName(configId, newName) {
2025-11-18 09:10:47 +08:00
const config = this.configs.find(c => c.id === configId);
if (config) {
config.name = newName;
vscode.window.showInformationMessage(`配置名称更新: ${newName}`);
2025-11-21 16:07:48 +08:00
await this.saveCurrentProjectData();
2025-11-18 09:10:47 +08:00
this.updateWebview();
}
}
2025-11-18 18:45:30 +08:00
// 创建新配置文件
2025-11-18 21:14:10 +08:00
async createConfig(name) {
2025-11-18 09:10:47 +08:00
const newId = 'cfg' + (this.configs.length + 1);
const newConfig = {
id: newId,
name: name,
2025-11-21 16:07:48 +08:00
fileName: name.toLowerCase().replace(/\s+/g, '_'),
2025-11-18 09:10:47 +08:00
content: `# ${name} 配置文件\n# 创建时间: ${new Date().toLocaleString()}\n# 您可以在此编辑配置内容\n\n`,
containerId: this.currentContainerId
};
this.configs.push(newConfig);
// 确保容器目录存在
2025-11-25 09:24:31 +08:00
await this.ensureContainerDirectoryExists(this.currentContainerId);
2025-11-18 09:10:47 +08:00
vscode.window.showInformationMessage(`新建配置: ${name}`);
2025-11-21 16:07:48 +08:00
await this.saveCurrentProjectData();
2025-11-18 09:10:47 +08:00
this.updateWebview();
}
// 删除配置文件 - 修复版本,同时删除磁盘文件
2025-11-18 21:14:10 +08:00
async deleteConfig(configId) {
2025-11-18 09:10:47 +08:00
const config = this.configs.find(c => c.id === configId);
if (!config)
return;
const confirm = await vscode.window.showWarningMessage(`确定要删除配置文件 "${config.name}" 吗?这将同时删除磁盘上的文件。`, { modal: true }, '确定删除', '取消');
if (confirm !== '确定删除') {
return;
}
try {
// 从内存中删除配置
2025-11-18 09:10:47 +08:00
this.configs = this.configs.filter(c => c.id !== configId);
// 删除磁盘上的配置文件
const container = this.containers.find(c => c.id === config.containerId);
if (container) {
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
if (aircraft) {
const projectPath = this.projectPaths.get(aircraft.projectId);
if (projectPath) {
const filePath = path.join(projectPath, aircraft.name, container.name, config.fileName);
// 检查文件是否存在,如果存在则删除
if (fs.existsSync(filePath)) {
await fs.promises.unlink(filePath);
console.log(`✅ 已删除配置文件: ${filePath}`);
}
}
}
}
2025-11-18 09:10:47 +08:00
vscode.window.showInformationMessage(`删除配置: ${config.name}`);
2025-11-21 16:07:48 +08:00
await this.saveCurrentProjectData();
2025-11-18 09:10:47 +08:00
this.updateWebview();
}
catch (error) {
vscode.window.showErrorMessage(`删除配置文件失败: ${error}`);
}
2025-11-18 09:10:47 +08:00
}
// === Git 分支管理 ===
async fetchBranches(url) {
try {
console.log('🌿 开始获取分支列表:', url);
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: '正在获取分支信息',
cancellable: false
}, async (progress) => {
progress.report({ increment: 0, message: '连接远程仓库...' });
try {
// 使用 isomorphic-git 的 listServerRefs
progress.report({ increment: 30, message: '获取远程引用...' });
console.log('🔍 使用 listServerRefs 获取分支信息...');
const refs = await isomorphic_git_1.default.listServerRefs({
http: node_1.default,
url: url
});
console.log('📋 获取到的引用:', refs);
// 过滤出分支引用 (refs/heads/ 和 refs/remotes/origin/)
const branchRefs = refs.filter(ref => ref.ref.startsWith('refs/heads/') || ref.ref.startsWith('refs/remotes/origin/'));
console.log('🌿 过滤后的分支引用:', branchRefs);
2025-11-25 16:32:06 +08:00
// 构建分支数据 - 修复分支名称显示
const branches = branchRefs.map(ref => {
2025-11-25 16:32:06 +08:00
let branchName;
2025-11-27 18:34:57 +08:00
if (ref.ref.startsWith('refs/remotes/')) {
2025-11-25 16:32:06 +08:00
// 远程分支:移除 refs/remotes/origin/ 前缀
branchName = ref.ref.replace('refs/remotes/origin/', '');
}
else {
// 本地分支:移除 refs/heads/ 前缀
branchName = ref.ref.replace('refs/heads/', '');
}
return {
name: branchName,
isCurrent: branchName === 'main' || branchName === 'master',
2025-11-25 16:32:06 +08:00
selected: false // 所有分支默认不选中
};
});
console.log('🎯 最终分支列表:', branches);
if (branches.length === 0) {
throw new Error('未找到任何分支');
}
progress.report({ increment: 80, message: '处理分支数据...' });
2025-11-27 18:34:57 +08:00
// === 新增:构建分支树状结构 ===
const branchTree = this.buildBranchTree(branches);
console.log('🌳 构建的分支树结构:', branchTree);
// 发送分支数据到前端 - 同时包含扁平列表和树状结构
if (!this.isWebviewDisposed) {
this.panel.webview.postMessage({
type: 'branchesFetched',
branches: branches,
2025-11-27 18:34:57 +08:00
branchTree: branchTree,
repoUrl: url
});
}
progress.report({ increment: 100, message: '完成' });
}
catch (error) {
console.error('❌ 使用 listServerRefs 获取分支失败:', error);
2025-11-26 09:29:09 +08:00
// 只在右下角显示分支获取失败的通知,不模拟分支数据
vscode.window.showErrorMessage(`获取分支失败: ${error}`);
}
});
}
catch (error) {
console.error('❌ 获取分支失败:', error);
vscode.window.showErrorMessage(`获取分支失败: ${error}`);
}
}
2025-11-27 18:34:57 +08:00
/**
* 构建分支树状结构
*/
buildBranchTree(branches) {
const root = [];
branches.forEach(branch => {
const parts = branch.name.split('/');
let currentLevel = root;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
const isLeaf = i === parts.length - 1;
const fullName = parts.slice(0, i + 1).join('/');
let node = currentLevel.find((n) => n.name === part);
if (!node) {
node = {
name: part,
fullName: fullName,
isLeaf: isLeaf,
children: [],
level: i,
expanded: true // 默认展开
};
currentLevel.push(node);
}
if (isLeaf) {
// 叶子节点存储分支信息
node.branch = branch;
}
currentLevel = node.children;
}
});
return root;
}
2025-11-28 14:16:19 +08:00
generateModuleFolderName(url, branch) {
const repoName = url.split('/').pop()?.replace('.git', '') || 'unknown-repo';
const branchSafeName = branch.replace(/[^a-zA-Z0-9-_]/g, '-');
return {
displayName: repoName,
folderName: branchSafeName // 实际文件夹名称(只显示分支名)
};
}
async cloneBranches(url, branches) {
try {
console.log('🚀 开始克隆分支:', { url, branches });
2025-11-28 14:16:19 +08:00
let successCount = 0;
let failCount = 0;
// 显示总进度
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: `正在克隆 ${branches.length} 个分支`,
cancellable: false
}, async (progress) => {
for (let i = 0; i < branches.length; i++) {
const branch = branches[i];
const progressPercent = (i / branches.length) * 100;
progress.report({
increment: progressPercent,
message: `克隆分支: ${branch} (${i + 1}/${branches.length})`
});
2025-11-28 14:16:19 +08:00
console.log(`📥 开始克隆分支: ${branch}`);
try {
const folderNames = this.generateModuleFolderName(url, branch);
await this.addGitModuleFolder(url, folderNames.displayName, folderNames.folderName, branch);
successCount++;
console.log(`✅ 分支克隆成功: ${branch}`);
}
catch (error) {
failCount++;
console.error(`❌ 分支克隆失败: ${branch}`, error);
// 继续克隆其他分支,不中断整个流程
}
}
});
2025-11-28 14:16:19 +08:00
// 显示最终结果
if (failCount === 0) {
vscode.window.showInformationMessage(`成功克隆 ${successCount} 个分支`);
}
else {
vscode.window.showWarningMessage(`克隆完成: ${successCount} 个成功, ${failCount} 个失败`);
}
}
catch (error) {
console.error('❌ 克隆分支失败:', error);
vscode.window.showErrorMessage(`克隆分支失败: ${error}`);
}
}
2025-11-18 14:03:22 +08:00
// 更新视图
2025-11-18 09:10:47 +08:00
updateWebview() {
// 检查 Webview 是否仍然有效
if (this.isWebviewDisposed) {
console.log('⚠️ Webview 已被销毁,跳过更新');
return;
}
try {
this.panel.webview.html = this.getWebviewContent();
}
catch (error) {
console.error('更新 Webview 失败:', error);
}
2025-11-18 09:10:47 +08:00
}
getWebviewContent() {
switch (this.currentView) {
2025-11-18 18:45:30 +08:00
case 'projects':
return this.projectView.render({
2025-11-18 14:03:22 +08:00
projects: this.projects,
projectPaths: this.projectPaths
});
2025-11-18 09:10:47 +08:00
case 'aircrafts':
2025-11-18 18:45:30 +08:00
const projectAircrafts = this.aircrafts.filter(a => a.projectId === this.currentProjectId);
return this.aircraftView.render({
aircrafts: projectAircrafts
});
case 'containers':
2025-11-18 09:10:47 +08:00
const currentProject = this.projects.find(p => p.id === this.currentProjectId);
2025-11-21 16:07:48 +08:00
const currentAircraft = this.aircrafts.find(a => a.id === this.currentAircraftId);
2025-11-18 09:10:47 +08:00
const projectContainers = this.containers.filter(c => c.aircraftId === this.currentAircraftId);
2025-11-18 18:45:30 +08:00
return this.containerView.render({
2025-11-18 09:10:47 +08:00
project: currentProject,
2025-11-21 16:07:48 +08:00
aircraft: currentAircraft,
2025-11-18 09:10:47 +08:00
containers: projectContainers
});
2025-11-18 18:45:30 +08:00
case 'configs':
2025-11-18 09:10:47 +08:00
const currentContainer = this.containers.find(c => c.id === this.currentContainerId);
const containerConfigs = this.configs.filter(cfg => cfg.containerId === this.currentContainerId);
2025-11-27 22:04:32 +08:00
const currentModuleFolder = this.moduleFolders.find(f => f.id === this.currentModuleFolderId);
// 获取当前容器的模块文件夹
const containerModuleFolders = this.moduleFolders.filter(folder => folder.containerId === this.currentContainerId);
2025-11-18 18:45:30 +08:00
return this.configView.render({
2025-11-18 09:10:47 +08:00
container: currentContainer,
configs: containerConfigs,
2025-11-27 22:04:32 +08:00
moduleFolders: containerModuleFolders,
currentModuleFolder: currentModuleFolder,
moduleFolderFileTree: this.currentModuleFolderFileTree,
moduleFolderLoading: false
2025-11-18 09:10:47 +08:00
});
default:
2025-11-18 18:45:30 +08:00
return this.projectView.render({
2025-11-18 14:03:22 +08:00
projects: this.projects,
projectPaths: this.projectPaths
2025-11-18 09:10:47 +08:00
});
}
}
2025-11-27 22:04:32 +08:00
async openGitRepoInVSCode(folderId) {
const folder = this.moduleFolders.find(f => f.id === folderId);
if (!folder) {
vscode.window.showErrorMessage('未找到指定的模块文件夹');
2025-11-25 16:32:06 +08:00
return;
}
try {
2025-11-27 22:04:32 +08:00
// 检查文件夹是否存在
const fullPath = this.getModuleFolderFullPath(folder);
if (!fullPath || !fs.existsSync(fullPath)) {
vscode.window.showErrorMessage('模块文件夹目录不存在');
2025-11-25 16:32:06 +08:00
return;
}
// 使用 VSCode 的文件选择器让用户选择要打开的文件
const fileUri = await vscode.window.showOpenDialog({
2025-11-27 22:04:32 +08:00
defaultUri: vscode.Uri.file(fullPath),
2025-11-25 16:32:06 +08:00
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: false,
openLabel: '选择要打开的文件',
2025-11-27 22:04:32 +08:00
title: `${folder.name} 中选择文件`
2025-11-25 16:32:06 +08:00
});
if (fileUri && fileUri.length > 0) {
// 打开选中的文件
const document = await vscode.workspace.openTextDocument(fileUri[0]);
await vscode.window.showTextDocument(document);
vscode.window.showInformationMessage(`已打开文件: ${path.basename(fileUri[0].fsPath)}`);
}
}
catch (error) {
2025-11-27 22:04:32 +08:00
vscode.window.showErrorMessage(`打开模块文件夹文件失败: ${error}`);
2025-11-25 16:32:06 +08:00
}
}
2025-11-25 21:13:41 +08:00
async openConfigFileInVSCode(configId) {
const config = this.configs.find(c => c.id === configId);
if (!config) {
vscode.window.showErrorMessage('未找到配置文件');
return;
}
const container = this.containers.find(c => c.id === config.containerId);
if (!container) {
vscode.window.showErrorMessage('未找到容器');
return;
}
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
if (!aircraft) {
vscode.window.showErrorMessage('未找到飞行器');
return;
}
const projectPath = this.projectPaths.get(aircraft.projectId);
if (!projectPath) {
vscode.window.showErrorMessage('未设置项目存储路径');
return;
}
// 构建文件路径
const filePath = path.join(projectPath, aircraft.name, container.name, config.fileName);
try {
// 检查文件是否存在
if (!fs.existsSync(filePath)) {
vscode.window.showWarningMessage('配置文件不存在,将创建新文件');
// 确保目录存在
const dirPath = path.dirname(filePath);
await fs.promises.mkdir(dirPath, { recursive: true });
// 创建文件
await fs.promises.writeFile(filePath, config.content || '');
}
// 在 VSCode 中打开文件
const document = await vscode.workspace.openTextDocument(filePath);
await vscode.window.showTextDocument(document);
}
catch (error) {
vscode.window.showErrorMessage(`打开配置文件失败: ${error}`);
}
}
async mergeConfigs(configIds, displayName, folderName) {
try {
if (!this.currentContainerId) {
vscode.window.showErrorMessage('未找到当前容器');
return;
}
if (configIds.length < 2) {
vscode.window.showErrorMessage('请至少选择两个配置文件进行合并');
return;
}
const container = this.containers.find(c => c.id === this.currentContainerId);
if (!container) {
vscode.window.showErrorMessage('未找到容器');
return;
}
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
if (!aircraft) {
vscode.window.showErrorMessage('未找到飞行器');
return;
}
const projectPath = this.projectPaths.get(aircraft.projectId);
if (!projectPath) {
vscode.window.showErrorMessage('未设置项目存储路径');
return;
}
// 获取选中的配置文件
const selectedConfigs = this.configs.filter(config => configIds.includes(config.id));
if (selectedConfigs.length !== configIds.length) {
vscode.window.showErrorMessage('部分配置文件未找到');
return;
}
// 创建合并文件夹
const mergeFolderPath = path.join(projectPath, aircraft.name, container.name, folderName);
await fs.promises.mkdir(mergeFolderPath, { recursive: true });
// 将选中的配置文件复制到合并文件夹中
const copiedFiles = [];
for (const config of selectedConfigs) {
const sourcePath = path.join(projectPath, aircraft.name, container.name, config.fileName);
const targetPath = path.join(mergeFolderPath, config.fileName);
if (fs.existsSync(sourcePath)) {
await fs.promises.copyFile(sourcePath, targetPath);
copiedFiles.push(config.fileName);
console.log(`✅ 已复制配置文件: ${config.fileName}`);
}
else {
// 如果源文件不存在,创建新文件
await fs.promises.writeFile(targetPath, config.content || '');
copiedFiles.push(config.fileName);
console.log(`✅ 已创建配置文件: ${config.fileName}`);
}
}
2025-11-27 22:04:32 +08:00
// 构建相对路径
const relativePath = `/${aircraft.projectId}/${aircraft.name}/${container.name}/${folderName}`;
// 创建合并文件夹记录
const newFolder = {
2025-11-28 14:16:19 +08:00
id: 'local-' + Date.now(),
2025-11-27 22:04:32 +08:00
name: displayName,
2025-11-28 14:16:19 +08:00
type: 'local',
2025-11-27 22:04:32 +08:00
localPath: relativePath,
containerId: this.currentContainerId
};
2025-11-27 22:04:32 +08:00
// 添加到模块文件夹列表
this.moduleFolders.push(newFolder);
// 删除原始配置文件
for (const configId of configIds) {
await this.deleteConfigInternal(configId);
}
// 保存数据
await this.saveCurrentProjectData();
vscode.window.showInformationMessage(`成功合并 ${selectedConfigs.length} 个配置文件到文件夹: ${folderName}`);
2025-11-27 22:04:32 +08:00
// 更新UI
this.updateWebview();
}
catch (error) {
console.error('❌ 合并配置文件失败:', error);
vscode.window.showErrorMessage(`合并配置文件失败: ${error}`);
}
}
/**
* 内部删除配置文件方法不显示确认对话框
*/
async deleteConfigInternal(configId) {
try {
const config = this.configs.find(c => c.id === configId);
if (!config)
return;
// 从内存中删除配置
this.configs = this.configs.filter(c => c.id !== configId);
// 删除磁盘上的配置文件
const container = this.containers.find(c => c.id === config.containerId);
if (container) {
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
if (aircraft) {
const projectPath = this.projectPaths.get(aircraft.projectId);
if (projectPath) {
const filePath = path.join(projectPath, aircraft.name, container.name, config.fileName);
// 检查文件是否存在,如果存在则删除
if (fs.existsSync(filePath)) {
await fs.promises.unlink(filePath);
console.log(`✅ 已删除配置文件: ${filePath}`);
}
}
}
}
console.log(`✅ 内部删除配置: ${config.name}`);
}
catch (error) {
console.error(`删除配置文件失败: ${error}`);
}
}
/**
* VSCode 中打开合并文件夹
*/
async openMergedFolderInVSCode(folderId) {
2025-11-27 22:04:32 +08:00
const folder = this.moduleFolders.find(f => f.id === folderId);
if (!folder) {
2025-11-27 22:04:32 +08:00
vscode.window.showErrorMessage('未找到指定的模块文件夹');
return;
}
try {
// 检查文件夹是否存在
2025-11-27 22:04:32 +08:00
const fullPath = this.getModuleFolderFullPath(folder);
if (!fullPath || !fs.existsSync(fullPath)) {
vscode.window.showErrorMessage('模块文件夹不存在');
return;
}
// 使用 VSCode 打开文件夹
2025-11-27 22:04:32 +08:00
const folderUri = vscode.Uri.file(fullPath);
vscode.commands.executeCommand('vscode.openFolder', folderUri, { forceNewWindow: false });
}
catch (error) {
2025-11-27 22:04:32 +08:00
vscode.window.showErrorMessage(`打开模块文件夹失败: ${error}`);
}
}
/**
* 统一方法 VSCode 中打开模块文件夹中的文件
* @param type 模块类型'git' 'merged'
* @param id 模块 ID
*/
async openTheModuleFolder(type, id) {
2025-11-27 22:04:32 +08:00
const folder = this.moduleFolders.find(f => f.id === id);
if (!folder) {
vscode.window.showErrorMessage('未找到指定的模块文件夹');
return;
}
try {
// 检查文件夹是否存在
2025-11-27 22:04:32 +08:00
const fullPath = this.getModuleFolderFullPath(folder);
if (!fullPath || !fs.existsSync(fullPath)) {
vscode.window.showErrorMessage('模块文件夹目录不存在');
return;
}
// 使用 VSCode 的文件选择器让用户选择要打开的文件
const fileUri = await vscode.window.showOpenDialog({
2025-11-27 22:04:32 +08:00
defaultUri: vscode.Uri.file(fullPath),
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: false,
openLabel: '选择要打开的文件',
2025-11-27 22:04:32 +08:00
title: `${folder.name} 中选择文件`
});
if (fileUri && fileUri.length > 0) {
// 打开选中的文件
const document = await vscode.workspace.openTextDocument(fileUri[0]);
await vscode.window.showTextDocument(document);
vscode.window.showInformationMessage(`已打开文件: ${path.basename(fileUri[0].fsPath)}`);
}
}
catch (error) {
2025-11-27 22:04:32 +08:00
vscode.window.showErrorMessage(`打开模块文件夹文件失败: ${error}`);
}
}
2025-11-27 22:04:32 +08:00
/**
* 获取模块文件夹的完整路径
*/
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);
}
async uploadGitModuleFolder(folderId, username, password) {
const folder = this.moduleFolders.find(f => f.id === folderId);
if (!folder || folder.type !== 'git') {
vscode.window.showErrorMessage('未找到指定的 Git 模块文件夹');
return;
}
// 获取完整路径
const fullPath = this.getModuleFolderFullPath(folder);
if (!fullPath) {
vscode.window.showErrorMessage('无法获取模块文件夹的完整路径');
return;
}
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: `正在上传 Git 仓库: ${folder.name}`,
cancellable: false
}, async (progress) => {
try {
progress.report({ increment: 0, message: '检查更改...' });
// 使用命令行 Git 提交并推送
await this.commitAndPushUsingCommandLine(fullPath);
progress.report({ increment: 100, message: '完成' });
vscode.window.showInformationMessage(`✅ Git 仓库上传成功: ${folder.name}`);
}
catch (error) {
console.error('❌ Git 上传失败:', error);
vscode.window.showErrorMessage(`Git 仓库上传失败: ${error.message}`);
}
});
}
/**
* 上传本地模块文件夹到新的 Git 仓库使用命令行 Git
*/
async uploadLocalModuleFolder(folderId, repoUrl, branchName) {
const folder = this.moduleFolders.find(f => f.id === folderId);
if (!folder || folder.type !== 'local') {
vscode.window.showErrorMessage('未找到指定的本地模块文件夹');
return;
}
// 获取完整路径
const fullPath = this.getModuleFolderFullPath(folder);
if (!fullPath) {
vscode.window.showErrorMessage('无法获取模块文件夹的完整路径');
return;
}
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: `正在上传本地文件夹到 Git 仓库: ${folder.name}`,
cancellable: false
}, async (progress) => {
try {
progress.report({ increment: 0, message: '检查目录...' });
// 检查目录是否存在
if (!fs.existsSync(fullPath)) {
throw new Error('本地文件夹不存在');
}
progress.report({ increment: 10, message: '初始化 Git 仓库...' });
// 使用命令行 Git 初始化仓库
await this.initGitRepository(fullPath, branchName);
progress.report({ increment: 20, message: '添加远程仓库...' });
// 使用命令行 Git 添加远程仓库
await this.addGitRemote(fullPath, repoUrl);
progress.report({ increment: 40, message: '提交初始文件...' });
// 使用命令行 Git 提交初始文件
await this.commitInitialFiles(fullPath);
progress.report({ increment: 60, message: '推送到远程仓库...' });
// 使用命令行 Git 推送到远程仓库
await this.pushToRemoteWithForce(fullPath, branchName);
progress.report({ increment: 100, message: '完成' });
// 更新文件夹类型为 git
folder.type = 'git';
await this.saveCurrentProjectData();
vscode.window.showInformationMessage(`本地文件夹成功上传到 Git 仓库: ${folder.name} -> ${branchName}`);
}
catch (error) {
console.error('❌ 本地文件夹上传失败:', error);
vscode.window.showErrorMessage(`本地文件夹上传失败: ${error}`);
// 清理:删除可能创建的 .git 文件夹
try {
const gitDir = path.join(fullPath, '.git');
if (fs.existsSync(gitDir)) {
await fs.promises.rm(gitDir, { recursive: true, force: true });
}
}
catch (cleanupError) {
console.error('清理 .git 文件夹失败:', cleanupError);
}
}
});
}
/**
* 使用命令行 Git 初始化仓库
*/
async initGitRepository(fullPath, branchName) {
return new Promise((resolve, reject) => {
const { exec } = require('child_process');
console.log('📁 初始化 Git 仓库...');
exec(`git init && git checkout -b ${branchName}`, {
cwd: fullPath,
encoding: 'utf8'
}, (error, stdout, stderr) => {
if (error) {
console.error('❌ Git 初始化失败:', error);
reject(new Error(`Git 初始化失败: ${stderr || error.message}`));
return;
}
console.log('✅ Git 初始化成功');
resolve();
});
});
}
/**
* 使用命令行 Git 添加远程仓库
*/
async addGitRemote(fullPath, repoUrl) {
return new Promise((resolve, reject) => {
const { exec } = require('child_process');
console.log('📡 添加远程仓库...');
exec(`git remote add origin ${repoUrl}`, {
cwd: fullPath,
encoding: 'utf8'
}, (error, stdout, stderr) => {
if (error) {
console.error('❌ 添加远程仓库失败:', error);
reject(new Error(`添加远程仓库失败: ${stderr || error.message}`));
return;
}
console.log('✅ 远程仓库添加成功');
resolve();
});
});
}
/**
* 使用命令行 Git 提交初始文件
*/
async commitInitialFiles(fullPath) {
return new Promise((resolve, reject) => {
const { exec } = require('child_process');
console.log('💾 提交初始文件...');
const commands = [
'git add .',
`git commit -m "Initial commit from DCSP - ${new Date().toLocaleString()}"`
];
exec(commands.join(' && '), {
cwd: fullPath,
encoding: 'utf8'
}, (error, stdout, stderr) => {
if (error) {
// 检查是否是"没有更改可提交"的错误
if (stderr.includes('nothing to commit') || stdout.includes('nothing to commit')) {
console.log(' 没有需要提交的更改');
resolve();
return;
}
console.error('❌ Git 提交失败:', error);
reject(new Error(`Git 提交失败: ${stderr || error.message}`));
return;
}
console.log('✅ 初始文件提交成功');
resolve();
});
});
}
/**
* 使用命令行 Git 强制推送到远程仓库
*/
async pushToRemoteWithForce(fullPath, branchName) {
return new Promise((resolve, reject) => {
const { exec } = require('child_process');
console.log('🚀 强制推送到远程仓库...');
exec(`git push -u origin ${branchName} --force`, {
cwd: fullPath,
encoding: 'utf8'
}, (error, stdout, stderr) => {
console.log('📋 Git push stdout:', stdout);
console.log('📋 Git push stderr:', stderr);
if (error) {
console.error('❌ Git 推送失败:', error);
// 提供更友好的错误信息
let userMessage = stderr || error.message;
if (stderr.includes('Authentication failed') || stderr.includes('401')) {
userMessage = '认证失败:请确保 Git 凭据已正确配置,或者使用 Personal Access Token';
}
else if (stderr.includes('Permission denied') || stderr.includes('403')) {
userMessage = '权限被拒绝:请检查您是否有权限推送到该仓库';
}
else if (stderr.includes('could not read Username')) {
userMessage = '认证配置问题:请检查 Git 凭据存储配置';
}
reject(new Error(userMessage));
return;
}
console.log('✅ Git 推送成功');
resolve();
});
});
}
async getCurrentBranch(dir) {
try {
const currentBranch = await isomorphic_git_1.default.currentBranch({
fs: fs,
dir: dir,
fullname: false
});
if (!currentBranch) {
// 如果没有当前分支,尝试从 HEAD 文件读取
const headPath = path.join(dir, '.git', 'HEAD');
if (fs.existsSync(headPath)) {
const headContent = await fs.promises.readFile(headPath, 'utf8');
const match = headContent.match(/ref: refs\/heads\/(.+)/);
if (match) {
return match[1];
}
}
throw new Error('无法确定当前分支');
}
return currentBranch;
}
catch (error) {
console.error('获取当前分支失败:', error);
throw new Error(`获取当前分支失败: ${error}`);
}
}
/**
* 使用命令行 Git 进行推送带详细调试
*/
async pushUsingCommandLine(fullPath) {
return new Promise((resolve, reject) => {
const { exec } = require('child_process');
console.log('🚀 使用命令行 Git 推送...');
console.log(`📁 工作目录: ${fullPath}`);
// 先检查 Git 状态
exec('git status', {
cwd: fullPath,
encoding: 'utf8'
}, (statusError, statusStdout, statusStderr) => {
console.log('🔍 Git 状态:', statusStdout);
// 然后执行推送
exec('git push', {
cwd: fullPath,
encoding: 'utf8'
}, (error, stdout, stderr) => {
console.log('📋 Git push stdout:', stdout);
console.log('📋 Git push stderr:', stderr);
if (error) {
console.error('❌ Git push 失败:', error);
reject(new Error(`Git 推送失败: ${stderr || error.message}`));
return;
}
// 检查实际的推送结果
if (stdout.includes('Everything up-to-date')) {
console.log(' 没有需要推送的更改');
resolve();
}
else if (stdout.includes('To http') || stdout.includes('To https') || stdout.includes('To git@')) {
console.log('✅ Git 推送成功');
resolve();
}
else {
console.log('⚠️ 推送结果不明确,可能需要手动检查');
reject(new Error('推送结果不明确,请检查远程仓库'));
}
});
});
});
}
/**
* 检查 Git 远程配置
*/
async checkGitRemote(fullPath) {
return new Promise((resolve, reject) => {
const { exec } = require('child_process');
console.log('🔍 检查 Git 远程配置...');
exec('git remote -v', {
cwd: fullPath,
encoding: 'utf8'
}, (error, stdout, stderr) => {
if (error) {
console.error('检查远程配置失败:', error);
reject(error);
return;
}
console.log('📡 Git 远程配置:', stdout);
resolve();
});
});
}
/**
* 使用命令行 Git 提交并推送
*/
async commitAndPushUsingCommandLine(fullPath) {
return new Promise((resolve, reject) => {
const { exec } = require('child_process');
console.log('🚀 使用命令行 Git 提交并推送...');
console.log(`📁 工作目录: ${fullPath}`);
// 先添加所有更改并提交,然后推送
const commands = [
'git add .',
`git commit -m "Auto commit from DCSP - ${new Date().toLocaleString()}"`,
'git push'
];
exec(commands.join(' && '), {
cwd: fullPath,
encoding: 'utf8'
}, (error, stdout, stderr) => {
console.log('📋 Git 命令输出:', stdout);
console.log('📋 Git 命令错误:', stderr);
if (error) {
// 检查是否是"没有更改可提交"的错误
if (stderr.includes('nothing to commit') || stdout.includes('nothing to commit')) {
console.log(' 没有需要提交的更改,直接推送');
// 如果没有更改,直接推送
exec('git push', {
cwd: fullPath,
encoding: 'utf8'
}, (pushError, pushStdout, pushStderr) => {
if (pushError) {
console.error('❌ Git push 失败:', pushError);
reject(new Error(`Git 推送失败: ${pushStderr || pushError.message}`));
return;
}
console.log('✅ Git 推送成功');
resolve();
});
return;
}
console.error('❌ Git 提交/推送失败:', error);
reject(new Error(`Git 操作失败: ${stderr || error.message}`));
return;
}
console.log('✅ Git 提交并推送成功');
resolve();
});
});
}
2025-11-18 09:10:47 +08:00
}
exports.ConfigPanel = ConfigPanel;
//# sourceMappingURL=ConfigPanel.js.map