Plugins: Simplify assetpath logic (#107876)

* simplify assetpath logic

* fix tests
This commit is contained in:
Will Browne
2025-07-09 15:48:52 +01:00
committed by GitHub
parent 1d252de1e9
commit 1e0fd825eb
5 changed files with 121 additions and 155 deletions

View File

@ -46,10 +46,6 @@ func DefaultService(cfg *config.PluginManagementCfg) *Service {
// Base returns the base path for the specified plugin.
func (s *Service) Base(n PluginInfo) (string, error) {
if n.class == plugins.ClassCore {
baseDir := getBaseDir(n.fs.Base())
return path.Join("public/app/plugins", string(n.pluginJSON.Type), baseDir), nil
}
if n.class == plugins.ClassCDN {
return n.fs.Base(), nil
}
@ -64,7 +60,6 @@ func (s *Service) Base(n PluginInfo) (string, error) {
if s.cdn.PluginSupported(n.parent.pluginJSON.ID) {
return s.cdn.AssetURL(n.parent.pluginJSON.ID, n.parent.pluginJSON.Info.Version, relPath)
}
return path.Join("public/plugins", n.parent.pluginJSON.ID, relPath), nil
}
return path.Join("public/plugins", n.pluginJSON.ID), nil
@ -73,32 +68,12 @@ func (s *Service) Base(n PluginInfo) (string, error) {
// Module returns the module.js path for the specified plugin.
func (s *Service) Module(n PluginInfo) (string, error) {
if n.class == plugins.ClassCore {
if filepath.Base(n.fs.Base()) == "dist" {
// The core plugin has been built externally, use the module from the dist folder
} else {
baseDir := getBaseDir(n.fs.Base())
return path.Join("core:plugin", baseDir), nil
if filepath.Base(n.fs.Base()) != "dist" {
return path.Join("core:plugin", filepath.Base(n.fs.Base())), nil
}
}
if n.class == plugins.ClassCDN {
return pluginscdn.JoinPath(n.fs.Base(), "module.js")
}
if s.cdn.PluginSupported(n.pluginJSON.ID) {
return s.cdn.AssetURL(n.pluginJSON.ID, n.pluginJSON.Info.Version, "module.js")
}
if n.parent != nil {
relPath, err := n.parent.fs.Rel(n.fs.Base())
if err != nil {
return "", err
}
if s.cdn.PluginSupported(n.parent.pluginJSON.ID) {
return s.cdn.AssetURL(n.parent.pluginJSON.ID, n.parent.pluginJSON.Info.Version, path.Join(relPath, "module.js"))
}
return path.Join("public/plugins", n.parent.pluginJSON.ID, relPath, "module.js"), nil
}
return path.Join("public/plugins", n.pluginJSON.ID, "module.js"), nil
return s.RelativeURL(n, "module.js")
}
// RelativeURL returns the relative URL for an arbitrary plugin asset.
@ -145,15 +120,6 @@ func (s *Service) DefaultLogoPath(pluginType plugins.Type) string {
return path.Join("public/img", fmt.Sprintf("icn-%s.svg", string(pluginType)))
}
func getBaseDir(pluginDir string) string {
baseDir := filepath.Base(pluginDir)
// Decoupled core plugins will be suffixed with "dist" if they have been built
if baseDir == "dist" {
return filepath.Base(strings.TrimSuffix(pluginDir, baseDir))
}
return baseDir
}
func (s *Service) GetTranslations(n PluginInfo) (map[string]string, error) {
pathToTranslations, err := s.RelativeURL(n, "locales")
if err != nil {