Skip to content

Commit

Permalink
Implement
Browse files Browse the repository at this point in the history
  • Loading branch information
peterebden committed Oct 1, 2023
1 parent 48379aa commit fe08718
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions test/embed/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ subinclude("//build_defs:go")
go_library(
name = "embed",
srcs = ["embed.go"],
resources = ["hello.txt"],
resources = ["hello.txt", "subdir"],
)

go_test(
name = "embed_test",
srcs = ["embed_test.go"],
resources = ["hello.txt"],
resources = ["hello.txt", "subdir"],
deps = [
":embed",
"//third_party/go:testify",
Expand Down
2 changes: 1 addition & 1 deletion test/embed/embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestEmbedDir(t *testing.T) {
}

func TestEmbedDirAll(t *testing.T) {
b, err := subdir.ReadFile("subdir/_test.txt")
b, err := subdirAll.ReadFile("subdir/_test.txt")
assert.NoError(t, err)
assert.Equal(t, "hello", strings.TrimSpace(string(b)))
}
13 changes: 12 additions & 1 deletion tools/please_go/embed/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go/build"
"io"
"io/fs"
"log"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -54,6 +55,7 @@ func Parse(gofiles []string) (*Cfg, error) {
// AddPackage parses a go package and adds any embed patterns to the configuration
func (cfg *Cfg) AddPackage(pkg *build.Package) error {
for _, pattern := range append(append(pkg.EmbedPatterns, pkg.TestEmbedPatterns...), pkg.XTestEmbedPatterns...) {
log.Printf("here %s", pattern)
paths, err := relglob(pkg.Dir, pattern)
if err != nil {
return err
Expand All @@ -79,6 +81,13 @@ func dirs(files []string) []string {
}

func relglob(dir, pattern string) ([]string, error) {
// Go allows prefixing the pattern with all: which picks up files prefixed with . or _ (by default these should be ignored)
includeHidden := false
if strings.HasPrefix(pattern, "all:") {
pattern = strings.TrimPrefix(pattern, "all:")
includeHidden = true
}

paths, err := filepath.Glob(path.Join(dir, pattern))
if err == nil && len(paths) == 0 {
return nil, fmt.Errorf("pattern %s: no matching paths found", pattern)
Expand All @@ -89,7 +98,9 @@ func relglob(dir, pattern string) ([]string, error) {
if err != nil {
return err
} else if !d.IsDir() {
ret = append(ret, strings.TrimLeft(strings.TrimPrefix(path, dir), string(filepath.Separator)))
if hidden := strings.HasPrefix(d.Name(), ".") || strings.HasPrefix(d.Name(), "_"); !hidden || includeHidden {
ret = append(ret, strings.TrimLeft(strings.TrimPrefix(path, dir), string(filepath.Separator)))
}
}
return nil
}); err != nil {
Expand Down

0 comments on commit fe08718

Please sign in to comment.