2017-01-02 19:43:41 +00:00
|
|
|
// +build generate
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-01-03 13:17:44 +00:00
|
|
|
"bytes"
|
2017-01-02 19:43:41 +00:00
|
|
|
"fmt"
|
2017-01-03 13:17:44 +00:00
|
|
|
"io/ioutil"
|
2017-01-02 19:43:41 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var protocMap = map[string]string{
|
|
|
|
"windows": filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", ".dev", "protoc", "windows", "protoc.exe"),
|
|
|
|
"darwin": filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", ".dev", "protoc", "macos", "protoc"),
|
|
|
|
"linux": filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", ".dev", "protoc", "linux", "protoc"),
|
|
|
|
}
|
|
|
|
|
2017-01-26 22:41:39 +00:00
|
|
|
func sdkPath(lang string) string {
|
|
|
|
path := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "v2ray", "sdk-"+lang, "proto")
|
2017-01-04 13:43:52 +00:00
|
|
|
os.MkdirAll(path, os.ModePerm)
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:43:41 +00:00
|
|
|
func main() {
|
|
|
|
protofiles := make(map[string][]string)
|
|
|
|
protoc := protocMap[runtime.GOOS]
|
|
|
|
gosrc := filepath.Join(os.Getenv("GOPATH"), "src")
|
2017-01-04 13:43:52 +00:00
|
|
|
reporoot := filepath.Join(gosrc, "v2ray.com", "core")
|
2017-01-02 19:43:41 +00:00
|
|
|
|
2017-01-04 13:43:52 +00:00
|
|
|
filepath.Walk(reporoot, func(path string, info os.FileInfo, err error) error {
|
2017-01-02 19:43:41 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := filepath.Dir(path)
|
|
|
|
filename := filepath.Base(path)
|
|
|
|
if strings.HasSuffix(filename, ".proto") {
|
|
|
|
protofiles[dir] = append(protofiles[dir], path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, files := range protofiles {
|
2017-01-26 22:41:39 +00:00
|
|
|
args := []string{"--proto_path", gosrc, "--go_out", gosrc, "--java_out", sdkPath("java")}
|
2017-01-02 19:43:41 +00:00
|
|
|
args = append(args, files...)
|
|
|
|
cmd := exec.Command(protoc, args...)
|
|
|
|
cmd.Env = append(cmd.Env, os.Environ()...)
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if len(output) > 0 {
|
|
|
|
fmt.Println(string(output))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
2017-01-03 13:17:44 +00:00
|
|
|
|
2017-01-04 13:43:52 +00:00
|
|
|
err := filepath.Walk(reporoot, func(path string, info os.FileInfo, err error) error {
|
2017-01-03 13:17:44 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(info.Name(), ".pb.go") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pos := bytes.Index(content, []byte("\npackage"))
|
|
|
|
if pos > 0 {
|
|
|
|
if err := ioutil.WriteFile(path, content[pos+1:], info.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-01-02 19:43:41 +00:00
|
|
|
}
|