# drawio-exporter makefile
# ========================

PROJECT_ID 		:= drawio-export
PROJECT_NAME	?= MouseSoft Draw.IO Export
BIN_SUFFIX		:=

TMPDIR			?= $(CURDIR)/tmp
OUTDIR			?= $(CURDIR)/out
BINDIR			?= $(OUTDIR)/bin
DOC_DIR			?= $(OUTDIR)/doc

VERSION			?= $(strip $(shell ./scripts/version.sh))
VERSION_NUMBER	:= $(strip $(shell ./scripts/version.sh number))

GOARCH			?= $(shell go env GOARCH)
GOOS			?= $(shell go env GOOS)

GO_LDFLAGS		?=
GO_OPT_V		:= -X "main.version=$(VERSION)"
GO_OPT_APP		:= -X "main.appname=$(PROJECT_NAME)"
GO_OPT_BASE		:= -mod vendor -ldflags '$(GO_OPT_V) $(GO_OPT_APP) $(GO_LDFLAGS)'
GO_OPT			?=
EXPORT_RESULT 	?= false # for CI please set EXPORT_RESULT to true
COVERAGE_FORMAT ?= html

GOCMD			:= go
GOTEST			:= $(GOCMD) test
GOVET			:= $(GOCMD) vet
ECHO_CMD		:= echo

ifeq ($(OS),Windows_NT)
DIST_SUFFIX		:= windows-$(GOARCH)
MSI_FILE		:= $(PROJECT_ID)_$(VERSION)_$(GOARCH).msi
DIST_EXT		:= .zip
DIST_OPTS		:= -a -cf
ECHO_CMD		:= echo -e
MSI_VERSION		:= $(shell echo $(VERSION_NUMBER) | sed -e 's/-.*//')
BIN_SUFFIX		:= .exe
else
PLATFORM		:= $(shell uname -s | tr '[:upper:]' '[:lower:]')
DIST_SUFFIX		:= $(GOOS)-$(GOARCH)
DIST_EXT		:= .tar.gz
DIST_OPTS		:= -czf
endif

PKG_NAME		:= $(PROJECT_ID)_$(VERSION)_$(DIST_SUFFIX)
DIST_FILE		:= $(PKG_NAME)$(DIST_EXT)

GREEN  := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE  := $(shell tput -Txterm setaf 7)
CYAN   := $(shell tput -Txterm setaf 6)
RESET  := $(shell tput -Txterm sgr0)

.DEFAULT_GOAL := all

version: ## Version of the project to be built
	@echo $(VERSION)
.PHONY:version

version-number: ## Version number of the project to be built
	@echo $(VERSION_NUMBER)
.PHONY:version-number

## Build

all: clean vendor build ## Build binary
.PHONY:all

