1
1
GO ?= $(shell command -v go 2> /dev/null)
2
2
NPM ?= $(shell command -v npm 2> /dev/null)
3
3
CURL ?= $(shell command -v curl 2> /dev/null)
4
- MANIFEST_FILE ?= plugin.json
4
+ MM_DEBUG ?=
5
5
GOPATH ?= $(shell go env GOPATH)
6
6
GO_TEST_FLAGS ?= -race
7
7
GO_BUILD_FLAGS ?=
8
8
MM_UTILITIES_DIR ?= ../mattermost-utilities
9
+ DLV_DEBUG_PORT := 2346
10
+ DEFAULT_GOOS := $(shell go env GOOS)
11
+ DEFAULT_GOARCH := $(shell go env GOARCH)
9
12
10
13
export GO111MODULE =on
11
14
15
+ # We need to export GOBIN to allow it to be set
16
+ # for processes spawned from the Makefile
17
+ export GOBIN ?= $(PWD ) /bin
18
+
12
19
# You can include assets this directory into the bundle. This can be e.g. used to include profile pictures.
13
20
ASSETS_DIR ?= assets
14
21
22
+ # # Define the default target (make all)
23
+ .PHONY : default
24
+ default : all
25
+
15
26
# Verify environment, and define PLUGIN_ID, PLUGIN_VERSION, HAS_SERVER and HAS_WEBAPP as needed.
16
27
include build/setup.mk
17
28
@@ -22,122 +33,195 @@ ifneq ($(wildcard build/custom.mk),)
22
33
include build/custom.mk
23
34
endif
24
35
36
+ ifneq ($(MM_DEBUG ) ,)
37
+ GO_BUILD_GCFLAGS = -gcflags "all=-N -l"
38
+ else
39
+ GO_BUILD_GCFLAGS =
40
+ endif
41
+
25
42
# # Checks the code style, tests, builds and bundles the plugin.
43
+ .PHONY : all
26
44
all : check-style test dist
27
45
28
- # # Propagates plugin manifest information into the server/ and webapp/ folders as required .
46
+ # # Propagates plugin manifest information into the server/ and webapp/ folders.
29
47
.PHONY : apply
30
48
apply :
31
49
./build/bin/manifest apply
32
50
33
- # # Runs golangci-lint and eslint.
51
+ # # Install go tools
52
+ install-go-tools :
53
+ @echo Installing go tools
54
+ $(GO ) install github.com/golangci/golangci-lint/cmd/
[email protected]
55
+ $(GO ) install gotest.tools/
[email protected]
56
+
57
+ # # Runs eslint and golangci-lint
34
58
.PHONY : check-style
35
- check-style : webapp/.npminstall golangci-lint
59
+ check-style : apply webapp/node_modules install-go-tools
36
60
@echo Checking for style guide compliance
37
61
38
62
ifneq ($(HAS_WEBAPP ) ,)
39
63
cd webapp && npm run lint
64
+ cd webapp && npm run check-types
40
65
endif
41
66
42
- # # Run golangci-lint on codebase.
43
- .PHONY : golangci-lint
44
- golangci-lint :
45
- @if ! [ -x " $$ (command -v golangci-lint)" ]; then \
46
- echo " golangci-lint is not installed. Please see https://github.com/golangci/golangci-lint#install for installation instructions." ; \
47
- exit 1; \
48
- fi ; \
49
-
67
+ # It's highly recommended to run go-vet first
68
+ # to find potential compile errors that could introduce
69
+ # weird reports at golangci-lint step
70
+ ifneq ($(HAS_SERVER ) ,)
50
71
@echo Running golangci-lint
51
- golangci-lint run ./...
72
+ $(GO) vet ./...
73
+ $(GOBIN)/golangci-lint run ./...
74
+ endif
52
75
53
- # # Builds the server, if it exists, including support for multiple architectures.
76
+ # # Builds the server, if it exists, for all supported architectures, unless MM_SERVICESETTINGS_ENABLEDEVELOPER is set .
54
77
.PHONY : server
55
78
server :
56
79
ifneq ($(HAS_SERVER ) ,)
80
+ ifneq ($(MM_DEBUG ) ,)
81
+ $(info DEBUG mode is on; to disable, unset MM_DEBUG)
82
+ endif
57
83
mkdir -p server/dist;
58
- cd server && env GOOS=linux GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-linux-amd64;
59
- cd server && env GOOS=linux GOARCH=arm64 $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-linux-arm64;
60
- cd server && env GOOS=darwin GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-darwin-amd64;
61
- cd server && env GOOS=darwin GOARCH=arm64 $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-darwin-arm64;
62
- cd server && env GOOS=windows GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-windows-amd64.exe;
84
+ ifneq ($(MM_SERVICESETTINGS_ENABLEDEVELOPER ) ,)
85
+ @echo Building plugin only for $(DEFAULT_GOOS)-$(DEFAULT_GOARCH) because MM_SERVICESETTINGS_ENABLEDEVELOPER is enabled
86
+ cd server && env CGO_ENABLED=0 $(GO) build $(GO_BUILD_FLAGS) $(GO_BUILD_GCFLAGS) -trimpath -o dist/plugin-$(DEFAULT_GOOS)-$(DEFAULT_GOARCH);
87
+ else
88
+ cd server && env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) $(GO_BUILD_GCFLAGS) -trimpath -o dist/plugin-linux-amd64;
89
+ cd server && env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 $(GO) build $(GO_BUILD_FLAGS) $(GO_BUILD_GCFLAGS) -trimpath -o dist/plugin-linux-arm64;
90
+ cd server && env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) $(GO_BUILD_GCFLAGS) -trimpath -o dist/plugin-darwin-amd64;
91
+ cd server && env CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 $(GO) build $(GO_BUILD_FLAGS) $(GO_BUILD_GCFLAGS) -trimpath -o dist/plugin-darwin-arm64;
92
+ cd server && env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) $(GO_BUILD_GCFLAGS) -trimpath -o dist/plugin-windows-amd64.exe;
93
+ endif
63
94
endif
64
95
65
96
# # Ensures NPM dependencies are installed without having to run this all the time.
66
- webapp/.npminstall :
97
+ webapp/node_modules : $( wildcard webapp/package.json)
67
98
ifneq ($(HAS_WEBAPP ) ,)
68
99
cd webapp && $(NPM) install
69
100
touch $@
70
101
endif
71
102
72
103
# # Builds the webapp, if it exists.
73
104
.PHONY : webapp
74
- webapp : webapp/.npminstall
105
+ webapp : webapp/node_modules
75
106
ifneq ($(HAS_WEBAPP ) ,)
107
+ ifeq ($(MM_DEBUG ) ,)
76
108
cd webapp && $(NPM) run build;
109
+ else
110
+ cd webapp && $(NPM) run debug;
77
111
endif
78
-
79
- # # Builds the webapp in debug mode, if it exists.
80
- .PHONY : webapp-debug
81
- webapp-debug : webapp/.npminstall
82
- ifneq ($(HAS_WEBAPP ) ,)
83
- cd webapp && \
84
- $(NPM) run debug;
85
112
endif
86
113
87
114
# # Generates a tar bundle of the plugin for install.
88
115
.PHONY : bundle
89
116
bundle :
90
117
rm -rf dist/
91
118
mkdir -p dist/$(PLUGIN_ID )
92
- cp $( MANIFEST_FILE ) dist/ $( PLUGIN_ID ) /
119
+ ./build/bin/manifest dist
93
120
ifneq ($(wildcard $(ASSETS_DIR ) /.) ,)
94
121
cp -r $(ASSETS_DIR) dist/$(PLUGIN_ID)/
95
122
endif
96
123
ifneq ($(HAS_PUBLIC ) ,)
97
- cp -r public/ dist/$(PLUGIN_ID)/
124
+ cp -r public dist/$(PLUGIN_ID)/
98
125
endif
99
126
ifneq ($(HAS_SERVER ) ,)
100
- mkdir -p dist/$(PLUGIN_ID)/server/dist;
101
- cp -r server/dist/* dist/$(PLUGIN_ID)/server/dist/;
127
+ mkdir -p dist/$(PLUGIN_ID)/server
128
+ cp -r server/dist dist/$(PLUGIN_ID)/server/
102
129
endif
103
130
ifneq ($(HAS_WEBAPP ) ,)
104
- mkdir -p dist/$(PLUGIN_ID)/webapp/dist;
105
- cp -r webapp/dist/* dist/$(PLUGIN_ID)/webapp/dist/;
131
+ mkdir -p dist/$(PLUGIN_ID)/webapp
132
+ cp -r webapp/dist dist/$(PLUGIN_ID)/webapp/
106
133
endif
107
134
cd dist && tar -cvzf $(BUNDLE_NAME) $(PLUGIN_ID)
108
135
109
136
@echo plugin built at: dist/$(BUNDLE_NAME)
110
137
111
138
# # Builds and bundles the plugin.
112
139
.PHONY : dist
113
- dist : apply server webapp bundle
140
+ dist : apply server webapp bundle
114
141
115
- # # Installs the plugin to a (development) server.
116
- # # It uses the API if appropriate environment variables are defined,
117
- # # and otherwise falls back to trying to copy the plugin to a sibling mattermost-server directory.
142
+ # # Builds and installs the plugin to a server.
118
143
.PHONY : deploy
119
144
deploy : dist
120
- ./build/bin/deploy $(PLUGIN_ID ) dist/$(BUNDLE_NAME )
145
+ ./build/bin/pluginctl deploy $(PLUGIN_ID ) dist/$(BUNDLE_NAME )
146
+
147
+ # # Builds and installs the plugin to a server, updating the webapp automatically when changed.
148
+ .PHONY : watch
149
+ watch : apply server bundle
150
+ ifeq ($(MM_DEBUG ) ,)
151
+ cd webapp && $(NPM) run build:watch
152
+ else
153
+ cd webapp && $(NPM) run debug:watch
154
+ endif
121
155
122
- .PHONY : debug-deploy
123
- debug-deploy : debug-dist deploy
156
+ # # Installs a previous built plugin with updated webpack assets to a server.
157
+ .PHONY : deploy-from-watch
158
+ deploy-from-watch : bundle
159
+ ./build/bin/pluginctl deploy $(PLUGIN_ID ) dist/$(BUNDLE_NAME )
124
160
125
- .PHONY : debug-dist
126
- debug-dist : apply server webapp-debug bundle
161
+ # # Setup dlv for attaching, identifying the plugin PID for other targets.
162
+ .PHONY : setup-attach
163
+ setup-attach :
164
+ $(eval PLUGIN_PID := $(shell ps aux | grep "plugins/${PLUGIN_ID}" | grep -v "grep" | awk -F " " '{print $$2}') )
165
+ $(eval NUM_PID := $(shell echo -n ${PLUGIN_PID} | wc -w) )
166
+
167
+ @if [ ${NUM_PID} -gt 2 ]; then \
168
+ echo "** There is more than 1 plugin process running. Run 'make kill reset' to restart just one."; \
169
+ exit 1; \
170
+ fi
171
+
172
+ # # Check if setup-attach succeeded.
173
+ .PHONY : check-attach
174
+ check-attach :
175
+ @if [ -z ${PLUGIN_PID} ]; then \
176
+ echo " Could not find plugin PID; the plugin is not running. Exiting." ; \
177
+ exit 1; \
178
+ else \
179
+ echo " Located Plugin running with PID: ${PLUGIN_PID} " ; \
180
+ fi
181
+
182
+ # # Attach dlv to an existing plugin instance.
183
+ .PHONY : attach
184
+ attach : setup-attach check-attach
185
+ dlv attach ${PLUGIN_PID}
186
+
187
+ # # Attach dlv to an existing plugin instance, exposing a headless instance on $DLV_DEBUG_PORT.
188
+ .PHONY : attach-headless
189
+ attach-headless : setup-attach check-attach
190
+ dlv attach ${PLUGIN_PID} --listen :$(DLV_DEBUG_PORT ) --headless=true --api-version=2 --accept-multiclient
191
+
192
+ # # Detach dlv from an existing plugin instance, if previously attached.
193
+ .PHONY : detach
194
+ detach : setup-attach
195
+ @DELVE_PID=$(shell ps aux | grep "dlv attach ${PLUGIN_PID}" | grep -v "grep" | awk -F " " '{print $$2}') && \
196
+ if [ " $$ DELVE_PID" -gt 0 ] > /dev/null 2>&1 ; then \
197
+ echo " Located existing delve process running with PID: $$ DELVE_PID. Killing." ; \
198
+ kill -9 $$ DELVE_PID ; \
199
+ fi
127
200
128
201
# # Runs any lints and unit tests defined for the server and webapp, if they exist.
129
202
.PHONY : test
130
- test : webapp/.npminstall
203
+ test : apply webapp/node_modules install-go-tools
131
204
ifneq ($(HAS_SERVER ) ,)
132
- $(GO) test -v $(GO_TEST_FLAGS) ./server /...
205
+ $(GOBIN)/gotestsum -- -v . /...
133
206
endif
134
207
ifneq ($(HAS_WEBAPP ) ,)
135
- cd webapp && $(NPM) run fix && $(NPM) run test;
208
+ cd webapp && $(NPM) run test;
209
+ endif
210
+
211
+ # # Runs any lints and unit tests defined for the server and webapp, if they exist, optimized
212
+ # # for a CI environment.
213
+ .PHONY : test-ci
214
+ test-ci : apply webapp/node_modules install-go-tools
215
+ ifneq ($(HAS_SERVER ) ,)
216
+ $(GOBIN)/gotestsum --format standard-verbose --junitfile report.xml -- ./...
217
+ endif
218
+ ifneq ($(HAS_WEBAPP ) ,)
219
+ cd webapp && $(NPM) run test;
136
220
endif
137
221
138
222
# # Creates a coverage report for the server code.
139
223
.PHONY : coverage
140
- coverage : webapp/.npminstall
224
+ coverage : apply webapp/node_modules
141
225
ifneq ($(HAS_SERVER ) ,)
142
226
$(GO) test $(GO_TEST_FLAGS) -coverprofile=server/coverage.txt ./server/...
143
227
$(GO) tool cover -html=server/coverage.txt
@@ -154,6 +238,31 @@ else
154
238
endif
155
239
endif
156
240
241
+ # # Disable the plugin.
242
+ .PHONY : disable
243
+ disable : detach
244
+ ./build/bin/pluginctl disable $(PLUGIN_ID )
245
+
246
+ # # Enable the plugin.
247
+ .PHONY : enable
248
+ enable :
249
+ ./build/bin/pluginctl enable $(PLUGIN_ID )
250
+
251
+ # # Reset the plugin, effectively disabling and re-enabling it on the server.
252
+ .PHONY : reset
253
+ reset : detach
254
+ ./build/bin/pluginctl reset $(PLUGIN_ID )
255
+
256
+ # # Kill all instances of the plugin, detaching any existing dlv instance.
257
+ .PHONY : kill
258
+ kill : detach
259
+ $(eval PLUGIN_PID := $(shell ps aux | grep "plugins/${PLUGIN_ID}" | grep -v "grep" | awk -F " " '{print $$2}') )
260
+
261
+ @for PID in ${PLUGIN_PID}; do \
262
+ echo "Killing plugin pid $$PID"; \
263
+ kill -9 $$PID; \
264
+ done; \
265
+
157
266
# # Clean removes all build artifacts.
158
267
.PHONY : clean
159
268
clean :
@@ -163,13 +272,20 @@ ifneq ($(HAS_SERVER),)
163
272
rm -fr server/dist
164
273
endif
165
274
ifneq ($(HAS_WEBAPP ) ,)
166
- rm -fr webapp/.npminstall
167
275
rm -fr webapp/junit.xml
168
276
rm -fr webapp/dist
169
277
rm -fr webapp/node_modules
170
278
endif
171
279
rm -fr build/bin/
172
280
281
+ .PHONY : logs
282
+ logs :
283
+ ./build/bin/pluginctl logs $(PLUGIN_ID )
284
+
285
+ .PHONY : logs-watch
286
+ logs-watch :
287
+ ./build/bin/pluginctl logs-watch $(PLUGIN_ID )
288
+
173
289
# Help documentation à la https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
174
290
help :
175
- @cat Makefile | grep -v ' \.PHONY' | grep -v ' \help:' | grep -B1 -E ' ^[a-zA-Z0-9_.-]+:.*' | sed -e " s/:.*//" | sed -e " s/^## //" | grep -v ' \-\-' | sed ' 1!G;h;$$!d' | awk ' NR%2{printf "\033[36m%-30s\033[0m",$$0;next;}1' | sort
291
+ @cat Makefile build/ * .mk | grep -v ' \.PHONY' | grep -v ' \help:' | grep -B1 -E ' ^[a-zA-Z0-9_.-]+:.*' | sed -e " s/:.*//" | sed -e " s/^## //" | grep -v ' \-\-' | sed ' 1!G;h;$$!d' | awk ' NR%2{printf "\033[36m%-30s\033[0m",$$0;next;}1' | sort
0 commit comments