# todo-go: the CARLOS bakeoff's TodoMVC in Go. See README.md.

VERSION ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo dev)
LDFLAGS := -s -w -X main.version=$(VERSION)
# make build-arm64 COLD=1 rebuilds every package, dependencies included,
# without touching the shared GOCACHE.
GOFLAGS_COLD := $(if $(COLD),-a,)

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

build: dist/app
build-arm64: dist/app-linux-arm64

# Both binaries are always handed to `go build`, which decides from its own
# cache what needs compiling, so a new VERSION or COLD=1 always takes effect.
dist/app: FORCE
	CGO_ENABLED=0 go build $(GOFLAGS_COLD) -trimpath -ldflags '$(LDFLAGS)' -o $@ .

dist/app-linux-arm64: FORCE
	CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build $(GOFLAGS_COLD) -trimpath -ldflags '$(LDFLAGS)' -o $@ .

FORCE:

test: css-check
	go vet ./...
	CGO_ENABLED=0 go test ./...

# The served stylesheets must stay byte-for-byte copies of shared/.
css-check:
	cmp ../../shared/base.css web/static/base.css
	cmp ../../shared/index.css web/static/index.css

clean:
	rm -rf dist

# Go keeps compiled packages in the shared GOCACHE, which this repo never
# changes or clears. Downloaded sources stay in the module cache; use
# `make build-arm64 COLD=1` to force every package to recompile.
clean-deps: clean

bump:
	sed -i 's/^const bumpMarker = "steady"$$/const bumpMarker = "bumped"/' web/web.go
	grep -q '^const bumpMarker = "bumped"$$' web/web.go

unbump:
	sed -i 's/^const bumpMarker = "bumped"$$/const bumpMarker = "steady"/' web/web.go
	grep -q '^const bumpMarker = "steady"$$' web/web.go

# Ships the binary `make build-arm64` made; it doesn't rebuild.
deploy:
	test -f dist/app-linux-arm64 || { echo 'run make build-arm64 first' >&2; exit 1; }
	carlos deploy --account bfj --app todo-go --target linux-arm64 --no-prompt \
		--version $(VERSION) --message "$(or $(MESSAGE),Deploy todo-go $(VERSION))" dist/app-linux-arm64
