diff --git a/.gitignore b/.gitignore index 414dad8..5ed3d85 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ _book *.pdf version.md - +gopl-zh-* +_book.zip diff --git a/Makefile b/Makefile index 5e8c85d..70cdcc1 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ default: go run update_version.go gitbook build go run fix-data-revision.go + go run builder.go zh2tw: go run zh2tw.go . .md$$ diff --git a/builder.go b/builder.go new file mode 100644 index 0000000..02defe9 --- /dev/null +++ b/builder.go @@ -0,0 +1,159 @@ +// Copyright 2015 ChaiShushan . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build ingore + +// 打包 gopl-zh 为 zip 文件. +// +// 文件名格式: gopl-zh-20151001-2ae607.zip +package main + +import ( + "archive/zip" + "fmt" + "io" + "io/ioutil" + "log" + "os" + "os/exec" + "path/filepath" + "strings" + "time" +) + +func main() { + // Git版本号 + gitVersion := getGitCommitVersion() + + // zip文件名 + zipBaseName := fmt.Sprintf("gopl-zh-%s-%s", time.Now().Format("20060102"), gitVersion[:6]) + + os.Remove(zipBaseName + ".zip") + file, err := os.Create(zipBaseName + ".zip") + if err != nil { + log.Fatal("os.Create: ", err) + } + defer file.Close() + + zipFile := zip.NewWriter(file) + defer zipFile.Close() + + // create /gopl-zh-20151001-2ae607/ + f, err := zipFile.Create(zipBaseName + "/") + if err != nil { + log.Fatal(err) + } + if _, err = f.Write([]byte("")); err != nil { + log.Fatal(err) + } + + dir := "_book" + filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + log.Fatal("filepath.Walk: ", err) + } + if info.IsDir() { + return nil + } + relpath, err := filepath.Rel(dir, path) + if err != nil { + log.Fatal("filepath.Rel: ", err) + } + + filename := filepath.ToSlash(relpath) + if isIngoreFile(filename) { + return nil + } + + data, err := ioutil.ReadFile(path) + if err != nil { + log.Fatal("ioutil.ReadFile: ", err) + } + + f, err := zipFile.Create(zipBaseName + "/" + filename) + if err != nil { + log.Fatal(err) + } + if _, err = f.Write(data); err != nil { + log.Fatal(err) + } + + fmt.Printf("%s\n", filename) + return nil + }) + + fmt.Printf("Done\n") +} + +// 获取Git最新的版本号 +// +// git log -1 +// commit 0460c1b3bb8fbb7e2fc88961e69aa37f4041d6c1 +// Merge: b2d582a e826457 +// Author: chai2010 +// Date: Mon Feb 1 08:04:44 2016 +0800 +// +// Merge pull request #249 from sunclx/patch-3 +// +// fix typo +func getGitCommitVersion() (version string) { + cmdOut, err := exec.Command(`git`, `log`, `-1`).CombinedOutput() + if err != nil { + return "master" + } + for _, line := range strings.Split(string(cmdOut), "\n") { + line := strings.TrimSpace(line) + if strings.HasPrefix(line, "commit") { + version = strings.TrimSpace(line[len("commit"):]) + return + } + } + return "master" +} + +func cpFile(dst, src string) { + err := os.MkdirAll(filepath.Dir(dst), 0666) + if err != nil && !os.IsExist(err) { + log.Fatal("cpFile: ", err) + } + fsrc, err := os.Open(src) + if err != nil { + log.Fatal("cpFile: ", err) + } + defer fsrc.Close() + + fdst, err := os.Create(dst) + if err != nil { + log.Fatal("cpFile: ", err) + } + defer fdst.Close() + if _, err = io.Copy(fdst, fsrc); err != nil { + log.Fatal("cpFile: ", err) + } +} + +func isIngoreFile(path string) bool { + if strings.HasPrefix(path, ".git") { + return true + } + if strings.HasSuffix(path, ".gitignore") { + return true + } + + if strings.HasPrefix(path, "rpc.v2014") { + return true + } + if strings.HasPrefix(path, "testdata") { + return true + } + + if strings.HasSuffix(path, "uis.zip") { + return true + } + if strings.HasSuffix(path, ".go") { + return true + } + + return false +}