DOCKER_TAG = eventbus/schema
DOCKER_VOLUME = eventbus-schema-cache
DOCKER_VOLUME_MOUNT = -v $(DOCKER_VOLUME):/.cache
DOCKER_LOCAL_MOUNT = -v $(shell pwd):$(shell pwd) -w $(shell pwd) -u $(shell id -u):$(shell id -g)
DOCKER_RUN = docker run --rm $(DOCKER_VOLUME_MOUNT) $(DOCKER_LOCAL_MOUNT) $(DOCKER_TAG)

default: build

docker-image:
	docker build -t $(DOCKER_TAG) .

docker-volume:
	docker volume create $(DOCKER_VOLUME)

build: docker-image docker-volume
	$(DOCKER_RUN) prototool generate events

fmt: docker-image docker-volume
	$(DOCKER_RUN) prototool format events --overwrite

lint: docker-image docker-volume
	$(DOCKER_RUN) prototool lint events
	$(DOCKER_RUN) prototool format events --lint

test: docker-image docker-volume
	$(DOCKER_RUN) prototool break check events --git-branch master

proto-lib: docker-image docker-volume
	$(DOCKER_RUN) protoc --go_out=../../.. proto-lib/eventbus/change.proto
	$(DOCKER_RUN) protoc --go_out=../../.. proto-lib/eventbus/authorization.proto
	$(DOCKER_RUN) protoc --go_out=../../.. proto-lib/eventbus/owner.proto

jenkins-check-gen: docker-image docker-volume
	mkdir -p ./tmp/pkg && \
	docker run --rm $(DOCKER_LOCAL_MOUNT) $(DOCKER_VOLUME_MOUNT) \
		-v $(shell pwd)/tmp/pkg:$(shell pwd)/pkg $(DOCKER_TAG) \
		bash -c '\
			prototool generate events && \
			protoc --go_out=. proto-lib/eventbus/change.proto && \
			protoc --go_out=. proto-lib/eventbus/authorization.proto && \
			mkdir -p pkg/eventbus && \
			mv code.justin.tv/eventbus/schema/pkg/eventbus/change pkg/eventbus/ && \
			mv code.justin.tv/eventbus/schema/pkg/eventbus/authorization pkg/eventbus/'

jenkins-generated-code-check:
	bash ./cmd/jenkins/generated-code-check.sh

jenkins-metadata-generation: docker-image docker-volume
	mkdir -p ./tmp/metadata &&\
	$(DOCKER_RUN) prototool generate \
		--config-data '{"excludes":["vendor"],"protoc":{"includes":["proto-lib", "events"]},"generate":{"plugins":[{"name":"eventbus-metadata","type":"go","output":"./tmp/metadata"}],"go_options":{"import_path":"code.justin.tv/eventbus/schema"}}}' \
		events

go-test:
	go test -race ./cmd/...

.PHONY: docker-image docker-volume build fmt lint test proto-lib
