Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #817 from gobuffalo/backy-io-master
Browse files Browse the repository at this point in the history
cleaned up issues from PR: Image tag, plugin search #815
  • Loading branch information
markbates authored Dec 20, 2017
2 parents 4a35c4c + ac16d3e commit 8b2d6c8
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 25 deletions.
19 changes: 15 additions & 4 deletions plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.`
50 changes: 29 additions & 21 deletions render/template_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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"
}
Expand All @@ -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()
}
87 changes: 87 additions & 0 deletions render/template_helpers_test.go
Original file line number Diff line number Diff line change
@@ -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(`<script src="/assets/application.js" type="text/javascript"></script>`), 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(`<script class="foo" src="/assets/application.js" type="text/javascript"></script>`), 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(`<link href="/assets/application.css" media="screen" rel="stylesheet" />`), 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(`<link class="foo" href="/assets/application.css" media="screen" rel="stylesheet" />`), 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(`<img src="/assets/foo.png" />`), 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(`<img class="foo" src="/assets/foo.png" />`), s)
}

0 comments on commit 8b2d6c8

Please sign in to comment.