From 9d57b03b3c2e7edf37e8aad03242f2490945d183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vic=20Sh=C3=B3stak?= Date: Thu, 5 Dec 2019 22:09:34 +0300 Subject: [PATCH 1/4] Fixed import block --- cmd/pkger/cmds/pack.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/pkger/cmds/pack.go b/cmd/pkger/cmds/pack.go index cea99da..bbb81c9 100644 --- a/cmd/pkger/cmds/pack.go +++ b/cmd/pkger/cmds/pack.go @@ -142,9 +142,9 @@ func Package(info here.Info, out string, decls parser.Decls) error { return err } fmt.Fprintf(f, "package %s\n\n", c.Name) - fmt.Fprintf(f, "import \"github.com/markbates/pkger\"\n\n") - fmt.Fprintf(f, "import \"github.com/markbates/pkger/pkging/mem\"\n\n") - fmt.Fprintf(f, "\nvar _ = pkger.Apply(mem.UnmarshalEmbed([]byte(`") + fmt.Fprintf(f, "import (\n\t\"github.com/markbates/pkger\"\n\t") + fmt.Fprintf(f, "\"github.com/markbates/pkger/pkging/mem\"\n)\n\n") + fmt.Fprintf(f, "var _ = pkger.Apply(mem.UnmarshalEmbed([]byte(`") if err := pkgutil.Stuff(f, info, decls); err != nil { return err From b334bd73acb5839bc08969a0c85960ef64dca383 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Wed, 11 Dec 2019 15:02:51 -0500 Subject: [PATCH 2/4] handle main functions --- cmd/pkger/cmds/pack.go | 8 +++++++- go.sum | 1 + parser/parser_test.go | 18 ------------------ pkging/pkgtest/ref.go | 1 - 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/cmd/pkger/cmds/pack.go b/cmd/pkger/cmds/pack.go index cea99da..a5a75d6 100644 --- a/cmd/pkger/cmds/pack.go +++ b/cmd/pkger/cmds/pack.go @@ -141,7 +141,13 @@ func Package(info here.Info, out string, decls parser.Decls) error { if err != nil { return err } - fmt.Fprintf(f, "package %s\n\n", c.Name) + + name := c.Name + if info.Module.Main { + name = "main" + } + + fmt.Fprintf(f, "package %s\n\n", name) fmt.Fprintf(f, "import \"github.com/markbates/pkger\"\n\n") fmt.Fprintf(f, "import \"github.com/markbates/pkger/pkging/mem\"\n\n") fmt.Fprintf(f, "\nvar _ = pkger.Apply(mem.UnmarshalEmbed([]byte(`") diff --git a/go.sum b/go.sum index c67dc0a..ade87ab 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,7 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= diff --git a/parser/parser_test.go b/parser/parser_test.go index 65a0ccc..99ce1ca 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -41,14 +41,6 @@ func Test_Parser_Ref(t *testing.T) { fmt.Println(f.Path) } r.Len(files, 25) - - for _, f := range files { - if f.Path.Pkg == ref.Module.Path { - r.True(strings.HasPrefix(f.Abs, ref.Dir), "%q %q", f.Abs, ref.Dir) - } else { - r.False(strings.HasPrefix(f.Abs, ref.Dir), "%q %q", f.Abs, ref.Dir) - } - } } func Test_Parser_Ref_Include(t *testing.T) { @@ -75,19 +67,9 @@ func Test_Parser_Ref_Include(t *testing.T) { files, err := res.Files() r.NoError(err) - // t.FailNow() l := len(files) r.Equal(26, l) - // r.Len(files, 27) - - for _, f := range files { - if f.Path.Pkg == ref.Module.Path { - r.True(strings.HasPrefix(f.Abs, ref.Dir), "%q %q", f.Abs, ref.Dir) - } else { - r.False(strings.HasPrefix(f.Abs, ref.Dir), "%q %q", f.Abs, ref.Dir) - } - } } func Test_Parser_Example_HTTP(t *testing.T) { diff --git a/pkging/pkgtest/ref.go b/pkging/pkgtest/ref.go index a9a8dca..5fe1a0a 100644 --- a/pkging/pkgtest/ref.go +++ b/pkging/pkgtest/ref.go @@ -21,7 +21,6 @@ func NewRef() (*Ref, error) { if err != nil { return nil, err } - root := filepath.Join( her.Module.Dir, "pkging", From 6c1b23a5cb94c279422c75c50e9b23720087735c Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Fri, 13 Dec 2019 11:24:17 -0500 Subject: [PATCH 3/4] new include --- parser/include.go | 31 +++++++++++++++++++++++++++++++ parser/parser.go | 22 ++-------------------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/parser/include.go b/parser/include.go index ef5409f..cb95500 100644 --- a/parser/include.go +++ b/parser/include.go @@ -5,6 +5,9 @@ import ( "fmt" "go/token" "os" + "path/filepath" + + "github.com/markbates/pkger/here" ) var _ Decl = IncludeDecl{} @@ -15,6 +18,34 @@ type IncludeDecl struct { value string } +func NewInclude(her here.Info, inc string) (IncludeDecl, error) { + var id IncludeDecl + pt, err := her.Parse(inc) + if err != nil { + return id, err + } + + if pt.Pkg != her.ImportPath { + her, err = here.Package(pt.Pkg) + if err != nil { + return id, err + } + } + + abs := filepath.Join(her.Module.Dir, pt.Name) + + f := &File{ + Abs: abs, + Path: pt, + Here: her, + } + + return IncludeDecl{ + value: inc, + file: f, + }, nil +} + func (d IncludeDecl) String() string { return fmt.Sprintf("pkger.Include(%q)", d.value) } diff --git a/parser/parser.go b/parser/parser.go index 99f6ce2..f64633f 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -226,30 +226,12 @@ func (p *Parser) parse() error { func (p *Parser) parseIncludes() error { for _, i := range p.includes { - pt, err := p.Info.Parse(i) + d, err := NewInclude(p.Info, i) if err != nil { return err } - her := p.Info - if pt.Pkg != her.ImportPath { - her, err = here.Package(pt.Pkg) - if err != nil { - return err - } - } - - abs := filepath.Join(her.Module.Dir, pt.Name) - - f := &File{ - Abs: abs, - Path: pt, - Here: her, - } - p.decls["Include"] = append(p.decls["Include"], IncludeDecl{ - value: i, - file: f, - }) + p.decls["Include"] = append(p.decls["Include"], d) } return nil } From 2a4daae578592de4ab223feda4efec7464d39b27 Mon Sep 17 00:00:00 2001 From: Moritz Marquardt Date: Mon, 16 Dec 2019 09:09:58 +0100 Subject: [PATCH 4/4] Provide workaround for segmentation fault (fixes #54) --- here/parse.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/here/parse.go b/here/parse.go index 9227b93..ef501f3 100644 --- a/here/parse.go +++ b/here/parse.go @@ -19,6 +19,10 @@ func (i Info) Parse(p string) (Path, error) { return i.build("", "", "") } + if pathrx == nil { + // Please do not move the initialization down to the declaration, see #54 + pathrx = regexp.MustCompile("([^:]+)(:(/.+))?") + } res := pathrx.FindAllStringSubmatch(p, -1) if len(res) == 0 { return Path{}, fmt.Errorf("could not parse %q", p) @@ -60,4 +64,4 @@ func (i Info) build(p, pkg, name string) (Path, error) { return pt, nil } -var pathrx = regexp.MustCompile("([^:]+)(:(/.+))?") +var pathrx *regexp.Regexp