2015-10-17 15:32:48 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2015-10-17 18:50:51 -04:00
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/tools/git"
|
2015-10-17 15:32:48 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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.")
|
|
|
|
)
|
|
|
|
|
2015-10-17 18:50:51 -04:00
|
|
|
func createTargetDirectory(version string, goOS GoOS, goArch GoArch) (string, error) {
|
2015-10-18 05:51:21 -04:00
|
|
|
suffix := getSuffix(goOS, goArch)
|
2015-10-17 18:50:51 -04:00
|
|
|
GOPATH := os.Getenv("GOPATH")
|
|
|
|
|
2015-10-18 05:42:00 -04:00
|
|
|
targetDir := filepath.Join(GOPATH, "bin", "v2ray-"+version+suffix)
|
2015-10-17 15:32:48 -04:00
|
|
|
if version != "custom" {
|
|
|
|
os.RemoveAll(targetDir)
|
|
|
|
}
|
|
|
|
err := os.MkdirAll(targetDir, os.ModeDir|0777)
|
|
|
|
return targetDir, err
|
|
|
|
}
|
|
|
|
|
2015-10-17 18:50:51 -04:00
|
|
|
func getTargetFile(goOS GoOS) string {
|
|
|
|
suffix := ""
|
2015-10-18 05:51:21 -04:00
|
|
|
if goOS == Windows {
|
2015-10-17 15:32:48 -04:00
|
|
|
suffix += ".exe"
|
|
|
|
}
|
|
|
|
return "v2ray" + suffix
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2015-10-17 18:50:51 -04:00
|
|
|
v2rayOS := parseOS(*targetOS)
|
|
|
|
v2rayArch := parseArch(*targetArch)
|
2015-10-17 15:32:48 -04:00
|
|
|
|
2015-10-17 18:50:51 -04:00
|
|
|
version, err := git.RepoVersionHead()
|
|
|
|
if version == git.VersionUndefined {
|
|
|
|
version = "custom"
|
2015-10-17 15:32:48 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unable to detect V2Ray version: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("Building V2Ray (%s) for %s %s\n", version, v2rayOS, v2rayArch)
|
|
|
|
|
|
|
|
targetDir, err := createTargetDirectory(version, v2rayOS, v2rayArch)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unable to create directory " + targetDir + ": " + err.Error())
|
|
|
|
}
|
|
|
|
|
2015-10-17 18:50:51 -04:00
|
|
|
targetFile := getTargetFile(v2rayOS)
|
|
|
|
err = buildV2Ray(filepath.Join(targetDir, targetFile), version, v2rayOS, v2rayArch)
|
2015-10-17 15:32:48 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unable to build V2Ray: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2015-10-17 18:50:51 -04:00
|
|
|
err = copyConfigFiles(targetDir, v2rayOS)
|
2015-10-17 15:32:48 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unable to copy config files: " + err.Error())
|
|
|
|
}
|
2015-10-18 05:42:00 -04:00
|
|
|
|
|
|
|
if *archive {
|
|
|
|
GOPATH := os.Getenv("GOPATH")
|
|
|
|
binPath := filepath.Join(GOPATH, "bin")
|
|
|
|
err := os.Chdir(binPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Unable to switch to directory (%s): %v\n", binPath, err)
|
|
|
|
}
|
|
|
|
suffix := getSuffix(v2rayOS, v2rayArch)
|
|
|
|
zipFile := "v2ray" + suffix + ".zip"
|
|
|
|
root := filepath.Base(targetDir)
|
|
|
|
err = zipFolder(root, zipFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unable to create archive (%s): %v\n", zipFile, err)
|
|
|
|
}
|
|
|
|
}
|
2015-10-17 15:32:48 -04:00
|
|
|
}
|