diff --git a/tools/build/build.go b/tools/build/build.go index c1293b75..10f2e8d7 100644 --- a/tools/build/build.go +++ b/tools/build/build.go @@ -11,16 +11,17 @@ import ( ) var ( - targetOS = flag.String("os", runtime.GOOS, "Target OS of this build.") - targetArch = flag.String("arch", runtime.GOARCH, "Target CPU arch of this build.") - archive = flag.Bool("zip", false, "Whether to make an archive of files or not.") + flagTargetOS = flag.String("os", runtime.GOOS, "Target OS of this build.") + flagTargetArch = flag.String("arch", runtime.GOARCH, "Target CPU arch of this build.") + flagArchive = flag.Bool("zip", false, "Whether to make an archive of files or not.") + + binPath string ) func createTargetDirectory(version string, goOS GoOS, goArch GoArch) (string, error) { suffix := getSuffix(goOS, goArch) - GOPATH := os.Getenv("GOPATH") - targetDir := filepath.Join(GOPATH, "bin", "v2ray-"+version+suffix) + targetDir := filepath.Join(binPath, "v2ray-"+version+suffix) if version != "custom" { os.RemoveAll(targetDir) } @@ -36,19 +37,31 @@ func getTargetFile(goOS GoOS) string { return "v2ray" + suffix } +func getBinPath() string { + GOPATH := os.Getenv("GOPATH") + return filepath.Join(GOPATH, "bin") +} + func main() { flag.Parse() + binPath = getBinPath() + build(*flagTargetOS, *flagTargetArch, *flagArchive, "") +} - v2rayOS := parseOS(*targetOS) - v2rayArch := parseArch(*targetArch) +func build(targetOS, targetArch string, archive bool, version string) { + v2rayOS := parseOS(targetOS) + v2rayArch := parseArch(targetArch) - version, err := git.RepoVersionHead() - if version == git.VersionUndefined { - version = "custom" - } - if err != nil { - fmt.Println("Unable to detect V2Ray version: " + err.Error()) - return + if len(version) == 0 { + v, err := git.RepoVersionHead() + if v == git.VersionUndefined { + v = "custom" + } + if err != nil { + fmt.Println("Unable to detect V2Ray version: " + err.Error()) + return + } + version = v } fmt.Printf("Building V2Ray (%s) for %s %s\n", version, v2rayOS, v2rayArch) @@ -68,9 +81,7 @@ func main() { fmt.Println("Unable to copy config files: " + err.Error()) } - if *archive { - GOPATH := os.Getenv("GOPATH") - binPath := filepath.Join(GOPATH, "bin") + if archive { err := os.Chdir(binPath) if err != nil { fmt.Printf("Unable to switch to directory (%s): %v\n", binPath, err) diff --git a/tools/build/build_test.go b/tools/build/build_test.go index 5c16c0e4..19c7092f 100644 --- a/tools/build/build_test.go +++ b/tools/build/build_test.go @@ -1,23 +1,59 @@ package main import ( + "fmt" "os" + "path/filepath" "testing" "github.com/v2ray/v2ray-core/testing/unit" ) +func cleanBinPath() { + os.RemoveAll(binPath) + os.Mkdir(binPath, os.ModeDir|0777) +} + +func fileExists(file string) bool { + _, err := os.Stat(file) + return err == nil +} + +func allFilesExists(files ...string) bool { + for _, file := range files { + fullPath := filepath.Join(binPath, file) + if !fileExists(fullPath) { + fmt.Println(fullPath + " doesn't exist.") + return false + } + } + return true +} + func TestBuildMacOS(t *testing.T) { assert := unit.Assert(t) + binPath = filepath.Join(os.Getenv("GOPATH"), "testing") + cleanBinPath() - targetFile := os.ExpandEnv("$GOPATH/bin/v2ray-macos.zip") - os.Remove(targetFile) + build("macos", "amd64", true, "test") + assert.Bool(allFilesExists( + "v2ray-macos.zip", + "v2ray-test-macos", + filepath.Join("v2ray-test-macos", "config.json"), + filepath.Join("v2ray-test-macos", "v2ray"))).IsTrue() - *targetOS = "macos" - *targetArch = "amd64" - *archive = true - main() + build("windows", "amd64", true, "test") + assert.Bool(allFilesExists( + "v2ray-windows-64.zip", + "v2ray-test-windows-64", + filepath.Join("v2ray-test-windows-64", "config.json"), + filepath.Join("v2ray-test-windows-64", "v2ray.exe"))).IsTrue() - _, err := os.Stat(targetFile) - assert.Error(err).IsNil() + build("linux", "amd64", true, "test") + assert.Bool(allFilesExists( + "v2ray-linux-64.zip", + "v2ray-test-linux-64", + filepath.Join("v2ray-test-linux-64", "vpoint_socks_vmess.json"), + filepath.Join("v2ray-test-linux-64", "vpoint_vmess_freedom.json"), + filepath.Join("v2ray-test-linux-64", "v2ray"))).IsTrue() }