diff --git a/.gitignore b/.gitignore index 1a366fb..414dad8 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,7 @@ _book # eBook build output *.epub *.mobi -*.pdf \ No newline at end of file +*.pdf + +version.md + diff --git a/Makefile b/Makefile index cfd0401..371a2f7 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ # http://www.imagemagick.org/ default: + go run update_version.go gitbook build zh2tw: @@ -26,6 +27,7 @@ loop: go run zh2tw.go . .md$$ zh2tw review: + go run update_version.go go run zh2tw.go . .md$$ tw2zh gitbook build go run zh2tw.go . .md$$ zh2tw diff --git a/preface.md b/preface.md index ece4de8..d2a326c 100644 --- a/preface.md +++ b/preface.md @@ -9,6 +9,7 @@ Go語言聖經 [《The Go Programming Language》](http://gopl.io) 中文版本 - 項目主頁:http://github.com/golang-china/gopl-zh - 原版官網:http://gopl.io +{% include "./version.md" %} ------- diff --git a/update_version.go b/update_version.go new file mode 100644 index 0000000..35060b3 --- /dev/null +++ b/update_version.go @@ -0,0 +1,72 @@ +// Copyright 2015 . 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 + +// 更新版本信息 +package main + +import ( + "fmt" + "io/ioutil" + "log" + "os/exec" + "strings" + "time" +) + +func main() { + data := makeVersionMarkdown() + + err := ioutil.WriteFile("./version.md", []byte(data), 0666) + if err != nil { + log.Fatalf("ioutil.WriteFile: err = %v", err) + } + + fmt.Println("MakeVersionMarkdown Done") +} + +// 生成版本文件 +func makeVersionMarkdown() string { + version := getGitCommitVersion() + buildTime := time.Now().Format("2006-01-02") + + return fmt.Sprintf(` + + +### 版本信息 + +- 仓库版本:[%s](https://github.com/golang-china/gopl-zh/commit/%s) +- 构建时间:%s +`, + version, version, + buildTime, + ) +} + +// 获取Git最新的版本号 +// +// git log HEAD -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`, `HEAD`, `-1`).CombinedOutput() + if err != nil { + log.Fatalf("getGitCommitVersion: err = %s", err) + } + 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" +}