From f5a9c2d31646667680af2191b29813680f1da1e8 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 29 May 2023 21:17:24 +0800 Subject: [PATCH 1/3] :art: Add plugin config items `backends` and `frontends` https://github.com/siyuan-note/siyuan/issues/8386 Improve plugin load performance https://github.com/siyuan-note/siyuan/issues/8397 --- kernel/api/bazaar.go | 5 ++--- kernel/api/plugin.go | 18 ++++++++++++++++-- kernel/bazaar/plugin.go | 39 +++++++++++++++++++++++++++++++++++++-- kernel/model/bazzar.go | 8 ++++---- kernel/model/plugin.go | 39 +++++++++++++++++++-------------------- 5 files changed, 78 insertions(+), 31 deletions(-) diff --git a/kernel/api/bazaar.go b/kernel/api/bazaar.go index b1f663add..7252e2240 100644 --- a/kernel/api/bazaar.go +++ b/kernel/api/bazaar.go @@ -110,16 +110,15 @@ func uninstallBazaarPlugin(c *gin.Context) { return } + frontend := arg["frontend"].(string) packageName := arg["packageName"].(string) - err := model.UninstallBazaarPlugin(packageName) + err := model.UninstallBazaarPlugin(packageName, frontend) if nil != err { ret.Code = -1 ret.Msg = err.Error() return } - frontend := arg["frontend"].(string) - ret.Data = map[string]interface{}{ "packages": model.BazaarPlugins(frontend), } diff --git a/kernel/api/plugin.go b/kernel/api/plugin.go index 3d59db613..9f6be4147 100644 --- a/kernel/api/plugin.go +++ b/kernel/api/plugin.go @@ -29,7 +29,14 @@ func loadPetals(c *gin.Context) { ret := gulu.Ret.NewResult() defer c.JSON(http.StatusOK, ret) - petals := model.LoadPetals() + arg, ok := util.JsonArg(c, ret) + if !ok { + return + } + + frontend := arg["frontend"].(string) + + petals := model.LoadPetals(frontend) ret.Data = petals } @@ -45,5 +52,12 @@ func setPetalEnabled(c *gin.Context) { packageName := arg["packageName"].(string) enabled := arg["enabled"].(bool) frontend := arg["frontend"].(string) - ret.Data = model.SetPetalEnabled(packageName, enabled, frontend) + data, err := model.SetPetalEnabled(packageName, enabled, frontend) + if nil != err { + ret.Code = -1 + ret.Msg = err.Error() + return + } + + ret.Data = data } diff --git a/kernel/bazaar/plugin.go b/kernel/bazaar/plugin.go index 082697cb5..b7602604e 100644 --- a/kernel/bazaar/plugin.go +++ b/kernel/bazaar/plugin.go @@ -108,7 +108,39 @@ func Plugins(frontend string) (plugins []*Plugin) { return } -func InstalledPlugins(frontend string) (ret []*Plugin) { +func IsIncompatibleInstalledPlugin(name, frontend string) (found, incompatible bool) { + pluginsPath := filepath.Join(util.DataDir, "plugins") + if !util.IsPathRegularDirOrSymlinkDir(pluginsPath) { + return + } + + pluginDirs, err := os.ReadDir(pluginsPath) + if nil != err { + logging.LogWarnf("read plugins folder failed: %s", err) + return + } + + for _, pluginDir := range pluginDirs { + if !util.IsDirRegularOrSymlink(pluginDir) { + continue + } + dirName := pluginDir.Name() + if name != dirName { + continue + } + + plugin, parseErr := PluginJSON(dirName) + if nil != parseErr || nil == plugin { + return + } + + found = true + incompatible = isIncompatiblePlugin(plugin, frontend) + } + return +} + +func InstalledPlugins(frontend string, checkUpdate bool) (ret []*Plugin) { ret = []*Plugin{} pluginsPath := filepath.Join(util.DataDir, "plugins") @@ -122,7 +154,10 @@ func InstalledPlugins(frontend string) (ret []*Plugin) { return } - bazaarPlugins := Plugins(frontend) + var bazaarPlugins []*Plugin + if checkUpdate { + bazaarPlugins = Plugins(frontend) + } for _, pluginDir := range pluginDirs { if !util.IsDirRegularOrSymlink(pluginDir) { diff --git a/kernel/model/bazzar.go b/kernel/model/bazzar.go index b19c344bd..271766166 100644 --- a/kernel/model/bazzar.go +++ b/kernel/model/bazzar.go @@ -50,9 +50,9 @@ func BazaarPlugins(frontend string) (plugins []*bazaar.Plugin) { } func InstalledPlugins(frontend string) (plugins []*bazaar.Plugin) { - plugins = bazaar.InstalledPlugins(frontend) + plugins = bazaar.InstalledPlugins(frontend, true) - petals := getPetals() + petals := getPetals(frontend) for _, plugin := range plugins { petal := getPetalByName(plugin.Name, petals) if nil != petal { @@ -71,14 +71,14 @@ func InstallBazaarPlugin(repoURL, repoHash, pluginName string) error { return nil } -func UninstallBazaarPlugin(pluginName string) error { +func UninstallBazaarPlugin(pluginName, frontend string) error { installPath := filepath.Join(util.DataDir, "plugins", pluginName) err := bazaar.UninstallPlugin(installPath) if nil != err { return errors.New(fmt.Sprintf(Conf.Language(47), err.Error())) } - petals := getPetals() + petals := getPetals(frontend) var tmp []*Petal for i, petal := range petals { if petal.Name != pluginName { diff --git a/kernel/model/plugin.go b/kernel/model/plugin.go index fe2b6f37a..6467cc521 100644 --- a/kernel/model/plugin.go +++ b/kernel/model/plugin.go @@ -17,6 +17,7 @@ package model import ( + "fmt" "os" "path/filepath" "sync" @@ -30,52 +31,50 @@ import ( // Petal represents a plugin's management status. type Petal struct { - Name string `json:"name"` // Plugin name - Enabled bool `json:"enabled"` // Whether enabled + Name string `json:"name"` // Plugin name + Enabled bool `json:"enabled"` // Whether enabled + Incompatible bool `json:"incompatible"` // Whether incompatible JS string `json:"js"` // JS code CSS string `json:"css"` // CSS code I18n map[string]interface{} `json:"i18n"` // i18n text } -func SetPetalEnabled(name string, enabled bool, frontend string) (ret *Petal) { +func SetPetalEnabled(name string, enabled bool, frontend string) (ret *Petal, err error) { petals := getPetals() - plugins := bazaar.InstalledPlugins(frontend) - var plugin *bazaar.Plugin - for _, p := range plugins { - if p.Name == name { - plugin = p - break - } - } - if nil == plugin { + found, incompatible := bazaar.IsIncompatibleInstalledPlugin(name, frontend) + if !found { logging.LogErrorf("plugin [%s] not found", name) return } - ret = getPetalByName(plugin.Name, petals) + ret = getPetalByName(name, petals) if nil == ret { ret = &Petal{ - Name: plugin.Name, - Enabled: enabled, + Name: name, } petals = append(petals, ret) - } else { - ret.Enabled = enabled + } + ret.Enabled = enabled + ret.Incompatible = incompatible + + if incompatible { + err = fmt.Errorf("plugin [%s] is incompatible", name) + return } savePetals(petals) - loadCode(ret) return } -func LoadPetals() (ret []*Petal) { +func LoadPetals(frontend string) (ret []*Petal) { ret = []*Petal{} petals := getPetals() for _, petal := range petals { - if !petal.Enabled { + _, petal.Incompatible = bazaar.IsIncompatibleInstalledPlugin(petal.Name, frontend) + if !petal.Enabled || petal.Incompatible { continue } From bd9affef7f0350010abf388de74d2355410cc8ed Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 29 May 2023 21:18:26 +0800 Subject: [PATCH 2/3] :art: Add plugin config items `backends` and `frontends` https://github.com/siyuan-note/siyuan/issues/8386 Improve plugin load performance https://github.com/siyuan-note/siyuan/issues/8397 --- kernel/model/bazzar.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/model/bazzar.go b/kernel/model/bazzar.go index 271766166..fe3ad56e7 100644 --- a/kernel/model/bazzar.go +++ b/kernel/model/bazzar.go @@ -52,7 +52,7 @@ func BazaarPlugins(frontend string) (plugins []*bazaar.Plugin) { func InstalledPlugins(frontend string) (plugins []*bazaar.Plugin) { plugins = bazaar.InstalledPlugins(frontend, true) - petals := getPetals(frontend) + petals := getPetals() for _, plugin := range plugins { petal := getPetalByName(plugin.Name, petals) if nil != petal { @@ -78,7 +78,7 @@ func UninstallBazaarPlugin(pluginName, frontend string) error { return errors.New(fmt.Sprintf(Conf.Language(47), err.Error())) } - petals := getPetals(frontend) + petals := getPetals() var tmp []*Petal for i, petal := range petals { if petal.Name != pluginName { From e5413297903d573c582861da8b70a2baaca31039 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 29 May 2023 21:21:18 +0800 Subject: [PATCH 3/3] :art: Add plugin config items `backends` and `frontends` https://github.com/siyuan-note/siyuan/issues/8386 Improve plugin load performance https://github.com/siyuan-note/siyuan/issues/8397 --- app/appearance/langs/en_US.json | 3 ++- app/appearance/langs/es_ES.json | 3 ++- app/appearance/langs/fr_FR.json | 3 ++- app/appearance/langs/zh_CHT.json | 3 ++- app/appearance/langs/zh_CN.json | 3 ++- kernel/model/plugin.go | 2 +- 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/appearance/langs/en_US.json b/app/appearance/langs/en_US.json index dc7d82d34..f279a212a 100644 --- a/app/appearance/langs/en_US.json +++ b/app/appearance/langs/en_US.json @@ -1160,6 +1160,7 @@ "201": "Failed to purge data repo: %s", "202": "Cleaning data repo...", "203": "The data repo is purged, [%d] snapshots and [%d] data objects have been deleted, and a total of [%s] disk space has been released", - "204": "The doc in the user guide does not support sharing to the community" + "204": "The doc in the user guide does not support sharing to the community", + "205": "The plugin is not supported in the current environment" } } diff --git a/app/appearance/langs/es_ES.json b/app/appearance/langs/es_ES.json index ff38493dd..7f4a12153 100644 --- a/app/appearance/langs/es_ES.json +++ b/app/appearance/langs/es_ES.json @@ -1160,6 +1160,7 @@ "201": "Error al purgar el repositorio de datos: %s", "202": "Limpiando repositorio de datos...", "203": "El repositorio de datos se purgó, [%d] instantáneas y [%d] objetos de datos se eliminaron y se liberó un total de [%s] espacio en disco", - "204": "La documentación en la guía del usuario no permite compartir con la comunidad" + "204": "La documentación en la guía del usuario no permite compartir con la comunidad", + "205": "El complemento no es compatible con el entorno actual" } } diff --git a/app/appearance/langs/fr_FR.json b/app/appearance/langs/fr_FR.json index 46bb9a000..c3d862344 100644 --- a/app/appearance/langs/fr_FR.json +++ b/app/appearance/langs/fr_FR.json @@ -1160,6 +1160,7 @@ "201": "Échec de la purge du référentiel de données : %s", "202": "Nettoyage du référentiel de données...", "203": "Le référentiel de données est purgé, [%d] instantanés et [%d] objets de données ont été supprimés, et un total de [%s] espace disque a été libéré", - "204": "La documentation du guide de l'utilisateur ne prend pas en charge le partage avec la communauté" + "204": "La documentation du guide de l'utilisateur ne prend pas en charge le partage avec la communauté", + "205": "Le plugin n'est pas pris en charge dans l'environnement actuel" } } diff --git a/app/appearance/langs/zh_CHT.json b/app/appearance/langs/zh_CHT.json index 13a3bbcca..ad7ad7c3c 100644 --- a/app/appearance/langs/zh_CHT.json +++ b/app/appearance/langs/zh_CHT.json @@ -1160,6 +1160,7 @@ "201": "清理數據倉庫失敗:%s", "202": "正在清理數據倉庫...", "203": "數據倉庫清理完畢,已刪除 [%d] 個快照和 [%d] 個數據對象,共釋放 [%s] 磁盤空間", - "204": "用戶指南中的文檔不支持分享到社區" + "204": "用戶指南中的文檔不支持分享到社區", + "205": "該插件不支持在當前環境下使用" } } diff --git a/app/appearance/langs/zh_CN.json b/app/appearance/langs/zh_CN.json index 0a1828a3f..aa56ef8be 100644 --- a/app/appearance/langs/zh_CN.json +++ b/app/appearance/langs/zh_CN.json @@ -1162,6 +1162,7 @@ "201": "清理数据仓库失败:%s", "202": "正在清理数据仓库...", "203": "数据仓库清理完毕,已删除 [%d] 个快照和 [%d] 个数据对象,共释放 [%s] 磁盘空间", - "204": "用户指南中的文档不支持分享到社区" + "204": "用户指南中的文档不支持分享到社区", + "205": "该插件不支持在当前环境下使用" } } diff --git a/kernel/model/plugin.go b/kernel/model/plugin.go index 6467cc521..b6e3a4411 100644 --- a/kernel/model/plugin.go +++ b/kernel/model/plugin.go @@ -60,7 +60,7 @@ func SetPetalEnabled(name string, enabled bool, frontend string) (ret *Petal, er ret.Incompatible = incompatible if incompatible { - err = fmt.Errorf("plugin [%s] is incompatible", name) + err = fmt.Errorf(Conf.Language(205)) return }