From bcce715b3f9d491516ed73c1140aa9492f707b3b Mon Sep 17 00:00:00 2001 From: ikawaha Date: Sun, 7 Jul 2019 19:41:54 +0900 Subject: [PATCH] Add tiny go.mod parser instead of third party package. --- cmd/goa/gen.go | 33 ++++++++++++++--------- cmd/goa/gen_test.go | 66 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 77 insertions(+), 22 deletions(-) diff --git a/cmd/goa/gen.go b/cmd/goa/gen.go index b9e0f7e0c5..2fdc509a21 100644 --- a/cmd/goa/gen.go +++ b/cmd/goa/gen.go @@ -1,9 +1,11 @@ package main import ( + "bufio" "fmt" "go/parser" "go/token" + "io" "io/ioutil" "os" "os/exec" @@ -13,7 +15,6 @@ import ( "strconv" "strings" - "github.com/sirkon/goproxy/gomod" "goa.design/goa/v3/codegen" "golang.org/x/tools/go/packages" ) @@ -162,26 +163,32 @@ func (g *Generator) goaPackage() (string, error) { return goaPkg, nil } goaPkg = fmt.Sprintf("goa.design/goa/v%d", g.DesignVersion) - gomod := findGoMod() - if _, err := os.Stat(gomod); err != nil { + path := findGoMod() + if _, err := os.Stat(path); err != nil { return goaPkg, nil } - b, err := ioutil.ReadFile(gomod) + fp, err := os.Open(path) if err != nil { - return "", fmt.Errorf("read go.mod error, %v", err) + return "", err } - return parseGoModGoaPackage(goaPkg, b) + defer fp.Close() + return parseGoModGoaPackage(goaPkg, fp) } -func parseGoModGoaPackage(goaPkg string, b []byte) (string, error) { - mod, err := gomod.Parse("go.mod", b) - if err != nil { - return "", fmt.Errorf("read go.mod error, %v", err) +var reMod = regexp.MustCompile(`(goa\.design/goa/v\d+?)\s+(\S+?)\s*$`) + +func parseGoModGoaPackage(pkg string, r io.Reader) (string, error) { + s := bufio.NewScanner(r) + for s.Scan() { + match := reMod.FindStringSubmatch(s.Text()) + if len(match) == 3 && match[1] == pkg { + return match[1] + "@" + match[2], nil + } } - if v, ok := mod.Require[goaPkg]; ok { - return goaPkg + "@" + v, nil + if err := s.Err(); err != nil { + return "", fmt.Errorf("scan error, %v", err) } - return goaPkg, nil + return pkg, nil } // Compile compiles the generator. diff --git a/cmd/goa/gen_test.go b/cmd/goa/gen_test.go index 86434f1e08..ef413d1827 100644 --- a/cmd/goa/gen_test.go +++ b/cmd/goa/gen_test.go @@ -1,6 +1,7 @@ package main import ( + "strings" "testing" ) @@ -37,20 +38,67 @@ func TestGenerator_goaPackage(t *testing.T) { } func TestParseGoModGoaPackage(t *testing.T) { - input := `module calc + cases := []struct { + Name string + Mod string + Package string + Expected string + }{ + { + Name: "simple require", + Mod: "require goa.design/goa/v3 v3.0.3-0.20190704022140-85024ebc66dc", + Package: "goa.design/goa/v3", + Expected: "goa.design/goa/v3@v3.0.3-0.20190704022140-85024ebc66dc", + }, + { + Name: "in require block", + Mod: `module calc go 1.12 require ( - github.com/smartystreets/assertions v1.0.0 // indirect + github.com/ikawaha/kagome v1.0.0 // indirect goa.design/goa/v3 v3.0.3-0.20190704022140-85024ebc66dc goa.design/plugins/v3 v3.0.1 ) -` - expected := `goa.design/goa/v3@v3.0.3-0.20190704022140-85024ebc66dc` - pkg, err := parseGoModGoaPackage("goa.design/goa/v3", []byte(input)) - if err != nil { - t.Fatalf("unexpected error, %v", err) +`, + Package: "goa.design/goa/v3", + Expected: `goa.design/goa/v3@v3.0.3-0.20190704022140-85024ebc66dc`, + }, + { + Name: "not found", + Mod: `module calc +go 1.12 +require ( + github.com/ikawaha/kagome v1.0.0 // indirect + goa.design/plugins/v3 v3.0.1 +) +`, + Package: "goa.design/goa/v3", + Expected: "goa.design/goa/v3", + }, + { + Name: "with replace", + Mod: `module calc +go 1.12 +replace goa.design/goa/v3 => ../../../goa.design/goa +require ( + github.com/ikawaha/kagome v1.0.0 // indirect + goa.design/goa/v3 v3.0.3-0.20190704022140-85024ebc66dc + goa.design/plugins/v3 v3.0.1 +) +`, + Package: "goa.design/goa/v3", + Expected: `goa.design/goa/v3@v3.0.3-0.20190704022140-85024ebc66dc`, + }, } - if pkg != expected { - t.Errorf("expected %v, got %v", expected, pkg) + for _, c := range cases { + t.Run(c.Name, func(t *testing.T) { + pkg, err := parseGoModGoaPackage("goa.design/goa/v3", strings.NewReader(c.Mod)) + if err != nil { + t.Fatalf("unexpected error, %v", err) + } + if pkg != c.Expected { + t.Errorf("expected %v, got %v", c.Expected, pkg) + } + }) } }