Skip to content

Commit

Permalink
feat(extract):#none add outer arg for yaegi extract command to suppor…
Browse files Browse the repository at this point in the history
…t generating import code not in github.com/traefik/yaegi/stdlib
  • Loading branch information
ludanfeng committed May 31, 2024
1 parent 381e045 commit d56abd8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cmd/yaegi/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ func extractCmd(arg []string) error {
var exclude string
var include string
var tag string
var outer bool

eflag := flag.NewFlagSet("run", flag.ContinueOnError)
eflag.StringVar(&licensePath, "license", "", "path to a LICENSE file")
eflag.StringVar(&name, "name", "", "the namespace for the extracted symbols")
eflag.StringVar(&exclude, "exclude", "", "comma separated list of regexp matching symbols to exclude")
eflag.StringVar(&include, "include", "", "comma separated list of regexp matching symbols to include")
eflag.StringVar(&tag, "tag", "", "comma separated list of build tags to be added to the created package")
eflag.BoolVar(&outer, "outer", false, "generated code is not in github.com/traefik/yaegi/stdlib")

eflag.Usage = func() {
fmt.Println("Usage: yaegi extract [options] packages...")
fmt.Println("Options:")
Expand Down Expand Up @@ -58,6 +61,7 @@ func extractCmd(arg []string) error {
ext := extract.Extractor{
Dest: name,
License: license,
Outer: outer,
}
if tag != "" {
ext.Tag = strings.Split(tag, ",")
Expand Down
7 changes: 6 additions & 1 deletion extract/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const model = `// Code generated by 'yaegi extract {{.ImportPath}}'. DO NOT EDIT
package {{.Dest}}
import (
{{- if .Outer }}
"github.com/traefik/yaegi/stdlib"
{{- end}}
{{- range $key, $value := .Imports }}
{{- if $value}}
"{{$key}}"
Expand All @@ -46,7 +49,7 @@ import (
)
func init() {
Symbols["{{.PkgName}}"] = map[string]reflect.Value{
{{if .Outer}}stdlib.{{end}}Symbols["{{.PkgName}}"] = map[string]reflect.Value{
{{- if .Val}}
// function, constant and variable definitions
{{range $key, $value := .Val -}}
Expand Down Expand Up @@ -139,6 +142,7 @@ type Extractor struct {
Exclude []string // Comma separated list of regexp matching symbols to exclude.
Include []string // Comma separated list of regexp matching symbols to include.
Tag []string // Comma separated of build tags to be added to the created package.
Outer bool // The project that generates code is not github.com/traefik/yaegi/stdlib.
}

func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, error) {
Expand Down Expand Up @@ -321,6 +325,7 @@ func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, err
"Wrap": wrap,
"BuildTags": buildTags,
"License": e.License,
"Outer": e.Outer,
}
err = parse.Execute(b, data)
if err != nil {
Expand Down
29 changes: 28 additions & 1 deletion extract/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ func init() {
}
`

var expectedOutputOuter = `// Code generated by 'yaegi extract guthib.com/baz'. DO NOT EDIT.
package bar
import (
"github.com/traefik/yaegi/stdlib"
"guthib.com/baz"
"reflect"
)
func init() {
stdlib.Symbols["guthib.com/baz/baz"] = map[string]reflect.Value{
// function, constant and variable definitions
"Hello": reflect.ValueOf(baz.Hello),
}
}
`

func TestPackages(t *testing.T) {
testCases := []struct {
desc string
Expand All @@ -35,6 +53,7 @@ func TestPackages(t *testing.T) {
expected string
contains string
dest string
outer bool
}{
{
desc: "stdlib math pkg, using go/importer",
Expand All @@ -51,6 +70,13 @@ func TestPackages(t *testing.T) {
arg: "../baz",
expected: expectedOutput,
},
{
desc: "using relative path, using go.mod, out of stdlib",
wd: "./testdata/1/src/guthib.com/bar",
arg: "../baz",
outer: true,
expected: expectedOutputOuter,
},
{
desc: "using relative path, manual import path",
wd: "./testdata/2/src/guthib.com/bar",
Expand Down Expand Up @@ -149,7 +175,8 @@ func (W _guthib_com_variadic_Variadic) Call(method string, args ...[]interface{}
dest = test.dest
}
ext := Extractor{
Dest: dest,
Dest: dest,
Outer: test.outer,
}

var out bytes.Buffer
Expand Down

0 comments on commit d56abd8

Please sign in to comment.