|
| 1 | +GO:=go |
| 2 | +GOARCH:=amd64 |
| 3 | +GOOS=$(shell uname -s | tr '[:upper:]' '[:lower:]') |
| 4 | +GOPATH ?= $(GOPATH:) |
| 5 | + |
| 6 | +MANIFEST_FILE ?= plugin.json |
| 7 | + |
| 8 | +PLUGINNAME=$(shell echo `grep '"'"id"'"\s*:\s*"' $(MANIFEST_FILE) | head -1 | cut -d'"' -f4`) |
| 9 | +PLUGINVERSION=v$(shell echo `grep '"'"version"'"\s*:\s*"' $(MANIFEST_FILE) | head -1 | cut -d'"' -f4`) |
| 10 | +PACKAGENAME=mattermost-plugin-$(PLUGINNAME)-$(PLUGINVERSION) |
| 11 | + |
| 12 | +HAS_WEBAPP=$(shell if [ "$(shell grep -E '[\^"]webapp["][ ]*[:]' $(MANIFEST_FILE) | wc -l)" -gt "0" ]; then echo "true"; fi) |
| 13 | +HAS_SERVER=$(shell if [ "$(shell grep -E '[\^"]server["][ ]*[:]' $(MANIFEST_FILE) | wc -l)" -gt "0" ]; then echo "true"; fi) |
| 14 | + |
| 15 | +TMPFILEGOLINT=golint.tmp |
| 16 | + |
| 17 | +BLACK=`tput setaf 0` |
| 18 | +RED=`tput setaf 1` |
| 19 | +GREEN=`tput setaf 2` |
| 20 | +YELLOW=`tput setaf 3` |
| 21 | +BLUE=`tput setaf 4` |
| 22 | +MAGENTA=`tput setaf 5` |
| 23 | +CYAN=`tput setaf 6` |
| 24 | +WHITE=`tput setaf 7` |
| 25 | + |
| 26 | +BOLD=`tput bold` |
| 27 | +INVERSE=`tput rev` |
| 28 | +RESET=`tput sgr0` |
| 29 | + |
| 30 | +.PHONY: default .npminstall vendor setup-plugin build test clean check-style check-js check-go govet golint gofmt .distclean dist format fix-js fix-go trigger-release install-dependencies |
| 31 | + |
| 32 | +default: dist |
| 33 | + |
| 34 | +setup-plugin: |
| 35 | +ifneq ($(HAS_WEBAPP),) |
| 36 | + @echo "export const PLUGIN_NAME = '`echo $(PLUGINNAME)`';" > webapp/src/constants/manifest.js |
| 37 | +endif |
| 38 | +ifneq ($(HAS_SERVER),) |
| 39 | + @echo "package config\n\nconst (\n\tPluginName = \""`echo $(PLUGINNAME)`"\"\n)" > server/config/manifest.go |
| 40 | +endif |
| 41 | + |
| 42 | +trigger-release: |
| 43 | + @if [ $$(git status --porcelain | wc -l) != "0" -o $$(git rev-list HEAD@{upstream}..HEAD | wc -l) != "0" ]; \ |
| 44 | + then echo ${RED}"local repo is not clean"${RESET}; exit 1; fi; |
| 45 | + @echo ${BOLD}"Creating a tag to trigger circleci build-and-release job\n"${RESET} |
| 46 | + git tag $(PLUGINVERSION) |
| 47 | + git push origin $(PLUGINVERSION) |
| 48 | + |
| 49 | +check-style: check-js check-go |
| 50 | + |
| 51 | +check-js: |
| 52 | +ifneq ($(HAS_WEBAPP),) |
| 53 | + @echo ${BOLD}Running ESLINT${RESET} |
| 54 | + @cd webapp && npm run lint |
| 55 | + @echo ${GREEN}"eslint success\n"${RESET} |
| 56 | +endif |
| 57 | + |
| 58 | +check-go: govet golint gofmt |
| 59 | + |
| 60 | +govet: |
| 61 | +ifneq ($(HAS_SERVER),) |
| 62 | + @go tool vet 2>/dev/null ; if [ $$? -eq 3 ]; then \ |
| 63 | + echo "--> installing govet"; \ |
| 64 | + go get golang.org/x/tools/cmd/vet; \ |
| 65 | + fi |
| 66 | + @echo ${BOLD}Running GOVET${RESET} |
| 67 | + @cd server |
| 68 | + $(eval PKGS := $(shell go list ./... | grep -v /vendor/)) |
| 69 | + @$(GO) vet -shadow $(PKGS) |
| 70 | + @echo ${GREEN}"govet success\n"${RESET} |
| 71 | +endif |
| 72 | + |
| 73 | +golint: |
| 74 | +ifneq ($(HAS_SERVER),) |
| 75 | + @command -v golint >/dev/null ; if [ $$? -ne 0 ]; then \ |
| 76 | + echo "--> installing golint"; \ |
| 77 | + go get -u golang.org/x/lint/golint; \ |
| 78 | + fi |
| 79 | + @echo ${BOLD}Running GOLINT${RESET} |
| 80 | + @cd server |
| 81 | + $(eval PKGS := $(shell go list ./... | grep -v /vendor/)) |
| 82 | + @touch $(TMPFILEGOLINT) |
| 83 | + -@for pkg in $(PKGS) ; do \ |
| 84 | + echo `golint $$pkg | grep -v "have comment" | grep -v "comment on exported" | grep -v "lint suggestions"` >> $(TMPFILEGOLINT) ; \ |
| 85 | + done |
| 86 | + @grep -Ev "^$$" $(TMPFILEGOLINT) || true |
| 87 | + @if [ "$$(grep -Ev "^$$" $(TMPFILEGOLINT) | wc -l)" -gt "0" ]; then \ |
| 88 | + rm -f $(TMPFILEGOLINT); echo ${RED}"golint failure\n"${RESET}; exit 1; else \ |
| 89 | + rm -f $(TMPFILEGOLINT); echo ${GREEN}"golint success\n"${RESET}; \ |
| 90 | + fi |
| 91 | +endif |
| 92 | + |
| 93 | +format: fix-js fix-go |
| 94 | + |
| 95 | +fix-js: |
| 96 | +ifneq ($(HAS_WEBAPP),) |
| 97 | + @echo ${BOLD}Formatting js giles${RESET} |
| 98 | + @cd webapp && npm run fix |
| 99 | + @echo "formatted js files\n" |
| 100 | +endif |
| 101 | + |
| 102 | +fix-go: |
| 103 | +ifneq ($(HAS_SERVER),) |
| 104 | + @command -v goimports >/dev/null ; if [ $$? -ne 0 ]; then \ |
| 105 | + echo "--> installing goimports"; \ |
| 106 | + go get golang.org/x/tools/cmd/goimports; \ |
| 107 | + fi |
| 108 | + @echo ${BOLD}Formatting go giles${RESET} |
| 109 | + @cd server |
| 110 | + @find ./ -type f -name "*.go" -not -path "./server/vendor/*" -exec goimports -w {} \; |
| 111 | + @echo "formatted go files\n" |
| 112 | +endif |
| 113 | + |
| 114 | +gofmt: |
| 115 | +ifneq ($(HAS_SERVER),) |
| 116 | + @echo ${BOLD}Running GOFMT${RESET} |
| 117 | + @for package in $$(go list ./server/...); do \ |
| 118 | + files=$$(go list -f '{{range .GoFiles}}{{$$.Dir}}/{{.}} {{end}}' $$package); \ |
| 119 | + if [ "$$files" ]; then \ |
| 120 | + gofmt_output=$$(gofmt -d -s $$files 2>&1); \ |
| 121 | + if [ "$$gofmt_output" ]; then \ |
| 122 | + echo "$$gofmt_output"; \ |
| 123 | + echo ${RED}"gofmt failure\n"${RESET}; \ |
| 124 | + exit 1; \ |
| 125 | + fi; \ |
| 126 | + fi; \ |
| 127 | + done |
| 128 | + @echo ${GREEN}"gofmt success\n"${RESET} |
| 129 | +endif |
| 130 | + |
| 131 | +test: |
| 132 | +ifneq ($(HAS_SERVER),) |
| 133 | + go test -v -coverprofile=coverage.txt ./... |
| 134 | +endif |
| 135 | + |
| 136 | +.npminstall: |
| 137 | +ifneq ($(HAS_WEBAPP),) |
| 138 | + @echo ${BOLD}"Getting dependencies using npm\n"${RESET} |
| 139 | + cd webapp && npm install |
| 140 | + @echo "\n" |
| 141 | +endif |
| 142 | + |
| 143 | +vendor: |
| 144 | +ifneq ($(HAS_SERVER),) |
| 145 | + @echo ${BOLD}"Getting dependencies using glide\n"${RESET} |
| 146 | + cd server && go get github.com/Masterminds/glide |
| 147 | + cd server && $(shell go env GOPATH)/bin/glide install |
| 148 | + @echo "\n" |
| 149 | +endif |
| 150 | + |
| 151 | +install-dependencies: .npminstall vendor |
| 152 | + |
| 153 | +dist: install-dependencies check-style test build |
| 154 | + |
| 155 | +build: .distclean $(MANIFEST_FILE) |
| 156 | + @echo ${BOLD}"Building plugin\n"${RESET} |
| 157 | + mkdir -p dist/$(PLUGINNAME)/ |
| 158 | + cp $(MANIFEST_FILE) dist/$(PLUGINNAME)/ |
| 159 | + |
| 160 | +ifneq ($(HAS_WEBAPP),) |
| 161 | + # Build and copy files from webapp |
| 162 | + cd webapp && npm run build |
| 163 | + mkdir -p dist/$(PLUGINNAME)/webapp |
| 164 | + cp -r webapp/dist/* dist/$(PLUGINNAME)/webapp/ |
| 165 | +endif |
| 166 | + |
| 167 | +ifneq ($(HAS_SERVER),) |
| 168 | + # Build files from server and copy server executables |
| 169 | + cd server && go get github.com/mitchellh/gox |
| 170 | + $(shell go env GOPATH)/bin/gox -osarch='darwin/amd64 linux/amd64 windows/amd64' -output 'dist/intermediate/plugin_{{.OS}}_{{.Arch}}' ./server |
| 171 | + mkdir -p dist/$(PLUGINNAME)/server |
| 172 | + |
| 173 | +endif |
| 174 | + |
| 175 | + # Compress plugin |
| 176 | +ifneq ($(HAS_SERVER),) |
| 177 | + mv dist/intermediate/plugin_darwin_amd64 dist/$(PLUGINNAME)/server/plugin.exe |
| 178 | + cd dist && tar -zcvf $(PACKAGENAME)-darwin-amd64.tar.gz $(PLUGINNAME)/* |
| 179 | + |
| 180 | + mv dist/intermediate/plugin_linux_amd64 dist/$(PLUGINNAME)/server/plugin.exe |
| 181 | + cd dist && tar -zcvf $(PACKAGENAME)-linux-amd64.tar.gz $(PLUGINNAME)/* |
| 182 | + |
| 183 | + mv dist/intermediate/plugin_windows_amd64.exe dist/$(PLUGINNAME)/server/plugin.exe |
| 184 | + cd dist && tar -zcvf $(PACKAGENAME)-windows-amd64.tar.gz $(PLUGINNAME)/* |
| 185 | +else ifneq ($(HAS_WEBAPP),) |
| 186 | + cd dist && tar -zcvf $(PACKAGENAME).tar.gz $(PLUGINNAME)/* |
| 187 | +endif |
| 188 | + |
| 189 | + # Clean up temp files |
| 190 | + rm -rf dist/$(PLUGINNAME) |
| 191 | + rm -rf dist/intermediate |
| 192 | + |
| 193 | +ifneq ($(HAS_SERVER),) |
| 194 | + @echo Linux plugin built at: dist/$(PACKAGENAME)-linux-amd64.tar.gz |
| 195 | + @echo MacOS X plugin built at: dist/$(PACKAGENAME)-darwin-amd64.tar.gz |
| 196 | + @echo Windows plugin built at: dist/$(PACKAGENAME)-windows-amd64.tar.gz |
| 197 | +else ifneq ($(HAS_WEBAPP),) |
| 198 | + @echo Cross-platform plugin built at: dist/$(PACKAGENAME)-amd64.tar.gz |
| 199 | +endif |
| 200 | + |
| 201 | +.distclean: |
| 202 | + @echo ${BOLD}"Cleaning dist files\n"${RESET} |
| 203 | + rm -rf dist |
| 204 | + rm -rf webapp/dist |
| 205 | + rm -f server/plugin.exe |
| 206 | + @echo "\n" |
| 207 | + |
| 208 | +clean: .distclean |
| 209 | + @echo ${BOLD}"Cleaning plugin\n"${RESET} |
| 210 | + rm -rf server/vendor |
| 211 | + rm -rf webapp/node_modules |
| 212 | + rm -rf webapp/.npminstall |
| 213 | + @echo "\n" |
0 commit comments