.PHONY:  build build-bin generate clean go-clean check

build: build-bin

# Go build
build-bin: bin/server

generate: db/entity/autogenerated_entity.go

clean: go-clean
	rm -f ./build/server/server ./build/server/worker ./build/server/migrate_cmd

go-clean:
	go clean

swagger:
	swag init -d ./ -g api/router.go --tags Billing --output docs/openapi


test: generate
	docker run -itd --name mysql-test -eMYSQL_ALLOW_EMPTY_PASSWORD=yes -p 3306:3306 gcr.io/pingcap-public/third-party/mysql:8.0
	sleep 10
	go install gotest.tools/gotestsum@latest
	go install github.com/jandelgado/gcov2lcov@latest
	gotestsum --junitfile unit-tests-billing2.xml -- -p=1 -coverprofile=coverage.out -coverpkg="./api/...,./model/..." ./...
	gcov2lcov -infile=coverage.out -outfile=coverage.out.lcov



# Binary build
# should use docker build instead of build options for compatible here.
bin/server: generate  cmd/server/main.go
	GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/server/server cmd/server/main.go

# should not generate buf (exclude), can not run docker in docker build
bin/server_in_docker: generate cmd/server/main.go
	GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/server/server cmd/server/main.go

bin/localserver: generate  cmd/server/main.go
	go build -o build/server/server cmd/server/main.go

GENQS := PATH="../../tools/bin:$(PATH)" CGO_ENABLED=0 go generate entity.go

tools/bin/goqueryset:
	cd tools/ && make bin/goqueryset

db/entity/autogenerated_entity.go: db/entity/entity.go tools/bin/goqueryset
	cd db/entity && $(GENQS)

lint: generate
	docker run --rm -v $(shell pwd):/app -w /app golangci/golangci-lint:v1.52.2 golangci-lint run

