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

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=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/nitro cmd/nitro/*.go

dev:
	AWS_PROFILE=samus-nitro-dev go run cmd/nitro/main.go

# dep
dep:
	retool do dep ensure

# TWIRP
twirp: setup generate generate-js build-twirp

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

generate-js:
	retool do protoc ./rpc/nitro_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/nitro/rpc

clean:
	rm -f twirp/*.pb.go
	rm -f 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/nitro .

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:
	find . -iname "mock*" -type f -not -path "./vendor*" -delete
	rm -rf mocks

	mockery --all --inpackage --dir=internal
	mockery --all --inpackage --dir=metrics

	mockery \
	    --dir=vendor/code.justin.tv/foundation/twitchclient \
	    --name=Client \
	    --output=mocks/TwitchClient

	mockery \
	    --dir=vendor/code.justin.tv/revenue/payments-service-go-client/client \
	    --name=Client \
	    --output=mocks/PaymentsClient

	mockery \
	    --dir=vendor/code.justin.tv/revenue/subscriptions/twirp \
	    --name=Subscriptions \
	    --output=mocks/SubscriptionsClient

	mockery \
	    --dir=vendor/code.justin.tv/amzn/TwitchVoyagerTwirp \
	    --name=TwitchVoyager \
	    --output=mocks/VoyagerClient

	mockery \
	    --dir=vendor/code.justin.tv/commerce/mako-client \
	    --name=Mako \
	    --output=mocks/MakoClient

	mockery \
	    --dir=vendor/github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface \
	    --name=CloudWatchAPI \
	    --output=mocks/CloudWatchAPI
