Skip to content

Commit

Permalink
feat(makefile): add versioning targets
Browse files Browse the repository at this point in the history
  • Loading branch information
gi8lino committed Feb 1, 2025
1 parent e65bc76 commit 0ead6b9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
39 changes: 36 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# Makefile
# Detect platform for sed compatibility
SED := $(shell if [ "$(shell uname)" = "Darwin" ]; then echo gsed; else echo sed; fi)

.PHONY: test cover clean
# VERSION defines the project version, extracted from cmd/portpatrol/main.go without leading 'v'.
VERSION := $(shell awk -F'"' '/const version/{gsub(/^v/, "", $$2); print $$2}' cmd/portpatrol/main.go)

.PHONY: test cover clean update patch minor major tag

# Run all unit tests
test:
go test ./... -v -count=1

# Generate and display test coverage
cover:
sudo go test ./cmd/... ./internal/... -count=1 -coverprofile=coverage.out
go test ./cmd/... ./internal/... -count=1 -coverprofile=coverage.out
go tool cover -html=coverage.out

# Clean up generated files
Expand All @@ -18,3 +22,32 @@ clean:
# Update dependencies
update:
go get -u ./...
go mod tidy

##@ Versioning

patch: ## Increment the patch version (x.y.Z -> x.y.(Z+1)).
@NEW_VERSION=$$(echo "$(VERSION)" | awk -F. '{print $$1"."$$2"."$$3+1}') && \
$(SED) -i -E "s/(const version string = \"v)[^\"]+/\1$${NEW_VERSION}/" cmd/portpatrol/main.go

minor: ## Increment the minor version (x.Y.z -> x.(Y+1).0).
@NEW_VERSION=$$(echo "$(VERSION)" | awk -F. '{print $$1"."$$2+1".0"}') && \
$(SED) -i -E "s/(const version string = \"v)[^\"]+/\1$${NEW_VERSION}/" cmd/portpatrol/main.go

major: ## Increment the major version (X.y.z -> (X+1).0.0).
@NEW_VERSION=$$(echo "$(VERSION)" | awk -F. '{print $$1+1".0.0"}') && \
$(SED) -i -E "s/(const version string = \"v)[^\"]+/\1$${NEW_VERSION}/" cmd/portpatrol/main.go

tag: ## Tag the current commit with the current version if no tag exists and the repository is clean.
@if [ -n "$$(git status --porcelain)" ]; then \
echo "Repository has uncommitted changes. Please commit or stash them before tagging."; \
exit 1; \
fi
@if [ -z "$$(git tag --list v$(VERSION))" ]; then \
echo "Tagging version v$(VERSION)"; \
git tag "v$(VERSION)"; \
git push origin "v$(VERSION)"; \
else \
echo "Tag v$(VERSION) already exists."; \
fi

2 changes: 1 addition & 1 deletion cmd/portpatrol/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"golang.org/x/sync/errgroup"
)

const version = "0.5.0"
const version string = "0.5.0"

// run is the main function of the application.
func run(ctx context.Context, args []string, output io.Writer) error {
Expand Down

0 comments on commit 0ead6b9

Please sign in to comment.