瀏覽代碼

generate protobuf files on the fly

Darien Raymond 8 年之前
父節點
當前提交
7cbef6723c
共有 7 個文件被更改,包括 70 次插入4 次删除
  1. 2 2
      .gitignore
  2. 1 0
      .travis.yml
  3. 1 1
      common/crypto/internal/chacha_core_gen.go
  4. 7 0
      tools/genproto/genproto.go
  5. 57 0
      tools/genproto/main.go
  6. 2 0
      tools/geoip/geoip.go
  7. 0 1
      tools/geoip/geoip_data.go

+ 2 - 2
.gitignore

@@ -1,2 +1,2 @@
-.generated.go
-.pb.go
+*.generated.go
+*.pb.go

+ 1 - 0
.travis.yml

@@ -5,6 +5,7 @@ go_import_path: v2ray.com/core
 git:
   depth: 5
 script:
+- go generate v2ray.com/core/...
 - go test -tags json v2ray.com/core/...
 after_success:
 - ./testing/coverage/coverall

+ 1 - 1
common/crypto/internal/chacha_core_gen.go

@@ -55,7 +55,7 @@ func ChaCha20Block(s *[16]uint32, out []byte, rounds int) {
 }
 
 func main() {
-	file, err := os.OpenFile("chacha_core_generated.go", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
+	file, err := os.OpenFile("chacha_core.generated.go", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
 	if err != nil {
 		log.Fatalf("Failed to generate chacha_core.go: %v", err)
 	}

+ 7 - 0
tools/genproto/genproto.go

@@ -0,0 +1,7 @@
+// +build windows darwin linux
+
+package genproto
+
+//go:generate go get -u "github.com/golang/protobuf/protoc-gen-go"
+//go:generate go get -u "github.com/golang/protobuf/proto"
+//go:generate go run main.go

+ 57 - 0
tools/genproto/main.go

@@ -0,0 +1,57 @@
+// +build generate
+
+package main
+
+import (
+	"fmt"
+	"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"),
+}
+
+func main() {
+	protofiles := make(map[string][]string)
+	protoc := protocMap[runtime.GOOS]
+	gosrc := filepath.Join(os.Getenv("GOPATH"), "src")
+
+	filepath.Walk(filepath.Join(gosrc, "v2ray.com", "core"), func(path string, info os.FileInfo, err error) error {
+		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 {
+		args := []string{"--proto_path", gosrc, "--go_out", gosrc}
+		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)
+		}
+	}
+}

+ 2 - 0
tools/geoip/geoip.go

@@ -1,3 +1,5 @@
+// +build fullgen
+
 package geoip
 
 //go:generate go run geoip_gen.go

文件差異過大導致無法顯示
+ 0 - 1
tools/geoip/geoip_data.go


部分文件因文件數量過多而無法顯示