Skip to content

Commit

Permalink
Add tiny go.mod parser instead of third party package.
Browse files Browse the repository at this point in the history
  • Loading branch information
ikawaha committed Jul 7, 2019
1 parent 84ab7ae commit bcce715
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 22 deletions.
33 changes: 20 additions & 13 deletions cmd/goa/gen.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"bufio"
"fmt"
"go/parser"
"go/token"
"io"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -13,7 +15,6 @@ import (
"strconv"
"strings"

"github.com/sirkon/goproxy/gomod"
"goa.design/goa/v3/codegen"
"golang.org/x/tools/go/packages"
)
Expand Down Expand Up @@ -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.
Expand Down
66 changes: 57 additions & 9 deletions cmd/goa/gen_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"strings"
"testing"
)

Expand Down Expand Up @@ -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/[email protected]",
},
{
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/[email protected]`
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/[email protected]`,
},
{
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/[email protected]`,
},
}
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)
}
})
}
}

0 comments on commit bcce715

Please sign in to comment.