github.com/whoyao/protocol@v0.0.0-20230519045905-2d8ace718ca5/magefile.go (about) 1 //go:build mage 2 // +build mage 3 4 package main 5 6 import ( 7 "context" 8 "fmt" 9 "go/build" 10 "os" 11 "os/exec" 12 "path/filepath" 13 14 "github.com/livekit/mageutil" 15 ) 16 17 var Default = Proto 18 19 func Bootstrap() error { 20 return mageutil.Run(context.Background(), 21 "go install github.com/twitchtv/twirp/protoc-gen-twirp@latest", 22 "go install google.golang.org/protobuf/cmd/protoc-gen-go@latest", 23 "go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest", 24 "go install github.com/livekit/psrpc/protoc-gen-psrpc@latest", 25 ) 26 } 27 28 // regenerate protobuf 29 func Proto() error { 30 twirpProtoFiles := []string{ 31 "livekit_egress.proto", 32 "livekit_ingress.proto", 33 "livekit_room.proto", 34 } 35 grpcProtoFiles := []string{ 36 "livekit_analytics.proto", 37 "livekit_internal.proto", 38 "livekit_models.proto", 39 "livekit_rpc_internal.proto", 40 "livekit_rtc.proto", 41 "livekit_webhook.proto", 42 } 43 psrpcProtoFiles := []string{ 44 "rpc/egress.proto", 45 "rpc/ingress.proto", 46 "rpc/io.proto", 47 "rpc/signal.proto", 48 } 49 50 fmt.Println("generating protobuf") 51 target := "livekit" 52 if err := os.MkdirAll(target, 0755); err != nil { 53 return err 54 } 55 56 protoc, err := getToolPath("protoc") 57 if err != nil { 58 return err 59 } 60 protocGoPath, err := getToolPath("protoc-gen-go") 61 if err != nil { 62 return err 63 } 64 twirpPath, err := getToolPath("protoc-gen-twirp") 65 if err != nil { 66 return err 67 } 68 protocGrpcGoPath, err := getToolPath("protoc-gen-go-grpc") 69 if err != nil { 70 return err 71 } 72 fmt.Println("generating twirp protobuf") 73 args := append([]string{ 74 "--go_out", target, 75 "--twirp_out", target, 76 "--go_opt=paths=source_relative", 77 "--twirp_opt=paths=source_relative", 78 "--plugin=go=" + protocGoPath, 79 "--plugin=twirp=" + twirpPath, 80 "-I=.", 81 }, twirpProtoFiles...) 82 cmd := exec.Command(protoc, args...) 83 connectStd(cmd) 84 if err := cmd.Run(); err != nil { 85 return err 86 } 87 fmt.Println("generating grpc protobuf") 88 args = append([]string{ 89 "--go_out", target, 90 "--go-grpc_out", target, 91 "--go_opt=paths=source_relative", 92 "--go-grpc_opt=paths=source_relative", 93 "--plugin=go=" + protocGoPath, 94 "--plugin=go-grpc=" + protocGrpcGoPath, 95 "-I=.", 96 }, grpcProtoFiles...) 97 cmd = exec.Command(protoc, args...) 98 connectStd(cmd) 99 if err := cmd.Run(); err != nil { 100 return err 101 } 102 103 fmt.Println("generating psrpc protobuf") 104 105 psrpcDir, err := mageutil.GetPkgDir("github.com/livekit/psrpc") 106 if err != nil { 107 return err 108 } 109 psrpcPath, err := mageutil.GetToolPath("protoc-gen-psrpc") 110 if err != nil { 111 return err 112 } 113 114 args = append([]string{ 115 "--go_out", ".", 116 "--psrpc_out", ".", 117 "--go_opt=paths=source_relative", 118 "--psrpc_opt=paths=source_relative", 119 "--plugin=go=" + protocGoPath, 120 "--plugin=psrpc=" + psrpcPath, 121 "-I" + psrpcDir + "/protoc-gen-psrpc/options", 122 "-I=.", 123 }, psrpcProtoFiles...) 124 cmd = exec.Command(protoc, args...) 125 mageutil.ConnectStd(cmd) 126 if err = cmd.Run(); err != nil { 127 return err 128 } 129 130 return nil 131 } 132 133 // run tests 134 func Test() error { 135 cmd := exec.Command("go", "test", "-race", "./...") 136 connectStd(cmd) 137 return cmd.Run() 138 } 139 140 // helpers 141 142 func getToolPath(name string) (string, error) { 143 if p, err := exec.LookPath(name); err == nil { 144 return p, nil 145 } 146 // check under gopath 147 gopath := os.Getenv("GOPATH") 148 if gopath == "" { 149 gopath = build.Default.GOPATH 150 } 151 p := filepath.Join(gopath, "bin", name) 152 if _, err := os.Stat(p); err != nil { 153 return "", err 154 } 155 return p, nil 156 } 157 158 func connectStd(cmd *exec.Cmd) { 159 cmd.Stdout = os.Stdout 160 cmd.Stderr = os.Stderr 161 }