You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.1 KiB
56 lines
1.1 KiB
package proto
|
|
|
|
import (
|
|
"context"
|
|
"github.com/magefile/mage/mg"
|
|
"io/fs"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func BaseProtocFlags() ([]string, error) {
|
|
return []string{"-I=proto"}, nil
|
|
}
|
|
|
|
type ProtocFlagsFunc func() ([]string, error)
|
|
|
|
func Sources() ([]string, error) {
|
|
result := []string(nil)
|
|
err := filepath.WalkDir("proto", func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !d.IsDir() && strings.HasSuffix(path, ".proto") {
|
|
result = append(result, path)
|
|
}
|
|
return nil
|
|
})
|
|
return result, err
|
|
}
|
|
|
|
func Compile(ctx context.Context, withPlugins []ProtocFlagsFunc) error {
|
|
flags, err := BaseProtocFlags()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, flagsFunc := range withPlugins {
|
|
pluginFlags, err := flagsFunc()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
flags = append(flags, pluginFlags...)
|
|
}
|
|
protoFiles, err := Sources()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
args := append(flags, protoFiles...)
|
|
cmd := exec.CommandContext(ctx, "protoc", args...)
|
|
cmd.Stderr = os.Stderr
|
|
if mg.Verbose() {
|
|
cmd.Stdout = os.Stdout
|
|
}
|
|
return cmd.Run()
|
|
}
|
|
|