|
1 | | -.PHONY: build test run clean stop check-style gofmt dist localdeploy |
2 | | - |
3 | | -GOOS=$(shell uname -s | tr '[:upper:]' '[:lower:]') |
4 | | -GOARCH=amd64 |
5 | | - |
6 | | -check-style: gofmt |
7 | | - @echo Checking for style guide compliance |
8 | | - |
9 | | - cd webapp && npm run check |
10 | | - |
11 | | -gofmt: |
12 | | - @echo Running GOFMT |
13 | | - |
14 | | - @for package in $$(go list ./server/...); do \ |
15 | | - echo "Checking "$$package; \ |
16 | | - files=$$(go list -f '{{range .GoFiles}}{{$$.Dir}}/{{.}} {{end}}' $$package); \ |
17 | | - if [ "$$files" ]; then \ |
18 | | - gofmt_output=$$(gofmt -d -s $$files 2>&1); \ |
19 | | - if [ "$$gofmt_output" ]; then \ |
20 | | - echo "$$gofmt_output"; \ |
21 | | - echo "gofmt failure"; \ |
22 | | - exit 1; \ |
23 | | - fi; \ |
24 | | - fi; \ |
25 | | - done |
26 | | - @echo "gofmt success"; \ |
27 | | - |
| 1 | +GO ?= $(shell command -v go 2> /dev/null) |
| 2 | +DEP ?= $(shell command -v dep 2> /dev/null) |
| 3 | +NPM ?= $(shell command -v npm 2> /dev/null) |
| 4 | +HTTP ?= $(shell command -v http 2> /dev/null) |
| 5 | +CURL ?= $(shell command -v curl 2> /dev/null) |
| 6 | +MANIFEST_FILE ?= plugin.json |
| 7 | + |
| 8 | +# Verify environment, and define PLUGIN_ID, HAS_SERVER and HAS_WEBAPP as needed. |
| 9 | +include build/setup.mk |
| 10 | + |
| 11 | +# all, the default target, tests, builds and bundles the plugin. |
| 12 | +all: test dist |
| 13 | + |
| 14 | +# apply propagates the plugin id into the server/ and webapp/ folders as required. |
| 15 | +.PHONY: apply |
| 16 | +apply: |
| 17 | + ./build/bin/manifest apply |
| 18 | + |
| 19 | +# server/.depensure ensures the server dependencies are installed |
| 20 | +server/.depensure: |
| 21 | +ifneq ($(HAS_SERVER),) |
| 22 | + cd server && $(DEP) ensure |
| 23 | + touch $@ |
| 24 | +endif |
| 25 | + |
| 26 | +# server builds the server, if it exists, including support for multiple architectures |
| 27 | +.PHONY: server |
| 28 | +server: server/.depensure |
| 29 | +ifneq ($(HAS_SERVER),) |
| 30 | + mkdir -p server/dist; |
| 31 | + cd server && env GOOS=linux GOARCH=amd64 $(GO) build -o dist/plugin-linux-amd64; |
| 32 | + cd server && env GOOS=darwin GOARCH=amd64 $(GO) build -o dist/plugin-darwin-amd64; |
| 33 | + cd server && env GOOS=windows GOARCH=amd64 $(GO) build -o dist/plugin-windows-amd64.exe; |
| 34 | +endif |
| 35 | + |
| 36 | +# webapp/.npminstall ensures NPM dependencies are installed without having to run this all the time |
28 | 37 | webapp/.npminstall: |
29 | | - @echo Getting dependencies using npm |
30 | | - |
31 | | - cd webapp && npm install |
| 38 | +ifneq ($(HAS_WEBAPP),) |
| 39 | + cd webapp && $(NPM) install |
32 | 40 | touch $@ |
33 | | - |
34 | | -dist: plugin.json |
35 | | - @echo Building plugin |
36 | | - |
37 | | - # Clean old dist |
38 | | - rm -rf dist |
39 | | - rm -f server/plugin.exe |
40 | | - |
41 | | - # Build files from server |
42 | | - cd server && go get github.com/mitchellh/gox |
43 | | - $(shell go env GOPATH)/bin/gox -osarch='darwin/amd64 linux/amd64 windows/amd64' -output 'dist/intermediate/plugin_{{.OS}}_{{.Arch}}' ./server |
44 | | - |
45 | | - mkdir -p dist/profanity-filter/ |
46 | | - |
47 | | - # Copy plugin files |
48 | | - cp plugin.json dist/profanity-filter/ |
49 | | - |
50 | | - # Copy server executables & compress plugin |
51 | | - mkdir -p dist/profanity-filter/server |
52 | | - mv dist/intermediate/plugin_linux_amd64 dist/profanity-filter/server/plugin.exe |
53 | | - cd dist && tar -zcvf mattermost-profanity-filter-plugin-linux-amd64.tar.gz profanity-filter/* |
54 | | - mv dist/intermediate/plugin_windows_amd64.exe dist/profanity-filter/server/plugin.exe |
55 | | - cd dist && tar -zcvf mattermost-profanity-filter-plugin-windows-amd64.tar.gz profanity-filter/* |
56 | | - mv dist/intermediate/plugin_darwin_amd64 dist/profanity-filter/server/plugin.exe |
57 | | - cd dist && tar -zcvf mattermost-profanity-filter-plugin-darwin-amd64.tar.gz profanity-filter/* |
58 | | - |
59 | | - # Clean up temp files |
60 | | - rm -rf dist/profanity-filter |
61 | | - rm -rf dist/intermediate |
62 | | - |
63 | | - @echo Linux plugin built at: dist/mattermost-profanity-filter-plugin-linux-amd64.tar.gz |
64 | | - @echo MacOS X plugin built at: dist/mattermost-profanity-filter-plugin-darwin-amd64.tar.gz |
65 | | - @echo Windows plugin built at: dist/mattermost-profanity-filter-plugin-windows-amd64.tar.gz |
66 | | - |
67 | | -localdeploy: dist |
68 | | - cp dist/mattermost-profanity-filter-plugin-$(GOOS)-$(GOARCH).tar.gz ../mattermost-server/plugins/ |
69 | | - rm -rf ../mattermost-server/plugins/profanity-filter |
70 | | - tar -C ../mattermost-server/plugins/ -zxvf ../mattermost-server/plugins/mattermost-profanity-filter-plugin-$(GOOS)-$(GOARCH).tar.gz |
71 | | - |
72 | | -stop: |
73 | | - @echo Not yet implemented |
74 | | - |
| 41 | +endif |
| 42 | + |
| 43 | +# webapp builds the webapp, if it exists |
| 44 | +.PHONY: webapp |
| 45 | +webapp: webapp/.npminstall |
| 46 | +ifneq ($(HAS_WEBAPP),) |
| 47 | + cd webapp && $(NPM) run build; |
| 48 | +endif |
| 49 | + |
| 50 | +# bundle generates a tar bundle of the plugin for install |
| 51 | +.PHONY: bundle |
| 52 | +bundle: |
| 53 | + rm -rf dist/ |
| 54 | + mkdir -p dist/$(PLUGIN_ID) |
| 55 | + cp $(MANIFEST_FILE) dist/$(PLUGIN_ID)/ |
| 56 | +ifneq ($(HAS_SERVER),) |
| 57 | + mkdir -p dist/$(PLUGIN_ID)/server/dist; |
| 58 | + cp -r server/dist/* dist/$(PLUGIN_ID)/server/dist/; |
| 59 | +endif |
| 60 | +ifneq ($(HAS_WEBAPP),) |
| 61 | + mkdir -p dist/$(PLUGIN_ID)/webapp/dist; |
| 62 | + cp -r webapp/dist/* dist/$(PLUGIN_ID)/webapp/dist/; |
| 63 | +endif |
| 64 | + cd dist && tar -cvzf $(PLUGIN_ID).tar.gz $(PLUGIN_ID) |
| 65 | + |
| 66 | + @echo plugin built at: dist/$(PLUGIN_ID).tar.gz |
| 67 | + |
| 68 | +# dist builds and bundles the plugin |
| 69 | +.PHONY: dist |
| 70 | +dist: apply \ |
| 71 | + server \ |
| 72 | + webapp \ |
| 73 | + bundle |
| 74 | + |
| 75 | +# deploy installs the plugin to a (development) server, using the API if appropriate environment |
| 76 | +# variables are defined, or copying the files directly to a sibling mattermost-server directory |
| 77 | +.PHONY: deploy |
| 78 | +deploy: dist |
| 79 | +ifneq ($(and $(MM_SERVICESETTINGS_SITEURL),$(MM_ADMIN_USERNAME),$(MM_ADMIN_PASSWORD),$(HTTP)),) |
| 80 | + @echo "Installing plugin via API" |
| 81 | + (TOKEN=`http --print h POST $(MM_SERVICESETTINGS_SITEURL)/api/v4/users/login login_id=$(MM_ADMIN_USERNAME) password=$(MM_ADMIN_PASSWORD) | grep Token | cut -f2 -d' '` && \ |
| 82 | + http --print b GET $(MM_SERVICESETTINGS_SITEURL)/api/v4/users/me Authorization:"Bearer $$TOKEN" && \ |
| 83 | + http --print b DELETE $(MM_SERVICESETTINGS_SITEURL)/api/v4/plugins/$(PLUGIN_ID) Authorization:"Bearer $$TOKEN" && \ |
| 84 | + http --print b --check-status --form POST $(MM_SERVICESETTINGS_SITEURL)/api/v4/plugins plugin@dist/$(PLUGIN_ID).tar.gz Authorization:"Bearer $$TOKEN" && \ |
| 85 | + http --print b POST $(MM_SERVICESETTINGS_SITEURL)/api/v4/plugins/$(PLUGIN_ID)/enable Authorization:"Bearer $$TOKEN" && \ |
| 86 | + http --print b POST $(MM_SERVICESETTINGS_SITEURL)/api/v4/users/logout Authorization:"Bearer $$TOKEN" \ |
| 87 | + ) |
| 88 | +else ifneq ($(and $(MM_SERVICESETTINGS_SITEURL),$(MM_ADMIN_USERNAME),$(MM_ADMIN_PASSWORD),$(CURL)),) |
| 89 | + @echo "Installing plugin via API" |
| 90 | + $(eval TOKEN := $(shell curl -i -X POST $(MM_SERVICESETTINGS_SITEURL)/api/v4/users/login -d '{"login_id": "$(MM_ADMIN_USERNAME)", "password": "$(MM_ADMIN_PASSWORD)"}' | grep Token | cut -f2 -d' ' 2> /dev/null)) |
| 91 | + @curl -s -H "Authorization: Bearer $(TOKEN)" -X DELETE $(MM_SERVICESETTINGS_SITEURL)/api/v4/plugins/$(PLUGIN_ID) > /dev/null |
| 92 | + @curl -s -H "Authorization: Bearer $(TOKEN)" -X POST $(MM_SERVICESETTINGS_SITEURL)/api/v4/plugins -F "plugin=@dist/$(PLUGIN_ID).tar.gz" > /dev/null && \ |
| 93 | + curl -s -H "Authorization: Bearer $(TOKEN)" -X POST $(MM_SERVICESETTINGS_SITEURL)/api/v4/plugins/$(PLUGIN_ID)/enable > /dev/null && \ |
| 94 | + echo "OK." || echo "Sorry, something went wrong." |
| 95 | +else ifneq ($(wildcard ../mattermost-server/.*),) |
| 96 | + @echo "Installing plugin via filesystem. Server restart and manual plugin enabling required" |
| 97 | + mkdir -p ../mattermost-server/plugins |
| 98 | + tar -C ../mattermost-server/plugins -zxvf dist/$(PLUGIN_ID).tar.gz |
| 99 | +else |
| 100 | + @echo "No supported deployment method available. Install plugin manually." |
| 101 | +endif |
| 102 | + |
| 103 | +# test runs any lints and unit tests defined for the server and webapp, if they exist |
| 104 | +.PHONY: test |
| 105 | +test: server/.depensure webapp/.npminstall |
| 106 | +ifneq ($(HAS_SERVER),) |
| 107 | + cd server && $(GO) test -v -coverprofile=coverage.txt ./... |
| 108 | +endif |
| 109 | +ifneq ($(HAS_WEBAPP),) |
| 110 | + cd webapp && $(NPM) run fix; |
| 111 | +endif |
| 112 | + |
| 113 | +# clean removes all build artifacts |
| 114 | +.PHONY: clean |
75 | 115 | clean: |
76 | | - @echo Cleaning plugin |
77 | | - |
78 | | - rm -rf dist |
79 | | - rm -rf webapp/dist |
80 | | - rm -rf webapp/node_modules |
81 | | - rm -rf webapp/.npminstall |
82 | | - rm -f server/plugin.exe |
| 116 | + rm -fr dist/ |
| 117 | +ifneq ($(HAS_SERVER),) |
| 118 | + rm -fr server/dist |
| 119 | + rm -fr server/.depensure |
| 120 | +endif |
| 121 | +ifneq ($(HAS_WEBAPP),) |
| 122 | + rm -fr webapp/.npminstall |
| 123 | + rm -fr webapp/dist |
| 124 | + rm -fr webapp/node_modules |
| 125 | +endif |
| 126 | + rm -fr build/bin/ |
0 commit comments