0
0

卫星模块获取功能已实现,仅简单测试,未深度测试

This commit is contained in:
xubing
2025-12-02 16:30:53 +08:00
parent 07acf9f617
commit 3f512e8646
9 changed files with 475 additions and 297 deletions

View File

@@ -1,4 +1,3 @@
// src/panels/ConfigPanel.ts
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
@@ -10,7 +9,6 @@ import { ContainerView } from './views/ContainerView';
import { ConfigView } from './views/ConfigView';
import { ModuleFolder } from './types/CommonTypes';
// =============================================
// 数据模型接口
// =============================================
@@ -93,7 +91,10 @@ export class ConfigPanel {
private moduleFolders: ModuleFolder[] = [];
private currentModuleFolderFileTree: GitFileTree[] = [];
private projectPaths: Map<string, string> = new Map();
private pendingUploadFolderId: string | null = null;
// 上传相关local 上传 / git 上传
private pendingUploadFolderId: string | null = null; // local -> git
private pendingGitUploadFolderId: string | null = null; // git -> repo更新或新分支
// 仓库配置
private repoConfigs: RepoConfigItem[] = [];
@@ -164,7 +165,6 @@ export class ConfigPanel {
private generateUniqueId(prefix: string, existingItems: any[]): string {
let idNumber = 1;
// 先找到当前最大的数字
const existingIds = existingItems.map(item => item.id);
const numberPattern = /\d+$/;
@@ -186,7 +186,6 @@ export class ConfigPanel {
// =============================================
private getRepoConfigPath(): string {
// 按你的需求:配置文件保存在插件安装位置
return path.join(this.extensionUri.fsPath, 'dcsp-repos.json');
}
@@ -206,7 +205,6 @@ export class ConfigPanel {
const parsed = JSON.parse(content);
if (Array.isArray(parsed)) {
// 兼容老格式:直接是数组
this.repoConfigs = parsed;
} else if (parsed && Array.isArray(parsed.repos)) {
this.repoConfigs = parsed.repos;
@@ -253,6 +251,9 @@ export class ConfigPanel {
}
}
/**
* 通用:弹出仓库选择弹窗(不区分“获取分支 / 上传”,使用 pendingXXX 做上下文判断)
*/
private async openRepoSelect(): Promise<void> {
await this.loadRepoConfigs();
@@ -262,62 +263,129 @@ export class ConfigPanel {
if (this.isWebviewDisposed) return;
// 仅传递仓库名给前端,用于下拉选择
this.panel.webview.postMessage({
type: 'showRepoSelect',
repos: this.repoConfigs.map(r => ({ name: r.name }))
});
}
/**
* 仓库选择确认后的统一入口
* - local 上传pendingUploadFolderId 有值
* - git 上传pendingGitUploadFolderId 有值
* - 其余:当作“获取分支”处理
*/
private async handleRepoSelectedForBranches(repoName: string): Promise<void> {
await this.loadRepoConfigs();
const repo = this.repoConfigs.find(r => r.name === repoName);
await this.loadRepoConfigs();
const repo = this.repoConfigs.find(r => r.name === repoName);
if (!repo) {
vscode.window.showErrorMessage(`在仓库配置中未找到名为 "${repoName}" 的仓库,请检查 dcsp-repos.json。`);
return;
}
// ---------------------------------------------------
// 1⃣ 新逻辑Local 上传pendingUploadFolderId 不为空)
// ---------------------------------------------------
if (this.pendingUploadFolderId) {
const localFolderId = this.pendingUploadFolderId;
this.pendingUploadFolderId = null; // 必须清空,避免影响其他操作
console.log("🚀 Local 上传流程repo =", repoName, "folderId =", localFolderId);
const folder = this.moduleFolders.find(f => f.id === localFolderId);
if (!folder) {
vscode.window.showErrorMessage("未找到要上传的本地模块文件夹");
if (!repo) {
vscode.window.showErrorMessage(`在仓库配置中未找到名为 "${repoName}" 的仓库,请检查 dcsp-repos.json。`);
return;
}
// 生成本地路径
const fullPath = this.getModuleFolderFullPath(folder);
if (!fullPath) {
vscode.window.showErrorMessage("无法确定本地模块文件夹路径");
// 1⃣ Local 模块上传local -> git
if (this.pendingUploadFolderId) {
const localFolderId = this.pendingUploadFolderId;
this.pendingUploadFolderId = null;
console.log("🚀 Local 上传流程repo =", repoName, "folderId =", localFolderId);
const folder = this.moduleFolders.find(f => f.id === localFolderId);
if (!folder || folder.type !== 'local') {
vscode.window.showErrorMessage("未找到要上传的本地模块文件夹");
return;
}
const fullPath = this.getModuleFolderFullPath(folder);
if (!fullPath) {
vscode.window.showErrorMessage("无法确定本地模块文件夹路径");
return;
}
const branchName = path.basename(fullPath);
console.log("🌿 Local 上传自动生成分支名:", branchName);
await this.uploadLocalModuleFolder(localFolderId, repo.url, branchName);
return;
}
// 自动从本地文件夹名生成分支名
const branchName = path.basename(fullPath);
console.log("🌿 自动生成分支名:", branchName);
// 2⃣ Git 模块上传git -> repo
if (this.pendingGitUploadFolderId) {
const gitFolderId = this.pendingGitUploadFolderId;
this.pendingGitUploadFolderId = null;
// 正式执行上传
await this.uploadLocalModuleFolder(localFolderId, repo.url, branchName);
console.log("🚀 Git 上传流程repo =", repoName, "folderId =", gitFolderId);
return;
const folder = this.moduleFolders.find(f => f.id === gitFolderId);
if (!folder || folder.type !== 'git') {
vscode.window.showErrorMessage("未找到要上传的 Git 模块文件夹");
return;
}
const newFolderName = folder.localPath.split('/').pop() ?? '';
const oldFolderName = folder.originalFolderName ?? newFolderName;
const isRenamed = newFolderName !== oldFolderName;
const isSameRepo = !!folder.originalRepoUrl && folder.originalRepoUrl === repo.url;
console.log('🔍 Git 上传判断:');
console.log(' oldFolderName:', oldFolderName);
console.log(' newFolderName:', newFolderName);
console.log(' isRenamed :', isRenamed);
console.log(' folderRepo :', folder.originalRepoUrl);
console.log(' selectedRepo :', repo.url);
console.log(' isSameRepo :', isSameRepo);
const fullPath = this.getModuleFolderFullPath(folder);
if (!fullPath) {
vscode.window.showErrorMessage("无法确定 Git 模块文件夹路径");
return;
}
// 2.1 未改名 + 还是原来的仓库 → 直接在原分支上 push更新代码
if (!isRenamed && isSameRepo) {
console.log('✅ 未改名 & 同仓库:执行原始 push 逻辑');
await this.uploadGitModuleFolder(gitFolderId);
return;
}
// 2.2 改了名字 或 换了仓库 → 使用“当前文件夹名”作为分支,推送到选中仓库
console.log('🆕 名称改变或仓库改变:以文件夹名作为新分支上传');
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: `正在上传 Git 仓库: ${folder.name}`,
cancellable: false
}, async (progress) => {
try {
progress.report({ increment: 0, message: '准备上传...' });
await this.pushGitModuleFolderToRepoWithBranch(fullPath, repo.url, newFolderName);
folder.uploaded = true;
folder.originalFolderName = newFolderName;
folder.originalRepoUrl = repo.url;
await this.saveCurrentProjectData();
progress.report({ increment: 100, message: '完成' });
vscode.window.showInformationMessage(`✅ Git 仓库已上传到 ${repo.name} 的分支 ${newFolderName}`);
this.updateWebview();
} catch (error: any) {
console.error('❌ Git 上传到新仓库/分支失败:', error);
vscode.window.showErrorMessage(`推送失败: ${error.message || error}`);
}
});
return;
}
// 3⃣ 默认:获取分支(用于“获取仓库”-> 克隆)
this.currentRepoForBranches = repo;
await this.fetchBranchesForRepo(repo);
}
// ---------------------------------------------------
// 2⃣ 旧逻辑:获取分支
// ---------------------------------------------------
this.currentRepoForBranches = repo;
await this.fetchBranchesForRepo(repo);
}
// =============================================
// Webview 消息处理
// =============================================
@@ -397,7 +465,9 @@ export class ConfigPanel {
// 上传功能
'uploadGitModuleFolder': (data) => this.uploadGitModuleFolder(data.folderId, data.username, data.password),
'openRepoSelectForUpload': (data) => this.handleOpenRepoSelectForUpload(data.folderId),
'uploadLocalModuleFolder': (data) => this.uploadLocalModuleFolder(data.folderId, data.repoUrl, data.branchName)
'uploadLocalModuleFolder': (data) => this.uploadLocalModuleFolder(data.folderId, data.repoUrl, data.branchName),
'openRepoSelectForGitUpload': (data) => this.openRepoSelectForGitUpload(data.folderId),
'repoSelectedForGitUpload': (data) => this.handleRepoSelectedForGitUpload(data.folderId, data.repoName)
};
const handler = messageHandlers[data.type];
@@ -649,13 +719,10 @@ export class ConfigPanel {
}
private async handleOpenRepoSelectForUpload(folderId: string): Promise<void> {
console.log("📌 Local 上传:收到 openRepoSelectForUploadfolderId =", folderId);
this.pendingUploadFolderId = folderId;
// 复用你现有的仓库选择弹窗
await this.openRepoSelect();
}
console.log("📌 Local 上传:收到 openRepoSelectForUploadfolderId =", folderId);
this.pendingUploadFolderId = folderId;
await this.openRepoSelect();
}
private async deleteConfig(configId: string): Promise<void> {
const config = this.configs.find(c => c.id === configId);
@@ -939,7 +1006,9 @@ export class ConfigPanel {
name: displayName,
type: 'git',
localPath: relativePath,
containerId: this.currentContainerId
containerId: this.currentContainerId,
originalFolderName: folderName,
originalRepoUrl: url
};
await vscode.window.withProgress({
@@ -1190,6 +1259,10 @@ export class ConfigPanel {
// 上传功能方法
// =============================================
/**
* 旧的 Git 模块上传逻辑:直接在当前远程 / 当前分支上 commit + push
* 现在只在“未改名 + 同仓库”的场景下被调用
*/
private async uploadGitModuleFolder(folderId: string, username?: string, password?: string): Promise<void> {
const folder = this.moduleFolders.find(f => f.id === folderId);
if (!folder || folder.type !== 'git') {
@@ -1269,6 +1342,9 @@ export class ConfigPanel {
folder.type = 'git';
folder.uploaded = true;
folder.originalFolderName = branchName;
folder.originalRepoUrl = repoUrl;
await this.saveCurrentProjectData();
vscode.window.showInformationMessage(`本地文件夹成功上传到 Git 仓库: ${folder.name} -> ${branchName}`);
@@ -1290,6 +1366,70 @@ export class ConfigPanel {
});
}
/**
* Git 模块以“当前文件夹名”为分支,推送到任意仓库 URL
* 不修改现有 remote 配置,直接用 URL 形式 push
*/
private async pushGitModuleFolderToRepoWithBranch(fullPath: string, repoUrl: string, branchName: string): Promise<void> {
return new Promise((resolve, reject) => {
const { exec } = require('child_process');
console.log('🚀 准备以新分支推送到仓库:', { fullPath, repoUrl, branchName });
// 先检查是否有未提交修改
exec('git status --porcelain', {
cwd: fullPath,
encoding: 'utf8'
}, (statusError: any, statusStdout: string, statusStderr: string) => {
if (statusError) {
console.error('❌ 检查 Git 状态失败:', statusError);
reject(new Error(`检查 Git 状态失败: ${statusStderr || statusError.message}`));
return;
}
const hasChanges = !!statusStdout.trim();
console.log('📋 有未提交更改?:', hasChanges, ', status =\n', statusStdout);
const commands: string[] = [];
// 获取完整的仓库历史
commands.push('git fetch --unshallow');
// 切换 / 创建分支
commands.push(`git checkout -B "${branchName}"`);
// 有变更就先提交
if (hasChanges) {
commands.push('git add .');
commands.push(`git commit -m "Auto commit from DCSP - ${new Date().toLocaleString()}"`);
}
// 直接用 URL 形式推送,不依赖 remote 名称
commands.push(`git push -u "${repoUrl}" "${branchName}" --force`);
console.log('🧰 即将执行命令链:', commands.join(' && '));
exec(commands.join(' && '), {
cwd: fullPath,
encoding: 'utf8'
}, (error: any, stdout: string, stderr: string) => {
console.log('📋 Git push stdout:', stdout);
console.log('📋 Git push stderr:', stderr);
if (error) {
console.error('❌ Git 推送失败:', error);
reject(new Error(stderr || error.message));
return;
}
console.log('✅ Git 推送到指定仓库/分支成功');
resolve();
});
});
});
}
// =============================================
// Git 命令行工具方法
// =============================================
@@ -1351,7 +1491,7 @@ export class ConfigPanel {
console.log('📁 初始化 Git 仓库...');
exec(`git init && git checkout -b ${branchName}`, {
exec(`git init && git checkout -b "${branchName}"`, {
cwd: fullPath,
encoding: 'utf8'
}, (error: any, stdout: string, stderr: string) => {
@@ -1373,7 +1513,7 @@ export class ConfigPanel {
console.log('📡 添加远程仓库...');
exec(`git remote add origin ${repoUrl}`, {
exec(`git remote add origin "${repoUrl}"`, {
cwd: fullPath,
encoding: 'utf8'
}, (error: any, stdout: string, stderr: string) => {
@@ -1397,7 +1537,7 @@ export class ConfigPanel {
const commands = [
'git add .',
`git commit -m "Initial commit from DCSP - ${new Date().toLocaleString()}"`,
`git commit -m "Initial commit from DCSP - ${new Date().toLocaleString()}"`
];
exec(commands.join(' && '), {
@@ -1428,7 +1568,7 @@ export class ConfigPanel {
console.log('🚀 强制推送到远程仓库...');
exec(`git push -u origin ${branchName} --force`, {
exec(`git push -u origin "${branchName}" --force`, {
cwd: fullPath,
encoding: 'utf8'
}, (error: any, stdout: string, stderr: string) => {
@@ -1893,6 +2033,12 @@ export class ConfigPanel {
}
}
private async handleRepoSelectedForGitUpload(folderId: string, repoName: string): Promise<void> {
// 兼容:如果前端用的是 repoSelectedForGitUpload 事件,则这里转成统一逻辑
this.pendingGitUploadFolderId = folderId;
await this.handleRepoSelectedForBranches(repoName);
}
// =============================================
// 工具方法
// =============================================
@@ -2030,38 +2176,36 @@ export class ConfigPanel {
}
private async renameModuleFolder(folderId: string, newName: string): Promise<void> {
const folder = this.moduleFolders.find(f => f.id === folderId);
if (!folder) {
vscode.window.showErrorMessage('未找到模块文件夹');
return;
const folder = this.moduleFolders.find(f => f.id === folderId);
if (!folder) {
vscode.window.showErrorMessage('未找到模块文件夹');
return;
}
const oldName = folder.localPath.split('/').pop();
if (!oldName) return;
const fullPath = this.getModuleFolderFullPath(folder);
if (!fullPath) return;
const newFullPath = path.join(path.dirname(fullPath), newName);
try {
await fs.promises.rename(fullPath, newFullPath);
// 更新 localPath注意originalFolderName 不更新,用于判断“是否改名过”)
folder.localPath = folder.localPath.replace(/\/[^/]+$/, '/' + newName);
await this.saveCurrentProjectData();
vscode.window.showInformationMessage(`已重命名文件夹: ${oldName}${newName}`);
this.updateWebview();
} catch (error) {
vscode.window.showErrorMessage('重命名失败: ' + error);
}
}
const oldName = folder.localPath.split('/').pop();
if (!oldName) return;
const fullPath = this.getModuleFolderFullPath(folder);
if (!fullPath) return;
const newFullPath = path.join(path.dirname(fullPath), newName);
try {
await fs.promises.rename(fullPath, newFullPath);
// 更新 localPath
folder.localPath = folder.localPath.replace(/\/[^/]+$/, '/' + newName);
await this.saveCurrentProjectData();
vscode.window.showInformationMessage(`已重命名文件夹: ${oldName}${newName}`);
this.updateWebview();
} catch (error) {
vscode.window.showErrorMessage('重命名失败: ' + error);
}
}
// =============================================
// Webview 更新方法
// =============================================
@@ -2079,6 +2223,12 @@ export class ConfigPanel {
}
}
private async openRepoSelectForGitUpload(folderId: string): Promise<void> {
console.log('📌 Git 上传:收到 openRepoSelectForGitUploadfolderId =', folderId);
this.pendingGitUploadFolderId = folderId;
await this.openRepoSelect();
}
private getWebviewContent(): string {
switch (this.currentView) {
case 'projects':