Skip to content

Commit dd42e4c

Browse files
committed
dubbogo-cli-v2 新增hessian生成器模式
1 parent d0cdc25 commit dd42e4c

File tree

7 files changed

+569
-0
lines changed

7 files changed

+569
-0
lines changed

cmd/dubbogo-cli-v2/cmd/hessian.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cmd
2+
3+
import (
4+
"runtime"
5+
)
6+
7+
import (
8+
"github.com/spf13/cobra"
9+
)
10+
11+
import (
12+
"github.com/dubbogo/tools/cmd/dubbogo-cli-v2/generator/sample"
13+
"github.com/dubbogo/tools/cmd/dubbogo-cli-v2/generator/sample/hessian"
14+
)
15+
16+
// hessianCmd represents the hessian-register-generator command
17+
var hessianCmd = &cobra.Command{
18+
Use: "hessian",
19+
Short: "automatic generate hessian pojo register statement",
20+
Run: generateHessianRegistry,
21+
}
22+
23+
func init() {
24+
rootCmd.AddCommand(hessianCmd)
25+
hessianCmd.Flags().StringP(hessian.CmdFlagInclude, "i", "./", "file scan directory path, default `./`")
26+
hessianCmd.Flags().IntP(hessian.CmdFlagThread, "t", runtime.NumCPU()*2, "worker thread limit, default (cpu core) * 2")
27+
hessianCmd.Flags().BoolP(hessian.CmdFlagOnlyError, "e", false, "only print error message, default false")
28+
}
29+
30+
func generateHessianRegistry(cmd *cobra.Command, _ []string) {
31+
var include string
32+
var thread int
33+
var onlyError bool
34+
var err error
35+
36+
include, err = cmd.Flags().GetString(hessian.CmdFlagInclude)
37+
if err != nil {
38+
panic(err)
39+
}
40+
41+
thread, err = cmd.Flags().GetInt(hessian.CmdFlagThread)
42+
if err != nil {
43+
panic(err)
44+
}
45+
46+
onlyError, err = cmd.Flags().GetBool(hessian.CmdFlagOnlyError)
47+
if err != nil {
48+
panic(err)
49+
}
50+
51+
if err = sample.HessianRegistryGenerate(include, thread, onlyError); err != nil {
52+
panic(err)
53+
}
54+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package hessian
19+
20+
const (
21+
CmdFlagInclude = "include"
22+
CmdFlagThread = "thread"
23+
CmdFlagOnlyError = "error"
24+
)
25+
26+
const (
27+
PackageRegexp = `^package\s[a-zA-Z_][0-9a-zA-Z_]*$`
28+
29+
LineCommentRegexp = `\/\/`
30+
MutLineCommentStartRegexp = `\/\*`
31+
MutLineCommentEndRegexp = `\*\/`
32+
33+
InitFunctionRegexp = `^func\sinit\(\)\s\{$`
34+
35+
HessianImportRegexp = `"github.com/apache/dubbo-go-hessian2"`
36+
37+
HessianPOJORegexp = `\*[0-9a-zA-Z_]+\)\sJavaClassName\(\)\sstring\s\{$`
38+
HessianPOJONameRegexp = `\*[0-9a-zA-Z_]+\)`
39+
)
40+
41+
const (
42+
newLine byte = '\n'
43+
funcEnd byte = '}'
44+
45+
TargetFileSuffix = ".go"
46+
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package hessian
19+
20+
import (
21+
"fmt"
22+
"io/fs"
23+
"os"
24+
"path/filepath"
25+
"strings"
26+
)
27+
28+
// ListFiles 获取目标目录下所有go文件
29+
func ListFiles(dirPath string, suffix string) (fileList []string, err error) {
30+
suffix = strings.ToUpper(suffix)
31+
fileList = make([]string, 0)
32+
_, err = os.Lstat(dirPath)
33+
if err != nil {
34+
return nil, fmt.Errorf("找不到该目录[%s]", dirPath)
35+
}
36+
err = filepath.WalkDir(dirPath, func(path string, d fs.DirEntry, err error) error { // 递归获取目录下所有go文件
37+
if d == nil || d.IsDir() {
38+
return nil
39+
}
40+
if strings.HasSuffix(strings.ToUpper(d.Name()), suffix) {
41+
fileList = append(fileList, path)
42+
}
43+
return nil
44+
})
45+
return
46+
}

0 commit comments

Comments
 (0)