2016-02-01 02:16:15 +00:00
|
|
|
|
// Copyright 2015 <chaishushan{AT}gmail.com>. 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() {
|
2016-04-27 02:01:45 +00:00
|
|
|
|
version, commitTime := getGitCommitVersion()
|
|
|
|
|
buildTime := time.Now()
|
|
|
|
|
|
|
|
|
|
data := makeVersionMarkdown(version, commitTime, buildTime)
|
2016-02-01 02:16:15 +00:00
|
|
|
|
|
|
|
|
|
err := ioutil.WriteFile("./version.md", []byte(data), 0666)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("ioutil.WriteFile: err = %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 02:30:44 +00:00
|
|
|
|
fmt.Println("build version", version)
|
2016-04-27 02:01:45 +00:00
|
|
|
|
fmt.Println(commitTime.Format("2006-01-02"))
|
|
|
|
|
fmt.Println(buildTime.Format("2006-01-02"))
|
2016-02-01 02:16:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 生成版本文件
|
2016-04-27 02:01:45 +00:00
|
|
|
|
func makeVersionMarkdown(version string, commitTime, buildTime time.Time) string {
|
2016-02-01 02:16:15 +00:00
|
|
|
|
return fmt.Sprintf(`
|
2016-02-02 07:27:02 +00:00
|
|
|
|
<!-- 版本号文件,用于被其它md文件包含 -->
|
2016-02-01 02:16:15 +00:00
|
|
|
|
|
|
|
|
|
### 版本信息
|
|
|
|
|
|
2016-02-23 01:40:52 +00:00
|
|
|
|
- 仓库版本:[%s](gopl-zh-%s.zip)
|
2016-04-27 02:01:45 +00:00
|
|
|
|
- 更新时间:%s
|
2016-02-01 02:16:15 +00:00
|
|
|
|
- 构建时间:%s
|
|
|
|
|
`,
|
2016-02-23 01:40:52 +00:00
|
|
|
|
version, version,
|
2016-04-27 02:01:45 +00:00
|
|
|
|
commitTime.Format("2006-01-02"),
|
|
|
|
|
buildTime.Format("2006-01-02"),
|
2016-02-01 02:16:15 +00:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取Git最新的版本号
|
|
|
|
|
//
|
2016-02-02 07:27:02 +00:00
|
|
|
|
// git log -1
|
2016-02-01 02:16:15 +00:00
|
|
|
|
// commit 0460c1b3bb8fbb7e2fc88961e69aa37f4041d6c1
|
|
|
|
|
// Merge: b2d582a e826457
|
2016-02-01 02:30:44 +00:00
|
|
|
|
// Author: chai2010 <chaishushan{AT}gmail.com>
|
2016-02-01 02:16:15 +00:00
|
|
|
|
// Date: Mon Feb 1 08:04:44 2016 +0800
|
|
|
|
|
//
|
|
|
|
|
// Merge pull request #249 from sunclx/patch-3
|
|
|
|
|
//
|
|
|
|
|
// fix typo
|
2016-04-27 02:01:45 +00:00
|
|
|
|
func getGitCommitVersion() (version string, date time.Time) {
|
2016-02-02 07:27:02 +00:00
|
|
|
|
cmdOut, err := exec.Command(`git`, `log`, `-1`).CombinedOutput()
|
2016-02-01 02:16:15 +00:00
|
|
|
|
if err != nil {
|
2016-04-27 02:01:45 +00:00
|
|
|
|
return "unknown", time.Date(2016, 2, 1, 0, 0, 0, 0, time.UTC) // 第一版发布时间
|
2016-02-01 02:16:15 +00:00
|
|
|
|
}
|
|
|
|
|
for _, line := range strings.Split(string(cmdOut), "\n") {
|
|
|
|
|
line := strings.TrimSpace(line)
|
|
|
|
|
if strings.HasPrefix(line, "commit") {
|
|
|
|
|
version = strings.TrimSpace(line[len("commit"):])
|
|
|
|
|
}
|
2016-04-27 02:01:45 +00:00
|
|
|
|
if strings.HasPrefix(line, "Date") {
|
|
|
|
|
const longForm = "Mon Jan 2 15:04:05 2006 -0700"
|
|
|
|
|
date, _ = time.Parse(longForm, strings.TrimSpace(line[len("Date:"):]))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if version == "" || date.IsZero() {
|
|
|
|
|
return "unknown", time.Date(2016, 2, 1, 0, 0, 0, 0, time.UTC) // 第一版发布时间
|
2016-02-01 02:16:15 +00:00
|
|
|
|
}
|
2016-04-27 02:01:45 +00:00
|
|
|
|
return
|
2016-02-01 02:16:15 +00:00
|
|
|
|
}
|