Skip to content

Commit

Permalink
🎨 集市支持已安装的包单独显示 #5678
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Sep 1, 2022
1 parent 6072f8a commit 4585cff
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
38 changes: 37 additions & 1 deletion kernel/bazaar/package.go
Expand Up @@ -62,6 +62,28 @@ type Package struct {
Downloads int `json:"downloads"`
}

func WidgetJSON(widgetDirName string) (ret map[string]interface{}, err error) {
p := filepath.Join(util.DataDir, widgetDirName, "widget.json")
if !gulu.File.IsExist(p) {
err = os.ErrNotExist
return
}
data, err := os.ReadFile(p)
if nil != err {
logging.LogErrorf("read widget.json [%s] failed: %s", p, err)
return
}
if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
logging.LogErrorf("parse widget.json [%s] failed: %s", p, err)
return
}
if 4 > len(ret) {
logging.LogWarnf("invalid widget.json [%s]", p)
return nil, errors.New("invalid widget.json")
}
return
}

func IconJSON(iconDirName string) (ret map[string]interface{}, err error) {
p := filepath.Join(util.ThemesPath, iconDirName, "icon.json")
if !gulu.File.IsExist(p) {
Expand All @@ -85,7 +107,7 @@ func IconJSON(iconDirName string) (ret map[string]interface{}, err error) {
}

func TemplateJSON(templateDirName string) (ret map[string]interface{}, err error) {
p := filepath.Join(util.ThemesPath, templateDirName, "template.json")
p := filepath.Join(util.DataDir, templateDirName, "template.json")
if !gulu.File.IsExist(p) {
err = os.ErrNotExist
return
Expand Down Expand Up @@ -178,6 +200,20 @@ func isOutdatedIcon(fullURL, version string, bazaarIcons []*Icon) bool {
return false
}

func isOutdatedWidget(fullURL, version string, bazaarWidgets []*Widget) bool {
if !strings.HasPrefix(fullURL, "https://github.com/") {
return false
}

url := strings.TrimPrefix(fullURL, "https://github.com/")
for _, pkg := range bazaarWidgets {
if url == pkg.URL && version != pkg.Version {
return true
}
}
return false
}

func isOutdatedTemplate(fullURL, version string, bazaarTemplates []*Template) bool {
if !strings.HasPrefix(fullURL, "https://github.com/") {
return false
Expand Down
52 changes: 52 additions & 0 deletions kernel/bazaar/widget.go
Expand Up @@ -19,10 +19,12 @@ package bazaar
import (
"errors"
"os"
"path/filepath"
"sort"
"strings"
"sync"

"github.com/88250/gulu"
"github.com/dustin/go-humanize"
ants "github.com/panjf2000/ants/v2"
"github.com/siyuan-note/httpclient"
Expand Down Expand Up @@ -94,6 +96,56 @@ func Widgets() (widgets []*Widget) {
return
}

func InstalledWidgets() (ret []*Icon) {
dir, err := os.Open(filepath.Join(util.DataDir, "widgets"))
if nil != err {
logging.LogWarnf("open widgets folder [%s] failed: %s", util.ThemesPath, err)
return
}
widgetDirs, err := dir.Readdir(-1)
if nil != err {
logging.LogWarnf("read widgets folder failed: %s", err)
return
}
dir.Close()

bazaarWidgets := Widgets()

for _, widgetDir := range widgetDirs {
if !widgetDir.IsDir() {
continue
}
dirName := widgetDir.Name()

widgetConf, parseErr := WidgetJSON(dirName)
if nil != parseErr || nil == widgetConf {
continue
}

icon := &Icon{}
icon.Name = widgetConf["name"].(string)
icon.Author = widgetConf["author"].(string)
icon.URL = widgetConf["url"].(string)
icon.Version = widgetConf["version"].(string)
icon.RepoURL = icon.URL
icon.PreviewURL = "/widgets/" + dirName + "/preview.png"
icon.PreviewURLThumb = "/widgets/" + dirName + "/preview.png"
icon.Updated = widgetDir.ModTime().Format("2006-01-02 15:04:05")
icon.Size = widgetDir.Size()
icon.HSize = humanize.Bytes(uint64(icon.Size))
icon.HUpdated = formatUpdated(icon.Updated)
readme, readErr := os.ReadFile(filepath.Join(util.DataDir, "widgets", dirName, "README.md"))
if nil != readErr {
logging.LogWarnf("read install icon README.md failed: %s", readErr)
continue
}
icon.README = gulu.Str.FromBytes(readme)
icon.Outdated = isOutdatedWidget(icon.URL, icon.Version, bazaarWidgets)
ret = append(ret, icon)
}
return
}

func InstallWidget(repoURL, repoHash, installPath string, systemID string) error {
repoURLHash := repoURL + "@" + repoHash
data, err := downloadPackage(repoURLHash, true, systemID)
Expand Down

0 comments on commit 4585cff

Please sign in to comment.