SHELL := /bin/bash
TOOL_DIR=$(CURDIR)/_tools/bin
RETOOL=$(TOOL_DIR)/retool

GO_MOCK=$(RETOOL) do mockery -dir $(GOPATH)/src/code.justin.tv/samus/red-dot
GO_MOCK_VENDOR=$(RETOOL) do mockery -dir $(GOPATH)/src/code.justin.tv/samus/red-dot/vendor
MOCKS_PATH=$(GOPATH)/src/code.justin.tv/samus/red-dot/mocks
export PATH := $(TOOL_DIR):$(PATH)

novendor = $(shell go list ./... | grep -v vendor/ | grep -v integration_test)

.PHONY: test mocks

all: release

# Runs all tasks that need to be done as part of a release. Think 'bb release"...
release: fmt lint test install

# Run all static checks
lint: setup
	retool do gometalinter --disable-all --vendor --tests --deadline=5m --enable=gofmt --enable=goimports -enable=errcheck --enable=vet ./...

# Run auto-formatting
fmt: setup
	find . -name "*.go" | egrep -v "vendor|_tools" | xargs retool do goimports -w

# Run tests
test:
	go test $(novendor)

integration-test:
	# go test ./integration_test

test-coverage:
	go get github.com/axw/gocov/gocov
	go get github.com/t-yuki/gocov-xml
	gocov test -race -v $(novendor) | gocov-xml > coverage.xml

# Build the executable
install:
	go build -o bin/red-dot cmd/red-dot/*.go

dev:
	AWS_PROFILE=red-dot-staging go run cmd/red-dot/main.go

# dep
dep:
	retool do dep ensure

# TWIRP
twirp: setup generate build-twirp

generate:
	retool do protoc --proto_path=$$GOPATH/src:. --twirp_out=. --go_out=. ./rpc/red_dot_service.proto

generate-js:
	retool do protoc ./rpc/red_dot_service.proto --proto_path=$$GOPATH/src:. --js_out=import_style=commonjs,binary:./rpc-js/ --twirp_js_out=./rpc-js/ --go_out=./

build-twirp:
	go build code.justin.tv/samus/red-dot/rpc

clean:
	rm twirp/*.pb.go
	rm twirp/*.twirp.go

# Setup
setup:
	./scripts/install_proto.bash
	# Setup path to use retool
	retool do gometalinter --install

### MANTA TASKS ###

# Kick off the full manta build
manta_bootstrap: manta_cleanup
	manta -v -f build.json

# Task that is run inside of the manta build to actually build the service
manta: dep release prune migrate

# Remove all build artifacts that we don't care about to minimize distro size
prune:
	find . -mindepth 1 -maxdepth 1 -not -name bin -not -name config -not -name courier -not -name campaign -exec rm -rf {} \;

# Remove manta artifacts
manta_cleanup:
	rm -rf .manta/bin
	rm -rf .manta/courier
	rm -rf .manta/config
	rm -rf .manta/campaign

# Move files that need to be manually included in the manta build
migrate:
	mv -f bin/red-dot .

manta_integration_tests: dep integration-test


### MOCKS ###

# When adding a new mock, the name input has to be equivalent to the name of the interface you want mocked
mocks:
	mockery -dir=metrics/ -name IMetricLogger
	mockery -dir=vendor/github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface -name CloudWatchAPI
	go generate $(novendor)
