github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/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_egress.proto",
    47  		"livekit_ingress.proto",
    48  		"livekit_room.proto",
    49  		"livekit_sip.proto",
    50  	}
    51  	grpcProtoFiles := []string{
    52  		"livekit_agent.proto",
    53  		"livekit_analytics.proto",
    54  		"livekit_internal.proto",
    55  		"livekit_models.proto",
    56  		"livekit_rtc.proto",
    57  		"livekit_webhook.proto",
    58  	}
    59  	infraProtoFiles := []string{
    60  		"infra/link.proto",
    61  	}
    62  	psrpcProtoFiles := []string{
    63  		"rpc/agent.proto",
    64  		"rpc/egress.proto",
    65  		"rpc/ingress.proto",
    66  		"rpc/io.proto",
    67  		"rpc/keepalive.proto",
    68  		"rpc/participant.proto",
    69  		"rpc/room.proto",
    70  		"rpc/signal.proto",
    71  		"rpc/sip.proto",
    72  	}
    73  
    74  	fmt.Println("generating protobuf")
    75  	target := "livekit"
    76  	if err := os.MkdirAll(target, 0755); err != nil {
    77  		return err
    78  	}
    79  
    80  	protoc, err := getToolPath("protoc")
    81  	if err != nil {
    82  		return err
    83  	}
    84  	protocGoPath, err := getToolPath("protoc-gen-go")
    85  	if err != nil {
    86  		return err
    87  	}
    88  	twirpPath, err := getToolPath("protoc-gen-twirp")
    89  	if err != nil {
    90  		return err
    91  	}
    92  	protocGrpcGoPath, err := getToolPath("protoc-gen-go-grpc")
    93  	if err != nil {
    94  		return err
    95  	}
    96  	fmt.Println("generating twirp protobuf")
    97  	args := append([]string{
    98  		"--go_out", target,
    99  		"--twirp_out", target,
   100  		"--go_opt=paths=source_relative",
   101  		"--twirp_opt=paths=source_relative",
   102  		"--plugin=go=" + protocGoPath,
   103  		"--plugin=twirp=" + twirpPath,
   104  		"-I=./protobufs",
   105  	}, twirpProtoFiles...)
   106  	cmd := exec.Command(protoc, args...)
   107  	connectStd(cmd)
   108  	if err := cmd.Run(); err != nil {
   109  		return err
   110  	}
   111  	fmt.Println("generating grpc protobuf")
   112  	args = append([]string{
   113  		"--go_out", target,
   114  		"--go-grpc_out", target,
   115  		"--go_opt=paths=source_relative",
   116  		"--go-grpc_opt=paths=source_relative",
   117  		"--plugin=go=" + protocGoPath,
   118  		"--plugin=go-grpc=" + protocGrpcGoPath,
   119  		"-I=./protobufs",
   120  	}, grpcProtoFiles...)
   121  	cmd = exec.Command(protoc, args...)
   122  	connectStd(cmd)
   123  	if err := cmd.Run(); err != nil {
   124  		return err
   125  	}
   126  	fmt.Println("generating infra protobuf")
   127  	args = append([]string{
   128  		"--go_out", ".",
   129  		"--go-grpc_out", ".",
   130  		"--go_opt=paths=source_relative",
   131  		"--go-grpc_opt=paths=source_relative",
   132  		"--plugin=go=" + protocGoPath,
   133  		"--plugin=go-grpc=" + protocGrpcGoPath,
   134  		"-I=./protobufs",
   135  	}, infraProtoFiles...)
   136  	cmd = exec.Command(protoc, args...)
   137  	connectStd(cmd)
   138  	if err := cmd.Run(); err != nil {
   139  		return err
   140  	}
   141  
   142  	fmt.Println("generating psrpc protobuf")
   143  
   144  	psrpcDir, err := mageutil.GetPkgDir("github.com/livekit/psrpc")
   145  	if err != nil {
   146  		return err
   147  	}
   148  	psrpcPath, err := mageutil.GetToolPath("protoc-gen-psrpc")
   149  	if err != nil {
   150  		return err
   151  	}
   152  	if err := psrpc.CheckCompilerVersion(psrpcPath); err != nil {
   153  		return err
   154  	}
   155  
   156  	args = append([]string{
   157  		"--go_out", ".",
   158  		"--psrpc_out", ".",
   159  		"--go_opt=paths=source_relative",
   160  		"--psrpc_opt=paths=source_relative",
   161  		"--plugin=go=" + protocGoPath,
   162  		"--plugin=psrpc=" + psrpcPath,
   163  		"-I" + psrpcDir + "/protoc-gen-psrpc/options",
   164  		"-I=./protobufs",
   165  	}, psrpcProtoFiles...)
   166  	cmd = exec.Command(protoc, args...)
   167  	mageutil.ConnectStd(cmd)
   168  	if err = cmd.Run(); err != nil {
   169  		return err
   170  	}
   171  
   172  	return nil
   173  }
   174  
   175  // run tests
   176  func Test() error {
   177  	cmd := exec.Command("go", "test", "-race", "./...")
   178  	connectStd(cmd)
   179  	return cmd.Run()
   180  }
   181  
   182  // helpers
   183  
   184  func getToolPath(name string) (string, error) {
   185  	if p, err := exec.LookPath(name); err == nil {
   186  		return p, nil
   187  	}
   188  	// check under gopath
   189  	gopath := os.Getenv("GOPATH")
   190  	if gopath == "" {
   191  		gopath = build.Default.GOPATH
   192  	}
   193  	p := filepath.Join(gopath, "bin", name)
   194  	if _, err := os.Stat(p); err != nil {
   195  		return "", err
   196  	}
   197  	return p, nil
   198  }
   199  
   200  func connectStd(cmd *exec.Cmd) {
   201  	cmd.Stdout = os.Stdout
   202  	cmd.Stderr = os.Stderr
   203  }