Merge branch 'master' into here-here

This commit is contained in:
Mark Bates 2019-12-17 10:12:20 -05:00
commit 7a3189e727
6 changed files with 44 additions and 44 deletions

View File

@ -141,10 +141,16 @@ func Package(info here.Info, out string, decls parser.Decls) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintf(f, "package %s\n\n", c.Name)
fmt.Fprintf(f, "import \"github.com/markbates/pkger\"\n\n") name := c.Name
fmt.Fprintf(f, "import \"github.com/markbates/pkger/pkging/mem\"\n\n") if info.Module.Main {
fmt.Fprintf(f, "\nvar _ = pkger.Apply(mem.UnmarshalEmbed([]byte(`") 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 { if err := pkgutil.Stuff(f, info, decls); err != nil {
return err return err

View File

@ -5,6 +5,9 @@ import (
"fmt" "fmt"
"go/token" "go/token"
"os" "os"
"path/filepath"
"github.com/markbates/pkger/here"
) )
var _ Decl = IncludeDecl{} var _ Decl = IncludeDecl{}
@ -15,6 +18,34 @@ type IncludeDecl struct {
value string 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 { func (d IncludeDecl) String() string {
return fmt.Sprintf("pkger.Include(%q)", d.value) return fmt.Sprintf("pkger.Include(%q)", d.value)
} }

View File

@ -226,30 +226,12 @@ func (p *Parser) parse() error {
func (p *Parser) parseIncludes() error { func (p *Parser) parseIncludes() error {
for _, i := range p.includes { for _, i := range p.includes {
pt, err := p.Info.Parse(i) d, err := NewInclude(p.Info, i)
if err != nil { if err != nil {
return err return err
} }
her := p.Info p.decls["Include"] = append(p.decls["Include"], d)
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,
})
} }
return nil return nil
} }

View File

@ -41,14 +41,6 @@ func Test_Parser_Ref(t *testing.T) {
fmt.Println(f.Path) fmt.Println(f.Path)
} }
r.Len(files, 25) 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) { func Test_Parser_Ref_Include(t *testing.T) {
@ -74,19 +66,9 @@ func Test_Parser_Ref_Include(t *testing.T) {
files, err := res.Files() files, err := res.Files()
r.NoError(err) r.NoError(err)
// t.FailNow()
l := len(files) l := len(files)
r.Equal(26, l) 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) { func Test_Parser_Example_HTTP(t *testing.T) {

View File

@ -21,7 +21,6 @@ func NewRef() (*Ref, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
root := filepath.Join( root := filepath.Join(
her.Module.Dir, her.Module.Dir,
"pkging", "pkging",

View File

@ -7,7 +7,7 @@ import (
) )
func Serve() { func Serve() {
pkger.Stat("github.com/stretchr/testify:/README.md") pkger.Stat("github.com/markbates/pkger:/README.md")
dir := http.FileServer(pkger.Dir("/public")) dir := http.FileServer(pkger.Dir("/public"))
http.ListenAndServe(":3000", dir) http.ListenAndServe(":3000", dir)
} }