Skip to content

Commit d56abd8

Browse files
author
ludanfeng
committed
feat(extract):#none add outer arg for yaegi extract command to support generating import code not in github.com/traefik/yaegi/stdlib
1 parent 381e045 commit d56abd8

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

cmd/yaegi/extract.go

+4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ func extractCmd(arg []string) error {
2020
var exclude string
2121
var include string
2222
var tag string
23+
var outer bool
2324

2425
eflag := flag.NewFlagSet("run", flag.ContinueOnError)
2526
eflag.StringVar(&licensePath, "license", "", "path to a LICENSE file")
2627
eflag.StringVar(&name, "name", "", "the namespace for the extracted symbols")
2728
eflag.StringVar(&exclude, "exclude", "", "comma separated list of regexp matching symbols to exclude")
2829
eflag.StringVar(&include, "include", "", "comma separated list of regexp matching symbols to include")
2930
eflag.StringVar(&tag, "tag", "", "comma separated list of build tags to be added to the created package")
31+
eflag.BoolVar(&outer, "outer", false, "generated code is not in github.com/traefik/yaegi/stdlib")
32+
3033
eflag.Usage = func() {
3134
fmt.Println("Usage: yaegi extract [options] packages...")
3235
fmt.Println("Options:")
@@ -58,6 +61,7 @@ func extractCmd(arg []string) error {
5861
ext := extract.Extractor{
5962
Dest: name,
6063
License: license,
64+
Outer: outer,
6165
}
6266
if tag != "" {
6367
ext.Tag = strings.Split(tag, ",")

extract/extract.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const model = `// Code generated by 'yaegi extract {{.ImportPath}}'. DO NOT EDIT
3434
package {{.Dest}}
3535
3636
import (
37+
{{- if .Outer }}
38+
"github.com/traefik/yaegi/stdlib"
39+
{{- end}}
3740
{{- range $key, $value := .Imports }}
3841
{{- if $value}}
3942
"{{$key}}"
@@ -46,7 +49,7 @@ import (
4649
)
4750
4851
func init() {
49-
Symbols["{{.PkgName}}"] = map[string]reflect.Value{
52+
{{if .Outer}}stdlib.{{end}}Symbols["{{.PkgName}}"] = map[string]reflect.Value{
5053
{{- if .Val}}
5154
// function, constant and variable definitions
5255
{{range $key, $value := .Val -}}
@@ -139,6 +142,7 @@ type Extractor struct {
139142
Exclude []string // Comma separated list of regexp matching symbols to exclude.
140143
Include []string // Comma separated list of regexp matching symbols to include.
141144
Tag []string // Comma separated of build tags to be added to the created package.
145+
Outer bool // The project that generates code is not github.com/traefik/yaegi/stdlib.
142146
}
143147

144148
func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, error) {
@@ -321,6 +325,7 @@ func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, err
321325
"Wrap": wrap,
322326
"BuildTags": buildTags,
323327
"License": e.License,
328+
"Outer": e.Outer,
324329
}
325330
err = parse.Execute(b, data)
326331
if err != nil {

extract/extract_test.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ func init() {
2525
}
2626
`
2727

28+
var expectedOutputOuter = `// Code generated by 'yaegi extract guthib.com/baz'. DO NOT EDIT.
29+
30+
package bar
31+
32+
import (
33+
"github.com/traefik/yaegi/stdlib"
34+
"guthib.com/baz"
35+
"reflect"
36+
)
37+
38+
func init() {
39+
stdlib.Symbols["guthib.com/baz/baz"] = map[string]reflect.Value{
40+
// function, constant and variable definitions
41+
"Hello": reflect.ValueOf(baz.Hello),
42+
}
43+
}
44+
`
45+
2846
func TestPackages(t *testing.T) {
2947
testCases := []struct {
3048
desc string
@@ -35,6 +53,7 @@ func TestPackages(t *testing.T) {
3553
expected string
3654
contains string
3755
dest string
56+
outer bool
3857
}{
3958
{
4059
desc: "stdlib math pkg, using go/importer",
@@ -51,6 +70,13 @@ func TestPackages(t *testing.T) {
5170
arg: "../baz",
5271
expected: expectedOutput,
5372
},
73+
{
74+
desc: "using relative path, using go.mod, out of stdlib",
75+
wd: "./testdata/1/src/guthib.com/bar",
76+
arg: "../baz",
77+
outer: true,
78+
expected: expectedOutputOuter,
79+
},
5480
{
5581
desc: "using relative path, manual import path",
5682
wd: "./testdata/2/src/guthib.com/bar",
@@ -149,7 +175,8 @@ func (W _guthib_com_variadic_Variadic) Call(method string, args ...[]interface{}
149175
dest = test.dest
150176
}
151177
ext := Extractor{
152-
Dest: dest,
178+
Dest: dest,
179+
Outer: test.outer,
153180
}
154181

155182
var out bytes.Buffer

0 commit comments

Comments
 (0)