✨ Add update service
This commit is contained in:
@@ -1,84 +0,0 @@
|
||||
//go:build !windows && !darwin && !linux
|
||||
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"voidraft/internal/models"
|
||||
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
"github.com/wailsapp/wails/v3/pkg/services/log"
|
||||
)
|
||||
|
||||
// HotkeyService 存根热键服务
|
||||
type HotkeyService struct {
|
||||
logger *log.LoggerService
|
||||
configService *ConfigService
|
||||
}
|
||||
|
||||
// HotkeyError 热键错误
|
||||
type HotkeyError struct {
|
||||
Operation string
|
||||
Err error
|
||||
}
|
||||
|
||||
// Error 实现error接口
|
||||
func (e *HotkeyError) Error() string {
|
||||
return fmt.Sprintf("hotkey %s: %v", e.Operation, e.Err)
|
||||
}
|
||||
|
||||
// Unwrap 获取原始错误
|
||||
func (e *HotkeyError) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
// NewHotkeyService 创建热键服务实例
|
||||
func NewHotkeyService(configService *ConfigService, logger *log.LoggerService) *HotkeyService {
|
||||
if logger == nil {
|
||||
logger = log.New()
|
||||
}
|
||||
|
||||
return &HotkeyService{
|
||||
logger: logger,
|
||||
configService: configService,
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize 初始化热键服务
|
||||
func (hs *HotkeyService) Initialize(app *application.App) error {
|
||||
hs.logger.Warning("Global hotkey is not supported on this platform")
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterHotkey 注册全局热键
|
||||
func (hs *HotkeyService) RegisterHotkey(hotkey *models.HotkeyCombo) error {
|
||||
return &HotkeyError{"register", fmt.Errorf("not supported on this platform")}
|
||||
}
|
||||
|
||||
// UnregisterHotkey 取消注册全局热键
|
||||
func (hs *HotkeyService) UnregisterHotkey() error {
|
||||
return &HotkeyError{"unregister", fmt.Errorf("not supported on this platform")}
|
||||
}
|
||||
|
||||
// UpdateHotkey 更新热键配置
|
||||
func (hs *HotkeyService) UpdateHotkey(enable bool, hotkey *models.HotkeyCombo) error {
|
||||
if enable {
|
||||
return hs.RegisterHotkey(hotkey)
|
||||
}
|
||||
return hs.UnregisterHotkey()
|
||||
}
|
||||
|
||||
// GetCurrentHotkey 获取当前热键
|
||||
func (hs *HotkeyService) GetCurrentHotkey() *models.HotkeyCombo {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsRegistered 检查是否已注册
|
||||
func (hs *HotkeyService) IsRegistered() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ServiceShutdown 关闭服务
|
||||
func (hs *HotkeyService) ServiceShutdown() error {
|
||||
return nil
|
||||
}
|
@@ -19,6 +19,7 @@ type ServiceManager struct {
|
||||
trayService *TrayService
|
||||
keyBindingService *KeyBindingService
|
||||
startupService *StartupService
|
||||
updateService *UpdateService
|
||||
logger *log.LoggerService
|
||||
}
|
||||
|
||||
@@ -57,6 +58,9 @@ func NewServiceManager() *ServiceManager {
|
||||
// 初始化开机启动服务
|
||||
startupService := NewStartupService(configService, logger)
|
||||
|
||||
// 初始化更新服务
|
||||
updateService := NewUpdateService(configService, logger)
|
||||
|
||||
// 使用新的配置通知系统设置热键配置变更监听
|
||||
err := configService.SetHotkeyChangeCallback(func(enable bool, hotkey *models.HotkeyCombo) error {
|
||||
return hotkeyService.UpdateHotkey(enable, hotkey)
|
||||
@@ -89,6 +93,7 @@ func NewServiceManager() *ServiceManager {
|
||||
trayService: trayService,
|
||||
keyBindingService: keyBindingService,
|
||||
startupService: startupService,
|
||||
updateService: updateService,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
@@ -105,6 +110,7 @@ func (sm *ServiceManager) GetServices() []application.Service {
|
||||
application.NewService(sm.trayService),
|
||||
application.NewService(sm.keyBindingService),
|
||||
application.NewService(sm.startupService),
|
||||
application.NewService(sm.updateService),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,3 +148,8 @@ func (sm *ServiceManager) GetKeyBindingService() *KeyBindingService {
|
||||
func (sm *ServiceManager) GetStartupService() *StartupService {
|
||||
return sm.startupService
|
||||
}
|
||||
|
||||
// GetUpdateService 获取更新服务实例
|
||||
func (sm *ServiceManager) GetUpdateService() *UpdateService {
|
||||
return sm.updateService
|
||||
}
|
||||
|
89
internal/services/update_service.go
Normal file
89
internal/services/update_service.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-github/v63/github"
|
||||
"github.com/wailsapp/wails/v3/pkg/services/log"
|
||||
)
|
||||
|
||||
// UpdateCheckResult 更新检查结果
|
||||
type UpdateCheckResult struct {
|
||||
HasUpdate bool `json:"hasUpdate"` // 是否有更新
|
||||
CurrentVer string `json:"currentVer"` // 当前版本
|
||||
LatestVer string `json:"latestVer"` // 最新版本
|
||||
ReleaseNotes string `json:"releaseNotes"` // 发布说明
|
||||
ReleaseURL string `json:"releaseURL"` // 发布页面URL
|
||||
Error string `json:"error"` // 错误信息
|
||||
}
|
||||
|
||||
// UpdateService 更新服务
|
||||
type UpdateService struct {
|
||||
logger *log.LoggerService
|
||||
configService *ConfigService
|
||||
githubClient *github.Client
|
||||
currentVersion string
|
||||
}
|
||||
|
||||
// NewUpdateService 创建更新服务实例
|
||||
func NewUpdateService(configService *ConfigService, logger *log.LoggerService) *UpdateService {
|
||||
config, err := configService.GetConfig()
|
||||
if err != nil {
|
||||
logger.Error("Failed to get config", "error", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
currentVersion := config.Updates.Version
|
||||
if currentVersion == "" {
|
||||
currentVersion = "1.0.0"
|
||||
}
|
||||
|
||||
httpClient := &http.Client{Timeout: 30 * time.Second}
|
||||
githubClient := github.NewClient(httpClient)
|
||||
|
||||
return &UpdateService{
|
||||
logger: logger,
|
||||
configService: configService,
|
||||
githubClient: githubClient,
|
||||
currentVersion: currentVersion,
|
||||
}
|
||||
}
|
||||
|
||||
// CheckForUpdates 检查更新
|
||||
func (us *UpdateService) CheckForUpdates() UpdateCheckResult {
|
||||
result := UpdateCheckResult{
|
||||
CurrentVer: us.currentVersion,
|
||||
HasUpdate: false,
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
release, _, err := us.githubClient.Repositories.GetLatestRelease(ctx, "landaiqing", "voidraft")
|
||||
if err != nil {
|
||||
result.Error = fmt.Sprintf("Failed to check for updates: %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
if release.GetDraft() || release.GetPrerelease() {
|
||||
result.Error = "Latest release is draft or prerelease"
|
||||
return result
|
||||
}
|
||||
|
||||
latestVer := strings.TrimPrefix(release.GetTagName(), "v")
|
||||
currentVer := strings.TrimPrefix(us.currentVersion, "v")
|
||||
|
||||
result.LatestVer = latestVer
|
||||
result.ReleaseNotes = release.GetBody()
|
||||
result.ReleaseURL = release.GetHTMLURL()
|
||||
|
||||
if latestVer != currentVer && latestVer > currentVer {
|
||||
result.HasUpdate = true
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
Reference in New Issue
Block a user