bazzar.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package model
  17. import (
  18. "errors"
  19. "fmt"
  20. "path/filepath"
  21. "github.com/88250/gulu"
  22. "github.com/siyuan-note/siyuan/kernel/util"
  23. "github.com/siyuan-note/siyuan/kernel/bazaar"
  24. )
  25. func GetPackageREADME(repoURL, repoHash, packageType string) (ret string) {
  26. ret = bazaar.GetPackageREADME(repoURL, repoHash, packageType)
  27. return
  28. }
  29. func BazaarPlugins(frontend string) (plugins []*bazaar.Plugin) {
  30. plugins = bazaar.Plugins(frontend)
  31. for _, plugin := range plugins {
  32. plugin.Installed = util.IsPathRegularDirOrSymlinkDir(filepath.Join(util.DataDir, "plugins", plugin.Name))
  33. if plugin.Installed {
  34. if plugin.Installed {
  35. if pluginConf, err := bazaar.PluginJSON(plugin.Name); nil == err && nil != plugin {
  36. if plugin.Version != pluginConf.Version {
  37. plugin.Outdated = true
  38. }
  39. }
  40. }
  41. }
  42. }
  43. return
  44. }
  45. func InstalledPlugins(frontend string) (plugins []*bazaar.Plugin) {
  46. plugins = bazaar.InstalledPlugins(frontend, true)
  47. petals := getPetals()
  48. for _, plugin := range plugins {
  49. petal := getPetalByName(plugin.Name, petals)
  50. if nil != petal {
  51. plugin.Enabled = petal.Enabled
  52. }
  53. }
  54. return
  55. }
  56. func InstallBazaarPlugin(repoURL, repoHash, pluginName string) error {
  57. installPath := filepath.Join(util.DataDir, "plugins", pluginName)
  58. err := bazaar.InstallPlugin(repoURL, repoHash, installPath, Conf.System.ID)
  59. if nil != err {
  60. return errors.New(fmt.Sprintf(Conf.Language(46), pluginName, err))
  61. }
  62. return nil
  63. }
  64. func UninstallBazaarPlugin(pluginName, frontend string) error {
  65. installPath := filepath.Join(util.DataDir, "plugins", pluginName)
  66. err := bazaar.UninstallPlugin(installPath)
  67. if nil != err {
  68. return errors.New(fmt.Sprintf(Conf.Language(47), err.Error()))
  69. }
  70. petals := getPetals()
  71. var tmp []*Petal
  72. for i, petal := range petals {
  73. if petal.Name != pluginName {
  74. tmp = append(tmp, petals[i])
  75. }
  76. }
  77. petals = tmp
  78. if 1 > len(petals) {
  79. petals = []*Petal{}
  80. }
  81. savePetals(petals)
  82. return nil
  83. }
  84. func BazaarWidgets() (widgets []*bazaar.Widget) {
  85. widgets = bazaar.Widgets()
  86. for _, widget := range widgets {
  87. widget.Installed = util.IsPathRegularDirOrSymlinkDir(filepath.Join(util.DataDir, "widgets", widget.Name))
  88. if widget.Installed {
  89. if widget.Installed {
  90. if widgetConf, err := bazaar.WidgetJSON(widget.Name); nil == err && nil != widget {
  91. if widget.Version != widgetConf.Version {
  92. widget.Outdated = true
  93. }
  94. }
  95. }
  96. }
  97. }
  98. return
  99. }
  100. func InstalledWidgets() (widgets []*bazaar.Widget) {
  101. widgets = bazaar.InstalledWidgets()
  102. return
  103. }
  104. func InstallBazaarWidget(repoURL, repoHash, widgetName string) error {
  105. installPath := filepath.Join(util.DataDir, "widgets", widgetName)
  106. err := bazaar.InstallWidget(repoURL, repoHash, installPath, Conf.System.ID)
  107. if nil != err {
  108. return errors.New(fmt.Sprintf(Conf.Language(46), widgetName, err))
  109. }
  110. return nil
  111. }
  112. func UninstallBazaarWidget(widgetName string) error {
  113. installPath := filepath.Join(util.DataDir, "widgets", widgetName)
  114. err := bazaar.UninstallWidget(installPath)
  115. if nil != err {
  116. return errors.New(fmt.Sprintf(Conf.Language(47), err.Error()))
  117. }
  118. return nil
  119. }
  120. func BazaarIcons() (icons []*bazaar.Icon) {
  121. icons = bazaar.Icons()
  122. for _, installed := range Conf.Appearance.Icons {
  123. for _, icon := range icons {
  124. if installed == icon.Name {
  125. icon.Installed = true
  126. if themeConf, err := bazaar.IconJSON(icon.Name); nil == err {
  127. if icon.Version != themeConf.Version {
  128. icon.Outdated = true
  129. }
  130. }
  131. }
  132. icon.Current = icon.Name == Conf.Appearance.Icon
  133. }
  134. }
  135. return
  136. }
  137. func InstalledIcons() (icons []*bazaar.Icon) {
  138. icons = bazaar.InstalledIcons()
  139. for _, icon := range icons {
  140. icon.Current = icon.Name == Conf.Appearance.Icon
  141. }
  142. return
  143. }
  144. func InstallBazaarIcon(repoURL, repoHash, iconName string) error {
  145. installPath := filepath.Join(util.IconsPath, iconName)
  146. err := bazaar.InstallIcon(repoURL, repoHash, installPath, Conf.System.ID)
  147. if nil != err {
  148. return errors.New(fmt.Sprintf(Conf.Language(46), iconName, err))
  149. }
  150. Conf.Appearance.Icon = iconName
  151. Conf.Save()
  152. InitAppearance()
  153. return nil
  154. }
  155. func UninstallBazaarIcon(iconName string) error {
  156. installPath := filepath.Join(util.IconsPath, iconName)
  157. err := bazaar.UninstallIcon(installPath)
  158. if nil != err {
  159. return errors.New(fmt.Sprintf(Conf.Language(47), err.Error()))
  160. }
  161. InitAppearance()
  162. return nil
  163. }
  164. func BazaarThemes() (ret []*bazaar.Theme) {
  165. ret = bazaar.Themes()
  166. installs := Conf.Appearance.DarkThemes
  167. installs = append(installs, Conf.Appearance.LightThemes...)
  168. for _, installed := range installs {
  169. for _, theme := range ret {
  170. if installed == theme.Name {
  171. theme.Installed = true
  172. if themeConf, err := bazaar.ThemeJSON(theme.Name); nil == err {
  173. theme.Outdated = theme.Version != themeConf.Version
  174. }
  175. theme.Current = theme.Name == Conf.Appearance.ThemeDark || theme.Name == Conf.Appearance.ThemeLight
  176. }
  177. }
  178. }
  179. return
  180. }
  181. func InstalledThemes() (ret []*bazaar.Theme) {
  182. ret = bazaar.InstalledThemes()
  183. for _, theme := range ret {
  184. theme.Current = theme.Name == Conf.Appearance.ThemeDark || theme.Name == Conf.Appearance.ThemeLight
  185. }
  186. return
  187. }
  188. func InstallBazaarTheme(repoURL, repoHash, themeName string, mode int, update bool) error {
  189. closeThemeWatchers()
  190. installPath := filepath.Join(util.ThemesPath, themeName)
  191. err := bazaar.InstallTheme(repoURL, repoHash, installPath, Conf.System.ID)
  192. if nil != err {
  193. return errors.New(fmt.Sprintf(Conf.Language(46), themeName, err))
  194. }
  195. if !update {
  196. // 更新主题后不需要对该主题进行切换 https://github.com/siyuan-note/siyuan/issues/4966
  197. if 0 == mode {
  198. Conf.Appearance.ThemeLight = themeName
  199. } else {
  200. Conf.Appearance.ThemeDark = themeName
  201. }
  202. Conf.Appearance.Mode = mode
  203. Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(installPath, "theme.js"))
  204. Conf.Save()
  205. }
  206. InitAppearance()
  207. return nil
  208. }
  209. func UninstallBazaarTheme(themeName string) error {
  210. closeThemeWatchers()
  211. installPath := filepath.Join(util.ThemesPath, themeName)
  212. err := bazaar.UninstallTheme(installPath)
  213. if nil != err {
  214. return errors.New(fmt.Sprintf(Conf.Language(47), err.Error()))
  215. }
  216. InitAppearance()
  217. return nil
  218. }
  219. func BazaarTemplates() (templates []*bazaar.Template) {
  220. templates = bazaar.Templates()
  221. for _, template := range templates {
  222. template.Installed = util.IsPathRegularDirOrSymlinkDir(filepath.Join(util.DataDir, "templates", template.Name))
  223. if template.Installed {
  224. if themeConf, err := bazaar.TemplateJSON(template.Name); nil == err && nil != themeConf {
  225. if template.Version != themeConf.Version {
  226. template.Outdated = true
  227. }
  228. }
  229. }
  230. }
  231. return
  232. }
  233. func InstalledTemplates() (templates []*bazaar.Template) {
  234. templates = bazaar.InstalledTemplates()
  235. return
  236. }
  237. func InstallBazaarTemplate(repoURL, repoHash, templateName string) error {
  238. installPath := filepath.Join(util.DataDir, "templates", templateName)
  239. err := bazaar.InstallTemplate(repoURL, repoHash, installPath, Conf.System.ID)
  240. if nil != err {
  241. return errors.New(fmt.Sprintf(Conf.Language(46), templateName, err))
  242. }
  243. return nil
  244. }
  245. func UninstallBazaarTemplate(templateName string) error {
  246. installPath := filepath.Join(util.DataDir, "templates", templateName)
  247. err := bazaar.UninstallTemplate(installPath)
  248. if nil != err {
  249. return errors.New(fmt.Sprintf(Conf.Language(47), err.Error()))
  250. }
  251. return nil
  252. }