Makefile 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. GO ?= $(shell command -v go 2> /dev/null)
  2. NPM ?= $(shell command -v npm 2> /dev/null)
  3. CURL ?= $(shell command -v curl 2> /dev/null)
  4. MM_DEBUG ?=
  5. MANIFEST_FILE ?= plugin.json
  6. GOPATH ?= $(shell go env GOPATH)
  7. GO_TEST_FLAGS ?= -race
  8. GO_BUILD_FLAGS ?=
  9. MM_UTILITIES_DIR ?= ../mattermost-utilities
  10. DLV_DEBUG_PORT := 2346
  11. export GO111MODULE=on
  12. # You can include assets this directory into the bundle. This can be e.g. used to include profile pictures.
  13. ASSETS_DIR ?= assets
  14. ## Define the default target (make all)
  15. .PHONY: default
  16. default: all
  17. # Verify environment, and define PLUGIN_ID, PLUGIN_VERSION, HAS_SERVER and HAS_WEBAPP as needed.
  18. include build/setup.mk
  19. include build/legacy.mk
  20. BUNDLE_NAME ?= $(PLUGIN_ID)-$(PLUGIN_VERSION).tar.gz
  21. # Include custom makefile, if present
  22. ifneq ($(wildcard build/custom.mk),)
  23. include build/custom.mk
  24. endif
  25. ## Checks the code style, tests, builds and bundles the plugin.
  26. .PHONY: all
  27. all: check-style test dist
  28. ## Runs eslint and golangci-lint
  29. .PHONY: check-style
  30. check-style: webapp/node_modules
  31. @echo Checking for style guide compliance
  32. ifneq ($(HAS_WEBAPP),)
  33. cd webapp && npm run lint
  34. cd webapp && npm run check-types
  35. endif
  36. ifneq ($(HAS_SERVER),)
  37. @if ! [ -x "$$(command -v golangci-lint)" ]; then \
  38. echo "golangci-lint is not installed. Please see https://github.com/golangci/golangci-lint#install for installation instructions."; \
  39. exit 1; \
  40. fi; \
  41. @echo Running golangci-lint
  42. golangci-lint run ./...
  43. endif
  44. ## Builds the server, if it exists, for all supported architectures.
  45. .PHONY: server
  46. server:
  47. ifneq ($(HAS_SERVER),)
  48. mkdir -p server/dist;
  49. ifeq ($(MM_DEBUG),)
  50. cd server && env GOOS=linux GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -o dist/plugin-linux-amd64;
  51. cd server && env GOOS=darwin GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -o dist/plugin-darwin-amd64;
  52. cd server && env GOOS=darwin GOARCH=arm64 $(GO) build $(GO_BUILD_FLAGS) -o dist/plugin-darwin-arm64;
  53. cd server && env GOOS=windows GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -o dist/plugin-windows-amd64.exe;
  54. else
  55. $(info DEBUG mode is on; to disable, unset MM_DEBUG)
  56. cd server && env GOOS=darwin GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -gcflags "all=-N -l" -o dist/plugin-darwin-amd64;
  57. cd server && env GOOS=darwin GOARCH=arm64 $(GO) build $(GO_BUILD_FLAGS) -gcflags "all=-N -l" -o dist/plugin-darwin-arm64;
  58. cd server && env GOOS=linux GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -gcflags "all=-N -l" -o dist/plugin-linux-amd64;
  59. cd server && env GOOS=windows GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -gcflags "all=-N -l" -o dist/plugin-windows-amd64.exe;
  60. endif
  61. endif
  62. ## Ensures NPM dependencies are installed without having to run this all the time.
  63. webapp/node_modules: $(wildcard webapp/package.json)
  64. ifneq ($(HAS_WEBAPP),)
  65. cd webapp && $(NPM) install
  66. touch $@
  67. endif
  68. ## Builds the webapp, if it exists.
  69. .PHONY: webapp
  70. webapp: webapp/node_modules
  71. ifneq ($(HAS_WEBAPP),)
  72. ifeq ($(MM_DEBUG),)
  73. cd webapp && $(NPM) run build;
  74. else
  75. cd webapp && $(NPM) run debug;
  76. endif
  77. endif
  78. ## Generates a tar bundle of the plugin for install.
  79. .PHONY: bundle
  80. bundle:
  81. rm -rf dist/
  82. mkdir -p dist/$(PLUGIN_ID)
  83. cp $(MANIFEST_FILE) dist/$(PLUGIN_ID)/
  84. ifneq ($(wildcard $(ASSETS_DIR)/.),)
  85. cp -r $(ASSETS_DIR) dist/$(PLUGIN_ID)/
  86. endif
  87. ifneq ($(HAS_PUBLIC),)
  88. cp -r public dist/$(PLUGIN_ID)/
  89. endif
  90. ifneq ($(HAS_SERVER),)
  91. mkdir -p dist/$(PLUGIN_ID)/server
  92. cp -r server/dist dist/$(PLUGIN_ID)/server/
  93. endif
  94. ifneq ($(HAS_WEBAPP),)
  95. mkdir -p dist/$(PLUGIN_ID)/webapp
  96. cp -r webapp/dist dist/$(PLUGIN_ID)/webapp/
  97. endif
  98. cd dist && tar -cvzf $(BUNDLE_NAME) $(PLUGIN_ID)
  99. @echo plugin built at: dist/$(BUNDLE_NAME)
  100. ## Builds and bundles the plugin.
  101. .PHONY: dist
  102. dist: server webapp bundle
  103. ## Builds and installs the plugin to a server.
  104. .PHONY: deploy
  105. deploy: dist
  106. ./build/bin/pluginctl deploy $(PLUGIN_ID) dist/$(BUNDLE_NAME)
  107. ## Builds and installs the plugin to a server, updating the webapp automatically when changed.
  108. .PHONY: watch
  109. watch: server bundle
  110. ifeq ($(MM_DEBUG),)
  111. cd webapp && $(NPM) run build:watch
  112. else
  113. cd webapp && $(NPM) run debug:watch
  114. endif
  115. ## Installs a previous built plugin with updated webpack assets to a server.
  116. .PHONY: deploy-from-watch
  117. deploy-from-watch: bundle
  118. ./build/bin/pluginctl deploy $(PLUGIN_ID) dist/$(BUNDLE_NAME)
  119. ## Setup dlv for attaching, identifying the plugin PID for other targets.
  120. .PHONY: setup-attach
  121. setup-attach:
  122. $(eval PLUGIN_PID := $(shell ps aux | grep "plugins/${PLUGIN_ID}" | grep -v "grep" | awk -F " " '{print $$2}'))
  123. $(eval NUM_PID := $(shell echo -n ${PLUGIN_PID} | wc -w))
  124. @if [ ${NUM_PID} -gt 2 ]; then \
  125. echo "** There is more than 1 plugin process running. Run 'make kill reset' to restart just one."; \
  126. exit 1; \
  127. fi
  128. ## Check if setup-attach succeeded.
  129. .PHONY: check-attach
  130. check-attach:
  131. @if [ -z ${PLUGIN_PID} ]; then \
  132. echo "Could not find plugin PID; the plugin is not running. Exiting."; \
  133. exit 1; \
  134. else \
  135. echo "Located Plugin running with PID: ${PLUGIN_PID}"; \
  136. fi
  137. ## Attach dlv to an existing plugin instance.
  138. .PHONY: attach
  139. attach: setup-attach check-attach
  140. dlv attach ${PLUGIN_PID}
  141. ## Attach dlv to an existing plugin instance, exposing a headless instance on $DLV_DEBUG_PORT.
  142. .PHONY: attach-headless
  143. attach-headless: setup-attach check-attach
  144. dlv attach ${PLUGIN_PID} --listen :$(DLV_DEBUG_PORT) --headless=true --api-version=2 --accept-multiclient
  145. ## Detach dlv from an existing plugin instance, if previously attached.
  146. .PHONY: detach
  147. detach: setup-attach
  148. @DELVE_PID=$(shell ps aux | grep "dlv attach ${PLUGIN_PID}" | grep -v "grep" | awk -F " " '{print $$2}') && \
  149. if [ "$$DELVE_PID" -gt 0 ] > /dev/null 2>&1 ; then \
  150. echo "Located existing delve process running with PID: $$DELVE_PID. Killing." ; \
  151. kill -9 $$DELVE_PID ; \
  152. fi
  153. ## Runs any lints and unit tests defined for the server and webapp, if they exist.
  154. .PHONY: test
  155. test: webapp/node_modules
  156. ifneq ($(HAS_SERVER),)
  157. $(GO) test -v $(GO_TEST_FLAGS) ./server/...
  158. endif
  159. ifneq ($(HAS_WEBAPP),)
  160. cd webapp && $(NPM) run test;
  161. endif
  162. ifneq ($(wildcard ./build/sync/plan/.),)
  163. cd ./build/sync && $(GO) test -v $(GO_TEST_FLAGS) ./...
  164. endif
  165. ## Creates a coverage report for the server code.
  166. .PHONY: coverage
  167. coverage: webapp/node_modules
  168. ifneq ($(HAS_SERVER),)
  169. $(GO) test $(GO_TEST_FLAGS) -coverprofile=server/coverage.txt ./server/...
  170. $(GO) tool cover -html=server/coverage.txt
  171. endif
  172. ## Extract strings for translation from the source code.
  173. .PHONY: i18n-extract
  174. i18n-extract:
  175. ifneq ($(HAS_WEBAPP),)
  176. ifeq ($(HAS_MM_UTILITIES),)
  177. @echo "You must clone github.com/mattermost/mattermost-utilities repo in .. to use this command"
  178. else
  179. cd $(MM_UTILITIES_DIR) && npm install && npm run babel && node mmjstool/build/index.js i18n extract-webapp --webapp-dir $(PWD)/webapp
  180. endif
  181. endif
  182. ## Disable the plugin.
  183. .PHONY: disable
  184. disable: detach
  185. ./build/bin/pluginctl disable $(PLUGIN_ID)
  186. ## Enable the plugin.
  187. .PHONY: enable
  188. enable:
  189. ./build/bin/pluginctl enable $(PLUGIN_ID)
  190. ## Reset the plugin, effectively disabling and re-enabling it on the server.
  191. .PHONY: reset
  192. reset: detach
  193. ./build/bin/pluginctl reset $(PLUGIN_ID)
  194. ## Kill all instances of the plugin, detaching any existing dlv instance.
  195. .PHONY: kill
  196. kill: detach
  197. $(eval PLUGIN_PID := $(shell ps aux | grep "plugins/${PLUGIN_ID}" | grep -v "grep" | awk -F " " '{print $$2}'))
  198. @for PID in ${PLUGIN_PID}; do \
  199. echo "Killing plugin pid $$PID"; \
  200. kill -9 $$PID; \
  201. done; \
  202. ## Clean removes all build artifacts.
  203. .PHONY: clean
  204. clean:
  205. rm -fr dist/
  206. ifneq ($(HAS_SERVER),)
  207. rm -fr server/coverage.txt
  208. rm -fr server/dist
  209. endif
  210. ifneq ($(HAS_WEBAPP),)
  211. rm -fr webapp/junit.xml
  212. rm -fr webapp/dist
  213. rm -fr webapp/node_modules
  214. endif
  215. rm -fr build/bin/
  216. ## Sync directory with a starter template
  217. sync:
  218. ifndef STARTERTEMPLATE_PATH
  219. @echo STARTERTEMPLATE_PATH is not set.
  220. @echo Set STARTERTEMPLATE_PATH to a local clone of https://github.com/mattermost/mattermost-plugin-starter-template and retry.
  221. @exit 1
  222. endif
  223. cd ${STARTERTEMPLATE_PATH} && go run ./build/sync/main.go ./build/sync/plan.yml $(PWD)
  224. # Help documentation à la https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
  225. help:
  226. @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