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 {

View File

@ -78,7 +78,7 @@ func TestService(t *testing.T) {
base, err = svc.Base(NewPluginInfo(jsonData["table-old"], plugins.ClassCore, tableOldFS, nil))
require.NoError(t, err)
require.Equal(t, "public/app/plugins/table-old", base)
require.Equal(t, "public/plugins/table-old", base)
parentFS := pluginFS(oneCDNURL)
parentFS.RelFunc = func(_ string) (string, error) {
@ -243,9 +243,9 @@ func TestService_ChildPlugins(t *testing.T) {
return childInfo
},
expected: expected{
module: "public/plugins/parent/child/module.js",
baseURL: "public/plugins/parent/child",
relURL: "public/plugins/parent/child/path/to/file.txt",
module: "public/plugins/child/module.js",
baseURL: "public/plugins/child",
relURL: "public/plugins/child/path/to/file.txt",
},
},
{
@ -256,8 +256,8 @@ func TestService_ChildPlugins(t *testing.T) {
},
expected: expected{
module: "core:plugin/parent",
baseURL: "public/app/plugins/parent",
relURL: "public/app/plugins/parent/path/to/file.txt",
baseURL: "public/plugins/parent",
relURL: "public/plugins/parent/path/to/file.txt",
},
},
{
@ -268,8 +268,8 @@ func TestService_ChildPlugins(t *testing.T) {
},
expected: expected{
module: "public/plugins/parent/module.js",
baseURL: "public/app/plugins/parent",
relURL: "public/app/plugins/parent/path/to/file.txt",
baseURL: "public/plugins/parent",
relURL: "public/plugins/parent/path/to/file.txt",
},
},
{

View File

@ -85,8 +85,8 @@ func TestLoader_Load(t *testing.T) {
Description: "Data source for Amazon AWS monitoring service",
Keywords: []string{"aws", "amazon"},
Logos: plugins.Logos{
Small: "public/app/plugins/datasource/cloudwatch/img/amazon-web-services.png",
Large: "public/app/plugins/datasource/cloudwatch/img/amazon-web-services.png",
Small: "public/plugins/cloudwatch/img/amazon-web-services.png",
Large: "public/plugins/cloudwatch/img/amazon-web-services.png",
},
Links: []plugins.InfoLink{
{Name: "Raise issue", URL: "https://github.com/grafana/grafana/issues/new"},
@ -123,7 +123,7 @@ func TestLoader_Load(t *testing.T) {
QueryOptions: map[string]bool{"minInterval": true},
},
Module: "core:plugin/cloudwatch",
BaseURL: "public/app/plugins/datasource/cloudwatch",
BaseURL: "public/plugins/cloudwatch",
FS: mustNewStaticFSForTests(t, filepath.Join(corePluginDir, "app/plugins/datasource/cloudwatch")),
Signature: plugins.SignatureStatusInternal,
Class: plugins.ClassCore,