-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: HeyJavaBean <[email protected]>
- Loading branch information
1 parent
1412205
commit 9124651
Showing
32 changed files
with
1,792 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ thriftgo | |
gen-* | ||
.vscode | ||
.idea | ||
tool/trimmer/trimmer_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2021 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 main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"github.com/cloudwego/thriftgo/plugin" | ||
"github.com/cloudwego/thriftgo/version" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// StringSlice implements the flag.Value interface on string slices | ||
// to allow a flag to be set multiple times. | ||
type StringSlice []string | ||
|
||
func (ss *StringSlice) String() string { | ||
return fmt.Sprintf("%v", *ss) | ||
} | ||
|
||
// Set implements the flag.Value interface. | ||
func (ss *StringSlice) Set(value string) error { | ||
*ss = append(*ss, value) | ||
return nil | ||
} | ||
|
||
// Arguments contains command line arguments for thriftgo. | ||
type Arguments struct { | ||
AskVersion bool | ||
OutputFile string | ||
IDL string | ||
Recurse string | ||
} | ||
|
||
// BuildFlags initializes command line flags. | ||
func (a *Arguments) BuildFlags() *flag.FlagSet { | ||
f := flag.NewFlagSet(os.Args[0], flag.ContinueOnError) | ||
|
||
f.BoolVar(&a.AskVersion, "version", false, "") | ||
|
||
f.StringVar(&a.OutputFile, "o", "", "") | ||
f.StringVar(&a.OutputFile, "out", "", "") | ||
|
||
f.StringVar(&a.Recurse, "r", "", "") | ||
f.StringVar(&a.Recurse, "recurse", "", "") | ||
|
||
f.Usage = help | ||
return f | ||
} | ||
|
||
// Parse parse command line arguments. | ||
func (a *Arguments) Parse(argv []string) error { | ||
f := a.BuildFlags() | ||
if err := f.Parse(argv[1:]); err != nil { | ||
return err | ||
} | ||
|
||
if a.AskVersion { | ||
return nil | ||
} | ||
|
||
rest := f.Args() | ||
if len(rest) != 1 { | ||
return fmt.Errorf("require exactly 1 argument for the IDL parameter, got: %d", len(rest)) | ||
} | ||
|
||
a.IDL = rest[0] | ||
return nil | ||
} | ||
|
||
func help() { | ||
println("Version:", version.ThriftgoVersion) | ||
println(`Usage: trimmer [options] file | ||
Options: | ||
--version Print the compiler version and exit. | ||
-h, --help Print help message and exit. | ||
-o, --out [file/dir] Specify the output IDL file/dir. | ||
-r, --recurse [dir] Specify a root dir and dump the included IDL recursively beneath the given root. -o should be set as a directory. | ||
`) | ||
// print backend options | ||
for _, b := range g.AllBackend() { | ||
name, lang := b.Name(), b.Lang() | ||
println(fmt.Sprintf(" %s (%s):", name, lang)) | ||
println(align(b.Options())) | ||
} | ||
println() | ||
os.Exit(2) | ||
} | ||
|
||
// align the help strings for plugin options. | ||
func align(opts []plugin.Option) string { | ||
var names, descs, ss []string | ||
max := 0 | ||
for _, opt := range opts { | ||
names = append(names, opt.Name) | ||
descs = append(descs, opt.Desc) | ||
if max <= len(opt.Name) { | ||
max = len(opt.Name) | ||
} | ||
} | ||
|
||
for i := range names { | ||
rest := 2 + max - len(names[i]) | ||
ss = append(ss, fmt.Sprintf( | ||
" %s:%s%s", | ||
names[i], | ||
strings.Repeat(" ", rest), | ||
descs[i], | ||
)) | ||
} | ||
return strings.Join(ss, "\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2023 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 main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// create directory-tree before dump | ||
func createDirTree(sourceDir string, destinationDir string) { | ||
err := filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if info.IsDir() { | ||
newDir := filepath.Join(destinationDir, path[len(sourceDir):]) | ||
if path[len(sourceDir)-1] != filepath.Separator { | ||
newDir = filepath.Join(destinationDir, path[len(sourceDir)-1:]) | ||
} | ||
err := os.MkdirAll(newDir, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
fmt.Printf("manage output error: %v\n", err) | ||
os.Exit(2) | ||
} | ||
} | ||
|
||
// remove empty directory of output dir-tree | ||
func removeEmptyDir(source string) { | ||
err := filepath.Walk(source, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if info.IsDir() { | ||
empty, err := isDirectoryEmpty(path) | ||
if err != nil { | ||
return err | ||
} | ||
if empty { | ||
err := os.Remove(path) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
parent := filepath.Dir(source) | ||
if parent != source { | ||
removeEmptyDir(parent) | ||
} | ||
|
||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
} | ||
} | ||
|
||
func isDirectoryEmpty(path string) (bool, error) { | ||
dir, err := os.Open(path) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer dir.Close() | ||
|
||
_, err = dir.Readdirnames(1) | ||
if err == nil { | ||
return false, nil | ||
} | ||
|
||
if len(err.Error()) > len("EOF") { | ||
return false, err | ||
} | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2023 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 dump | ||
|
||
import ( | ||
"bytes" | ||
"github.com/cloudwego/thriftgo/parser" | ||
"html" | ||
"html/template" | ||
"strings" | ||
) | ||
|
||
// DumpIDL Dump the ast to idl string | ||
func DumpIDL(ast *parser.Thrift) (string, error) { | ||
tmpl, _ := template.New("thrift").Funcs(template.FuncMap{"RemoveLastComma": RemoveLastComma, | ||
"JoinQuotes": JoinQuotes, "ReplaceQuotes": ReplaceQuotes}). | ||
Parse(IDLTemplate + TypeDefTemplate + AnnotationsTemplate + | ||
ConstantTemplate + EnumTemplate + ConstValueTemplate + FieldTemplate + StructTemplate + UnionTemplate + | ||
ExceptionTemplate + ServiceTemplate + FunctionTemplate + SingleLineFieldTemplate + TypeTemplate + | ||
ConstListTemplate + ConstMapTemplate) | ||
var buf bytes.Buffer | ||
if err := tmpl.Execute(&buf, ast); err != nil { | ||
return "", err | ||
} | ||
// deal with \\ | ||
escapedString := strings.Replace(buf.String(), """, "\\\"", -1) | ||
outString := strings.Replace(escapedString, "#OUTQUOTES", "\"", -1) | ||
return html.UnescapeString(outString), nil | ||
} |
Oops, something went wrong.