SHELL :=/bin/bash -e -o pipefail
PWD   :=$(shell pwd)

.DEFAULT_GOAL := all
.PHONY: all
all: ## build pipeline
all: get format check test-unit

.PHONY: ci
ci: ## CI build pipeline
ci: all

.PHONY: precommit
precommit: ## validate the branch before commit
precommit: all

.PHONY: help
help: ## Help dialog
				@echo 'Usage: make <OPTIONS> ... <TARGETS>'
				@echo ''
				@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: doctor
doctor: ## Check fvm flutter doctor
				@fvm flutter doctor

.PHONY: version
version: ## Check fvm flutter version
				@fvm flutter --version

.PHONY: format
format: ## Format code
				@fvm dart format . --set-exit-if-changed --line-length 80 -o none || (echo "¯\_(ツ)_/¯ Format code error"; exit 1)

.PHONY: fix
fix: format ## Fix code
				@fvm dart fix --apply lib

.PHONY: clean
clean: ## Clean flutter
				@fvm flutter clean
				@cd example && fvm flutter clean

.PHONY: get
get: ## Get dependencies
				@fvm flutter pub get || (echo "¯\_(ツ)_/¯ Get dependencies error"; exit 1)

.PHONY: init-ios-pods
init-ios-pods: ## Init ios pods (use Podfile)
				@fvm flutter config --no-enable-swift-package-manager && flutter config --no-enable-swift-package-manager
				@cd example && fvm flutter clean
				@cd example && fvm flutter pub get
				@cd example/ios && \
						( [ -f "_Podfile" ] && [ ! -f "Podfile" ] && mv "_Podfile" "Podfile" || true ) && \
						rm -rf Pods Podfile.lock && \
						pod install
				@cd example && fvm flutter pub get || (echo "¯\_(ツ)_/¯ Init ios pods error"; exit 1)

.PHONY: init-ios-spm
init-ios-spm: ## Init ios swift package manager (use _Podfile)
				@fvm flutter config --enable-swift-package-manager && flutter config --enable-swift-package-manager
				@cd example && fvm flutter clean
				@cd example/ios && \
						( [ -f "Podfile" ] && mv "Podfile" "_Podfile" || true ) && \
						rm -rf Pods Podfile.lock && \
						( pod deintegrate || true )
				@cd example && fvm flutter pub get || (echo "¯\_(ツ)_/¯ Init ios swift package manager error"; exit 1)

.PHONY: analyze
analyze: get format ## Analyze code
				@fvm flutter analyze --fatal-warnings --no-fatal-infos lib/ test/

.PHONY: check
check: analyze ## Check code
				@echo "╠ RUN CECK PUBLISH..."
				@dart pub publish --dry-run
				@echo "╠ CECKED PUBLISH SUCCESSFULLY"

.PHONY: publish
publish: ## Publish package
				@dart pub publish --server=https://pub.dartlang.org || (echo "¯\_(ツ)_/¯ Publish error"; exit 1)

.PHONY: coverage
coverage: ## Runs get coverage
				@lcov --summary coverage/lcov.info

.PHONY: run-genhtml
run-genhtml: ## Runs generage coverage html
				@genhtml coverage/lcov.info -o coverage/html

.PHONY: test-unit
test-unit: ## Runs unit tests
				@flutter test --coverage || (echo "Error while running tests"; exit 1)
				@genhtml coverage/lcov.info --output=coverage -o coverage/html || (echo "Error while running genhtml with coverage"; exit 2)

.PHONY: tag
tag: ## Add a tag to the current commit
				@dart run tool/tag.dart

.PHONY: tag-add
tag-add: ## Make command to add TAG. E.g: make tag-add TAG=v1.0.0
				@if [ -z "$(TAG)" ]; then echo "TAG is not set"; exit 1; fi
				@git tag $(TAG)
				@git push origin $(TAG)

.PHONY: tag-remove
tag-remove: ## Make command to delete TAG. E.g: make tag-delete TAG=v1.0.0
				@if [ -z "$(TAG)" ]; then echo "TAG is not set"; exit 1; fi
				@git tag -d $(TAG)
				@git push origin --delete $(TAG)

.PHONY: build
build:  ## Build test apk for android on example apps
				@cd example && fvm flutter clean && fvm flutter pub get && fvm flutter build apk --release && fvm flutter build ios --release --no-codesign
