1- # File: Makefile
2- # Purpose:
3- # - "install": ensure modules are tidy if go.mod or go.sum changed.
4- # - "test": run coverage if code changed.
5- # - "coverage": display coverage summary from coverage.out
6- # - "build": recompile binary if Go source or mod files changed.
7- # - "run": executes the compiled binary
8- # - "tree": display directory structure
9- # - All skip with "No changes detected, skipping X." if nothing changed.
10-
11- .PHONY : install test coverage build run tree
12-
13- # Directories for build artifacts and stamp files
14- BUILD_DIR := build
15- STAMPS_DIR := $(BUILD_DIR ) /.stamps
16-
17- # Stamp file paths for each step
18- INSTALL_STAMP := $(STAMPS_DIR ) /install.stamp
19- BUILD_STAMP := $(STAMPS_DIR ) /build.stamp
20- TEST_STAMP := $(STAMPS_DIR ) /test.stamp
21-
22- BINARY := $(BUILD_DIR ) /scrapeycli
23-
24- # Coverage output
25- COVER_DIR := ${BUILD_DIR}/coverage
26- COVER_PROFILE := $(COVER_DIR ) /coverage.txt
27- COVER_HTML := $(COVER_DIR ) /coverage.html
28-
29- # All Go source files (including _test.go files)
30- GO_FILES := $(shell find . -type f -name '* .go' -not -path "./vendor/* ")
31-
32- # Reusable messages
33- SKIP_MSG := No changes detected, skipping
34- CHANGE_MSG := Some files changed; re-running
35-
36- # ------------------------------------------------------------------------------
37- # install: ensure go.mod/go.sum are tidy if changed.
38- # ------------------------------------------------------------------------------
39- install :
40- @mkdir -p $(STAMPS_DIR )
41- @TARGET=install; \
42- if [ ! -f " $( INSTALL_STAMP) " ] || [ go.mod -nt " $( INSTALL_STAMP) " ] || [ go.sum -nt " $( INSTALL_STAMP) " ]; then \
43- echo " $( CHANGE_MSG) $$ TARGET..." ; \
44- go mod tidy; \
45- if ! git diff --exit-code go.sum; then \
46- echo " go.sum updated. Please commit the changes." ; \
47- exit 1; \
48- fi ; \
49- touch " $( INSTALL_STAMP) " ; \
50- echo " Done with installing." ; \
51- else \
52- echo " $( SKIP_MSG) $$ TARGET." ; \
53- fi
54-
55- # ------------------------------------------------------------------------------
56- # test: run tests and update coverage if any Go source (including _test.go files) have changed.
57- # Ensures gotestsum is installed before running tests.
58- # Depends on install.
59- # ------------------------------------------------------------------------------
60- test : install
61- @if ! command -v gotestsum > /dev/null 2>&1 ; then \
62- echo " Installing gotestsum..." ; \
63- go install gotest.tools/gotestsum@latest; \
64- fi
65- @mkdir -p $(COVER_DIR ) $(STAMPS_DIR )
66- @TARGET=test; \
67- if [ ! -f " $( TEST_STAMP) " ] || [ -n " $$ (find $( GO_FILES) -newer " $( TEST_STAMP) " 2>/dev/null)" ]; then \
68- echo " $( CHANGE_MSG) $$ TARGET..." ; \
69- > " $( COVER_PROFILE) " ; \
70- if gotestsum --format short-verbose ./... && \
71- go test -cover -covermode=atomic -coverpkg=./... -coverprofile=" $( COVER_PROFILE) " ./... > /dev/null; then \
72- if [ -f " $( COVER_PROFILE) " ]; then \
73- go tool cover -html=" $( COVER_PROFILE) " -o " $( COVER_HTML) " ; \
74- echo " Coverage file generated at: $( COVER_PROFILE) " ; \
75- echo " HTML coverage report at: $( COVER_HTML) " ; \
76- else \
77- echo " ERROR: Coverage file was not generated!" ; \
78- fi ; \
79- touch " $( TEST_STAMP) " ; \
80- else \
81- echo " Tests failed! Skipping stamp update." ; \
82- exit 1; \
83- fi ; \
84- else \
85- echo " $( SKIP_MSG) $$ TARGET." ; \
86- fi
87-
88- # ------------------------------------------------------------------------------
89- # coverage: displays a colorized coverage summary from the coverage file.
90- # Depends on test.
91- # ------------------------------------------------------------------------------
92- coverage : test
93- @echo " ================== COVERAGE SUMMARY =================="
94- @go tool cover -func=" $( COVER_PROFILE) " | go run ./scripts/coverage_formatter.go
95- @echo " ====================================================="
96-
97- # ------------------------------------------------------------------------------
98- # build: compile binary if Go sources changed.
99- # Depends on install.
100- # ------------------------------------------------------------------------------
101- build : install
102- @mkdir -p $(BUILD_DIR )
103- @mkdir -p $(STAMPS_DIR )
104- @TARGET=build; \
105- if [ ! -f " $( BUILD_STAMP) " ] || [ -n " $$ (find $( GO_FILES) -newer " $( BUILD_STAMP) " 2>/dev/null)" ]; then \
106- echo " $( CHANGE_MSG) $$ TARGET..." ; \
107- go build -o $(BINARY ) ./cmd/scrapeycli; \
108- touch " $( BUILD_STAMP) " ; \
109- echo " Done with building." ; \
110- else \
111- echo " $( SKIP_MSG) $$ TARGET." ; \
112- fi
113-
114- # ------------------------------------------------------------------------------
115- # run: execute the compiled binary.
116- # Depends on build.
117- # ------------------------------------------------------------------------------
118- run : build
119- @echo " Running application..."
120- @$(BINARY )
121-
122- # ------------------------------------------------------------------------------
123- # tree: displays directory structure (installs tree if missing).
124- # ------------------------------------------------------------------------------
125- tree :
126- @if ! command -v tree > /dev/null 2>&1 ; then \
127- echo " tree command not found. Attempting to install..." ; \
128- OS=$$(uname ) ; \
129- if [ " $$ OS" = " Linux" ]; then \
130- sudo apt-get update && sudo apt-get install -y tree; \
131- elif [ " $$ OS" = " Darwin" ]; then \
132- brew install tree; \
133- else \
134- echo " Automatic installation not supported on $$ OS. Please install manually." ; \
135- exit 1; \
136- fi ; \
137- fi ; \
1+ # File: Makefile
2+ # Purpose:
3+ # - "install": ensure modules are tidy if go.mod or go.sum changed.
4+ # - "test": run coverage if code changed.
5+ # - "coverage": display coverage summary from coverage.out
6+ # - "build": recompile binary if Go source or mod files changed.
7+ # - "run": executes the compiled binary
8+ # - "tree": display directory structure
9+ # - All skip with "No changes detected, skipping X." if nothing changed.
10+
11+ .PHONY : install test coverage build run tree
12+
13+ # Directories for build artifacts and stamp files
14+ BUILD_DIR := build
15+ STAMPS_DIR := $(BUILD_DIR ) /.stamps
16+
17+ # Stamp file paths for each step
18+ INSTALL_STAMP := $(STAMPS_DIR ) /install.stamp
19+ BUILD_STAMP := $(STAMPS_DIR ) /build.stamp
20+ TEST_STAMP := $(STAMPS_DIR ) /test.stamp
21+
22+ BINARY := $(BUILD_DIR ) /scrapeycli
23+
24+ # Coverage output
25+ COVER_DIR := ${BUILD_DIR}/coverage
26+ COVER_PROFILE := $(COVER_DIR ) /coverage.txt
27+ COVER_HTML := $(COVER_DIR ) /coverage.html
28+
29+ # All Go source files (including _test.go files)
30+ GO_FILES := $(shell find . -type f -name '* .go' -not -path "./vendor/* ")
31+
32+ # Reusable messages
33+ SKIP_MSG := No changes detected, skipping
34+ CHANGE_MSG := Some files changed; re-running
35+
36+ # ------------------------------------------------------------------------------
37+ # install: ensure go.mod/go.sum are tidy if changed.
38+ # ------------------------------------------------------------------------------
39+ install :
40+ @mkdir -p $(STAMPS_DIR )
41+ @TARGET=install; \
42+ if [ ! -f " $( INSTALL_STAMP) " ] || [ go.mod -nt " $( INSTALL_STAMP) " ] || [ go.sum -nt " $( INSTALL_STAMP) " ]; then \
43+ echo " $( CHANGE_MSG) $$ TARGET..." ; \
44+ go mod tidy; \
45+ if ! git diff --exit-code go.sum; then \
46+ echo " go.sum updated. Please commit the changes." ; \
47+ exit 1; \
48+ fi ; \
49+ touch " $( INSTALL_STAMP) " ; \
50+ echo " Done with installing." ; \
51+ else \
52+ echo " $( SKIP_MSG) $$ TARGET." ; \
53+ fi
54+
55+ # ------------------------------------------------------------------------------
56+ # test: run tests and update coverage if any Go source (including _test.go files) have changed.
57+ # Ensures gotestsum is installed before running tests.
58+ # Depends on install.
59+ # ------------------------------------------------------------------------------
60+ test : install
61+ @if ! command -v gotestsum > /dev/null 2>&1 ; then \
62+ echo " Installing gotestsum..." ; \
63+ go install gotest.tools/gotestsum@latest; \
64+ fi
65+ @mkdir -p $(COVER_DIR ) $(STAMPS_DIR )
66+ @TARGET=test; \
67+ if [ ! -f " $( TEST_STAMP) " ] || [ -n " $$ (find $( GO_FILES) -newer " $( TEST_STAMP) " 2>/dev/null)" ]; then \
68+ echo " $( CHANGE_MSG) $$ TARGET..." ; \
69+ > " $( COVER_PROFILE) " ; \
70+ if gotestsum --format short-verbose ./... && \
71+ go test -cover -covermode=atomic -coverpkg=./... -coverprofile=" $( COVER_PROFILE) " ./... > /dev/null; then \
72+ if [ -f " $( COVER_PROFILE) " ]; then \
73+ go tool cover -html=" $( COVER_PROFILE) " -o " $( COVER_HTML) " ; \
74+ echo " Coverage file generated at: $( COVER_PROFILE) " ; \
75+ echo " HTML coverage report at: $( COVER_HTML) " ; \
76+ else \
77+ echo " ERROR: Coverage file was not generated!" ; \
78+ fi ; \
79+ touch " $( TEST_STAMP) " ; \
80+ else \
81+ echo " Tests failed! Skipping stamp update." ; \
82+ exit 1; \
83+ fi ; \
84+ else \
85+ echo " $( SKIP_MSG) $$ TARGET." ; \
86+ fi
87+
88+ # ------------------------------------------------------------------------------
89+ # coverage: displays a colorized coverage summary from the coverage file.
90+ # Depends on test.
91+ # ------------------------------------------------------------------------------
92+ coverage : test
93+ @echo " ================== COVERAGE SUMMARY =================="
94+ @go tool cover -func=" $( COVER_PROFILE) " | go run ./scripts/coverage_formatter.go
95+ @echo " ====================================================="
96+
97+ # ------------------------------------------------------------------------------
98+ # build: compile binary if Go sources changed.
99+ # Depends on install.
100+ # ------------------------------------------------------------------------------
101+ build : install
102+ @mkdir -p $(BUILD_DIR )
103+ @mkdir -p $(STAMPS_DIR )
104+ @TARGET=build; \
105+ if [ ! -f " $( BUILD_STAMP) " ] || [ -n " $$ (find $( GO_FILES) -newer " $( BUILD_STAMP) " 2>/dev/null)" ]; then \
106+ echo " $( CHANGE_MSG) $$ TARGET..." ; \
107+ go build -o $(BINARY ) ./cmd/scrapeycli; \
108+ touch " $( BUILD_STAMP) " ; \
109+ echo " Done with building." ; \
110+ else \
111+ echo " $( SKIP_MSG) $$ TARGET." ; \
112+ fi
113+
114+ # ------------------------------------------------------------------------------
115+ # run: execute the compiled binary.
116+ # Depends on build.
117+ # ------------------------------------------------------------------------------
118+ run : build
119+ @echo " Running application..."
120+ @$(BINARY )
121+
122+ # ------------------------------------------------------------------------------
123+ # tree: displays directory structure (installs tree if missing).
124+ # ------------------------------------------------------------------------------
125+ tree :
126+ @if ! command -v tree > /dev/null 2>&1 ; then \
127+ echo " tree command not found. Attempting to install..." ; \
128+ OS=$$(uname ) ; \
129+ if [ " $$ OS" = " Linux" ]; then \
130+ sudo apt-get update && sudo apt-get install -y tree; \
131+ elif [ " $$ OS" = " Darwin" ]; then \
132+ brew install tree; \
133+ else \
134+ echo " Automatic installation not supported on $$ OS. Please install manually." ; \
135+ exit 1; \
136+ fi ; \
137+ fi ; \
138138 tree -n -I " vendor|.git"
0 commit comments