# Go + Rastrillo TodoMVC. See README.md.
#
# GOCACHE stays at its default (a shared machine rule): COLD=1 forces a
# from-scratch compile with -a instead of deleting the cache.

APP      := todo-go-rastrillo
ACCOUNT  := bfj
VERSION  ?= $(shell git rev-parse --short HEAD)
LDFLAGS  := -s -w -X 'amadan.net/rastrillo/rastrillo.BuildVersion=$(VERSION)'
COLDFLAG := $(if $(COLD),-a,)
RASTRILLO := go run amadan.net/rastrillo/rastrillo/cmd/rastrillo
MESSAGE  ?= Deploy $(VERSION)

# The string constant make bump edits: in handlers.go, beside GET /.
BUMP_FILE := internal/todo/handlers.go
BUMP_FROM := const notFoundTitle = "That item is not in your list"
BUMP_TO   := const notFoundTitle = "That item is not in your list."

export CGO_ENABLED = 0

.PHONY: build build-arm64 test perf migration-check clean clean-deps bump unbump deploy

build:
	go build $(COLDFLAG) -trimpath -ldflags="$(LDFLAGS)" -o dist/app ./cmd/todo

build-arm64:
	GOOS=linux GOARCH=arm64 go build $(COLDFLAG) -trimpath -ldflags="$(LDFLAGS)" -o dist/app-linux-arm64 ./cmd/todo

# Rastrillo's test runner: go test plus each package's time budget.
test:
	go vet ./...
	test -z "$$(gofmt -l .)" || { gofmt -l .; exit 1; }
	$(RASTRILLO) budget test ./...
	$(RASTRILLO) migration check

# Every GET screen at 150ms to first byte, cold boot at 500ms. Serial
# and uncached, because a measurement taken beside other work isn't one.
perf:
	$(RASTRILLO) budget test -no-time -require TestPerfScreens,TestPerfBoot -tags perf -count=1 -p 1 -parallel 1 -run '^TestPerf' ./...

clean:
	rm -rf dist

# Go keeps compiled packages in the shared GOCACHE, which this target
# leaves alone; `make build-arm64 COLD=1` recompiles everything with -a.
clean-deps: clean

bump:
	sed -i 's/^$(BUMP_FROM)$$/$(BUMP_TO)/' $(BUMP_FILE)
	grep -qxF '$(BUMP_TO)' $(BUMP_FILE)

unbump:
	sed -i 's/^$(BUMP_TO)$$/$(BUMP_FROM)/' $(BUMP_FILE)
	grep -qxF '$(BUMP_FROM)' $(BUMP_FILE)

deploy:
	carlos deploy --account $(ACCOUNT) --app $(APP) --target linux-arm64 --message "$(MESSAGE)" dist/app-linux-arm64
