-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
87 lines (72 loc) · 2.67 KB
/
magefile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//go:build mage
package main
import (
"go/format"
"io"
"net/http"
"os"
"regexp"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var Aliases = map[string]any{
"install": Install.Release,
}
type Install mg.Namespace
func (Install) Release() error {
return sh.RunWith(map[string]string{
"CGO_ENABLED": "0",
}, mg.GoCmd(), "install", "-ldflags", "-w -s", "-trimpath", "./cmd/protols")
}
func (Install) Debug() error {
return sh.RunWith(map[string]string{
"CGO_ENABLED": "0",
}, mg.GoCmd(), "install", "-gcflags", "all=-N -l", "./cmd/protols")
}
func Test() error {
return sh.RunV(mg.GoCmd(), "test", "-race", "./...")
}
type Maintenance mg.Namespace
func (Maintenance) SyncUpstreamGrpcGen() error {
const upstreamUrl = "https://raw.githubusercontent.com/grpc/grpc-go/master/cmd/protoc-gen-go-grpc/grpc.go"
const filename = "sdk/codegen/generators/golang/grpc/upstream_gen.go"
resp, err := http.Get(upstreamUrl)
if err != nil {
return err
}
upstreamBytes, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return err
}
upstream := string(upstreamBytes)
upstream = strings.Replace(upstream, "package main", "package grpc", 1)
// remove serviceGenerateHelper type
upstream = strings.Replace(upstream, "type serviceGenerateHelper struct{}", "", 1)
// remove global helper instance
upstream = strings.Replace(upstream, "var helper serviceGenerateHelperInterface = serviceGenerateHelper{}", "", 1)
// convert all serviceGenerateHelper methods to generator methods
upstream = strings.ReplaceAll(upstream, "func (serviceGenerateHelper) ", "func (x_ generator)")
// replace usages of global helper methods with generator methods
upstream = strings.ReplaceAll(upstream, "helper.", "x_.")
// replace usages of global variables with generator scoped variables
upstream = strings.ReplaceAll(upstream, "*requireUnimplemented", "x_.requireUnimplemented")
upstream = strings.ReplaceAll(upstream, "*useGenericStreams", "x_.useGenericStreams")
// find all unexported functions in $filename
matches := regexp.MustCompile(`(?m)^func ([a-z][a-zA-Z]+)\(`).FindAllStringSubmatch(upstream, -1)
for _, match := range matches {
funcName := match[1]
// prefix existing usages with 'x_.'
upstream = strings.ReplaceAll(upstream, funcName+"(", "x_."+funcName+"(")
// convert the function to be a generator method
upstream = strings.ReplaceAll(upstream, "func x_."+funcName, "func (x_ generator) "+funcName)
// fix up doc comments
upstream = strings.ReplaceAll(upstream, "// x_."+funcName, "// "+funcName)
}
formattedBytes, err := format.Source([]byte(upstream))
if err != nil {
return err
}
return os.WriteFile(filename, formattedBytes, 0644)
}