This repository was archived by the owner on Aug 8, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
151 lines (132 loc) · 5.22 KB
/
Copy pathMakefile
File metadata and controls
151 lines (132 loc) · 5.22 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
BINARY := nodeshare
MODULE := nodeshare
GO := go
export PATH := $(HOME)/go/bin:$(HOME)/.local/bin:$(PATH)
ifeq ($(OS),Windows_NT)
GOFLAGS :=
TEST_FLAGS := -count=1
BINARY_EXT := .exe
else
GOFLAGS := CGO_ENABLED=0
TEST_FLAGS := -race -count=1
BINARY_EXT :=
endif
PROTO_DIR := proto
GEN_DIR := gen
PROTOC := protoc
PROTOC_GEN := protoc-gen-go
BUILD_DIR := dist
VERSION := 0.1.0
LDFLAGS := -ldflags "-s -w -X main.version=$(VERSION)"
PLATFORMS := linux/amd64 linux/arm64 windows/amd64 darwin/amd64 darwin/arm64
ifeq ($(OS),Windows_NT)
CHECK_PROTOC := where $(PROTOC) >nul 2>nul || (echo ERROR: protoc not found. Install protoc.&& exit 1)
CHECK_PROTOC_GEN := where $(PROTOC_GEN) >nul 2>nul || (echo ERROR: protoc-gen-go not found. Run: go install google.golang.org/protobuf/cmd/protoc-gen-go@latest&& exit 1)
MKDIR_P := if not exist $(GEN_DIR) mkdir $(GEN_DIR)
RM_RF := rmdir /s /q
RM_F := del /f /q
else
CHECK_PROTOC := which $(PROTOC) >/dev/null 2>&1 || (echo "ERROR: protoc not found. Install protoc."; exit 1)
CHECK_PROTOC_GEN := which $(PROTOC_GEN) >/dev/null 2>&1 || (echo "ERROR: protoc-gen-go not found. Run: go install google.golang.org/protobuf/cmd/protoc-gen-go@latest"; exit 1)
MKDIR_P := mkdir -p $(GEN_DIR)
RM_RF := rm -rf
RM_F := rm -f
endif
.PHONY: all build proto gen test lint clean fmt vet tidy run help install
## all: generate proto, build, and test
all: proto build test
## build: compile the nodeshare binary for the current platform
build: proto
$(GOFLAGS) $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY)$(BINARY_EXT) ./cmd/nodeshare
## proto: generate Go code from .proto definitions
proto:
@$(CHECK_PROTOC)
@$(CHECK_PROTOC_GEN)
@$(MKDIR_P)
$(PROTOC) \
--go_out=$(GEN_DIR) \
--go_opt=paths=source_relative \
-I$(PROTO_DIR) \
$(PROTO_DIR)/nodeshare.proto
## test: run all unit tests
test:
$(GOFLAGS) $(GO) test $(TEST_FLAGS) ./...
## test-v: run tests in verbose mode
test-v:
$(GOFLAGS) $(GO) test $(TEST_FLAGS) -v ./...
## lint: run golangci-lint (must be installed separately)
lint:
@which golangci-lint || (echo "ERROR: golangci-lint not found. See https://golangci-lint.run/usage/install/"; exit 1)
golangci-lint run ./...
## fmt: format all Go source files
fmt:
$(GO) fmt ./...
## vet: run go vet
vet:
$(GOFLAGS) $(GO) vet ./...
## tidy: tidy and verify the module graph
tidy:
$(GO) mod tidy
$(GO) mod verify
## run: start the daemon on the default port
run: build
./$(BUILD_DIR)/$(BINARY)$(BINARY_EXT) start
## cross: build release binaries for all target platforms
cross:
@mkdir -p $(BUILD_DIR)
@for platform in $(PLATFORMS); do \
os=$$(echo $$platform | cut -d/ -f1); \
arch=$$(echo $$platform | cut -d/ -f2); \
output=$(BUILD_DIR)/$(BINARY)-$$os-$$arch; \
[ "$$os" = "windows" ] && output=$$output.exe; \
echo "Building $$output …"; \
GOOS=$$os GOARCH=$$arch CGO_ENABLED=0 $(GO) build $(LDFLAGS) -o $$output ./cmd/nodeshare || exit 1; \
done
## clean: remove build artefacts
clean:
ifeq ($(OS),Windows_NT)
-@$(RM_RF) $(BUILD_DIR) 2>nul || exit 0
-@$(RM_F) $(subst /,\,$(GEN_DIR))\*.pb.go 2>nul || exit 0
else
@$(RM_RF) $(BUILD_DIR)
@$(RM_F) $(GEN_DIR)/*.pb.go
endif
## help: print this help
help:
@grep -E '^##' $(MAKEFILE_LIST) | sed 's/## //' | column -t -s ':'
## install: install NodeShare to the system path
install:
@echo "\033[1;36m========================================================\033[0m"
@echo "\033[1;36m NodeShare Installation Step \033[0m"
@echo "\033[1;36m========================================================\033[0m"
@if [ ! -f .env ]; then \
echo "\033[1;33mCreating .env from .env.example...\033[0m"; \
cp .env.example .env; \
echo "Please review the newly created \033[1;32m.env\033[0m file to customize configurations."; \
else \
echo "\033[1;32mFound existing .env file.\033[0m"; \
fi
@echo ""
@echo "Current configurations in your .env:"
@echo "\033[0;32m"
@cat .env | grep -v '^#' | grep -v '^$$' || true
@echo "\033[0m"
@echo "\033[1;34mBuilding the latest NodeShare binary...\033[0m"
@$(MAKE) build
@echo ""
@echo "\033[1;34mInstalling binary to system path...\033[0m"
@if [ -w /usr/local/bin ]; then \
cp $(BUILD_DIR)/$(BINARY)$(BINARY_EXT) /usr/local/bin/$(BINARY)$(BINARY_EXT); \
echo "\033[1;32mSuccessfully installed to /usr/local/bin/$(BINARY)$(BINARY_EXT)\033[0m"; \
elif [ -d $(HOME)/.local/bin ] && [ -w $(HOME)/.local/bin ]; then \
cp $(BUILD_DIR)/$(BINARY)$(BINARY_EXT) $(HOME)/.local/bin/$(BINARY)$(BINARY_EXT); \
echo "\033[1;32mSuccessfully installed to $(HOME)/.local/bin/$(BINARY)$(BINARY_EXT)\033[0m"; \
else \
echo "\033[1;33mNo write permission to /usr/local/bin. Retrying installation with sudo...\033[0m"; \
sudo cp $(BUILD_DIR)/$(BINARY)$(BINARY_EXT) /usr/local/bin/$(BINARY)$(BINARY_EXT); \
echo "\033[1;32mSuccessfully installed to /usr/local/bin/$(BINARY)$(BINARY_EXT)\033[0m"; \
fi
@echo ""
@echo "\033[1;36m========================================================\033[0m"
@echo "\033[1;32mInstallation complete! Run 'nodeshare --help' to start.\033[0m"
@echo "\033[1;36m========================================================\033[0m"