package server import ( "context" "fmt" "git.reya.zone/reya/hexmap/build" "git.reya.zone/reya/hexmap/proto" "github.com/magefile/mage/mg" "io/fs" "os" "path/filepath" "strings" ) // TODO: GoGet // TODO: Build // TODO: Clean type Protobuf mg.Namespace func (Protobuf) InstallGoPlugin(ctx context.Context) error { alreadyDone, err := build.HasExecutableInTools("protoc-gen-go") if err != nil { return err } if alreadyDone { return nil } return build.InstallGoExecutable(ctx, "google.golang.org/protobuf/cmd/protoc-gen-go@v1.27.1") } func (Protobuf) InstallPlugins(ctx context.Context) { mg.CtxDeps(ctx, Protobuf.InstallGoPlugin) } func ProtocFlags() ([]string, error) { buildPath, err := filepath.Abs(filepath.Join(build.ToolsDir, "protoc-gen-go")) if err != nil { return nil, err } return []string{"--plugin=" + buildPath, "--go_out=.", "--go_opt=module=git.reya.zone/reya/hexmap"}, nil } func (Protobuf) Build(ctx context.Context) error { mg.SerialCtxDeps(ctx, Protobuf.Clean, Protobuf.InstallPlugins) return proto.Compile(ctx, []proto.ProtocFlagsFunc{ProtocFlags}) } func (Protobuf) Clean(ctx context.Context) error { return filepath.WalkDir("server", func(path string, d fs.DirEntry, dirErr error) error { if ctx.Err() != nil { return ctx.Err() } if dirErr != nil { return dirErr } if strings.HasSuffix(path, ".pb.go") && !d.IsDir() { if mg.Verbose() { fmt.Printf("Removing generated protobuf code at %s\n", path) } err := os.Remove(path) if err != nil { return err } } return nil }) }