Browse Source

:art: Improve marketplace loading performance https://github.com/siyuan-note/siyuan/issues/10973 https://github.com/siyuan-note/siyuan/issues/11007

Daniel 1 year ago
parent
commit
afeea6a80e

+ 2 - 8
kernel/bazaar/icon.go

@@ -17,7 +17,6 @@
 package bazaar
 
 import (
-	"errors"
 	"os"
 	"path/filepath"
 	"sort"
@@ -184,14 +183,9 @@ func InstallIcon(repoURL, repoHash, installPath string, systemID string) error {
 	if nil != err {
 		return err
 	}
-	return installPackage(data, installPath)
+	return installPackage(data, installPath, repoURLHash)
 }
 
 func UninstallIcon(installPath string) error {
-	if err := os.RemoveAll(installPath); nil != err {
-		logging.LogErrorf("remove icon [%s] failed: %s", installPath, err)
-		return errors.New("remove community icon failed")
-	}
-	//logging.Logger.Infof("uninstalled icon [%s]", installPath)
-	return nil
+	return uninstallPackage(installPath)
 }

+ 21 - 1
kernel/bazaar/package.go

@@ -19,6 +19,7 @@ package bazaar
 import (
 	"bytes"
 	"errors"
+	"fmt"
 	"os"
 	"path/filepath"
 	"strings"
@@ -588,7 +589,26 @@ func incPackageDownloads(repoURLHash, systemID string) {
 		}).Post(u)
 }
 
