# Color definitions
RESET=\033[0m
GREY=\033[38;5;242m
LIGHT_GREY=\033[38;5;247m
TITLE=\033[38;5;144m

.PHONY: default
default: help

#  __
# /    _   _   _ |_  _   _  |_  _
# \__ (_) | ) _) |_ (_| | ) |_ _)

GREEN := \033[0;32m
PURPLE := \033[0;35m
WHITE := \033[0m

REACT_NATIVE_LIBRARY_TARBALL_NAME := bosch-tt-react-native-shc-connector
REACT_NATIVE_LIBRARY_DEPENDENCY_EXAMPLE_PATH := ./Example/node_modules/bosch-tt-react-native-shc-connector/

#  __
# |  \  _  _   _  _   _|  _  _ .  _  _
# |__/ (- |_) (- | ) (_| (- (_ | (- _)
#         |

node_modules: package.json yarn.lock
ifdef CI
# under CI
	yarn install --frozen-lockfile --no-progress --cache-folder ./yarn_cache
else
	yarn install
endif

.PHONY: Example/node_modules
example/node_modules:
	cd Example && make node_modules

.PHONY: kill
kill:
	${react_native_kill_packager}

#  __
# |__)     . |  _|
# |__) |_| | | (_|

.PHONY: build
build: node_modules clean ## Transpiles the project to JavaScript
	yarn tsc

.PHONY: watch
watch: node_modules ## Runs TypeScript compilator in watch mode and updates example app when changes are made
	yarn run tsc-watch --onSuccess 'make update-example'

# |   .  _  |_
# |__ | | ) |_

.PHONY: lint
lint: node_modules example/node_modules ## Lint source code and fix problems where possible
	yarn run eslint --fix --ext .js,.ts,.tsx --max-warnings=0 .

.PHONY: lint-ci
lint-ci: node_modules example/node_modules ## Lint source code in CI mode
	yarn run eslint --ignore-pattern 'yarn_cache/*' --ext .js,.ts,.tsx --max-warnings=0 .

.PHONY: lint-example
lint-example: node_modules example/node_modules ## Lint source code of example app and fix problems where possible
	yarn run eslint --fix --ext .js,.ts,.tsx --max-warnings=0 ./Example

.PHONY: lint-ci-example
lint-ci-example: node_modules example/node_modules ## Lint source code of example app in CI mode
	yarn run eslint --ignore-pattern 'yarn_cache/*' --ext .js,.ts,.tsx --max-warnings=0 ./Example

# ___
#  |   _   _ |_   _  | |
#  |  (_| |  |_) (_| | |

.PHONY: tarball
tarball: ## Packs the react-native library into a tarball
	@echo "$(PURPLE)Create library tarball$(WHITE)"
	@rm -rf ./*.tgz
	@npm pack --quiet > /dev/null
	@mv ./${REACT_NATIVE_LIBRARY_TARBALL_NAME}-*.tgz ./${REACT_NATIVE_LIBRARY_TARBALL_NAME}.tgz
	@echo "$(GREEN)Done$(WHITE)"

.PHONY: update-example
update-example: ## Copies the transpiled code and resources into the Example app from the packed tarball
	@echo "$(PURPLE)Update example app$(WHITE)"
	@tar -xzf ${REACT_NATIVE_LIBRARY_TARBALL_NAME}.tgz
	@mkdir -p ${REACT_NATIVE_LIBRARY_DEPENDENCY_EXAMPLE_PATH}
	@rm -rf ${REACT_NATIVE_LIBRARY_DEPENDENCY_EXAMPLE_PATH}
	@mv ./package ${REACT_NATIVE_LIBRARY_DEPENDENCY_EXAMPLE_PATH}
	@echo "$(GREEN)Done$(WHITE)"

# |__|  _ |  _
# |  | (- | |_)
#           |
### Help

# This help parses this makefile and lists all targets that have a double # comment next to them
# Everything found after a second double # in the command description comment will be parsed as
# argument usage indicator.
# A comment with three #s will be printed a section header.
# grep command src: https://gist.github.com/prwhite/8168133
.PHONY: help
help: ## Show this help
	@echo "List of make targets: "
	@{ \
		fgrep -h "##" $(MAKEFILE_LIST) | \
		egrep -v 'fgrep|sed|awk' | \
		sed -e 's/### \(.*\)/===== \1 =====/' | \
		sed -e 's/\\$$//' | sed -e 's/:[^#]*//' | \
		awk -F'##' '/^[^=]/ {printf "  %s${LIGHT_GREY}%s${RESET}\n   ${GREY}%s${RESET}\n\n", $$1, $$3, $$2}\
		/^=/ { printf "${TITLE}%s${RESET}\n\n", $$0 }'; \
	}
