feat(apps): lots of work making apps easier to develop, module paths are handled automatically

This commit is contained in:
Torkel Ödegaard 2016-02-09 22:10:36 +01:00
parent fe2e6b8a80
commit baff9b0267
6 changed files with 41 additions and 18 deletions

View File

@ -1,6 +1,5 @@
{ {
"type": "panel", "type": "panel",
"name": "Nginx Panel", "name": "Nginx Panel",
"id": "nginx-panel", "id": "nginx-panel"
"staticRoot": "."
} }

View File

@ -57,18 +57,24 @@ func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error {
return err return err
} }
app.PluginDir = pluginDir
Apps[app.Id] = app
return nil
}
func (app *AppPlugin) initApp() {
app.initFrontendPlugin()
if app.Css != nil { if app.Css != nil {
app.Css.Dark = evalRelativePluginUrlPath(app.Css.Dark, app.Id) app.Css.Dark = evalRelativePluginUrlPath(app.Css.Dark, app.Id)
app.Css.Light = evalRelativePluginUrlPath(app.Css.Light, app.Id) app.Css.Light = evalRelativePluginUrlPath(app.Css.Light, app.Id)
} }
app.PluginDir = pluginDir
app.initFrontendPlugin()
// check if we have child panels // check if we have child panels
for _, panel := range Panels { for _, panel := range Panels {
if strings.HasPrefix(panel.PluginDir, app.PluginDir) { if strings.HasPrefix(panel.PluginDir, app.PluginDir) {
panel.IncludedInAppId = app.Id panel.setPathsBasedOnApp(app)
app.Includes = append(app.Includes, &AppIncludeInfo{ app.Includes = append(app.Includes, &AppIncludeInfo{
Name: panel.Name, Name: panel.Name,
Id: panel.Id, Id: panel.Id,
@ -80,7 +86,7 @@ func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error {
// check if we have child datasources // check if we have child datasources
for _, ds := range DataSources { for _, ds := range DataSources {
if strings.HasPrefix(ds.PluginDir, app.PluginDir) { if strings.HasPrefix(ds.PluginDir, app.PluginDir) {
ds.IncludedInAppId = app.Id ds.setPathsBasedOnApp(app)
app.Includes = append(app.Includes, &AppIncludeInfo{ app.Includes = append(app.Includes, &AppIncludeInfo{
Name: ds.Name, Name: ds.Name,
Id: ds.Id, Id: ds.Id,
@ -95,7 +101,4 @@ func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error {
page.Slug = slug.Make(page.Name) page.Slug = slug.Make(page.Name)
} }
} }
Apps[app.Id] = app
return nil
} }

View File

@ -18,7 +18,6 @@ func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error {
} }
p.PluginDir = pluginDir p.PluginDir = pluginDir
p.initFrontendPlugin()
DataSources[p.Id] = p DataSources[p.Id] = p
return nil return nil

View File

@ -4,6 +4,10 @@ import (
"net/url" "net/url"
"path" "path"
"path/filepath" "path/filepath"
"strings"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/util"
) )
type FrontendPluginBase struct { type FrontendPluginBase struct {
@ -23,18 +27,27 @@ func (fp *FrontendPluginBase) initFrontendPlugin() {
}) })
} }
fp.handleModuleDefaults()
fp.Info.Logos.Small = evalRelativePluginUrlPath(fp.Info.Logos.Small, fp.Id) fp.Info.Logos.Small = evalRelativePluginUrlPath(fp.Info.Logos.Small, fp.Id)
fp.Info.Logos.Large = evalRelativePluginUrlPath(fp.Info.Logos.Large, fp.Id) fp.Info.Logos.Large = evalRelativePluginUrlPath(fp.Info.Logos.Large, fp.Id)
for i := -0; i < len(fp.Info.Screenshots); i++ {
for i := 0; i < len(fp.Info.Screenshots); i++ {
fp.Info.Screenshots[i].Path = evalRelativePluginUrlPath(fp.Info.Screenshots[i].Path, fp.Id) fp.Info.Screenshots[i].Path = evalRelativePluginUrlPath(fp.Info.Screenshots[i].Path, fp.Id)
} }
fp.handleModuleDefaults() }
func (fp *FrontendPluginBase) setPathsBasedOnApp(app *AppPlugin) {
// log.Info("Module Before: %v", fp.Module)
// find out plugins path relative to app static root
appSubPath := strings.Replace(fp.PluginDir, app.StaticRootAbs, "", 1)
fp.IncludedInAppId = app.Id
fp.BaseUrl = app.BaseUrl
fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module"
log.Info("setting paths based on app: subpath = %v, module: %v", appSubPath, fp.Module)
} }
func (fp *FrontendPluginBase) handleModuleDefaults() { func (fp *FrontendPluginBase) handleModuleDefaults() {
if fp.Module != "" {
return
}
if fp.StaticRoot != "" { if fp.StaticRoot != "" {
fp.Module = path.Join("plugins", fp.Id, "module") fp.Module = path.Join("plugins", fp.Id, "module")

View File

@ -12,7 +12,6 @@ func (p *PanelPlugin) Load(decoder *json.Decoder, pluginDir string) error {
} }
p.PluginDir = pluginDir p.PluginDir = pluginDir
p.initFrontendPlugin()
Panels[p.Id] = p Panels[p.Id] = p
return nil return nil

View File

@ -41,7 +41,17 @@ func Init() error {
scan(path.Join(setting.StaticRootPath, "app/plugins")) scan(path.Join(setting.StaticRootPath, "app/plugins"))
scan(setting.PluginsPath) scan(setting.PluginsPath)
checkPluginPaths() checkPluginPaths()
// checkDependencies()
for _, panel := range Panels {
panel.initFrontendPlugin()
}
for _, panel := range DataSources {
panel.initFrontendPlugin()
}
for _, app := range Apps {
app.initApp()
}
return nil return nil
} }