-func installPackage(data []byte, installPath string) (err error) {
+func uninstallPackage(installPath string) (err error) {
+	if err = os.RemoveAll(installPath); nil != err {
+		logging.LogErrorf("remove [%s] failed: %s", installPath, err)
+		return fmt.Errorf("remove community package [%s] failed", filepath.Base(installPath))
+	}
+	packageCache.Flush()
+	return
+}
+
+func installPackage(data []byte, installPath, repoURLHash string) (err error) {
+	err = installPackage0(data, installPath)
+	if nil != err {
+		return
+	}
+
+	packageCache.Delete(strings.TrimPrefix(repoURLHash, "https://github.com/"))
+	return
+}
+
+func installPackage0(data []byte, installPath string) (err error) {
 	tmpPackage := filepath.Join(util.TempDir, "bazaar", "package")
 	if err = os.MkdirAll(tmpPackage, 0755); nil != err {
 		return

+ 2 - 9
kernel/bazaar/plugin.go

@@ -17,7 +17,6 @@
 package bazaar
 
 import (
-	"errors"
 	"os"
 	"path/filepath"
 	"runtime"
@@ -27,7 +26,6 @@ import (
 
 	"github.com/dustin/go-humanize"
 	ants "github.com/panjf2000/ants/v2"
-	"github.com/siyuan-note/filelock"
 	"github.com/siyuan-note/httpclient"
 	"github.com/siyuan-note/logging"
 	"github.com/siyuan-note/siyuan/kernel/util"
@@ -220,16 +218,11 @@ func InstallPlugin(repoURL, repoHash, installPath string, systemID string) error
 	if nil != err {
 		return err
 	}
-	return installPackage(data, installPath)
+	return installPackage(data, installPath, repoURLHash)
 }
 
 func UninstallPlugin(installPath string) error {
-	if err := filelock.Remove(installPath); nil != err {
-		logging.LogErrorf("remove plugin [%s] failed: %s", installPath, err)
-		return errors.New("remove community plugin failed")
-	}
-	//logging.Logger.Infof("uninstalled plugin [%s]", installPath)
-	return nil
+	return uninstallPackage(installPath)
 }
 
 func isIncompatiblePlugin(plugin *Plugin, currentFrontend string) bool {

+ 2 - 8
kernel/bazaar/template.go

@@ -17,7 +17,6 @@
 package bazaar
 
 import (
-	"errors"
 	"os"
 	"path/filepath"
 	"sort"
@@ -27,7 +26,6 @@ import (
 
 	"github.com/dustin/go-humanize"
 	"github.com/panjf2000/ants/v2"
-	"github.com/siyuan-note/filelock"
 	"github.com/siyuan-note/httpclient"
 	"github.com/siyuan-note/logging"
 	"github.com/siyuan-note/siyuan/kernel/util"
@@ -182,15 +180,11 @@ func InstallTemplate(repoURL, repoHash, installPath string, systemID string) err
 	if nil != err {
 		return err
 	}
-	return installPackage(data, installPath)
+	return installPackage(data, installPath, repoURLHash)
 }
 
 func UninstallTemplate(installPath string) error {
-	if err := filelock.Remove(installPath); nil != err {
-		logging.LogErrorf("remove template [%s] failed: %s", installPath, err)
-		return errors.New("remove community template failed")
-	}
-	return nil
+	return uninstallPackage(installPath)
 }
 
 func filterLegacyTemplates(templates []*Template) (ret []*Template) {

+ 2 - 8
kernel/bazaar/theme.go

@@ -17,7 +17,6 @@
 package bazaar
 
 import (
-	"errors"
 	"os"
 	"path/filepath"
 	"sort"
@@ -186,14 +185,9 @@ func InstallTheme(repoURL, repoHash, installPath string, systemID string) error
 	if nil != err {
 		return err
 	}
-	return installPackage(data, installPath)
+	return installPackage(data, installPath, repoURLHash)
 }
 
 func UninstallTheme(installPath string) error {
-	if err := os.RemoveAll(installPath); nil != err {
-		logging.LogErrorf("remove theme [%s] failed: %s", installPath, err)
-		return errors.New("remove community theme failed")
-	}
-	//logging.Logger.Infof("uninstalled theme [%s]", installPath)
-	return nil
+	return uninstallPackage(installPath)
 }

+ 2 - 9
kernel/bazaar/widget.go

@@ -17,7 +17,6 @@
 package bazaar
 
 import (
-	"errors"
 	"os"
 	"path/filepath"
 	"sort"
@@ -26,7 +25,6 @@ import (
 
 	"github.com/dustin/go-humanize"
 	ants "github.com/panjf2000/ants/v2"
-	"github.com/siyuan-note/filelock"
 	"github.com/siyuan-note/httpclient"
 	"github.com/siyuan-note/logging"
 	"github.com/siyuan-note/siyuan/kernel/util"
@@ -180,14 +178,9 @@ func InstallWidget(repoURL, repoHash, installPath string, systemID string) error
 	if nil != err {
 		return err
 	}
-	return installPackage(data, installPath)
+	return installPackage(data, installPath, repoURLHash)
 }
 
 func UninstallWidget(installPath string) error {
-	if err := filelock.Remove(installPath); nil != err {
-		logging.LogErrorf("remove widget [%s] failed: %s", installPath, err)
-		return errors.New("remove community widget failed")
-	}
-	//logging.Logger.Infof("uninstalled widget [%s]", installPath)
-	return nil
+	return uninstallPackage(installPath)
 }

+ 11 - 18
kernel/model/bazzar.go

@@ -19,16 +19,17 @@ package model
 import (
 	"errors"
 	"fmt"
-	"github.com/88250/gulu"
-	"github.com/siyuan-note/logging"
-	"github.com/siyuan-note/siyuan/kernel/util"
 	"path"
 	"path/filepath"
 	"strings"
 	"sync"
 	"time"
 
+	"github.com/88250/gulu"
+	"github.com/siyuan-note/logging"
 	"github.com/siyuan-note/siyuan/kernel/bazaar"
+	"github.com/siyuan-note/siyuan/kernel/util"
+	"golang.org/x/mod/semver"
 )
 
 func BatchUpdateBazaarPackages(frontend string) {
@@ -202,9 +203,7 @@ func BazaarPlugins(frontend, keyword string) (plugins []*bazaar.Plugin) {
 		plugin.Installed = util.IsPathRegularDirOrSymlinkDir(filepath.Join(util.DataDir, "plugins", plugin.Name))
 		if plugin.Installed {
 			if pluginConf, err := bazaar.PluginJSON(plugin.Name); nil == err && nil != plugin {
-				if plugin.Version != pluginConf.Version {
-					plugin.Outdated = true
-				}
+				plugin.Outdated = 0 > semver.Compare("v"+pluginConf.Version, "v"+plugin.Version)
 			}
 		}
 	}
@@ -273,9 +272,7 @@ func BazaarWidgets(keyword string) (widgets []*bazaar.Widget) {
 		widget.Installed = util.IsPathRegularDirOrSymlinkDir(filepath.Join(util.DataDir, "widgets", widget.Name))
 		if widget.Installed {
 			if widgetConf, err := bazaar.WidgetJSON(widget.Name); nil == err && nil != widget {
-				if widget.Version != widgetConf.Version {
-					widget.Outdated = true
-				}
+				widget.Outdated = 0 > semver.Compare("v"+widgetConf.Version, "v"+widget.Version)
 			}
 		}
 	}
@@ -324,10 +321,8 @@ func BazaarIcons(keyword string) (icons []*bazaar.Icon) {
 		for _, icon := range icons {
 			if installed == icon.Name {
 				icon.Installed = true
-				if themeConf, err := bazaar.IconJSON(icon.Name); nil == err {
-					if icon.Version != themeConf.Version {
-						icon.Outdated = true
-					}
+				if iconConf, err := bazaar.IconJSON(icon.Name); nil == err {
+					icon.Outdated = 0 > semver.Compare("v"+iconConf.Version, "v"+icon.Version)
 				}
 			}
 			icon.Current = icon.Name == Conf.Appearance.Icon
@@ -389,7 +384,7 @@ func BazaarThemes(keyword string) (ret []*bazaar.Theme) {
 			if installed == theme.Name {
 				theme.Installed = true
 				if themeConf, err := bazaar.ThemeJSON(theme.Name); nil == err {
-					theme.Outdated = theme.Version != themeConf.Version
+					theme.Outdated = 0 > semver.Compare("v"+themeConf.Version, "v"+theme.Version)
 				}
 				theme.Current = theme.Name == Conf.Appearance.ThemeDark || theme.Name == Conf.Appearance.ThemeLight
 			}
@@ -462,10 +457,8 @@ func BazaarTemplates(keyword string) (templates []*bazaar.Template) {
 	for _, template := range templates {
 		template.Installed = util.IsPathRegularDirOrSymlinkDir(filepath.Join(util.DataDir, "templates", template.Name))
 		if template.Installed {
-			if themeConf, err := bazaar.TemplateJSON(template.Name); nil == err && nil != themeConf {
-				if template.Version != themeConf.Version {
-					template.Outdated = true
-				}
+			if templateConf, err := bazaar.TemplateJSON(template.Name); nil == err && nil != templateConf {
+				template.Outdated = 0 > semver.Compare("v"+templateConf.Version, "v"+template.Version)
 			}
 		}
 	}