-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMakefile
More file actions
254 lines (206 loc) · 7.76 KB
/
Makefile
File metadata and controls
254 lines (206 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# Makefile for audiobookshelf-hardcover-sync
# Project information
PROJECT_NAME := audiobookshelf-hardcover-sync
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
GIT_COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo "unknown")
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
# Directories
BIN_DIR := bin
DIST_DIR := dist
COVERAGE_DIR := coverage
# Output binary names
BINARY := $(BIN_DIR)/$(PROJECT_NAME)
TOOLS := edition image-tool hardcover-lookup
TOOL_BINARIES := $(addprefix $(BIN_DIR)/,$(TOOLS))
# Go parameters
GO_FILES := $(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./test/*")
GO_PKGS := $(shell go list ./... | grep -v /vendor/)
# Build flags
LDFLAGS := -ldflags="\
-X 'main.version=$(VERSION)' \
-X 'main.commit=$(GIT_COMMIT)' \
-X 'main.date=$(BUILD_DATE)' \
-X 'main.branch=$(GIT_BRANCH)'"
# Tools
GOLANGCI_LINT = $(shell go env GOPATH)/bin/golangci-lint
GOVERALLS = $(shell go env GOPATH)/bin/goveralls
GORELEASER = $(shell go env GOPATH)/bin/goreleaser
# Docker parameters
DOCKER_IMAGE := $(PROJECT_NAME)
DOCKER_TAG := $(VERSION)
ifneq ($(findstring dirty,$(VERSION)),)
DOCKER_TAG := latest
endif
# Default target
.PHONY: all
all: test lint build build-tools
# Help target
.PHONY: help
help:
@echo "Available targets:"
@echo " all - Run tests, linters and build (default)"
@echo " build - Build the main binary and all tools"
@echo " build-all - Build main binary for all platforms"
@echo " install - Install the binary"
@echo " test - Run tests with race detection and coverage"
@echo " test-verbose - Run tests with verbose output"
@echo " coverage - Generate and display test coverage"
@echo " coverage-html - Generate HTML coverage report"
@echo " coverage-ci - Generate coverage report for CI"
@echo " lint - Run linters"
@echo " lint-fix - Run linters and fix issues"
@echo " clean - Remove build artifacts"
@echo " docker-build - Build Docker image"
@echo " docker-push - Push Docker image to registry"
@echo " release - Create a new release"
# Build targets
.PHONY: build
default: build-tools
build: $(BINARY) build-tools
.PHONY: build-tools
build-tools: $(TOOL_BINARIES)
# Build individual tools
$(BIN_DIR)/%: cmd/%/main.go $(GO_FILES)
@echo "Building $@"
@mkdir -p $(@D)
go build -v -o $@ $(LDFLAGS) ./$<
$(BINARY): $(GO_FILES)
@echo "Building $(BINARY) $(VERSION)"
@mkdir -p $(BIN_DIR)
go build -v -o $@ $(LDFLAGS) ./cmd/$(PROJECT_NAME)
.PHONY: build-all
build-all: build-linux build-darwin build-windows
.PHONY: build-linux
build-linux:
@echo "Building Linux amd64 binary"
@mkdir -p $(DIST_DIR)
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -v -o $(DIST_DIR)/$(PROJECT_NAME)-linux-amd64 $(LDFLAGS) ./cmd/$(PROJECT_NAME)
.PHONY: build-darwin
build-darwin:
@echo "Building Darwin amd64 binary"
@mkdir -p $(DIST_DIR)
GOOS=darwin GOARCH=amd64 go build -v -o $(DIST_DIR)/$(PROJECT_NAME)-darwin-amd64 $(LDFLAGS) ./cmd/$(PROJECT_NAME)
.PHONY: build-windows
build-windows:
@echo "Building Windows amd64 binary"
@mkdir -p $(DIST_DIR)
GOOS=windows GOARCH=amd64 go build -v -o $(DIST_DIR)/$(PROJECT_NAME)-windows-amd64.exe $(LDFLAGS) ./cmd/$(PROJECT_NAME)
# Install target
.PHONY: install
install:
@echo "Installing to $(shell go env GOPATH)/bin/$(PROJECT_NAME)"
go install $(LDFLAGS) ./cmd/$(PROJECT_NAME)
# Filter out test packages with build failures
TEST_PKGS := $(shell go list ./... | grep -v /vendor/ | grep -v /archive/ | grep -v /internal/testutils)
# Test targets
.PHONY: test test-race test-core test-all test-verbose
# Default test target runs core tests with race detector
test: test-race
# Run core tests with race detector
test-race: test-core
# Run core tests only
test-core:
@echo "Running core tests with race detector"
@mkdir -p $(COVERAGE_DIR)
go test -race -coverprofile=$(COVERAGE_DIR)/coverage.out -covermode=atomic $(TEST_PKGS)
# Run all tests including legacy and testutils
test-all:
@echo "Running all tests with race detector (including legacy and testutils)"
@mkdir -p $(COVERAGE_DIR)
go test -race -coverprofile=$(COVERAGE_DIR)/coverage.out -covermode=atomic $(GO_PKGS)
# Run tests with verbose output
test-verbose:
@echo "Running tests with verbose output"
@mkdir -p $(COVERAGE_DIR)
go test -v -race -coverprofile=$(COVERAGE_DIR)/coverage.out -covermode=atomic $(GO_PKGS)
# Coverage file generation rule - must be defined before any targets that use it
$(COVERAGE_DIR)/coverage.out:
@mkdir -p $(COVERAGE_DIR)
@$(MAKE) test-core
# Coverage related targets
.PHONY: coverage coverage-html coverage-ci
# Show coverage report in console
coverage: $(COVERAGE_DIR)/coverage.out
@go tool cover -func=$<
# Generate HTML coverage report
coverage-html: $(COVERAGE_DIR)/coverage.out
@go tool cover -html=$< -o $(COVERAGE_DIR)/coverage.html
@echo "Coverage report generated at $(COVERAGE_DIR)/coverage.html"
# Generate coverage report for CI
coverage-ci: $(GOVERALLS) $(COVERAGE_DIR)/coverage.out
@$(GOVERALLS) -coverprofile=$(COVERAGE_DIR)/coverage.out -service=github
# Linting
.PHONY: lint
lint: $(GOLANGCI_LINT)
@echo "Running linters..."
@$(GOLANGCI_LINT) run --timeout 5m
.PHONY: lint-fix
lint-fix: $(GOLANGCI_LINT)
@echo "Running linters and fixing issues..."
@$(GOLANGCI_LINT) run --fix --timeout 5m
# Docker targets
.PHONY: docker-build
docker-build:
@echo "Building Docker image $(DOCKER_IMAGE):$(DOCKER_TAG)"
docker build -t $(DOCKER_IMAGE):$(DOCKER_TAG) .
.PHONY: docker-push
docker-push: docker-build
@echo "Pushing Docker image $(DOCKER_IMAGE):$(DOCKER_TAG)"
docker push $(DOCKER_IMAGE):$(DOCKER_TAG)
# Release targets
.PHONY: prepare-release release
# Prepare a release by updating the changelog from "Unreleased" to the version number
prepare-release:
@echo "Preparing release $(VERSION)..."
@if [ "$(VERSION)" = "dev" ]; then \
echo "Error: Cannot prepare release with VERSION=dev. Use VERSION=vX.Y.Z"; \
exit 1; \
fi
@echo "Updating CHANGELOG.md for $(VERSION)..."
@sed -i.bak -e "s/^## \[Unreleased\]$$/## [$(VERSION)] - $(shell date +%Y-%m-%d)/" CHANGELOG.md
@rm -f CHANGELOG.md.bak
@echo "Changelog updated. Review changes, commit, and create a tag:"
@echo " git add CHANGELOG.md"
@echo " git commit -m 'Release $(VERSION)'"
@echo " git tag -a $(VERSION) -m 'Release $(VERSION)'"
@echo " git push origin main $(VERSION)"
# Create a release using GoReleaser (not typically used - GitHub Actions handles this)
# See RELEASE.md for the recommended release process
release: $(GORELEASER)
@echo "Creating release $(VERSION) with GoReleaser (only for local testing)"
@echo "NOTE: GitHub Actions should handle the actual release process"
$(GORELEASER) release --rm-dist
# Clean target
.PHONY: clean
clean:
@echo "Cleaning..."
@rm -rf $(BIN_DIR) $(DIST_DIR) $(COVERAGE_DIR) $(TOOL_BINARIES)
@find . -name "coverage.out" -delete
@find . -name "coverage.html" -delete
# Tool installation
$(GOLANGCI_LINT):
@echo "Installing golangci-lint..."
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
$(GOVERALLS):
@echo "Installing goveralls..."
go install github.com/mattn/goveralls@latest
$(GORELEASER):
@echo "Installing goreleaser..."
go install github.com/goreleaser/goreleaser@latest
# Ensure dependencies are installed
.PHONY: deps
deps: $(GOLANGCI_LINT) $(GOVERALLS) $(GORELEASER)
@echo "All dependencies are installed"
# Ensure the dist directory exists
$(DIST_DIR):
@mkdir -p $(DIST_DIR)
# Ensure the bin directory exists
$(BIN_DIR):
@mkdir -p $(BIN_DIR)
# Ensure the coverage directory exists
ensure-coverage-dir:
@mkdir -p $(COVERAGE_DIR)
# Update coverage target to depend on directory creation
coverage: ensure-coverage-dir $(COVERAGE_DIR)/coverage.out
# End of Makefile