Skip to content

Commit

Permalink
version examples & Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
researchlab committed Jul 28, 2023
1 parent bc95eb4 commit 3d32fef
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
27 changes: 27 additions & 0 deletions example_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gdk_test

import (
"fmt"
"net/http"
"os"

"github.com/researchlab/gdk"
)

// build binary by makefile, command
// make default -f version.Makefile
// ExampleVersion version examples
func ExampleVersion() {
v := gdk.Version()
// version by console cmd
args := os.Args
if len(args) >= 2 && args[1] == "version" {
fmt.Println(v.String())
return
}
// version by http request
http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, v)
})
http.ListenAndServe(":8082", nil)
}
49 changes: 49 additions & 0 deletions version.Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.PHONY: list vet fmt default clean
all: list vet fmt default clean
# ###### 只需要设置下面两个参数, 其它都不需要修改
BINARY="binary"
VERSION=0.0.1
# #####
BUILD=`date +%F`
SHELL := /bin/bash
BASEDIR = $(shell pwd)

# build with verison infos
versionDir="github.com/researchlab/gdk"
gitTag=$(shell if [ "`git describe --tags --abbrev=0 2>/dev/null`" != "" ];then git describe --tags --abbrev=0; else git log --pretty=format:'%h' -n 1; fi) # format:'%H' 完整的commit id
gitBranch=$(shell git rev-parse --abbrev-ref HEAD)
buildDate=$(shell TZ=Asia/Shanghai date +%FT%T%z)
gitCommit=$(shell git rev-parse --short HEAD) # 去掉--short 就是完整的commit-id
gitTreeState=$(shell if git status|grep -q 'clean';then echo clean; else echo dirty; fi)

ldflags="-s -w -X ${versionDir}.gitTag=${gitTag} -X ${versionDir}.buildDate=${buildDate} -X ${versionDir}.gitCommit=${gitCommit} -X ${versionDir}.gitTreeState=${gitTreeState} -X ${versionDir}.version=${VERSION} -X ${versionDir}.gitBranch=${gitBranch}"


PACKAGES=`go list ./... | grep -v /vendor/`
VETPACKAGES=`go list ./... | grep -v /vendor/ | grep -v /examples/`
GOFILES=`find . -name "*.go" -type f -not -path "./vendor/*"`

default:
@echo "build the ${BINARY}"
@GOOS=linux GOARCH=amd64 go build -ldflags ${ldflags} -o build/${BINARY}.linux -tags=jsoniter
@GOOS=darwin GOARCH=arm64 go build -ldflags ${ldflags} -o build/${BINARY}.mac -tags=jsoniter
@echo "build done."

list:
@echo ${PACKAGES}
@echo ${VETPACKAGES}
@echo ${GOFILES}

fmt:
@echo "fmt the project"
@gofmt -s -w ${GOFILES}

vet:
@echo "check the project codes."
@go vet $(VETPACKAGES)
@echo "check done."


clean:
@rm -rf build/*

0 comments on commit 3d32fef

Please sign in to comment.