Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions src/providers/vueWebviewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@ import { ConfigService } from '../services/configService';
import { LogService } from '../services/logService';
import { RegionService } from '../services/regionService';

const RELEASE_BRANCH_PREFIX = 'release/';

interface SdkInstallVersionPayload {
name: string;
type: 'release' | 'branch';
tagName?: string;
gitRef?: string;
}

function normalizeBranchGitRef(branchName: string): string {
if (branchName === 'latest' || branchName === 'main') {
return 'main';
}

return branchName.startsWith(RELEASE_BRANCH_PREFIX) ? branchName : `${RELEASE_BRANCH_PREFIX}${branchName}`;
}

function getBranchFolderName(branchName: string): string {
const normalizedBranchName = branchName === 'latest' ? 'main' : branchName;

return normalizedBranchName.startsWith(RELEASE_BRANCH_PREFIX)
? normalizedBranchName.slice(RELEASE_BRANCH_PREFIX.length)
: normalizedBranchName;
}

export class VueWebviewProvider {
private static instance: VueWebviewProvider;
private terminalService: TerminalService;
Expand Down Expand Up @@ -317,7 +342,13 @@ export class VueWebviewProvider {
const installationLogs: string[] = [];
try {
console.log('[VueWebviewProvider] Starting SDK installation...');
const { sdkSource, version, installPath, toolchainSource, toolsPath } = message.data;
const { sdkSource, version, installPath, toolchainSource, toolsPath } = message.data as {
sdkSource: 'github' | 'gitee';
version: SdkInstallVersionPayload;
installPath: string;
toolchainSource: 'github' | 'sifli';
toolsPath?: string;
};

console.log('[VueWebviewProvider] Installation parameters:', {
sdkSource,
Expand All @@ -337,7 +368,7 @@ export class VueWebviewProvider {
};

// 存储工具链路径以便后续使用
const toolsPathForEnv = toolsPath && toolsPath.trim() !== '' ? toolsPath.trim() : null;
const toolsPathForEnv = toolsPath && toolsPath.trim() !== '' ? toolsPath.trim() : undefined;

if (toolsPathForEnv) {
console.log('[VueWebviewProvider] Tools path provided:', toolsPathForEnv);
Expand Down Expand Up @@ -369,7 +400,12 @@ export class VueWebviewProvider {
console.log('[VueWebviewProvider] Repository URL:', repoUrl);

// 修正目录名,确保与前端显示一致
const dirName = version.name === 'latest' ? 'main' : version.name;
const dirName =
version.type === 'branch'
? getBranchFolderName(version.name || version.gitRef || 'main')
: version.name === 'latest'
? 'main'
: version.name;

// 创建安装目录 - 修正路径结构为 installPath/SiFli-SDK/dirName
const sdkBasePath = path.join(installPath, 'SiFli-SDK');
Expand Down Expand Up @@ -400,8 +436,21 @@ export class VueWebviewProvider {

console.log('[VueWebviewProvider] Starting clone operation...');

// 确保 'latest' 被转换为 'main' ===
let branchName = version.type === 'release' ? version.tagName : version.name;
let branchName =
version.type === 'release' ? version.tagName || version.name : version.gitRef || version.name;

if (!branchName) {
throw new Error('Missing SDK version or branch information. Cannot clone repository.');
}

if (version.type === 'branch' && !version.gitRef) {
const normalizedBranchName = normalizeBranchGitRef(branchName);
if (normalizedBranchName !== branchName) {
sendLog(`Normalized legacy branch ref to "${normalizedBranchName}"`);
}
branchName = normalizedBranchName;
}

if (branchName === 'latest') {
branchName = 'main';
console.log('[VueWebviewProvider] Corrected branch name from "latest" to "main"');
Expand Down
16 changes: 11 additions & 5 deletions webview-vue/src/components/sdk/InstallPathSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ import { computed, onMounted, ref } from 'vue';
import BaseButton from '@/components/common/BaseButton.vue';
import { useVsCodeApi } from '@/composables/useVsCodeApi';

const RELEASE_BRANCH_PREFIX = 'release/';

function getBranchFolderName(branchRef: string): string {
const normalizedBranchRef = branchRef === 'latest' ? 'main' : branchRef;

return normalizedBranchRef.startsWith(RELEASE_BRANCH_PREFIX)
? normalizedBranchRef.slice(RELEASE_BRANCH_PREFIX.length)
: normalizedBranchRef;
}
Comment on lines +62 to +70

interface Props {
modelValue: string;
selectedVersion?: string;
Expand Down Expand Up @@ -88,11 +98,7 @@ const pathSuffix = computed(() => {
if (props.downloadType === 'release' && props.selectedVersion) {
return `SiFli-SDK/${props.selectedVersion}`;
} else if (props.downloadType === 'branch' && props.selectedBranch) {
// 处理分支名称,移除 'release/' 前缀
let folderName = props.selectedBranch;
if (folderName.startsWith('release/')) {
folderName = folderName.replace('release/', '');
}
const folderName = getBranchFolderName(props.selectedBranch);
return `SiFli-SDK/${folderName}`;
}
return '';
Expand Down
154 changes: 81 additions & 73 deletions webview-vue/src/composables/useSdkManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { ref, computed, watch } from 'vue';
import { useVsCodeApi } from './useVsCodeApi';
import type { SdkManagerState, SdkVersionInfo } from '@/types';
import type {
SdkInstallRequestData,
SdkInstallVersionPayload,
SdkManagerState,
SdkRelease,
SdkVersionInfo,
} from '@/types';

const RELEASE_BRANCH_PREFIX = 'release/';

function getBranchFolderName(branchRef: string): string {
const normalizedBranchRef = branchRef === 'latest' ? 'main' : branchRef;

return normalizedBranchRef.startsWith(RELEASE_BRANCH_PREFIX)
? normalizedBranchRef.slice(RELEASE_BRANCH_PREFIX.length)
: normalizedBranchRef;
}
Comment on lines +11 to +19

export function useSdkManager() {
const { postMessage, onMessage } = useVsCodeApi();
Expand All @@ -10,6 +26,8 @@ export function useSdkManager() {
sdkSource: 'github',
downloadType: 'release',
availableVersions: [], // 新的统一版本信息
availableReleases: [],
availableBranches: [],
selectedVersion: '',
selectedBranch: '',
installPath: '',
Expand Down Expand Up @@ -39,33 +57,22 @@ export function useSdkManager() {
const releases = computed(() => {
return state.value.availableVersions
.filter((v: SdkVersionInfo) => !v.type || v.type !== 'branch')
.map((v: SdkVersionInfo) => ({
tagName: v.version,
name: v.version,
supportedChips: v.supported_chips,
}));
.map(
(v: SdkVersionInfo): SdkRelease => ({
tagName: v.version,
name: v.version,
supportedChips: v.supported_chips,
})
);
});

const branches = computed(() => {
return state.value.availableVersions
.filter((v: SdkVersionInfo) => v.type === 'branch')
.map((v: SdkVersionInfo) => {
let branchName = v.version;

// 处理分支名称逻辑
if (v.version === 'latest') {
// latest 分支改为 main
branchName = 'main';
} else {
// 其他分支加上 release/ 前缀
branchName = `release/${v.version}`;
}

return {
name: branchName,
supportedChips: v.supported_chips,
};
});
.map((v: SdkVersionInfo) => ({
name: v.version === 'latest' ? 'main' : `${RELEASE_BRANCH_PREFIX}${v.version}`,
supportedChips: v.supported_chips,
}));
});

// 计算最终的安装路径
Expand All @@ -84,8 +91,8 @@ export function useSdkManager() {

// 处理分支名称,移除 'release/' 前缀用于目录名
let folderName = selectedName;
if (state.value.downloadType === 'branch' && selectedName.startsWith('release/')) {
folderName = selectedName.replace('release/', '');
if (state.value.downloadType === 'branch') {
folderName = getBranchFolderName(selectedName);
}

return `${basePath}/SiFli-SDK/${folderName}`;
Expand Down Expand Up @@ -143,49 +150,56 @@ export function useSdkManager() {
};

// 确定版本信息
const selectedVersionInfo =
state.value.downloadType === 'release'
? state.value.availableVersions.find((v: SdkVersionInfo) => v.version === state.value.selectedVersion)
: state.value.availableVersions.find(
(v: SdkVersionInfo) =>
v.type === 'branch' &&
(v.version === 'latest' ? 'main' : `release/${v.version}`) === state.value.selectedBranch
);

if (!selectedVersionInfo) {
throw new Error('未找到选择的版本信息');
let versionPayload: SdkInstallVersionPayload;

if (state.value.downloadType === 'release') {
const selectedRelease = state.value.availableVersions.find(
(v: SdkVersionInfo) => v.version === state.value.selectedVersion && (!v.type || v.type !== 'branch')
);

if (!selectedRelease) {
throw new Error('未找到选择的版本信息');
}

const tagName = selectedRelease.version === 'latest' ? 'main' : selectedRelease.version;
versionPayload = {
name: tagName,
tagName,
type: 'release',
};
} else {
if (!state.value.selectedBranch) {
throw new Error('未找到选择的分支信息');
}

const gitRef = state.value.selectedBranch === 'latest' ? 'main' : state.value.selectedBranch;
versionPayload = {
name: getBranchFolderName(gitRef),
gitRef,
type: 'branch',
Comment on lines +171 to +179
};
}

console.log('[useSdkManager] Starting SDK installation with data:', {
sdkSource: state.value.sdkSource,
version: selectedVersionInfo,
version: versionPayload,
installPath: state.value.installPath,
toolchainSource: state.value.toolchainSource,
toolsPath: state.value.toolsPath,
});

// 确保前端也处理 'latest' 到 'main' 的转换
// 虽然后端也有这个转换逻辑,但在前端也进行转换可以确保一致性
let versionName = selectedVersionInfo.version;
if (versionName === 'latest') {
versionName = 'main';
console.log('[useSdkManager] Corrected version name from "latest" to "main"');
}

// 发送安装请求
const installRequest: SdkInstallRequestData = {
sdkSource: state.value.sdkSource,
version: versionPayload,
installPath: state.value.installPath,
toolchainSource: state.value.toolchainSource,
toolsPath: state.value.toolsPath,
};

postMessage({
command: 'installSdk',
data: {
sdkSource: state.value.sdkSource,
version: {
name: versionName,
tagName: versionName,
type: selectedVersionInfo.type || 'release',
},
installPath: state.value.installPath,
toolchainSource: state.value.toolchainSource,
toolsPath: state.value.toolsPath,
},
data: installRequest,
});
} catch (error) {
console.error('Installation failed:', error);
Expand All @@ -201,28 +215,22 @@ export function useSdkManager() {
onMessage('displayVersions', (data: { versions: SdkVersionInfo[] }) => {
console.log('[useSdkManager] Received versions:', data.versions);
state.value.availableVersions = data.versions;

// 为兼容性更新旧数组
state.value.availableReleases = data.versions
.filter(v => !v.type || v.type !== 'branch')
.map(v => ({
tagName: v.version,
name: v.version,
}));
.map(
(v): SdkRelease => ({
tagName: v.version,
name: v.version,
supportedChips: v.supported_chips,
})
);

state.value.availableBranches = data.versions
.filter(v => v.type === 'branch')
.map(v => {
let displayName = v.version;
if (v.version === 'latest') {
displayName = 'main';
} else {
displayName = `release/${v.version}`;
}
return {
name: displayName,
};
});
.map(v => ({
name: v.version === 'latest' ? 'main' : `${RELEASE_BRANCH_PREFIX}${v.version}`,
supportedChips: v.supported_chips,
}));

// 清空选择
state.value.selectedVersion = '';
Expand Down
19 changes: 19 additions & 0 deletions webview-vue/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ export interface SdkRelease {
name?: string;
publishedAt?: string;
prerelease?: boolean;
supportedChips?: string[];
}

export interface SdkBranch {
name: string;
supportedChips?: string[];
commit?: {
sha: string;
url: string;
Expand All @@ -32,6 +34,21 @@ export type SdkSource = 'github' | 'gitee';
export type ToolchainSource = 'github' | 'sifli';
export type DownloadType = 'release' | 'branch';

export interface SdkInstallVersionPayload {
name: string;
type: DownloadType;
tagName?: string;
gitRef?: string;
}

export interface SdkInstallRequestData {
sdkSource: SdkSource;
version: SdkInstallVersionPayload;
installPath: string;
toolchainSource: ToolchainSource;
toolsPath: string;
}

// 消息类型
export interface WebviewMessage {
command: string;
Expand All @@ -43,6 +60,8 @@ export interface SdkManagerState {
sdkSource: SdkSource;
downloadType: DownloadType;
availableVersions: SdkVersionInfo[]; // 统一版本信息
availableReleases: SdkRelease[];
availableBranches: SdkBranch[];
selectedVersion: string;
selectedBranch: string;
installPath: string;
Expand Down
Loading