-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
172 additions
and
162 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
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,27 @@ | ||
package rpc | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func genProto() { | ||
path := fmt.Sprintf("rpc/%s/v%s/%s.proto", server, version, service) | ||
if !fileExists(path) { | ||
tpl := &protoTpl{ | ||
Server: server, | ||
Version: version, | ||
Service: upper1st(service), | ||
} | ||
|
||
save(path, tpl) | ||
} | ||
|
||
cmd := exec.Command("make", "rpc") | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
if err := cmd.Run(); err != nil { | ||
panic(err) | ||
} | ||
} |
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,112 @@ | ||
package rpc | ||
|
||
import ( | ||
"bytes" | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"os" | ||
"path/filepath" | ||
"text/template" | ||
|
||
"golang.org/x/mod/modfile" | ||
) | ||
|
||
func module() string { | ||
b, err := os.ReadFile("go.mod") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
f, err := modfile.Parse("", b, nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return f.Module.Mod.Path | ||
} | ||
|
||
func isSniperDir() bool { | ||
dirs, err := os.ReadDir(".") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// 检查 sniper 项目目录结构 | ||
// sniper 项目依赖 cmd/pkg/rpc 三个目录 | ||
sniperDirs := map[string]bool{"cmd": true, "pkg": true, "rpc": true} | ||
|
||
c := 0 | ||
for _, d := range dirs { | ||
if sniperDirs[d.Name()] { | ||
c++ | ||
} | ||
} | ||
|
||
return c != len(sniperDirs) | ||
} | ||
|
||
func parseAST(file string, b []byte) (*ast.File, *token.FileSet) { | ||
if b == nil { | ||
var err error | ||
b, err = os.ReadFile(file) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
fset := token.NewFileSet() | ||
a, err := parser.ParseFile(fset, "", string(b), parser.ParseComments) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return a, fset | ||
} | ||
|
||
func upper1st(s string) string { | ||
if len(s) == 0 { | ||
return s | ||
} | ||
|
||
r := []rune(s) | ||
|
||
if r[0] >= 97 && r[0] <= 122 { | ||
r[0] -= 32 // 大小写字母ASCII值相差32位 | ||
} | ||
|
||
return string(r) | ||
} | ||
|
||
func save(path string, t tpl) { | ||
buf := &bytes.Buffer{} | ||
|
||
tmpl, err := template.New("sniper").Parse(t.tpl()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = tmpl.Execute(buf, t) | ||
if err != nil { | ||
panic(err) | ||
} | ||
dir := filepath.Dir(path) | ||
if err := os.MkdirAll(dir, 0755); err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := os.WriteFile(path, buf.Bytes(), 0644); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// 判断文件是否存在 | ||
func fileExists(file string) bool { | ||
fd, err := os.Open(file) | ||
defer fd.Close() | ||
|
||
if err != nil && os.IsNotExist(err) { | ||
return false | ||
} | ||
return true | ||
} |