Files
gocryptotrader/Makefile
Adrian Gallagher ef8cb7b1e7 Makefile: Improve existing and add new targets (#1937)
* I've made a comprehensive overhaul of the Makefile for your Go development workflow.

This update brings a significant series of enhancements to the project's Makefile, aimed at modernizing and improving your Go development experience. This includes improvements to linting, formatting, dependency management, Docker integration, and the overall Makefile structure, style, and usability.

Here are the key improvements:

1.  **Linting (`golangci-lint`):**
    *   **Version & Path:** I've upgraded `golangci-lint` to use the v2 path (`github.com/golangci/golangci-lint/cmd/golangci-lint/v2`) and pinned it to a specific version (`v2.1.6`) for consistency.
    *   **Target Renaming:** The primary linting target was renamed from `linter` to `lint` to align with common conventions.
    *   **Command Simplification:** The `lint` target's command was streamlined to `$(LINTBIN) run --verbose`, relying directly on `golangci-lint`'s exit code for success/failure reporting.
    *   **Redundancy Removal:** A superfluous `go install $(GCTPKG)` command was removed from the `lint` target.

2.  **Dockerized Linting (`lint_docker`):**
    *   A new `lint_docker` target (renamed from `lint-docker` for consistent naming) was introduced to execute `golangci-lint` within a Docker container (`golangci/golangci-lint:v2.1.6`).
    *   This ensures a consistent linting environment across different developer setups and CI.
    *   The target uses `$(CURDIR)` for reliable Docker volume mounting.
    *   It includes the `--verbose` flag for detailed linter output and the `-t` flag to allocate a pseudo-TTY, enabling colored output from the linter.
    *   It incorporates a check for Docker's availability before attempting to run.
    *   Whitespace in the Docker check command was normalized for readability (single space after `||`).

3.  **Code Formatting (`gofmt` & `gofumpt`):**
    *   **Default Formatter:** The main `fmt` target now consistently uses `gofmt` for standard Go formatting.
    *   **Stricter Formatting Option:** A new `gofumpt` target has been added, allowing you to use the stricter `gofumpt` formatter. This target includes a command to automatically install `gofumpt` if it's not already present.
    *   **File Exclusion Control (`GO_FILES_TO_FORMAT`):**
        - A Make variable `GO_FILES_TO_FORMAT` was introduced to precisely define the set of Go files to be processed by the formatters.
        - This variable is configured to exclude:
            - Generated ORM code (e.g., `./database/models/*`).
            - Vendored dependencies (`./vendor/*`).
            - Protobuf generated files (`*.pb.go`).
            - gRPC gateway generated files (`*.pb.gw.go`).
        - Both the `fmt` and `gofumpt` targets now utilize this variable, preventing accidental reformatting of generated or third-party code.

4.  **Makefile Structure & General Enhancements:**
    *   **Redundant Target Removal:** The `get` target was removed, as its functionality was fully covered by the more optimized `install` target.
    *   **`.PHONY` Declarations:**
        - The default `all` target was explicitly added to the `.PHONY` list for improved clarity and robustness.
        - All relevant Makefile targets, including newly added and renamed ones (`lint`, `gofumpt`, `lint_docker`, `fmt`), are explicitly declared in the `.PHONY` list, adhering to best practices.
        - The order of targets in the main `.PHONY` declaration was adjusted to match the sequence of their definitions in the Makefile, enhancing readability.

These comprehensive changes aim to provide you with a more robust, flexible, consistent, and maintainable build and development environment.

* Fix: Ensure GOPATH is defined in Makefile

This commit enhances the Makefile by ensuring that the GOPATH
variable is defined before being used by LINTBIN and GOFUMPTBIN.

The line `GOPATH ?= $(shell go env GOPATH)` has been added.
This means:
- If GOPATH is set in the environment, that value will be used.
- Otherwise, GOPATH will be set to the output of `go env GOPATH`,
  which is the canonical way to determine the Go path.

This change makes the Makefile more robust and less dependent on
your shell environment having GOPATH explicitly set.

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2025-06-12 16:25:35 +10:00

92 lines
2.7 KiB
Makefile

LDFLAGS = -ldflags "-w -s"
GCTPKG = github.com/thrasher-corp/gocryptotrader
LINTPKG = github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.1.6
GOPATH ?= $(shell go env GOPATH)
LINTBIN = $(GOPATH)/bin/golangci-lint
GOFUMPTBIN = $(GOPATH)/bin/gofumpt
GCTLISTENPORT=9050
GCTPROFILERLISTENPORT=8085
GO_FILES_TO_FORMAT := $(shell find . -type f -name '*.go' -not -path "./database/models/*" -not -path "./vendor/*" -not -name "*.pb.go" -not -name "*.pb.gw.go")
DRIVER ?= psql
RACE_FLAG := $(if $(NO_RACE_TEST),,-race)
CONFIG_FLAG = $(if $(CONFIG),-config $(CONFIG),)
.PHONY: all lint lint_docker check test build install fmt gofumpt update_deps
all: check build
lint:
go install $(LINTPKG)
$(LINTBIN) run --verbose
lint_docker:
@command -v docker >/dev/null 2>&1 || (echo "Docker not found. Please install Docker to run this target." && exit 1)
docker run --rm -t -v $(CURDIR):/app -w /app golangci/golangci-lint:v2.1.6 golangci-lint run --verbose
check: lint test
test:
go test $(RACE_FLAG) -coverprofile=coverage.txt -covermode=atomic ./...
build:
go build $(LDFLAGS)
install:
go install $(LDFLAGS)
fmt:
gofmt -l -w -s $(GO_FILES_TO_FORMAT)
gofumpt:
@command -v gofumpt >/dev/null 2>&1 || go install mvdan.cc/gofumpt@latest
$(GOFUMPTBIN) -l -w $(GO_FILES_TO_FORMAT)
update_deps:
go mod verify
go mod tidy
rm -rf vendor
go mod vendor
.PHONY: profile_heap
profile_heap:
go tool pprof -http "localhost:$(GCTPROFILERLISTENPORT)" 'http://localhost:$(GCTLISTENPORT)/debug/pprof/heap'
.PHONY: profile_cpu
profile_cpu:
go tool pprof -http "localhost:$(GCTPROFILERLISTENPORT)" 'http://localhost:$(GCTLISTENPORT)/debug/pprof/profile'
.PHONY: gen_db_models
gen_db_models: target/sqlboiler.json
ifeq ($(DRIVER), psql)
sqlboiler -c $< -o database/models/postgres -p postgres --no-auto-timestamps --wipe $(DRIVER)
else ifeq ($(DRIVER), sqlite3)
sqlboiler -c $< -o database/models/sqlite3 -p sqlite3 --no-auto-timestamps --wipe $(DRIVER)
else
$(error Driver '$(DRIVER)' not supported)
endif
target/sqlboiler.json:
mkdir -p $(@D)
go run ./cmd/gen_sqlboiler_config/main.go $(CONFIG_FLAG) -outdir $(@D)
.PHONY: lint_configs
lint_configs: check-jq
@$(call sort-json,config_example.json)
@$(call sort-json,testdata/configtest.json)
define sort-json
@printf "Processing $(1)... "
@jq '.exchanges |= sort_by(.name)' --indent 1 $(1) > $(1).temp && \
(mv $(1).temp $(1) && printf "OK\n") || \
(rm $(1).temp; printf "FAILED\n"; exit 1)
endef
.PHONY: check-jq
check-jq:
@printf "Checking if jq is installed... "
@command -v jq >/dev/null 2>&1 && { printf "OK\n"; } || { printf "FAILED. Please install jq to proceed.\n"; exit 1; }
.PHONY: sonic
sonic:
go build $(LDFLAGS) -tags "sonic_on"