APPS := $(patsubst cmd/%/, %, $(dir $(wildcard cmd/*/main.go)))

build: vendor $(addprefix $(BINDIR)/, $(APPS)) ## Build your project and put the output binary in out/bin/
	@mkdir -p $(BINDIR)
	@$(ECHO_CMD) "Build\t\t${GREEN}[OK]${RESET}"
.PHONY:build

$(BINDIR)/%: cmd/%/main.go $(patsubst cmd/%/main.go,cmd/%/*.go,$<)
	@rm -f "$(BINDIR)/$(BIN_PREFIX)$(patsubst cmd/%/main.go,%,$<)$(BIN_SUFFIX)"
	GO111MODULE=on $(GOCMD) build $(GO_OPT_BASE) $(GO_OPT) \
	-o "$(BINDIR)/$(BIN_PREFIX)$(patsubst cmd/%/main.go,%,$<)$(BIN_SUFFIX)" \
	$(patsubst %/main.go,./%,$<)

dist: build ## Create all distro packages
	@rm -f "$(OUTDIR)/$(DIST_FILE)"
	tar $(DIST_OPTS) "$(OUTDIR)/$(DIST_FILE)" -C "$(OUTDIR)" bin
	@$(ECHO_CMD) "Dist\t\t${GREEN}[OK]${RESET}"
.PHONY:dist

vendor: ## Copy of all packages needed to support builds and tests in the vendor directory
	$(GOCMD) mod vendor
	@$(ECHO_CMD) "Vendor\t\t${GREEN}[OK]${RESET}"
.PHONY:vendor

clean: ## Remove build related files
	@rm -fr $(TMPDIR)
	@rm -fr $(OUTDIR)
	@rm -fr dist/
	@$(ECHO_CMD) "Clean\t\t${GREEN}[OK]${RESET}"
.PHONY:clean

## Package

ifeq ($(OS),Windows_NT)
PACKAGE_TARGETS := pkg-msi
else
PACKAGE_TARGETS := pkg-deb
endif

package: $(PACKAGE_TARGETS) ## Build all available packages
	@$(ECHO_CMD) "Package\t\t${GREEN}[OK]${RESET}"
.PHONY:package

ifeq ($(OS),Windows_NT)
pkg-msi: build ## Create MSI package
	@rm -rf $(TMPDIR)
	@rm -f $(OUTDIR)/$(MSI_FILE)
	@mkdir -p $(TMPDIR)
	go-msi to-rtf --src LICENSE --out $(TMPDIR)/LICENSE.rtf -e
	go-msi generate-templates --path build/package/msi/wix.json \
	 --src build/package/msi/templates --out $(TMPDIR) \
	 --version $(MSI_VERSION) --display $(VERSION) --license tmp/LICENSE.rtf
	go-msi gen-wix-cmd --path build/package/msi/wix.json --src tmp \
	 --out $(TMPDIR) --arch amd64 --msi $(TMPDIR)/$(MSI_FILE) \
	 --version $(MSI_VERSION) --display $(VERSION)
	@rm -f wix.dynamic.json
	cd $(TMPDIR) && ./build.bat
	mv $(TMPDIR)/$(MSI_FILE) $(OUTDIR)/
	@$(ECHO_CMD) "MSI package\t${GREEN}[OK]${RESET}"
.PHONY:pkg-msi
else

DEB_NAME := $(PROJECT_ID)_$(VERSION_NUMBER)-1_$(GOARCH)

pkg-deb: build ## Build debian package
	@rm -rf $(TMPDIR)
	@mkdir -p $(TMPDIR)/$(DEB_NAME)/usr/bin
	@mkdir -p $(TMPDIR)/$(DEB_NAME)/debian 
	@mkdir -p $(TMPDIR)/$(DEB_NAME)/DEBIAN
	@cp -a $(BINDIR)/* $(TMPDIR)/$(DEB_NAME)/usr/bin/
	@sed -e "s/VERSION/$(VERSION_NUMBER)/g" \
		$(CURDIR)/build/package/debian/changelog.tpl \
		> $(TMPDIR)/$(DEB_NAME)/changelog
	@sed -e "s/ARCH/$(GOARCH)/g" $(CURDIR)/build/package/debian/control.tpl \
		> $(TMPDIR)/$(DEB_NAME)/control
	DEB_HOST_ARCH=$(GOARCH) dpkg-gencontrol -v$(VERSION_NUMBER)-1 \
		-c$(TMPDIR)/$(DEB_NAME)/control \
		-l$(TMPDIR)/$(DEB_NAME)/changelog \
		-f$(TMPDIR)/$(DEB_NAME)/debian/files -Ptmp/$(DEB_NAME)
	dpkg-deb --build --root-owner-group $(TMPDIR)/$(DEB_NAME)
	dpkg-genchanges --build=binary \
		-c$(TMPDIR)/$(DEB_NAME)/control \
		-l$(TMPDIR)/$(DEB_NAME)/changelog \
		-f$(TMPDIR)/$(DEB_NAME)/debian/files \
		-u$(TMPDIR) -O$(OUTDIR)/$(DEB_NAME).changes
	@mv $(TMPDIR)/*.deb $(OUTDIR)/
	@$(ECHO_CMD) "pkg-deb\t\t${GREEN}[OK]${RESET}"
.PHONY:pkg-deb
endif

## Test

test: ## Run the tests of the project
ifeq ($(EXPORT_RESULT), true)
	@mkdir -p $(OUTDIR)
	$(eval OUTPUT_OPTIONS = | go-junit-report -set-exit-code > $(OUTDIR)/junit-report.xml)
endif
	$(GOTEST) -v $(GO_OPT) ./... $(OUTPUT_OPTIONS)
	@$(ECHO_CMD) "Test\t\t${GREEN}[OK]${RESET}"
.PHONY:test

coverage: ## Run the tests of the project and export the coverage report.
	@mkdir -p out
	$(GOTEST) -cover -covermode=count -coverprofile=$(OUTDIR)/profile.cov ./...
	$(GOCMD) tool cover -func $(OUTDIR)/profile.cov
ifeq ($(EXPORT_RESULT), true)
ifeq ($(COVERAGE_FORMAT), html)
	gocov convert $(OUTDIR)/profile.cov | gocov-html > $(OUTDIR)/coverage.html
else
	gocov convert $(OUTDIR)/profile.cov | gocov-xml > $(OUTDIR)/coverage.xml
endif
endif
	@$(ECHO_CMD) "Coverage\t${GREEN}[OK]${RESET}"
.PHONY:coverage

## Lint

lint: lint-go ## Run all available linters.
	@$(ECHO_CMD) "Lint\t\t${GREEN}[OK]${RESET}"
.PHONY:lint

lint-go:
	go fmt ./...
	go vet ./...
.PHONY:lint-go

## Documentation

doc: doc-golds ## Generate all documents.
	@$(ECHO_CMD) "Doc\t\t${GREEN}[OK]${RESET}"
.PHONY:doc

doc-common:
	@mkdir -p $(DOC_DIR)
.PHONY:doc-common

doc-golds: doc-common ## Generate HTML documentation with Golds.
	@mkdir -p $(DOC_DIR)/html
	golds -gen -dir=$(DOC_DIR)/html -nouses -source-code-reading=rich \
		-silent -only-list-exporteds -wdpkgs-listing=solo ./...
.PHONY:doc-golds

## Deploy

deploy: ## deploy debian packages
	find -O1 $(OUT_DIR) -name '*.changes' -exec dput mousesoft \{\} \;
.PHONY:deploy-deb

## Help

help: ## Show this help.
	@$(ECHO_CMD) ''
	@$(ECHO_CMD) 'Usage:'
	@$(ECHO_CMD) '  ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
	@$(ECHO_CMD) ''
	@$(ECHO_CMD) 'Targets:'
	@awk 'BEGIN {FS = ":.*?## "} { \
		if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf "    ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \
		else if (/^## .*$$/) {printf "  ${CYAN}%s${RESET}\n", substr($$1,4)} \
		}' $(MAKEFILE_LIST)
.PHONY:help
