Skip to content

Commit

Permalink
feat(tool): support custom generate path (cloudwego#775)
Browse files Browse the repository at this point in the history
HeyJavaBean authored Jan 4, 2023
1 parent 8047347 commit 5756d3b
Showing 9 changed files with 83 additions and 29 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -6,10 +6,10 @@ require (
github.com/apache/thrift v0.13.0
github.com/bytedance/gopkg v0.0.0-20220531084716-665b4f21126f
github.com/choleraehyq/pid v0.0.15
github.com/cloudwego/fastpb v0.0.3
github.com/cloudwego/fastpb v0.0.4-0.20221229062926-593805b771c0
github.com/cloudwego/frugal v0.1.3
github.com/cloudwego/netpoll v0.3.1
github.com/cloudwego/thriftgo v0.2.4
github.com/cloudwego/thriftgo v0.2.6
github.com/golang/mock v1.6.0
github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3
github.com/jhump/protoreflect v1.8.2
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -17,17 +17,17 @@ github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4M
github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic=
github.com/chzyer/test v0.0.0-20210722231415-061457976a23/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudwego/fastpb v0.0.3 h1:GZE0WzlnjQFE3+vkYFZd964PGT9AXOuvir+JGzuBSPM=
github.com/cloudwego/fastpb v0.0.3/go.mod h1:/V13XFTq2TUkxj2qWReV8MwfPC4NnPcy6FsrojnsSG0=
github.com/cloudwego/fastpb v0.0.4-0.20221229062926-593805b771c0 h1:5C8dxHrgHMg3GNEiwPj1xt+iIFanOpb12sM4eFxQV+Q=
github.com/cloudwego/fastpb v0.0.4-0.20221229062926-593805b771c0/go.mod h1:/V13XFTq2TUkxj2qWReV8MwfPC4NnPcy6FsrojnsSG0=
github.com/cloudwego/frugal v0.1.3 h1:tw3+hh4YMmtHFHRue3OGYjAnkxnZRHqeAyG18+7z5aI=
github.com/cloudwego/frugal v0.1.3/go.mod h1:b981ViPYdhI56aFYsoMjl9kv6yeqYSO+iEz2jrhkCgI=
github.com/cloudwego/kitex v0.3.2/go.mod h1:/XD07VpUD9VQWmmoepASgZ6iw//vgWikVA9MpzLC5i0=
github.com/cloudwego/netpoll v0.2.4/go.mod h1:1T2WVuQ+MQw6h6DpE45MohSvDTKdy2DlzCx2KsnPI4E=
github.com/cloudwego/netpoll v0.3.1 h1:xByoORmCLIyKZ8gS+da06WDo3j+jvmhaqS2KeKejtBk=
github.com/cloudwego/netpoll v0.3.1/go.mod h1:1T2WVuQ+MQw6h6DpE45MohSvDTKdy2DlzCx2KsnPI4E=
github.com/cloudwego/thriftgo v0.1.2/go.mod h1:LzeafuLSiHA9JTiWC8TIMIq64iadeObgRUhmVG1OC/w=
github.com/cloudwego/thriftgo v0.2.4 h1:o3JTSygQXaNHmggZYqAkfCBdPGWuKH1Q8XCflCvsSIY=
github.com/cloudwego/thriftgo v0.2.4/go.mod h1:8i9AF5uDdWHGqzUhXDlubCjx4MEfKvWXGQlMWyR0tM4=
github.com/cloudwego/thriftgo v0.2.6 h1:tU0E1UIrCogbiW0SD+1XW3Nv6JAGvx08aAmW2Kefztg=
github.com/cloudwego/thriftgo v0.2.6/go.mod h1:8i9AF5uDdWHGqzUhXDlubCjx4MEfKvWXGQlMWyR0tM4=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -216,7 +216,6 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
google.golang.org/protobuf v1.25.1-0.20200805231151-a709e31e5d12/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
16 changes: 11 additions & 5 deletions tool/cmd/kitex/args/args.go
Original file line number Diff line number Diff line change
@@ -107,6 +107,8 @@ func (a *Arguments) buildFlags(version string) *flag.FlagSet {
"Use frugal to compile arguments and results when new clients and servers.")
f.BoolVar(&a.Record, "record", false,
"Record Kitex cmd into kitex-all.sh.")
f.StringVar(&a.GenPath, "gen-path", generator.KitexGenPath,
"Specify a code gen path.")
a.RecordCmd = os.Args
a.Version = version
a.ThriftOptions = append(a.ThriftOptions,
@@ -151,6 +153,10 @@ func (a *Arguments) ParseArgs(version string) {

a.checkIDL(f.Args())
a.checkServiceName()
// todo finish protobuf
if a.IDLType != "thrift" {
a.GenPath = generator.KitexGenPath
}
a.checkPath()
}

@@ -215,7 +221,7 @@ func (a *Arguments) checkPath() {
log.Warn("Get GOPATH/src relpath failed:", err.Error())
os.Exit(1)
}
a.PackagePrefix = filepath.Join(a.PackagePrefix, generator.KitexGenPath)
a.PackagePrefix = filepath.Join(a.PackagePrefix, a.GenPath)
} else {
if a.ModuleName == "" {
log.Warn("Outside of $GOPATH. Please specify a module name with the '-module' flag.")
@@ -236,13 +242,13 @@ func (a *Arguments) checkPath() {
log.Warn("Get package prefix failed:", err.Error())
os.Exit(1)
}
a.PackagePrefix = filepath.Join(a.ModuleName, a.PackagePrefix, generator.KitexGenPath)
a.PackagePrefix = filepath.Join(a.ModuleName, a.PackagePrefix, a.GenPath)
} else {
if err = initGoMod(pathToGo, a.ModuleName); err != nil {
log.Warn("Init go mod failed:", err.Error())
os.Exit(1)
}
a.PackagePrefix = filepath.Join(a.ModuleName, generator.KitexGenPath)
a.PackagePrefix = filepath.Join(a.ModuleName, a.GenPath)
}
}

@@ -296,7 +302,7 @@ func (a *Arguments) BuildCmd(out io.Writer) *exec.Cmd {
cmd.Args = append(cmd.Args, "-r")
}
cmd.Args = append(cmd.Args,
"-o", generator.KitexGenPath,
"-o", a.GenPath,
"-g", gas,
"-p", "kitex="+exe+":"+kas,
)
@@ -317,7 +323,7 @@ func (a *Arguments) BuildCmd(out io.Writer) *exec.Cmd {
for _, inc := range a.Includes {
cmd.Args = append(cmd.Args, "-I", inc)
}
outPath := filepath.Join(".", generator.KitexGenPath)
outPath := filepath.Join(".", a.GenPath)
if a.Use == "" {
os.MkdirAll(outPath, 0o755)
} else {
7 changes: 3 additions & 4 deletions tool/internal_pkg/generator/generator.go
Original file line number Diff line number Diff line change
@@ -123,6 +123,8 @@ type Config struct {

Record bool
RecordCmd []string

GenPath string
}

// Pack packs the Config into a slice of "key=val" strings.
@@ -361,10 +363,7 @@ func (g *generator) GenerateMainPackage(pkg *PackageInfo) (fs []*File, err error

func (g *generator) GenerateService(pkg *PackageInfo) ([]*File, error) {
g.updatePackageInfo(pkg)
output := filepath.Join(g.OutputPath, KitexGenPath)
if pkg.Namespace != "" {
output = filepath.Join(output, strings.ReplaceAll(pkg.Namespace, ".", "/"))
}
output := filepath.Join(g.OutputPath, util.CombineOutputPath(g.GenPath, pkg.Namespace))
svcPkg := strings.ToLower(pkg.ServiceName)
output = filepath.Join(output, svcPkg)
ext := g.tmplExt
2 changes: 1 addition & 1 deletion tool/internal_pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ func TestConfig_Pack(t *testing.T) {
{
name: "some",
fields: fields{Features: []feature{feature(999)}, ThriftPluginTimeLimit: 30 * time.Second},
wantRes: []string{"Verbose=false", "GenerateMain=false", "GenerateInvoker=false", "Version=", "NoFastAPI=false", "ModuleName=", "ServiceName=", "Use=", "IDLType=", "Includes=", "ThriftOptions=", "ProtobufOptions=", "IDL=", "OutputPath=", "PackagePrefix=", "CombineService=false", "CopyIDL=false", "Features=999", "FrugalPretouch=false", "ThriftPluginTimeLimit=30s", "ExtensionFile=", "Record=false", "RecordCmd="},
wantRes: []string{"Verbose=false", "GenerateMain=false", "GenerateInvoker=false", "Version=", "NoFastAPI=false", "ModuleName=", "ServiceName=", "Use=", "IDLType=", "Includes=", "ThriftOptions=", "ProtobufOptions=", "IDL=", "OutputPath=", "PackagePrefix=", "CombineService=false", "CopyIDL=false", "Features=999", "FrugalPretouch=false", "ThriftPluginTimeLimit=30s", "ExtensionFile=", "Record=false", "RecordCmd=", "GenPath="},
},
}
for _, tt := range tests {
7 changes: 3 additions & 4 deletions tool/internal_pkg/pluginmode/thriftgo/convertor.go
Original file line number Diff line number Diff line change
@@ -24,6 +24,8 @@ import (
"regexp"
"strings"

"github.com/cloudwego/kitex/tool/internal_pkg/util"

"github.com/cloudwego/thriftgo/generator/backend"
"github.com/cloudwego/thriftgo/generator/golang"
"github.com/cloudwego/thriftgo/parser"
@@ -298,7 +300,6 @@ func (c *converter) convertTypes(req *plugin.Request) error {
c.svc2ast = make(map[*generator.ServiceInfo]*parser.Thrift)
for ast := range req.AST.DepthFirstSearch() {
ref, pkg, pth := c.Utils.ParseNamespace(ast)

// make the current ast as an include to produce correct type references.
fake := c.copyTreeWithRef(ast, ref)
fake.Name2Category = nil
@@ -316,13 +317,11 @@ func (c *converter) convertTypes(req *plugin.Request) error {
return fmt.Errorf("build scope for fake ast '%s': %w", ast.Filename, err)
}
c.Utils.SetRootScope(scope)

pi := generator.PkgInfo{
PkgName: pkg,
PkgRefName: pkg,
ImportPath: filepath.Join(c.Config.PackagePrefix, pth),
ImportPath: util.CombineOutputPath(c.Config.PackagePrefix, pth),
}

for _, svc := range scope.Services() {
si, err := c.makeService(pi, svc)
if err != nil {
14 changes: 6 additions & 8 deletions tool/internal_pkg/pluginmode/thriftgo/patcher.go
Original file line number Diff line number Diff line change
@@ -176,17 +176,15 @@ func (p *patcher) patch(req *plugin.Request) (patches []*plugin.Generated, err e
}
p.utils.SetRootScope(scope)

namespace := ast.GetNamespaceOrReferenceName("go")
pkgName := p.utils.NamespaceToPackage(namespace)
pkgName := p.utils.RootScope().FilePackage()

path := p.utils.GetFilePath(ast)
full := filepath.Join(req.OutputPath, path)
dir, base := filepath.Split(full)
target := filepath.Join(dir, "k-"+base)
path := p.utils.CombineOutputPath(req.OutputPath, ast)
base := p.utils.GetFilename(ast)
target := filepath.Join(path, "k-"+base)

// Define KitexUnusedProtection in k-consts.go .
// Add k-consts.go before target to force the k-consts.go generated by consts.thrift to be renamed.
consts := filepath.Join(filepath.Dir(full), "k-consts.go")
consts := filepath.Join(path, "k-consts.go")
if protection[consts] == nil {
patch := &plugin.Generated{
Content: "package " + pkgName + "\n" + kitexUnusedProtection,
@@ -226,7 +224,7 @@ func (p *patcher) patch(req *plugin.Request) (patches []*plugin.Generated, err e
if err != nil {
return nil, fmt.Errorf("read %q: %w", ast.Filename, err)
}
path := filepath.Join(filepath.Dir(full), filepath.Base(ast.Filename))
path := filepath.Join(path, filepath.Base(ast.Filename))
patches = append(patches, &plugin.Generated{
Content: string(content),
Name: &path,
19 changes: 19 additions & 0 deletions tool/internal_pkg/util/util.go
Original file line number Diff line number Diff line change
@@ -212,3 +212,22 @@ func RunGitCommand(gitLink string) (string, string, error) {

return gitPath, "", nil
}

// CombineOutputPath read the output and path variables and render them into the final path
func CombineOutputPath(outputPath, ns string) string {
if ns != "" {
ns = strings.ReplaceAll(ns, ".", "/")
}
hasVarNs := strings.Contains(outputPath, "{namespace}")
hasVarNsUnderscore := strings.Contains(outputPath, "{namespaceUnderscore}")
if hasVarNs || hasVarNsUnderscore {
if hasVarNs {
outputPath = strings.ReplaceAll(outputPath, "{namespace}", ns)
} else if hasVarNsUnderscore {
outputPath = strings.ReplaceAll(outputPath, "{namespaceUnderscore}", strings.ReplaceAll(ns, "/", "_"))
}
} else {
outputPath = filepath.Join(outputPath, ns)
}
return outputPath
}
34 changes: 34 additions & 0 deletions tool/internal_pkg/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2022 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util

import (
"testing"

"github.com/cloudwego/kitex/internal/test"
)

func TestCombineOutputPath(t *testing.T) {
ns := "aaa.bbb.ccc"
path1 := "kitex_path/code"
output1 := CombineOutputPath(path1, ns)
test.Assert(t, output1 == "kitex_path/code/aaa/bbb/ccc")
path2 := "kitex_path/{namespace}/code"
output2 := CombineOutputPath(path2, ns)
test.Assert(t, output2 == "kitex_path/aaa/bbb/ccc/code")
path3 := "kitex_path/{namespaceUnderscore}/code"
output3 := CombineOutputPath(path3, ns)
test.Assert(t, output3 == "kitex_path/aaa_bbb_ccc/code")
}

0 comments on commit 5756d3b

Please sign in to comment.