mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-01-24 21:42:51 -05:00
themes: incorporate theme registry, browser, dms URI scheme handling
This commit is contained in:
@@ -131,6 +131,12 @@ func runOpen(target string) {
|
||||
detectedRequestType = "url"
|
||||
}
|
||||
log.Infof("Detected HTTP(S) URL")
|
||||
} else if strings.HasPrefix(target, "dms://") {
|
||||
// Handle DMS internal URLs (theme/plugin install, etc.)
|
||||
if detectedRequestType == "" {
|
||||
detectedRequestType = "url"
|
||||
}
|
||||
log.Infof("Detected DMS internal URL")
|
||||
} else if _, err := os.Stat(target); err == nil {
|
||||
// Handle local file paths directly (not file:// URIs)
|
||||
// Convert to absolute path
|
||||
@@ -177,7 +183,7 @@ func runOpen(target string) {
|
||||
}
|
||||
|
||||
method := "apppicker.open"
|
||||
if detectedMimeType == "" && len(detectedCategories) == 0 && (strings.HasPrefix(target, "http://") || strings.HasPrefix(target, "https://")) {
|
||||
if detectedMimeType == "" && len(detectedCategories) == 0 && (strings.HasPrefix(target, "http://") || strings.HasPrefix(target, "https://") || strings.HasPrefix(target, "dms://")) {
|
||||
method = "browser.open"
|
||||
params["url"] = target
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/server/models"
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/server/network"
|
||||
serverPlugins "github.com/AvengeMedia/DankMaterialShell/core/internal/server/plugins"
|
||||
serverThemes "github.com/AvengeMedia/DankMaterialShell/core/internal/server/themes"
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/server/wayland"
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/server/wlroutput"
|
||||
)
|
||||
@@ -37,6 +38,11 @@ func RouteRequest(conn net.Conn, req models.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasPrefix(req.Method, "themes.") {
|
||||
serverThemes.HandleRequest(conn, req)
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasPrefix(req.Method, "loginctl.") {
|
||||
if loginctlManager == nil {
|
||||
models.RespondError(conn, req.ID, "loginctl manager not initialized")
|
||||
|
||||
27
core/internal/server/themes/handlers.go
Normal file
27
core/internal/server/themes/handlers.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package themes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/server/models"
|
||||
)
|
||||
|
||||
func HandleRequest(conn net.Conn, req models.Request) {
|
||||
switch req.Method {
|
||||
case "themes.list":
|
||||
HandleList(conn, req)
|
||||
case "themes.listInstalled":
|
||||
HandleListInstalled(conn, req)
|
||||
case "themes.install":
|
||||
HandleInstall(conn, req)
|
||||
case "themes.uninstall":
|
||||
HandleUninstall(conn, req)
|
||||
case "themes.update":
|
||||
HandleUpdate(conn, req)
|
||||
case "themes.search":
|
||||
HandleSearch(conn, req)
|
||||
default:
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("unknown method: %s", req.Method))
|
||||
}
|
||||
}
|
||||
52
core/internal/server/themes/install.go
Normal file
52
core/internal/server/themes/install.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package themes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/server/models"
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/themes"
|
||||
)
|
||||
|
||||
func HandleInstall(conn net.Conn, req models.Request) {
|
||||
idOrName, ok := req.Params["name"].(string)
|
||||
if !ok {
|
||||
models.RespondError(conn, req.ID, "missing or invalid 'name' parameter")
|
||||
return
|
||||
}
|
||||
|
||||
registry, err := themes.NewRegistry()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to create registry: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
themeList, err := registry.List()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to list themes: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
theme := themes.FindByIDOrName(idOrName, themeList)
|
||||
if theme == nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("theme not found: %s", idOrName))
|
||||
return
|
||||
}
|
||||
|
||||
manager, err := themes.NewManager()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to create manager: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
registryThemeDir := registry.GetThemeDir(theme.SourceDir)
|
||||
if err := manager.Install(*theme, registryThemeDir); err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to install theme: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
models.Respond(conn, req.ID, models.SuccessResult{
|
||||
Success: true,
|
||||
Message: fmt.Sprintf("theme installed: %s", theme.Name),
|
||||
})
|
||||
}
|
||||
52
core/internal/server/themes/list.go
Normal file
52
core/internal/server/themes/list.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package themes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/server/models"
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/themes"
|
||||
)
|
||||
|
||||
func HandleList(conn net.Conn, req models.Request) {
|
||||
registry, err := themes.NewRegistry()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to create registry: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
themeList, err := registry.List()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to list themes: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
manager, err := themes.NewManager()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to create manager: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
result := make([]ThemeInfo, len(themeList))
|
||||
for i, t := range themeList {
|
||||
installed, _ := manager.IsInstalled(t)
|
||||
result[i] = ThemeInfo{
|
||||
ID: t.ID,
|
||||
Name: t.Name,
|
||||
Version: t.Version,
|
||||
Author: t.Author,
|
||||
Description: t.Description,
|
||||
PreviewPath: t.PreviewPath,
|
||||
SourceDir: t.SourceDir,
|
||||
Installed: installed,
|
||||
FirstParty: isFirstParty(t.Author),
|
||||
}
|
||||
}
|
||||
|
||||
models.Respond(conn, req.ID, result)
|
||||
}
|
||||
|
||||
func isFirstParty(author string) bool {
|
||||
return strings.EqualFold(author, "Avenge Media") || strings.EqualFold(author, "AvengeMedia")
|
||||
}
|
||||
82
core/internal/server/themes/list_installed.go
Normal file
82
core/internal/server/themes/list_installed.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package themes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/server/models"
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/themes"
|
||||
)
|
||||
|
||||
func HandleListInstalled(conn net.Conn, req models.Request) {
|
||||
manager, err := themes.NewManager()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to create manager: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
installedIDs, err := manager.ListInstalled()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to list installed themes: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
registry, err := themes.NewRegistry()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to create registry: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
allThemes, err := registry.List()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to list themes: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
themeMap := make(map[string]themes.Theme)
|
||||
for _, t := range allThemes {
|
||||
themeMap[t.ID] = t
|
||||
}
|
||||
|
||||
result := make([]ThemeInfo, 0, len(installedIDs))
|
||||
for _, id := range installedIDs {
|
||||
if theme, ok := themeMap[id]; ok {
|
||||
hasUpdate := false
|
||||
if hasUpdates, err := manager.HasUpdates(id, theme); err == nil {
|
||||
hasUpdate = hasUpdates
|
||||
}
|
||||
|
||||
result = append(result, ThemeInfo{
|
||||
ID: theme.ID,
|
||||
Name: theme.Name,
|
||||
Version: theme.Version,
|
||||
Author: theme.Author,
|
||||
Description: theme.Description,
|
||||
SourceDir: id,
|
||||
FirstParty: isFirstParty(theme.Author),
|
||||
HasUpdate: hasUpdate,
|
||||
})
|
||||
} else {
|
||||
installed, err := manager.GetInstalledTheme(id)
|
||||
if err != nil {
|
||||
result = append(result, ThemeInfo{
|
||||
ID: id,
|
||||
Name: id,
|
||||
SourceDir: id,
|
||||
})
|
||||
continue
|
||||
}
|
||||
result = append(result, ThemeInfo{
|
||||
ID: installed.ID,
|
||||
Name: installed.Name,
|
||||
Version: installed.Version,
|
||||
Author: installed.Author,
|
||||
Description: installed.Description,
|
||||
SourceDir: id,
|
||||
FirstParty: isFirstParty(installed.Author),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
models.Respond(conn, req.ID, result)
|
||||
}
|
||||
53
core/internal/server/themes/search.go
Normal file
53
core/internal/server/themes/search.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package themes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/server/models"
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/themes"
|
||||
)
|
||||
|
||||
func HandleSearch(conn net.Conn, req models.Request) {
|
||||
query, ok := req.Params["query"].(string)
|
||||
if !ok {
|
||||
models.RespondError(conn, req.ID, "missing or invalid 'query' parameter")
|
||||
return
|
||||
}
|
||||
|
||||
registry, err := themes.NewRegistry()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to create registry: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
themeList, err := registry.List()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to list themes: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
searchResults := themes.FuzzySearch(query, themeList)
|
||||
|
||||
manager, err := themes.NewManager()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to create manager: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
result := make([]ThemeInfo, len(searchResults))
|
||||
for i, t := range searchResults {
|
||||
installed, _ := manager.IsInstalled(t)
|
||||
result[i] = ThemeInfo{
|
||||
ID: t.ID,
|
||||
Name: t.Name,
|
||||
Version: t.Version,
|
||||
Author: t.Author,
|
||||
Description: t.Description,
|
||||
Installed: installed,
|
||||
FirstParty: isFirstParty(t.Author),
|
||||
}
|
||||
}
|
||||
|
||||
models.Respond(conn, req.ID, result)
|
||||
}
|
||||
14
core/internal/server/themes/types.go
Normal file
14
core/internal/server/themes/types.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package themes
|
||||
|
||||
type ThemeInfo struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Author string `json:"author,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
PreviewPath string `json:"previewPath,omitempty"`
|
||||
SourceDir string `json:"sourceDir,omitempty"`
|
||||
Installed bool `json:"installed,omitempty"`
|
||||
FirstParty bool `json:"firstParty,omitempty"`
|
||||
HasUpdate bool `json:"hasUpdate,omitempty"`
|
||||
}
|
||||
63
core/internal/server/themes/uninstall.go
Normal file
63
core/internal/server/themes/uninstall.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package themes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/server/models"
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/themes"
|
||||
)
|
||||
|
||||
func HandleUninstall(conn net.Conn, req models.Request) {
|
||||
idOrName, ok := req.Params["name"].(string)
|
||||
if !ok {
|
||||
models.RespondError(conn, req.ID, "missing or invalid 'name' parameter")
|
||||
return
|
||||
}
|
||||
|
||||
manager, err := themes.NewManager()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to create manager: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
registry, err := themes.NewRegistry()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to create registry: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
themeList, _ := registry.List()
|
||||
theme := themes.FindByIDOrName(idOrName, themeList)
|
||||
|
||||
if theme != nil {
|
||||
installed, err := manager.IsInstalled(*theme)
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to check if theme is installed: %v", err))
|
||||
return
|
||||
}
|
||||
if !installed {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("theme not installed: %s", idOrName))
|
||||
return
|
||||
}
|
||||
if err := manager.Uninstall(*theme); err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to uninstall theme: %v", err))
|
||||
return
|
||||
}
|
||||
models.Respond(conn, req.ID, models.SuccessResult{
|
||||
Success: true,
|
||||
Message: fmt.Sprintf("theme uninstalled: %s", theme.Name),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if err := manager.UninstallByID(idOrName); err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("theme not found: %s", idOrName))
|
||||
return
|
||||
}
|
||||
|
||||
models.Respond(conn, req.ID, models.SuccessResult{
|
||||
Success: true,
|
||||
Message: fmt.Sprintf("theme uninstalled: %s", idOrName),
|
||||
})
|
||||
}
|
||||
57
core/internal/server/themes/update.go
Normal file
57
core/internal/server/themes/update.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package themes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/server/models"
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/themes"
|
||||
)
|
||||
|
||||
func HandleUpdate(conn net.Conn, req models.Request) {
|
||||
idOrName, ok := req.Params["name"].(string)
|
||||
if !ok {
|
||||
models.RespondError(conn, req.ID, "missing or invalid 'name' parameter")
|
||||
return
|
||||
}
|
||||
|
||||
manager, err := themes.NewManager()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to create manager: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
registry, err := themes.NewRegistry()
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to create registry: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
themeList, _ := registry.List()
|
||||
theme := themes.FindByIDOrName(idOrName, themeList)
|
||||
|
||||
if theme == nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("theme not found in registry: %s", idOrName))
|
||||
return
|
||||
}
|
||||
|
||||
installed, err := manager.IsInstalled(*theme)
|
||||
if err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to check if theme is installed: %v", err))
|
||||
return
|
||||
}
|
||||
if !installed {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("theme not installed: %s", idOrName))
|
||||
return
|
||||
}
|
||||
|
||||
if err := manager.Update(*theme); err != nil {
|
||||
models.RespondError(conn, req.ID, fmt.Sprintf("failed to update theme: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
models.Respond(conn, req.ID, models.SuccessResult{
|
||||
Success: true,
|
||||
Message: fmt.Sprintf("theme updated: %s", theme.Name),
|
||||
})
|
||||
}
|
||||
244
core/internal/themes/manager.go
Normal file
244
core/internal/themes/manager.go
Normal file
@@ -0,0 +1,244 @@
|
||||
package themes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/log"
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
type Manager struct {
|
||||
fs afero.Fs
|
||||
themesDir string
|
||||
}
|
||||
|
||||
func NewManager() (*Manager, error) {
|
||||
return NewManagerWithFs(afero.NewOsFs())
|
||||
}
|
||||
|
||||
func NewManagerWithFs(fs afero.Fs) (*Manager, error) {
|
||||
themesDir := getThemesDir()
|
||||
return &Manager{
|
||||
fs: fs,
|
||||
themesDir: themesDir,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getThemesDir() string {
|
||||
configDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
log.Error("failed to get user config dir", "err", err)
|
||||
return ""
|
||||
}
|
||||
return filepath.Join(configDir, "DankMaterialShell", "themes")
|
||||
}
|
||||
|
||||
func (m *Manager) IsInstalled(theme Theme) (bool, error) {
|
||||
path := m.getInstalledPath(theme.ID)
|
||||
exists, err := afero.Exists(m.fs, path)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func (m *Manager) getInstalledDir(themeID string) string {
|
||||
return filepath.Join(m.themesDir, themeID)
|
||||
}
|
||||
|
||||
func (m *Manager) getInstalledPath(themeID string) string {
|
||||
return filepath.Join(m.getInstalledDir(themeID), "theme.json")
|
||||
}
|
||||
|
||||
func (m *Manager) Install(theme Theme, registryThemeDir string) error {
|
||||
themeDir := m.getInstalledDir(theme.ID)
|
||||
|
||||
exists, err := afero.DirExists(m.fs, themeDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to check if theme exists: %w", err)
|
||||
}
|
||||
|
||||
if exists {
|
||||
return fmt.Errorf("theme already installed: %s", theme.Name)
|
||||
}
|
||||
|
||||
if err := m.fs.MkdirAll(themeDir, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create theme directory: %w", err)
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(theme, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal theme: %w", err)
|
||||
}
|
||||
|
||||
themePath := filepath.Join(themeDir, "theme.json")
|
||||
if err := afero.WriteFile(m.fs, themePath, data, 0644); err != nil {
|
||||
return fmt.Errorf("failed to write theme file: %w", err)
|
||||
}
|
||||
|
||||
for _, preview := range []string{"preview-dark.svg", "preview-light.svg"} {
|
||||
srcPath := filepath.Join(registryThemeDir, preview)
|
||||
exists, _ := afero.Exists(m.fs, srcPath)
|
||||
if !exists {
|
||||
continue
|
||||
}
|
||||
data, err := afero.ReadFile(m.fs, srcPath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
dstPath := filepath.Join(themeDir, preview)
|
||||
_ = afero.WriteFile(m.fs, dstPath, data, 0644)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) InstallFromRegistry(registry *Registry, themeID string) error {
|
||||
theme, err := registry.Get(themeID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
registryThemeDir := registry.GetThemeDir(theme.SourceDir)
|
||||
return m.Install(*theme, registryThemeDir)
|
||||
}
|
||||
|
||||
func (m *Manager) Update(theme Theme) error {
|
||||
themePath := m.getInstalledPath(theme.ID)
|
||||
|
||||
exists, err := afero.Exists(m.fs, themePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to check if theme exists: %w", err)
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return fmt.Errorf("theme not installed: %s", theme.Name)
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(theme, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal theme: %w", err)
|
||||
}
|
||||
|
||||
if err := afero.WriteFile(m.fs, themePath, data, 0644); err != nil {
|
||||
return fmt.Errorf("failed to write theme file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) Uninstall(theme Theme) error {
|
||||
return m.UninstallByID(theme.ID)
|
||||
}
|
||||
|
||||
func (m *Manager) UninstallByID(themeID string) error {
|
||||
themeDir := m.getInstalledDir(themeID)
|
||||
|
||||
exists, err := afero.DirExists(m.fs, themeDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to check if theme exists: %w", err)
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return fmt.Errorf("theme not installed: %s", themeID)
|
||||
}
|
||||
|
||||
if err := m.fs.RemoveAll(themeDir); err != nil {
|
||||
return fmt.Errorf("failed to remove theme: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) ListInstalled() ([]string, error) {
|
||||
exists, err := afero.DirExists(m.fs, m.themesDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
entries, err := afero.ReadDir(m.fs, m.themesDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read themes directory: %w", err)
|
||||
}
|
||||
|
||||
var installed []string
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
themeID := entry.Name()
|
||||
themePath := filepath.Join(m.themesDir, themeID, "theme.json")
|
||||
if exists, _ := afero.Exists(m.fs, themePath); exists {
|
||||
installed = append(installed, themeID)
|
||||
}
|
||||
}
|
||||
|
||||
return installed, nil
|
||||
}
|
||||
|
||||
func (m *Manager) GetInstalledTheme(themeID string) (*Theme, error) {
|
||||
themePath := m.getInstalledPath(themeID)
|
||||
|
||||
data, err := afero.ReadFile(m.fs, themePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read theme file: %w", err)
|
||||
}
|
||||
|
||||
var theme Theme
|
||||
if err := json.Unmarshal(data, &theme); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse theme file: %w", err)
|
||||
}
|
||||
|
||||
return &theme, nil
|
||||
}
|
||||
|
||||
func (m *Manager) HasUpdates(themeID string, registryTheme Theme) (bool, error) {
|
||||
installed, err := m.GetInstalledTheme(themeID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return compareVersions(installed.Version, registryTheme.Version) < 0, nil
|
||||
}
|
||||
|
||||
func compareVersions(installed, registry string) int {
|
||||
installedParts := strings.Split(installed, ".")
|
||||
registryParts := strings.Split(registry, ".")
|
||||
|
||||
maxLen := len(installedParts)
|
||||
if len(registryParts) > maxLen {
|
||||
maxLen = len(registryParts)
|
||||
}
|
||||
|
||||
for i := 0; i < maxLen; i++ {
|
||||
var installedNum, registryNum int
|
||||
if i < len(installedParts) {
|
||||
fmt.Sscanf(installedParts[i], "%d", &installedNum)
|
||||
}
|
||||
if i < len(registryParts) {
|
||||
fmt.Sscanf(registryParts[i], "%d", ®istryNum)
|
||||
}
|
||||
|
||||
if installedNum < registryNum {
|
||||
return -1
|
||||
}
|
||||
if installedNum > registryNum {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *Manager) GetThemesDir() string {
|
||||
return m.themesDir
|
||||
}
|
||||
236
core/internal/themes/registry.go
Normal file
236
core/internal/themes/registry.go
Normal file
@@ -0,0 +1,236 @@
|
||||
package themes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-git/go-git/v6"
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
const registryRepo = "https://github.com/AvengeMedia/dms-plugin-registry.git"
|
||||
|
||||
type ColorScheme struct {
|
||||
Primary string `json:"primary"`
|
||||
PrimaryText string `json:"primaryText"`
|
||||
PrimaryContainer string `json:"primaryContainer"`
|
||||
Secondary string `json:"secondary"`
|
||||
Surface string `json:"surface"`
|
||||
SurfaceText string `json:"surfaceText"`
|
||||
SurfaceVariant string `json:"surfaceVariant"`
|
||||
SurfaceVariantText string `json:"surfaceVariantText"`
|
||||
SurfaceTint string `json:"surfaceTint"`
|
||||
Background string `json:"background"`
|
||||
BackgroundText string `json:"backgroundText"`
|
||||
Outline string `json:"outline"`
|
||||
SurfaceContainer string `json:"surfaceContainer"`
|
||||
SurfaceContainerHigh string `json:"surfaceContainerHigh"`
|
||||
Error string `json:"error"`
|
||||
Warning string `json:"warning"`
|
||||
Info string `json:"info"`
|
||||
}
|
||||
|
||||
type Theme struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Author string `json:"author"`
|
||||
Description string `json:"description"`
|
||||
Dark ColorScheme `json:"dark"`
|
||||
Light ColorScheme `json:"light"`
|
||||
PreviewPath string `json:"-"`
|
||||
SourceDir string `json:"sourceDir,omitempty"`
|
||||
}
|
||||
|
||||
type GitClient interface {
|
||||
PlainClone(path string, url string) error
|
||||
Pull(path string) error
|
||||
}
|
||||
|
||||
type realGitClient struct{}
|
||||
|
||||
func (g *realGitClient) PlainClone(path string, url string) error {
|
||||
_, err := git.PlainClone(path, &git.CloneOptions{
|
||||
URL: url,
|
||||
Progress: os.Stdout,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (g *realGitClient) Pull(path string) error {
|
||||
repo, err := git.PlainOpen(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
worktree, err := repo.Worktree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = worktree.Pull(&git.PullOptions{})
|
||||
if err != nil && err.Error() != "already up-to-date" {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Registry struct {
|
||||
fs afero.Fs
|
||||
cacheDir string
|
||||
themes []Theme
|
||||
git GitClient
|
||||
}
|
||||
|
||||
func NewRegistry() (*Registry, error) {
|
||||
return NewRegistryWithFs(afero.NewOsFs())
|
||||
}
|
||||
|
||||
func NewRegistryWithFs(fs afero.Fs) (*Registry, error) {
|
||||
cacheDir := getCacheDir()
|
||||
return &Registry{
|
||||
fs: fs,
|
||||
cacheDir: cacheDir,
|
||||
git: &realGitClient{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getCacheDir() string {
|
||||
return filepath.Join(os.TempDir(), "dankdots-plugin-registry")
|
||||
}
|
||||
|
||||
func (r *Registry) Update() error {
|
||||
exists, err := afero.DirExists(r.fs, r.cacheDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to check cache directory: %w", err)
|
||||
}
|
||||
|
||||
if !exists {
|
||||
if err := r.fs.MkdirAll(filepath.Dir(r.cacheDir), 0755); err != nil {
|
||||
return fmt.Errorf("failed to create cache directory: %w", err)
|
||||
}
|
||||
|
||||
if err := r.git.PlainClone(r.cacheDir, registryRepo); err != nil {
|
||||
return fmt.Errorf("failed to clone registry: %w", err)
|
||||
}
|
||||
} else {
|
||||
if err := r.git.Pull(r.cacheDir); err != nil {
|
||||
if err := r.fs.RemoveAll(r.cacheDir); err != nil {
|
||||
return fmt.Errorf("failed to remove corrupted registry: %w", err)
|
||||
}
|
||||
|
||||
if err := r.fs.MkdirAll(filepath.Dir(r.cacheDir), 0755); err != nil {
|
||||
return fmt.Errorf("failed to create cache directory: %w", err)
|
||||
}
|
||||
|
||||
if err := r.git.PlainClone(r.cacheDir, registryRepo); err != nil {
|
||||
return fmt.Errorf("failed to re-clone registry: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return r.loadThemes()
|
||||
}
|
||||
|
||||
func (r *Registry) loadThemes() error {
|
||||
themesDir := filepath.Join(r.cacheDir, "themes")
|
||||
|
||||
entries, err := afero.ReadDir(r.fs, themesDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read themes directory: %w", err)
|
||||
}
|
||||
|
||||
r.themes = []Theme{}
|
||||
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
themeDir := filepath.Join(themesDir, entry.Name())
|
||||
themeFile := filepath.Join(themeDir, "theme.json")
|
||||
|
||||
data, err := afero.ReadFile(r.fs, themeFile)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
var theme Theme
|
||||
if err := json.Unmarshal(data, &theme); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if theme.ID == "" {
|
||||
theme.ID = entry.Name()
|
||||
}
|
||||
theme.SourceDir = entry.Name()
|
||||
|
||||
previewPath := filepath.Join(themeDir, "preview.svg")
|
||||
if exists, _ := afero.Exists(r.fs, previewPath); exists {
|
||||
theme.PreviewPath = previewPath
|
||||
}
|
||||
|
||||
r.themes = append(r.themes, theme)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registry) List() ([]Theme, error) {
|
||||
if len(r.themes) == 0 {
|
||||
if err := r.Update(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return SortByFirstParty(r.themes), nil
|
||||
}
|
||||
|
||||
func (r *Registry) Search(query string) ([]Theme, error) {
|
||||
allThemes, err := r.List()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if query == "" {
|
||||
return allThemes, nil
|
||||
}
|
||||
|
||||
return SortByFirstParty(FuzzySearch(query, allThemes)), nil
|
||||
}
|
||||
|
||||
func (r *Registry) Get(idOrName string) (*Theme, error) {
|
||||
themes, err := r.List()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, t := range themes {
|
||||
if t.ID == idOrName {
|
||||
return &t, nil
|
||||
}
|
||||
}
|
||||
|
||||
for _, t := range themes {
|
||||
if t.Name == idOrName {
|
||||
return &t, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("theme not found: %s", idOrName)
|
||||
}
|
||||
|
||||
func (r *Registry) GetThemeSourcePath(themeID string) string {
|
||||
return filepath.Join(r.cacheDir, "themes", themeID, "theme.json")
|
||||
}
|
||||
|
||||
func (r *Registry) GetThemeDir(themeID string) string {
|
||||
return filepath.Join(r.cacheDir, "themes", themeID)
|
||||
}
|
||||
|
||||
func SortByFirstParty(themes []Theme) []Theme {
|
||||
return themes
|
||||
}
|
||||
40
core/internal/themes/search.go
Normal file
40
core/internal/themes/search.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package themes
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/utils"
|
||||
)
|
||||
|
||||
func FuzzySearch(query string, themes []Theme) []Theme {
|
||||
if query == "" {
|
||||
return themes
|
||||
}
|
||||
|
||||
queryLower := strings.ToLower(query)
|
||||
return utils.Filter(themes, func(t Theme) bool {
|
||||
return fuzzyMatch(queryLower, strings.ToLower(t.Name)) ||
|
||||
fuzzyMatch(queryLower, strings.ToLower(t.Description)) ||
|
||||
fuzzyMatch(queryLower, strings.ToLower(t.Author))
|
||||
})
|
||||
}
|
||||
|
||||
func fuzzyMatch(query, text string) bool {
|
||||
queryIdx := 0
|
||||
for _, char := range text {
|
||||
if queryIdx < len(query) && char == rune(query[queryIdx]) {
|
||||
queryIdx++
|
||||
}
|
||||
}
|
||||
return queryIdx == len(query)
|
||||
}
|
||||
|
||||
func FindByIDOrName(idOrName string, themes []Theme) *Theme {
|
||||
if t, found := utils.Find(themes, func(t Theme) bool { return t.ID == idOrName }); found {
|
||||
return &t
|
||||
}
|
||||
if t, found := utils.Find(themes, func(t Theme) bool { return t.Name == idOrName }); found {
|
||||
return &t
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user