diff --git a/cmd/pkger/cmds/pack.go b/cmd/pkger/cmds/pack.go index 257e1c6..3961643 100644 --- a/cmd/pkger/cmds/pack.go +++ b/cmd/pkger/cmds/pack.go @@ -141,10 +141,16 @@ 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) - 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(`") + + name := c.Name + if info.Module.Main { + name = "main" + } + + fmt.Fprintf(f, "package %s\n\n", name) + 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 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 cd6997d..2ee1f9a 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 } diff --git a/parser/parser_test.go b/parser/parser_test.go index 700e51b..0f9f22d 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) { @@ -74,19 +66,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 84e9ba9..e7f5986 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", diff --git a/pkging/pkgtest/testdata/ref/web/web.go b/pkging/pkgtest/testdata/ref/web/web.go index 2172d41..33e4296 100644 --- a/pkging/pkgtest/testdata/ref/web/web.go +++ b/pkging/pkgtest/testdata/ref/web/web.go @@ -7,7 +7,7 @@ import ( ) func Serve() { - pkger.Stat("github.com/stretchr/testify:/README.md") + pkger.Stat("github.com/markbates/pkger:/README.md") dir := http.FileServer(pkger.Dir("/public")) http.ListenAndServe(":3000", dir) }