Skip to content

Commit

Permalink
Add Makefile, support dynamic version number
Browse files Browse the repository at this point in the history
  • Loading branch information
ma6254 committed Mar 12, 2019
1 parent e977bc2 commit 4b1c953
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ go:
- 1.11.x
- master
script:
- go build github.com/ma6254/FictionDown/cmd/FictionDown
- make build_tool
- make build
notifications:
email:
recipients:
Expand Down
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
BUILD_TIME := $(shell date "+%F %T")
COMMIT_ID := $(shell git rev-parse HEAD)
BUILD_VERSION := $(shell git describe --tags $(COMMIT_ID))

Package := "github.com/ma6254/FictionDown/cmd/FictionDown"

LDFLAG := "\
-s -w \
-X 'main.Version=${BUILD_VERSION}' \
-X 'main.CommitID=${COMMIT_ID}' \
-X 'main.BuildData=${BUILD_TIME}' \
"

build_tool:
go get -v -u github.com/golang/dep/cmd/dep
go get -v -u github.com/mitchellh/gox
dep ensure -v

build:
go build --ldflags $(LDFLAG) $(Package)

multiple_build:
gox -osarch="linux/arm" -osarch="linux/amd64" --osarch="darwin/amd64" -osarch="windows/amd64" -ldflags $(LDFLAG) -output "{{.Dir}}_$(BUILD_VERSION)_{{.OS}}_{{.Arch}}" github.com/ma6254/FictionDown/cmd/FictionDown

install:
go install --ldflags $(LDFLAG) $(Package)
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@
包管理采用godep

1. `dep ensure -v`
2. `gox github.com/ma6254/FictionDown/cmd/FictionDown`
2. `make` or `make build` 当前目录下就会产生可执行文件

### 交叉编译

需要安装gox

`make multiple_build`

## 支持的盗版站点

Expand Down
33 changes: 22 additions & 11 deletions cmd/FictionDown/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ import (

var (

// Software Version
Version = "0.1.0"
// Version git or release tag
Version = ""
// CommitID latest commit id
CommitID = ""
// BuildData build data
BuildData = ""

tSleep time.Duration
errSleep time.Duration
Expand All @@ -35,8 +39,11 @@ func main() {

app := cli.NewApp()

app.Usage = `https://github.com/ma6254/FictionDown`
app.Version = Version

app.Description = fmt.Sprintf("BuildData: %s\n CommitID: %s ", BuildData, CommitID)

app.Flags = []cli.Flag{
cli.StringFlag{
Name: "url",
Expand Down Expand Up @@ -103,7 +110,7 @@ func main() {
},
cli.StringFlag{
Name: "driver",
Usage: "请求方式",
Usage: "请求方式,support: none,phantomjs,chromedp",
},
cli.StringFlag{
Name: "f",
Expand Down Expand Up @@ -132,6 +139,10 @@ func main() {
},
Action: func(c *cli.Context) error {

fmt.Printf("Commit ID: %s\n", CommitID)
fmt.Printf("Build Data: %s\n", BuildData)
fmt.Printf("Build Version: %s\n", Version)

if logfile := c.GlobalString("log"); logfile != "" {
fmt.Printf("Set log file: %s\n", logfile)
f, err := os.Create(logfile)
Expand Down Expand Up @@ -163,14 +174,12 @@ func main() {
log.Printf("URL: %#v", bookURL.String())
switch c.String("driver") {
case "phantomjs":
if c.String("driver") == "phantomjs" {
log.Printf("Init PhantomJS")
site.InitPhantomJS()
defer func() {
log.Printf("Close PhantomJS")
site.ClosePhantomJS()
}()
}
log.Printf("Init PhantomJS")
site.InitPhantomJS()
defer func() {
log.Printf("Close PhantomJS")
site.ClosePhantomJS()
}()
for errCount := 0; errCount < 20; errCount++ {
Chapter, err = site.PhBookInfo(bookURL.String())
if err == nil {
Expand All @@ -181,8 +190,10 @@ func main() {
}
}
case "chromedp":
log.Printf("Chromedp Running...")
Chapter, err = site.ChromedpBookInfo(bookURL.String(), c.String("chromedp-log"))
default:
log.Printf("use golang default http")
for errCount := 0; errCount < 20; errCount++ {
Chapter, err = site.BookInfo(bookURL.String())
if err == nil {
Expand Down

0 comments on commit 4b1c953

Please sign in to comment.