Makefile 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # The import path is where your repository can be found.
  2. # To import subpackages, always prepend the full import path.
  3. # If you change this, run `make clean`. Read more: https://git.io/vM7zV
  4. IMPORT_PATH := github.com/platformsh/example
  5. # V := 1 # When V is set, print commands and build progress.
  6. # Space separated patterns of packages to skip in list, test, format.
  7. IGNORED_PACKAGES := /vendor/
  8. .PHONY: all
  9. all: build
  10. .PHONY: build
  11. build: .GOPATH/.ok
  12. $Q go install $(if $V,-v) $(VERSION_FLAGS) $(IMPORT_PATH)
  13. .PHONY: deps
  14. deps:
  15. bin/gvt fetch github.com/platformsh/gohelper
  16. bin/gvt fetch github.com/go-sql-driver/mysql
  17. ### Code not in the repository root? Another binary? Add to the path like this.
  18. # .PHONY: otherbin
  19. # otherbin: .GOPATH/.ok
  20. # $Q go install $(if $V,-v) $(VERSION_FLAGS) $(IMPORT_PATH)/cmd/otherbin
  21. ##### ^^^^^^ EDIT ABOVE ^^^^^^ #####
  22. ##### =====> Utility targets <===== #####
  23. .PHONY: clean test list cover format
  24. clean:
  25. $Q rm -rf bin .GOPATH
  26. test: .GOPATH/.ok
  27. $Q go test $(if $V,-v) -i -race $(allpackages) # install -race libs to speed up next run
  28. ifndef CI
  29. $Q go vet $(allpackages)
  30. $Q GODEBUG=cgocheck=2 go test -race $(allpackages)
  31. else
  32. $Q ( go vet $(allpackages); echo $$? ) | \
  33. tee .GOPATH/test/vet.txt | sed '$$ d'; exit $$(tail -1 .GOPATH/test/vet.txt)
  34. $Q ( GODEBUG=cgocheck=2 go test -v -race $(allpackages); echo $$? ) | \
  35. tee .GOPATH/test/output.txt | sed '$$ d'; exit $$(tail -1 .GOPATH/test/output.txt)
  36. endif
  37. list: .GOPATH/.ok
  38. @echo $(allpackages)
  39. cover: bin/gocovmerge .GOPATH/.ok
  40. @echo "NOTE: make cover does not exit 1 on failure, don't use it to check for tests success!"
  41. $Q rm -f .GOPATH/cover/*.out .GOPATH/cover/all.merged
  42. $(if $V,@echo "-- go test -coverpkg=./... -coverprofile=.GOPATH/cover/... ./...")
  43. @for MOD in $(allpackages); do \
  44. go test -coverpkg=`echo $(allpackages)|tr " " ","` \
  45. -coverprofile=.GOPATH/cover/unit-`echo $$MOD|tr "/" "_"`.out \
  46. $$MOD 2>&1 | grep -v "no packages being tested depend on"; \
  47. done
  48. $Q ./bin/gocovmerge .GOPATH/cover/*.out > .GOPATH/cover/all.merged
  49. ifndef CI
  50. $Q go tool cover -html .GOPATH/cover/all.merged
  51. else
  52. $Q go tool cover -html .GOPATH/cover/all.merged -o .GOPATH/cover/all.html
  53. endif
  54. @echo ""
  55. @echo "=====> Total test coverage: <====="
  56. @echo ""
  57. $Q go tool cover -func .GOPATH/cover/all.merged
  58. format: bin/goimports .GOPATH/.ok
  59. $Q find .GOPATH/src/$(IMPORT_PATH)/ -iname \*.go | grep -v \
  60. -e "^$$" $(addprefix -e ,$(IGNORED_PACKAGES)) | xargs ./bin/goimports -w
  61. ##### =====> Internals <===== #####
  62. .PHONY: setup
  63. setup: clean .GOPATH/.ok
  64. @if ! grep "/.GOPATH" .gitignore > /dev/null 2>&1; then \
  65. echo "/.GOPATH" >> .gitignore; \
  66. echo "/bin" >> .gitignore; \
  67. fi
  68. go get -u github.com/FiloSottile/gvt
  69. - ./bin/gvt fetch golang.org/x/tools/cmd/goimports
  70. - ./bin/gvt fetch github.com/wadey/gocovmerge
  71. DATE := $(shell date -u '+%Y-%m-%d-%H%M UTC')
  72. VERSION_FLAGS := -ldflags='-X "main.BuildTime=$(DATE)"'
  73. #VERSION := $(shell git describe --tags --always --dirty="-dev")
  74. #DATE := $(shell date -u '+%Y-%m-%d-%H%M UTC')
  75. #VERSION_FLAGS := -ldflags='-X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
  76. # cd into the GOPATH to workaround ./... not following symlinks
  77. _allpackages = $(shell ( cd $(CURDIR)/.GOPATH/src/$(IMPORT_PATH) && \
  78. GOPATH=$(CURDIR)/.GOPATH go list ./... 2>&1 1>&3 | \
  79. grep -v -e "^$$" $(addprefix -e ,$(IGNORED_PACKAGES)) 1>&2 ) 3>&1 | \
  80. grep -v -e "^$$" $(addprefix -e ,$(IGNORED_PACKAGES)))
  81. # memoize allpackages, so that it's executed only once and only if used
  82. allpackages = $(if $(__allpackages),,$(eval __allpackages := $$(_allpackages)))$(__allpackages)
  83. export GOPATH := $(CURDIR)/.GOPATH
  84. unexport GOBIN
  85. Q := $(if $V,,@)
  86. .GOPATH/.ok:
  87. $Q mkdir -p "$(dir .GOPATH/src/$(IMPORT_PATH))"
  88. $Q ln -s ../../../.. ".GOPATH/src/$(IMPORT_PATH)"
  89. $Q mkdir -p .GOPATH/test .GOPATH/cover
  90. $Q mkdir -p bin
  91. $Q ln -s ../bin .GOPATH/bin
  92. $Q touch $@
  93. .PHONY: bin/gocovmerge bin/goimports
  94. bin/gocovmerge: .GOPATH/.ok
  95. @test -d ./vendor/github.com/wadey/gocovmerge || \
  96. { echo "Vendored gocovmerge not found, try running 'make setup'..."; exit 1; }
  97. $Q go install $(IMPORT_PATH)/vendor/github.com/wadey/gocovmerge
  98. bin/goimports: .GOPATH/.ok
  99. @test -d ./vendor/golang.org/x/tools/cmd/goimports || \
  100. { echo "Vendored goimports not found, try running 'make setup'..."; exit 1; }
  101. $Q go install $(IMPORT_PATH)/vendor/golang.org/x/tools/cmd/goimports
  102. # Based on https://github.com/cloudflare/hellogopher - v1.1 - MIT License
  103. #
  104. # Copyright (c) 2017 Cloudflare
  105. #
  106. # Permission is hereby granted, free of charge, to any person obtaining a copy
  107. # of this software and associated documentation files (the "Software"), to deal
  108. # in the Software without restriction, including without limitation the rights
  109. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  110. # copies of the Software, and to permit persons to whom the Software is
  111. # furnished to do so, subject to the following conditions:
  112. #
  113. # The above copyright notice and this permission notice shall be included in all
  114. # copies or substantial portions of the Software.
  115. #
  116. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  117. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  118. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  119. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  120. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  121. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  122. # SOFTWARE.