-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
156 lines (138 loc) · 5.92 KB
/
Makefile
File metadata and controls
156 lines (138 loc) · 5.92 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
.PHONY: help build backend-build frontend-build sync-static release-build release release-install release-start release-stop release-restart release-uninstall dev-backend dev-frontend clean docker-build
APP_NAME := mkBlog
ROOT_DIR := $(abspath .)
BUILD_DIR := $(ROOT_DIR)/build
BIN_DIR := $(BUILD_DIR)/bin
BIN_PATH := $(BIN_DIR)/$(APP_NAME)
FRONTEND_DIR := $(ROOT_DIR)/frontend
FRONTEND_DIST := $(FRONTEND_DIR)/dist
STATIC_DIR := $(ROOT_DIR)/static
STATIC_ASSETS_DIR := $(STATIC_DIR)/assets
DATA_DIR := $(ROOT_DIR)/data
OS := $(shell uname -s)
SERVICE_NAME ?= mkblog
SYSTEMD_SERVICE_PATH ?= /etc/systemd/system/$(SERVICE_NAME).service
LAUNCHD_LABEL ?= com.mkblog.app
LAUNCHD_PLIST_PATH ?= $(HOME)/Library/LaunchAgents/$(LAUNCHD_LABEL).plist
help:
@echo "Available targets:"
@echo " build Build backend, frontend, and sync static assets"
@echo " backend-build Build Go binary to build/bin/$(APP_NAME)"
@echo " frontend-build Install frontend deps and build Vite assets"
@echo " sync-static Copy frontend build output into static/"
@echo " release One-command release build + service install + start"
@echo " release-install Install service definition for Linux(systemd) or macOS(launchd)"
@echo " release-start Start registered service"
@echo " release-stop Stop registered service"
@echo " release-restart Restart registered service"
@echo " release-uninstall Remove registered service definition"
@echo " dev-backend Run backend with go run"
@echo " dev-frontend Run frontend with Vite"
@echo " clean Remove local build artifacts"
@echo " docker-build Build Docker image mkblog:latest"
build: release-build
backend-build:
@echo "Building Go binary..."
@mkdir -p "$(BIN_DIR)"
go build -o "$(BIN_PATH)" .
frontend-build:
@echo "Installing frontend dependencies and building..."
cd "$(FRONTEND_DIR)" && npm install && npm run build
sync-static:
@echo "Syncing frontend assets to static/..."
@test -d "$(FRONTEND_DIST)" || (echo "Missing frontend build output at $(FRONTEND_DIST). Run 'make frontend-build' first." && exit 1)
@mkdir -p "$(STATIC_DIR)"
@find "$(STATIC_DIR)" -mindepth 1 -maxdepth 1 ! -name 'icon.svg' -exec rm -rf {} +
cp -R "$(FRONTEND_DIST)/." "$(STATIC_DIR)/"
release-build: backend-build frontend-build sync-static
@echo "Release build completed."
release: release-build release-install release-start
@echo "Release workflow completed."
release-install: release-build
ifeq ($(OS),Linux)
@echo "Installing systemd service to $(SYSTEMD_SERVICE_PATH)..."
@mkdir -p "$(BUILD_DIR)"
@sed \
-e 's|__APP_NAME__|$(APP_NAME)|g' \
-e 's|__ROOT_DIR__|$(ROOT_DIR)|g' \
-e 's|__BIN_PATH__|$(BIN_PATH)|g' \
-e 's|__USER__|$(shell id -un)|g' \
-e 's|__GROUP__|$(shell id -gn)|g' \
"$(ROOT_DIR)/deploy/systemd/mkblog.service.tpl" > "$(BUILD_DIR)/$(SERVICE_NAME).service"
sudo install -m 0644 "$(BUILD_DIR)/$(SERVICE_NAME).service" "$(SYSTEMD_SERVICE_PATH)"
sudo systemctl daemon-reload
sudo systemctl enable "$(SERVICE_NAME)"
else ifeq ($(OS),Darwin)
@echo "Installing launchd plist to $(LAUNCHD_PLIST_PATH)..."
@mkdir -p "$(BUILD_DIR)" "$(HOME)/Library/LaunchAgents" "$(DATA_DIR)"
@sed \
-e 's|__LABEL__|$(LAUNCHD_LABEL)|g' \
-e 's|__ROOT_DIR__|$(ROOT_DIR)|g' \
-e 's|__BIN_PATH__|$(BIN_PATH)|g' \
-e 's|__STDOUT_PATH__|$(DATA_DIR)/launchd.stdout.log|g' \
-e 's|__STDERR_PATH__|$(DATA_DIR)/launchd.stderr.log|g' \
"$(ROOT_DIR)/deploy/launchd/com.mkblog.app.plist.tpl" > "$(BUILD_DIR)/$(LAUNCHD_LABEL).plist"
cp "$(BUILD_DIR)/$(LAUNCHD_LABEL).plist" "$(LAUNCHD_PLIST_PATH)"
launchctl enable "gui/$$(id -u)/$(LAUNCHD_LABEL)" >/dev/null 2>&1 || true
launchctl bootout "gui/$$(id -u)" "$(LAUNCHD_PLIST_PATH)" >/dev/null 2>&1 || true
launchctl bootstrap "gui/$$(id -u)" "$(LAUNCHD_PLIST_PATH)"
else
@echo "Unsupported OS: $(OS)" && exit 1
endif
release-start:
ifeq ($(OS),Linux)
sudo systemctl start "$(SERVICE_NAME)"
sudo systemctl status "$(SERVICE_NAME)" --no-pager || true
else ifeq ($(OS),Darwin)
launchctl enable "gui/$$(id -u)/$(LAUNCHD_LABEL)" >/dev/null 2>&1 || true
launchctl bootstrap "gui/$$(id -u)" "$(LAUNCHD_PLIST_PATH)" >/dev/null 2>&1 || true
launchctl kickstart -k "gui/$$(id -u)/$(LAUNCHD_LABEL)"
launchctl print "gui/$$(id -u)/$(LAUNCHD_LABEL)" || true
else
@echo "Unsupported OS: $(OS)" && exit 1
endif
release-stop:
ifeq ($(OS),Linux)
sudo systemctl stop "$(SERVICE_NAME)"
else ifeq ($(OS),Darwin)
launchctl disable "gui/$$(id -u)/$(LAUNCHD_LABEL)" >/dev/null 2>&1 || true
launchctl bootout "gui/$$(id -u)" "$(LAUNCHD_PLIST_PATH)" >/dev/null 2>&1 || true
else
@echo "Unsupported OS: $(OS)" && exit 1
endif
release-restart:
ifeq ($(OS),Linux)
sudo systemctl restart "$(SERVICE_NAME)"
sudo systemctl status "$(SERVICE_NAME)" --no-pager || true
else ifeq ($(OS),Darwin)
launchctl enable "gui/$$(id -u)/$(LAUNCHD_LABEL)" >/dev/null 2>&1 || true
launchctl bootout "gui/$$(id -u)" "$(LAUNCHD_PLIST_PATH)" >/dev/null 2>&1 || true
launchctl bootstrap "gui/$$(id -u)" "$(LAUNCHD_PLIST_PATH)"
launchctl kickstart -k "gui/$$(id -u)/$(LAUNCHD_LABEL)"
launchctl print "gui/$$(id -u)/$(LAUNCHD_LABEL)" || true
else
@echo "Unsupported OS: $(OS)" && exit 1
endif
release-uninstall:
ifeq ($(OS),Linux)
-sudo systemctl stop "$(SERVICE_NAME)"
-sudo systemctl disable "$(SERVICE_NAME)"
-sudo rm -f "$(SYSTEMD_SERVICE_PATH)"
sudo systemctl daemon-reload
else ifeq ($(OS),Darwin)
-launchctl disable "gui/$$(id -u)/$(LAUNCHD_LABEL)" >/dev/null 2>&1 || true
-launchctl bootout "gui/$$(id -u)" "$(LAUNCHD_PLIST_PATH)" >/dev/null 2>&1 || launchctl unload "$(LAUNCHD_PLIST_PATH)" >/dev/null 2>&1 || true
-rm -f "$(LAUNCHD_PLIST_PATH)"
else
@echo "Unsupported OS: $(OS)" && exit 1
endif
dev-backend:
go run . -debug
dev-frontend:
cd "$(FRONTEND_DIR)" && npm run dev
clean:
@echo "Removing build artifacts..."
@rm -rf "$(BUILD_DIR)" "$(FRONTEND_DIST)" "$(FRONTEND_DIR)/build"
@rm -f "$(ROOT_DIR)/$(APP_NAME)"
docker-build:
docker build -f docker/Dockerfile -t mkblog:latest .