diff --git a/plugins/plugins.go b/plugins/plugins.go index 1262904aa..67de6d73c 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -12,7 +12,9 @@ import ( "strings" "time" + "github.com/gobuffalo/envy" "github.com/pkg/errors" + "github.com/sirupsen/logrus" ) // List maps a Buffalo command to a slice of Command @@ -27,16 +29,23 @@ type List map[string]Commands // * file/command must respond to `available` and return JSON of // plugins.Commands{} // -// Caveats: -// * The C:\Windows directory is excluded +// Limit full path scan with direct plugin path func Available() (List, error) { list := List{} paths := []string{"plugins"} + + from, err := envy.MustGet("BUFFALO_PLUGIN_PATH") + if err != nil { + logrus.Warn(warningMessage) + from = envy.Get("PATH", "") + } + if runtime.GOOS == "windows" { - paths = append(paths, strings.Split(os.Getenv("PATH"), ";")...) + paths = append(paths, strings.Split(from, ";")...) } else { - paths = append(paths, strings.Split(os.Getenv("PATH"), ":")...) + paths = append(paths, strings.Split(from, ":")...) } + for _, p := range paths { if ignorePath(p) { continue @@ -103,3 +112,5 @@ func ignorePath(p string) bool { } return false } + +const warningMessage = `Could not find BUFFALO_PLUGIN_PATH environment variable, default to PATH instead. Consider setting the BUFFALO_PLUGIN_PATH variable to speed up loading of plugins and/or to set a custom path for locating them.` diff --git a/render/template_helpers.go b/render/template_helpers.go index d5b46348f..4346e0d0b 100644 --- a/render/template_helpers.go +++ b/render/template_helpers.go @@ -32,36 +32,41 @@ func assetPathFor(file string) string { return filepath.ToSlash(filepath.Join("/assets", filePath)) } +type helperTag struct { + name string + fn func(string, tags.Options) template.HTML +} + func (s templateRenderer) addAssetsHelpers(helpers Helpers) Helpers { helpers["assetPath"] = func(file string) (string, error) { return s.assetPath(file) } - helpers["javascriptTag"] = func(file string, options tags.Options) (template.HTML, error) { - h, err := s.assetPath(file) - if err != nil { - return "", errors.WithStack(err) - } - return jsTag(h, options), nil + ah := []helperTag{ + {"javascriptTag", jsTag}, + {"stylesheetTag", cssTag}, + {"imgTag", imgTag}, } - helpers["stylesheetTag"] = func(file string, options tags.Options) (template.HTML, error) { - h, err := s.assetPath(file) - if err != nil { - return "", errors.WithStack(err) - } - return cssTag(h, options), nil + for _, h := range ah { + func(h helperTag) { + helpers[h.name] = func(file string, options tags.Options) (template.HTML, error) { + if options == nil { + options = tags.Options{} + } + f, err := s.assetPath(file) + if err != nil { + return "", errors.WithStack(err) + } + return h.fn(f, options), nil + } + }(h) } return helpers } func jsTag(src string, options tags.Options) template.HTML { - - if options == nil { - options = tags.Options{} - } - if options["type"] == nil { options["type"] = "text/javascript" } @@ -73,10 +78,6 @@ func jsTag(src string, options tags.Options) template.HTML { } func cssTag(href string, options tags.Options) template.HTML { - if options == nil { - options = tags.Options{} - } - if options["rel"] == nil { options["rel"] = "stylesheet" } @@ -90,3 +91,10 @@ func cssTag(href string, options tags.Options) template.HTML { return cssTag.HTML() } + +func imgTag(src string, options tags.Options) template.HTML { + options["src"] = src + imgTag := tags.New("img", options) + + return imgTag.HTML() +} diff --git a/render/template_helpers_test.go b/render/template_helpers_test.go new file mode 100644 index 000000000..8f970a062 --- /dev/null +++ b/render/template_helpers_test.go @@ -0,0 +1,87 @@ +package render + +import ( + "html/template" + "testing" + + "github.com/gobuffalo/packr" + "github.com/gobuffalo/tags" + "github.com/stretchr/testify/require" +) + +func Test_javascriptTag(t *testing.T) { + r := require.New(t) + re := New(Options{ + AssetsBox: packr.NewBox(""), + }) + tr := re.Template("").(templateRenderer) + h := tr.addAssetsHelpers(Helpers{}) + f := h["javascriptTag"].(func(string, tags.Options) (template.HTML, error)) + s, err := f("application.js", nil) + r.NoError(err) + r.Equal(template.HTML(``), s) +} + +func Test_javascriptTag_Options(t *testing.T) { + r := require.New(t) + re := New(Options{ + AssetsBox: packr.NewBox(""), + }) + tr := re.Template("").(templateRenderer) + h := tr.addAssetsHelpers(Helpers{}) + f := h["javascriptTag"].(func(string, tags.Options) (template.HTML, error)) + s, err := f("application.js", tags.Options{"class": "foo"}) + r.NoError(err) + r.Equal(template.HTML(``), s) + +} +func Test_stylesheetTag(t *testing.T) { + r := require.New(t) + re := New(Options{ + AssetsBox: packr.NewBox(""), + }) + tr := re.Template("").(templateRenderer) + h := tr.addAssetsHelpers(Helpers{}) + f := h["stylesheetTag"].(func(string, tags.Options) (template.HTML, error)) + s, err := f("application.css", nil) + r.NoError(err) + r.Equal(template.HTML(``), s) +} + +func Test_stylesheetTag_Options(t *testing.T) { + r := require.New(t) + re := New(Options{ + AssetsBox: packr.NewBox(""), + }) + tr := re.Template("").(templateRenderer) + h := tr.addAssetsHelpers(Helpers{}) + f := h["stylesheetTag"].(func(string, tags.Options) (template.HTML, error)) + s, err := f("application.css", tags.Options{"class": "foo"}) + r.NoError(err) + r.Equal(template.HTML(``), s) +} +func Test_imgTag(t *testing.T) { + r := require.New(t) + re := New(Options{ + AssetsBox: packr.NewBox(""), + }) + tr := re.Template("").(templateRenderer) + h := tr.addAssetsHelpers(Helpers{}) + f := h["imgTag"].(func(string, tags.Options) (template.HTML, error)) + s, err := f("foo.png", nil) + r.NoError(err) + r.Equal(template.HTML(``), s) +} + +func Test_imgTag_Options(t *testing.T) { + r := require.New(t) + re := New(Options{ + AssetsBox: packr.NewBox(""), + }) + tr := re.Template("").(templateRenderer) + h := tr.addAssetsHelpers(Helpers{}) + f := h["imgTag"].(func(string, tags.Options) (template.HTML, error)) + s, err := f("foo.png", tags.Options{"class": "foo"}) + r.NoError(err) + r.Equal(template.HTML(``), s) +}