From 9f9cf84ea8c3ddf48b149cf355a738d1d5b1dd0e Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Wed, 30 Jul 2025 00:16:49 +0800 Subject: [PATCH] refactor(resource): improve directory cleanup in `Extract` function * Added checks to remove existing directories before extracting files for hosts, ui, others, and script_plugin_templates. * Enhanced logging in `StartUIApp` to include the app path in the error message when the UI app does not exist. --- wox.core/resource/resource.go | 42 +++++++++++++++++++++++++++++++---- wox.core/ui/manager.go | 2 +- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/wox.core/resource/resource.go b/wox.core/resource/resource.go index 9e4b3954..ec26e975 100644 --- a/wox.core/resource/resource.go +++ b/wox.core/resource/resource.go @@ -35,26 +35,60 @@ var embedThemes = []string{} func Extract(ctx context.Context) error { start := util.GetSystemTimestamp() - extractHostErr := extractFiles(ctx, HostFS, util.GetLocation().GetHostDirectory(), "hosts", false) + + // hosts + hostDirectory := util.GetLocation().GetHostDirectory() + if util.IsDirExists(hostDirectory) { + rmErr := os.RemoveAll(hostDirectory) + if rmErr != nil { + return rmErr + } + } + extractHostErr := extractFiles(ctx, HostFS, hostDirectory, "hosts", false) if extractHostErr != nil { return extractHostErr } - flutterErr := extractFiles(ctx, UIFS, util.GetLocation().GetUIDirectory(), "ui/flutter", true) + // ui + uiDiretory := util.GetLocation().GetUIDirectory() + if util.IsDirExists(uiDiretory) { + rmErr := os.RemoveAll(uiDiretory) + if rmErr != nil { + return rmErr + } + } + flutterErr := extractFiles(ctx, UIFS, uiDiretory, "ui/flutter", true) if flutterErr != nil { return flutterErr } - othersErr := extractFiles(ctx, OthersFS, util.GetLocation().GetOthersDirectory(), "others", false) + // others + othersDirectory := util.GetLocation().GetOthersDirectory() + if util.IsDirExists(othersDirectory) { + rmErr := os.RemoveAll(othersDirectory) + if rmErr != nil { + return rmErr + } + } + othersErr := extractFiles(ctx, OthersFS, othersDirectory, "others", false) if othersErr != nil { return othersErr } - scriptPluginTemplatesErr := extractFiles(ctx, ScriptPluginTemplatesFS, util.GetLocation().GetScriptPluginTemplatesDirectory(), "script_plugin_templates", false) + // script_plugin_templates + scriptPluginTemplatesDirectory := util.GetLocation().GetScriptPluginTemplatesDirectory() + if util.IsDirExists(scriptPluginTemplatesDirectory) { + rmErr := os.RemoveAll(scriptPluginTemplatesDirectory) + if rmErr != nil { + return rmErr + } + } + scriptPluginTemplatesErr := extractFiles(ctx, ScriptPluginTemplatesFS, scriptPluginTemplatesDirectory, "script_plugin_templates", false) if scriptPluginTemplatesErr != nil { return scriptPluginTemplatesErr } + // themes themeErr := parseThemes(ctx) if themeErr != nil { return themeErr diff --git a/wox.core/ui/manager.go b/wox.core/ui/manager.go index a76db77c..bde70284 100644 --- a/wox.core/ui/manager.go +++ b/wox.core/ui/manager.go @@ -295,7 +295,7 @@ func (m *Manager) UpdateServerPort(port int) { func (m *Manager) StartUIApp(ctx context.Context) error { var appPath = util.GetLocation().GetUIAppPath() if fileInfo, statErr := os.Stat(appPath); os.IsNotExist(statErr) { - logger.Info(ctx, "UI app not exist") + logger.Info(ctx, "UI app not exist: "+appPath) return errors.New("UI app not exist") } else { if !util.IsFileExecAny(fileInfo.Mode()) {