refactor(updater): 重构 Go 版本更新器

- 更新项目名称为 AUTO_MAA_Go_Updater
- 重构代码结构,优化函数命名和逻辑
- 移除 CDK 相关的冗余代码
- 调整版本号为 git commit hash
- 更新构建配置和脚本
- 优化 API 客户端实现
This commit is contained in:
2025-07-22 21:51:58 +08:00
parent 747ad6387b
commit 6b646378b6
21 changed files with 887 additions and 1673 deletions

View File

@@ -8,16 +8,16 @@ import (
"strconv"
"strings"
"lightweight-updater/logger"
"AUTO_MAA_Go_Updater/logger"
)
// VersionInfo represents the version information from version.json
// VersionInfo 表示来自 version.json 的版本信息
type VersionInfo struct {
MainVersion string `json:"main_version"`
VersionInfo map[string]map[string][]string `json:"version_info"`
}
// ParsedVersion represents a parsed version with major, minor, patch, and beta components
// ParsedVersion 表示解析后的版本,包含主版本号、次版本号、补丁版本号和测试版本号组件
type ParsedVersion struct {
Major int
Minor int
@@ -25,13 +25,13 @@ type ParsedVersion struct {
Beta int
}
// VersionManager handles version-related operations
// VersionManager 处理版本相关操作
type VersionManager struct {
executableDir string
logger logger.Logger
}
// NewVersionManager creates a new version manager
// NewVersionManager 创建新的版本管理器
func NewVersionManager() *VersionManager {
execPath, _ := os.Executable()
execDir := filepath.Dir(execPath)
@@ -41,103 +41,93 @@ func NewVersionManager() *VersionManager {
}
}
// NewVersionManagerWithLogger creates a new version manager with a custom logger
func NewVersionManagerWithLogger(customLogger logger.Logger) *VersionManager {
execPath, _ := os.Executable()
execDir := filepath.Dir(execPath)
return &VersionManager{
executableDir: execDir,
logger: customLogger,
}
}
// createDefaultVersion creates a default version structure with v0.0.0
// createDefaultVersion 创建默认版本结构 v0.0.0
func (vm *VersionManager) createDefaultVersion() *VersionInfo {
return &VersionInfo{
MainVersion: "0.0.0.0", // Corresponds to v0.0.0
MainVersion: "0.0.0.0", // 对应 v0.0.0
VersionInfo: make(map[string]map[string][]string),
}
}
// LoadVersionFromFile loads version information from resources/version.json with fallback handling
// LoadVersionFromFile resources/version.json 加载版本信息并处理回退
func (vm *VersionManager) LoadVersionFromFile() (*VersionInfo, error) {
versionPath := filepath.Join(vm.executableDir, "resources", "version.json")
data, err := os.ReadFile(versionPath)
if err != nil {
if os.IsNotExist(err) {
vm.logger.Info("Version file not found at %s, will use default version", versionPath)
fmt.Println("未读取到版本信息,使用默认版本进行更新。")
return vm.createDefaultVersion(), nil
}
vm.logger.Warn("Failed to read version file at %s: %v, will use default version", versionPath, err)
vm.logger.Warn("读取版本文件 %s 失败: %v将使用默认版本", versionPath, err)
return vm.createDefaultVersion(), nil
}
var versionInfo VersionInfo
if err := json.Unmarshal(data, &versionInfo); err != nil {
vm.logger.Warn("Failed to parse version file at %s: %v, will use default version", versionPath, err)
vm.logger.Warn("解析版本文件 %s 失败: %v将使用默认版本", versionPath, err)
return vm.createDefaultVersion(), nil
}
vm.logger.Debug("Successfully loaded version information from %s", versionPath)
vm.logger.Debug("成功从 %s 加载版本信息", versionPath)
return &versionInfo, nil
}
// LoadVersionWithDefault loads version information with guaranteed fallback to default
// LoadVersionWithDefault 加载版本信息并保证回退到默认版本
func (vm *VersionManager) LoadVersionWithDefault() *VersionInfo {
versionInfo, err := vm.LoadVersionFromFile()
if err != nil {
// This should not happen with the updated LoadVersionFromFile, but adding as extra safety
vm.logger.Error("Unexpected error loading version file: %v, using default version", err)
// 这在更新的 LoadVersionFromFile 中不应该发生,但添加作为额外安全措施
vm.logger.Error("加载版本文件时出现意外错误: %v使用默认版本", err)
return vm.createDefaultVersion()
}
// Validate that we have a valid version structure
// 验证我们有一个有效的版本结构
if versionInfo == nil {
vm.logger.Warn("Version info is nil, using default version")
vm.logger.Warn("版本信息为空,使用默认版本")
return vm.createDefaultVersion()
}
if versionInfo.MainVersion == "" {
vm.logger.Warn("Version info has empty main version, using default version")
vm.logger.Warn("版本信息主版本为空,使用默认版本")
return vm.createDefaultVersion()
}
if versionInfo.VersionInfo == nil {
vm.logger.Debug("Version info map is nil, initializing empty map")
vm.logger.Debug("版本信息映射为空,初始化空映射")
versionInfo.VersionInfo = make(map[string]map[string][]string)
}
return versionInfo
}
// ParseVersion parses a version string like "4.4.1.3" into components
// ParseVersion 解析版本字符串如 "4.4.1.3" 为组件
func ParseVersion(versionStr string) (*ParsedVersion, error) {
parts := strings.Split(versionStr, ".")
if len(parts) < 3 || len(parts) > 4 {
return nil, fmt.Errorf("invalid version format: %s", versionStr)
return nil, fmt.Errorf("无效的版本格式: %s", versionStr)
}
major, err := strconv.Atoi(parts[0])
if err != nil {
return nil, fmt.Errorf("invalid major version: %s", parts[0])
return nil, fmt.Errorf("无效的主版本号: %s", parts[0])
}
minor, err := strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("invalid minor version: %s", parts[1])
return nil, fmt.Errorf("无效的次版本号: %s", parts[1])
}
patch, err := strconv.Atoi(parts[2])
if err != nil {
return nil, fmt.Errorf("invalid patch version: %s", parts[2])
return nil, fmt.Errorf("无效的补丁版本号: %s", parts[2])
}
beta := 0
if len(parts) == 4 {
beta, err = strconv.Atoi(parts[3])
if err != nil {
return nil, fmt.Errorf("invalid beta version: %s", parts[3])
return nil, fmt.Errorf("无效的测试版本号: %s", parts[3])
}
}
@@ -149,7 +139,7 @@ func ParseVersion(versionStr string) (*ParsedVersion, error) {
}, nil
}
// ToVersionString converts a ParsedVersion back to version string format
// ToVersionString ParsedVersion 转换回版本字符串格式
func (pv *ParsedVersion) ToVersionString() string {
if pv.Beta == 0 {
return fmt.Sprintf("%d.%d.%d.0", pv.Major, pv.Minor, pv.Patch)
@@ -157,7 +147,7 @@ func (pv *ParsedVersion) ToVersionString() string {
return fmt.Sprintf("%d.%d.%d.%d", pv.Major, pv.Minor, pv.Patch, pv.Beta)
}
// ToDisplayVersion converts version to display format (v4.4.0 or v4.4.1-beta3)
// ToDisplayVersion 将版本转换为显示格式 (v4.4.0 v4.4.1-beta3)
func (pv *ParsedVersion) ToDisplayVersion() string {
if pv.Beta == 0 {
return fmt.Sprintf("v%d.%d.%d", pv.Major, pv.Minor, pv.Patch)
@@ -165,7 +155,7 @@ func (pv *ParsedVersion) ToDisplayVersion() string {
return fmt.Sprintf("v%d.%d.%d-beta%d", pv.Major, pv.Minor, pv.Patch, pv.Beta)
}
// GetChannel returns the channel (stable or beta) based on version
// GetChannel 根据版本返回渠道 (stable beta)
func (pv *ParsedVersion) GetChannel() string {
if pv.Beta == 0 {
return "stable"
@@ -173,12 +163,7 @@ func (pv *ParsedVersion) GetChannel() string {
return "beta"
}
// GetDefaultChannel returns the default channel
func GetDefaultChannel() string {
return "stable"
}
// IsNewer checks if this version is newer than the other version
// IsNewer 检查此版本是否比其他版本更新
func (pv *ParsedVersion) IsNewer(other *ParsedVersion) bool {
if pv.Major != other.Major {
return pv.Major > other.Major

View File

@@ -1,41 +1,19 @@
package version
import (
"fmt"
"runtime"
)
var (
// Version is the current version of the application
// Version 应用程序的当前版本
Version = "1.0.0"
// BuildTime is set during build time
// BuildTime 在构建时设置
BuildTime = "unknown"
// GitCommit is set during build time
// GitCommit 在构建时设置
GitCommit = "unknown"
// GoVersion is the Go version used to build
// GoVersion 用于构建的 Go 版本
GoVersion = runtime.Version()
)
// GetVersionInfo returns formatted version information
func GetVersionInfo() string {
return fmt.Sprintf("Version: %s\nBuild Time: %s\nGit Commit: %s\nGo Version: %s",
Version, BuildTime, GitCommit, GoVersion)
}
// GetShortVersion returns just the version number
func GetShortVersion() string {
return Version
}
// GetBuildInfo returns build-specific information
func GetBuildInfo() map[string]string {
return map[string]string{
"version": Version,
"build_time": BuildTime,
"git_commit": GitCommit,
"go_version": GoVersion,
}
}