github.com/livekit/protocol@v1.39.3/magefile.go (about) 1 // Copyright 2023 LiveKit, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //go:build mage 16 // +build mage 17 18 package main 19 20 import ( 21 "context" 22 "fmt" 23 "go/build" 24 "os" 25 "os/exec" 26 "path/filepath" 27 28 "github.com/livekit/mageutil" 29 "github.com/livekit/protocol/psrpc" 30 ) 31 32 var Default = Proto 33 34 func Bootstrap() error { 35 return mageutil.Run(context.Background(), 36 "go install github.com/twitchtv/twirp/protoc-gen-twirp@latest", 37 "go install google.golang.org/protobuf/cmd/protoc-gen-go@latest", 38 "go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest", 39 "go install github.com/livekit/psrpc/protoc-gen-psrpc@latest", 40 ) 41 } 42 43 // regenerate protobuf 44 func Proto() error { 45 twirpProtoFiles := []string{ 46 "livekit_agent_dispatch.proto", 47 "livekit_egress.proto", 48 "livekit_ingress.proto", 49 "livekit_room.proto", 50 "livekit_sip.proto", 51 "livekit_cloud_agent.proto", 52 } 53 protoFiles := []string{ 54 "livekit_agent.proto", 55 "livekit_analytics.proto", 56 "livekit_internal.proto", 57 "livekit_models.proto", 58 "livekit_rtc.proto", 59 "livekit_webhook.proto", 60 "livekit_metrics.proto", 61 } 62 grpcProtoFiles := []string{ 63 "infra/link.proto", 64 "rpc/analytics.proto", 65 } 66 psrpcProtoFiles := []string{ 67 "rpc/agent.proto", 68 "rpc/agent_dispatch.proto", 69 "rpc/egress.proto", 70 "rpc/ingress.proto", 71 "rpc/io.proto", 72 "rpc/keepalive.proto", 73 "rpc/participant.proto", 74 "rpc/room.proto", 75 "rpc/roommanager.proto", 76 "rpc/signal.proto", 77 "rpc/rest_signal.proto", 78 "rpc/sip.proto", 79 } 80 81 fmt.Println("generating protobuf") 82 target := "livekit" 83 if err := os.MkdirAll(target, 0755); err != nil { 84 return err 85 } 86 87 protoc, err := getToolPath("protoc") 88 if err != nil { 89 return err 90 } 91 protocGoPath, err := getToolPath("protoc-gen-go") 92 if err != nil { 93 return err 94 } 95 twirpPath, err := getToolPath("protoc-gen-twirp") 96 if err != nil { 97 return err 98 } 99 protocGrpcGoPath, err := getToolPath("protoc-gen-go-grpc") 100 if err != nil { 101 return err 102 } 103 104 fmt.Println("generating twirp protobuf") 105 args := append([]string{ 106 "--go_out", target, 107 "--twirp_out", target, 108 "--go_opt=paths=source_relative", 109 "--twirp_opt=paths=source_relative", 110 "--plugin=go=" + protocGoPath, 111 "--plugin=twirp=" + twirpPath, 112 "-I=./protobufs", 113 }, twirpProtoFiles...) 114 cmd := exec.Command(protoc, args...) 115 connectStd(cmd) 116 if err := cmd.Run(); err != nil { 117 return err 118 } 119 120 fmt.Println("generating replay twirp protobuf") 121 replayTarget := "replay" 122 args = append([]string{ 123 "--go_out", replayTarget, 124 "--twirp_out", replayTarget, 125 "--go_opt=paths=source_relative", 126 "--twirp_opt=paths=source_relative", 127 "--plugin=go=" + protocGoPath, 128 "--plugin=twirp=" + twirpPath, 129 "-I=./protobufs", 130 }, "cloud_replay.proto") 131 cmd = exec.Command(protoc, args...) 132 connectStd(cmd) 133 if err := cmd.Run(); err != nil { 134 return err 135 } 136 137 fmt.Println("generating protobuf") 138 args = append([]string{ 139 "--go_out", target, 140 "--go_opt=paths=source_relative", 141 "--plugin=go=" + protocGoPath, 142 "-I=./protobufs", 143 }, protoFiles...) 144 cmd = exec.Command(protoc, args...) 145 connectStd(cmd) 146 if err := cmd.Run(); err != nil { 147 return err 148 } 149 fmt.Println("generating grpc protobuf") 150 args = append([]string{ 151 "--go_out", ".", 152 "--go-grpc_out", ".", 153 "--go_opt=paths=source_relative", 154 "--go-grpc_opt=paths=source_relative", 155 "--plugin=go=" + protocGoPath, 156 "--plugin=go-grpc=" + protocGrpcGoPath, 157 "-I=./protobufs", 158 }, grpcProtoFiles...) 159 cmd = exec.Command(protoc, args...) 160 connectStd(cmd) 161 if err := cmd.Run(); err != nil { 162 return err 163 } 164 165 fmt.Println("generating psrpc protobuf") 166 167 psrpcDir, err := mageutil.GetPkgDir("github.com/livekit/psrpc") 168 if err != nil { 169 return err 170 } 171 psrpcPath, err := mageutil.GetToolPath("protoc-gen-psrpc") 172 if err != nil { 173 return err 174 } 175 if err := psrpc.CheckCompilerVersion(psrpcPath); err != nil { 176 return err 177 } 178 179 args = append([]string{ 180 "--go_out", ".", 181 "--psrpc_out", ".", 182 "--go_opt=paths=source_relative", 183 "--psrpc_opt=paths=source_relative", 184 "--plugin=go=" + protocGoPath, 185 "--plugin=psrpc=" + psrpcPath, 186 "-I" + psrpcDir + "/protoc-gen-psrpc/options", 187 "-I=./protobufs", 188 }, psrpcProtoFiles...) 189 cmd = exec.Command(protoc, args...) 190 mageutil.ConnectStd(cmd) 191 if err = cmd.Run(); err != nil { 192 return err 193 } 194 195 return nil 196 } 197 198 // run tests 199 func Test() error { 200 cmd := exec.Command("go", "test", "-race", "./...") 201 connectStd(cmd) 202 return cmd.Run() 203 } 204 205 // helpers 206 207 func getToolPath(name string) (string, error) { 208 if p, err := exec.LookPath(name); err == nil { 209 return p, nil 210 } 211 // check under gopath 212 gopath := os.Getenv("GOPATH") 213 if gopath == "" { 214 gopath = build.Default.GOPATH 215 } 216 p := filepath.Join(gopath, "bin", name) 217 if _, err := os.Stat(p); err != nil { 218 return "", err 219 } 220 return p, nil 221 } 222 223 func connectStd(cmd *exec.Cmd) { 224 cmd.Stdout = os.Stdout 225 cmd.Stderr = os.Stderr 226 }