2020-10-29 16:16:25 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2022-03-21 14:48:03 +00:00
|
|
|
//go:build ignore
|
2020-10-29 16:16:25 +00:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-10-30 10:41:22 +00:00
|
|
|
mainpkg = flag.String("pkg", "storj.io/storj/satellite/internalpb", "main package name")
|
2020-10-29 16:16:25 +00:00
|
|
|
protoc = flag.String("protoc", "protoc", "protoc compiler")
|
|
|
|
)
|
|
|
|
|
|
|
|
var ignoreProto = map[string]bool{
|
|
|
|
"gogo.proto": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
func ignore(files []string) []string {
|
|
|
|
xs := []string{}
|
|
|
|
for _, file := range files {
|
|
|
|
if !ignoreProto[file] {
|
|
|
|
xs = append(xs, file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return xs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Programs needed for code generation:
|
|
|
|
//
|
|
|
|
// github.com/ckaznocha/protoc-gen-lint
|
|
|
|
// storj.io/drpc/cmd/protoc-gen-drpc
|
|
|
|
// github.com/nilslice/protolock/cmd/protolock
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// TODO: protolock
|
|
|
|
|
|
|
|
{
|
|
|
|
// cleanup previous files
|
|
|
|
localfiles, err := filepath.Glob("*.pb.go")
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
all := []string{}
|
|
|
|
all = append(all, localfiles...)
|
|
|
|
for _, match := range all {
|
|
|
|
_ = os.Remove(match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
protofiles, err := filepath.Glob("*.proto")
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
protofiles = ignore(protofiles)
|
|
|
|
|
|
|
|
commonPb := os.Getenv("STORJ_COMMON_PB")
|
|
|
|
if commonPb == "" {
|
2020-10-30 10:41:22 +00:00
|
|
|
commonPb = "../../../common/pb"
|
2020-10-29 16:16:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 10:41:22 +00:00
|
|
|
overrideImports := ",Mgoogle/protobuf/timestamp.proto=storj.io/storj/satellite/internalpb"
|
2020-10-29 16:16:25 +00:00
|
|
|
args := []string{
|
|
|
|
"--lint_out=.",
|
2021-03-23 10:41:41 +00:00
|
|
|
"--gogo_out=paths=source_relative" + overrideImports + ":.",
|
|
|
|
"--go-drpc_out=protolib=github.com/gogo/protobuf,paths=source_relative:.",
|
2020-10-29 16:16:25 +00:00
|
|
|
"-I=.",
|
|
|
|
"-I=" + commonPb,
|
|
|
|
}
|
|
|
|
args = append(args, protofiles...)
|
|
|
|
|
|
|
|
// generate new code
|
|
|
|
cmd := exec.Command(*protoc, args...)
|
|
|
|
fmt.Println(strings.Join(cmd.Args, " "))
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
fmt.Println(string(out))
|
|
|
|
check(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
files, err := filepath.Glob("*.pb.go")
|
|
|
|
check(err)
|
|
|
|
for _, file := range files {
|
|
|
|
process(file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// format code to get rid of extra imports
|
|
|
|
out, err := exec.Command("goimports", "-local", "storj.io", "-w", ".").CombinedOutput()
|
|
|
|
fmt.Println(string(out))
|
|
|
|
check(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func process(file string) {
|
2022-10-11 12:39:08 +01:00
|
|
|
data, err := os.ReadFile(file)
|
2020-10-29 16:16:25 +00:00
|
|
|
check(err)
|
|
|
|
|
|
|
|
source := string(data)
|
|
|
|
|
|
|
|
// When generating code to the same path as proto, it will
|
|
|
|
// end up generating an `import _ "."`, the following replace removes it.
|
|
|
|
source = strings.Replace(source, `_ "."`, "", -1)
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
err = os.WriteFile(file, []byte(source), 0644)
|
2020-10-29 16:16:25 +00:00
|
|
|
check(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|