From 74cba8b177b726f5cffd57e9594cc8f74eba46f3 Mon Sep 17 00:00:00 2001 From: V2Ray Date: Sun, 18 Oct 2015 11:42:00 +0200 Subject: [PATCH] Able to zip folder when building --- tools/build/archive.go | 62 ++++++++++++++++++++++++++++++++++++++++++ tools/build/build.go | 18 +++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 tools/build/archive.go diff --git a/tools/build/archive.go b/tools/build/archive.go new file mode 100644 index 000000000..1b2cab1dd --- /dev/null +++ b/tools/build/archive.go @@ -0,0 +1,62 @@ +package main + +import ( + "archive/zip" + "io" + "os" + "path/filepath" +) + +type ZipWorker struct { + zipWriter *zip.Writer + root string +} + +func NewZipWorker(zipFile io.Writer, root string) *ZipWorker { + return &ZipWorker{ + zipWriter: zip.NewWriter(zipFile), + root: root, + } +} + +func (worker *ZipWorker) run() error { + defer worker.close() + return filepath.Walk(worker.root, worker.zipAllFiles) +} + +func (worker *ZipWorker) zipAllFiles(path string, info os.FileInfo, err error) error { + if info.IsDir() { + return nil + } + fileWriter, err := worker.zipWriter.Create(path) + if err != nil { + return err + } + fileReader, err := os.Open(path) + if err != nil { + return err + } + _, err = io.Copy(fileWriter, fileReader) + if err != nil { + return err + } + return nil +} + +func (worker *ZipWorker) close() { + worker.zipWriter.Close() +} + +func zipFolder(folder string, file string) error { + if _, err := os.Stat(file); err == nil { + os.Remove(file) + } + + zipFile, err := os.Create(file) + if err != nil { + return err + } + defer zipFile.Close() + + return NewZipWorker(zipFile, folder).run() +} diff --git a/tools/build/build.go b/tools/build/build.go index 1f6799be0..df9438b9d 100644 --- a/tools/build/build.go +++ b/tools/build/build.go @@ -23,7 +23,7 @@ func createTargetDirectory(version string, goOS GoOS, goArch GoArch) (string, er } GOPATH := os.Getenv("GOPATH") - targetDir := filepath.Join(GOPATH, "bin", "v2ray"+suffix) + targetDir := filepath.Join(GOPATH, "bin", "v2ray-"+version+suffix) if version != "custom" { os.RemoveAll(targetDir) } @@ -72,4 +72,20 @@ func main() { if err != nil { fmt.Println("Unable to copy config files: " + err.Error()) } + + 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) + } + } }