Makefile 8.1 KB

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