Unverified Commit 322d3407 authored by Michael Kochell's avatar Michael Kochell Committed by GitHub
Browse files

Unify structure with other plugins (#10)

Unify structure with other plugins
parents ab771492 bc405251
version: 2.1
jobs:
test:
docker:
- image: circleci/golang:1.12
working_directory: /go/src/github.com/mattermost/mattermost-plugin-memes
steps:
- checkout
- run: make test
orbs:
plugin-ci: mattermost/plugin-ci@volatile
workflows:
version: 2
untagged-build:
ci:
jobs:
- test
- plugin-ci/lint:
filters:
tags:
only: /^v.*/
- plugin-ci/coverage:
filters:
tags:
only: /^v.*/
- plugin-ci/build:
filters:
tags:
only: /^v.*/
- plugin-ci/deploy-ci:
filters:
branches:
only: master
context: plugin-ci
requires:
- plugin-ci/lint
- plugin-ci/coverage
- plugin-ci/build
- plugin-ci/deploy-release:
filters:
tags:
only: /^v.*/
branches:
ignore: /.*/
context: plugin-ci
requires:
- plugin-ci/lint
- plugin-ci/coverage
- plugin-ci/build
- plugin-ci/deploy-release-github:
filters:
tags:
only: /^v.*/
branches:
ignore: /.*/
context: matterbuild-github-token
requires:
- plugin-ci/lint
- plugin-ci/coverage
- plugin-ci/build
# http://editorconfig.org/
root = true
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
[*.go]
indent_style = tab
[*.{js,jsx,json,html}]
indent_style = space
indent_size = 4
[webapp/package.json]
indent_size = 2
[Makefile,*.mk]
indent_style = tab
[*.md]
indent_style = space
indent_size = 4
trim_trailing_whitespace = false
/plugin.exe
/mattermost-memes-plugin.tar.gz
/memelibrary/assets.go
/dist
/vendor
dist
.PHONY: test
GO ?= $(shell command -v go 2> /dev/null)
NPM ?= $(shell command -v npm 2> /dev/null)
CURL ?= $(shell command -v curl 2> /dev/null)
MANIFEST_FILE ?= plugin.json
GOPATH ?= $(shell go env GOPATH)
GO_TEST_FLAGS ?= -race
GO_BUILD_FLAGS ?=
MM_UTILITIES_DIR ?= ../mattermost-utilities
export GO111MODULE=on
# You can include assets this directory into the bundle. This can be e.g. used to include profile pictures.
ASSETS_DIR ?= assets
all: test dist
# Verify environment, and define PLUGIN_ID, PLUGIN_VERSION, HAS_SERVER and HAS_WEBAPP as needed.
include build/setup.mk
TAR_PLUGIN_EXE_TRANSFORM = --transform 'flags=r;s|dist/intermediate/plugin_.*|plugin.exe|'
ifneq (,$(findstring bsdtar,$(shell tar --version)))
TAR_PLUGIN_EXE_TRANSFORM = -s '|dist/intermediate/plugin_.*|plugin.exe|'
BUNDLE_NAME ?= $(PLUGIN_ID)-$(PLUGIN_VERSION).tar.gz
# Include custom makefile, if pressent
ifneq ($(wildcard build/custom.mk),)
include build/custom.mk
endif
dist: vendor memelibrary/assets.go $(shell go list -f '{{range .GoFiles}}{{$$.Dir}}/{{.}} {{end}}' ./...) plugin.yaml
rm -rf ./dist
go get github.com/mitchellh/gox
$(shell go env GOPATH)/bin/gox -osarch='darwin/amd64 linux/amd64 windows/amd64' -output 'dist/intermediate/plugin_{{.OS}}_{{.Arch}}'
tar -czvf dist/mattermost-memes-plugin-darwin-amd64.tar.gz $(TAR_PLUGIN_EXE_TRANSFORM) dist/intermediate/plugin_darwin_amd64 plugin.yaml
tar -czvf dist/mattermost-memes-plugin-linux-amd64.tar.gz $(TAR_PLUGIN_EXE_TRANSFORM) dist/intermediate/plugin_linux_amd64 plugin.yaml
tar -czvf dist/mattermost-memes-plugin-windows-amd64.tar.gz $(TAR_PLUGIN_EXE_TRANSFORM) dist/intermediate/plugin_windows_amd64.exe plugin.yaml
rm -rf dist/intermediate
## Checks the code style, tests, builds and bundles the plugin.
all: check-style test dist
## Propagates plugin manifest information into the server/ and webapp/ folders as required.
.PHONY: apply
apply:
./build/bin/manifest apply
memelibrary/assets.go: $(shell find memelibrary/assets)
go get github.com/jteeuwen/go-bindata/...
$(shell go env GOPATH)/bin/go-bindata -o $@ -pkg memelibrary -prefix memelibrary/assets/ -ignore '(^|/)\..*' memelibrary/assets/...
## Runs govet and gofmt against all packages.
.PHONY: check-style
# Disable golint for now
check-style: webapp/.npminstall gofmt govet # golint
@echo Checking for style guide compliance
mattermost-memes-plugin.tar.gz: vendor memelibrary/assets.go $(shell go list -f '{{range .GoFiles}}{{$$.Dir}}/{{.}} {{end}}' ./...) plugin.yaml
go build -o plugin.exe
tar -czvf $@ plugin.exe plugin.yaml
rm plugin.exe
ifneq ($(HAS_WEBAPP),)
cd webapp && npm run lint
endif
## Runs gofmt against all packages.
.PHONY: gofmt
gofmt:
ifneq ($(HAS_SERVER),)
@echo Running gofmt
@for package in $$(go list ./...); do \
echo "Checking "$$package; \
files=$$(go list -f '{{range .GoFiles}}{{$$.Dir}}/{{.}} {{end}}' $$package); \
if [ "$$files" ]; then \
gofmt_output=$$(gofmt -d -s $$files 2>&1); \
if [ "$$gofmt_output" ]; then \
echo "$$gofmt_output"; \
echo "Gofmt failure"; \
exit 1; \
fi; \
fi; \
done
@echo Gofmt success
endif
## Runs govet against all packages.
.PHONY: govet
govet:
ifneq ($(HAS_SERVER),)
@echo Running govet
@# Workaround because you can't install binaries without adding them to go.mod
env GO111MODULE=off $(GO) get golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
$(GO) vet ./...
$(GO) vet -vettool=$(GOPATH)/bin/shadow ./...
@echo Govet success
endif
test: vendor memelibrary/assets.go
go test -v ./...
## Runs golint against all packages.
.PHONY: golint
golint:
@echo Running lint
env GO111MODULE=off $(GO) get golang.org/x/lint/golint
$(GOPATH)/bin/golint -set_exit_status ./...
@echo lint success
## Builds the server, if it exists, including support for multiple architectures.
.PHONY: server
server:
ifneq ($(HAS_SERVER),)
mkdir -p server/dist;
cd server && env GOOS=linux GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -o dist/plugin-linux-amd64;
cd server && env GOOS=darwin GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -o dist/plugin-darwin-amd64;
cd server && env GOOS=windows GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -o dist/plugin-windows-amd64.exe;
endif
## Ensures NPM dependencies are installed without having to run this all the time.
webapp/.npminstall:
ifneq ($(HAS_WEBAPP),)
cd webapp && $(NPM) install
touch $@
endif
## Builds the webapp, if it exists.
.PHONY: webapp
webapp: webapp/.npminstall
ifneq ($(HAS_WEBAPP),)
cd webapp && $(NPM) run build;
endif
## Generates a tar bundle of the plugin for install.
.PHONY: bundle
bundle:
rm -rf dist/
mkdir -p dist/$(PLUGIN_ID)
cp $(MANIFEST_FILE) dist/$(PLUGIN_ID)/
ifneq ($(wildcard $(ASSETS_DIR)/.),)
cp -r $(ASSETS_DIR) dist/$(PLUGIN_ID)/
endif
ifneq ($(HAS_PUBLIC),)
cp -r public/ dist/$(PLUGIN_ID)/
endif
ifneq ($(HAS_SERVER),)
mkdir -p dist/$(PLUGIN_ID)/server/dist;
cp -r server/dist/* dist/$(PLUGIN_ID)/server/dist/;
endif
ifneq ($(HAS_WEBAPP),)
mkdir -p dist/$(PLUGIN_ID)/webapp/dist;
cp -r webapp/dist/* dist/$(PLUGIN_ID)/webapp/dist/;
endif
cd dist && tar -cvzf $(BUNDLE_NAME) $(PLUGIN_ID)
@echo plugin built at: dist/$(BUNDLE_NAME)
## Builds and bundles the plugin.
.PHONY: dist
dist: apply server webapp bundle
## Installs the plugin to a (development) server.
.PHONY: deploy
deploy: dist
## It uses the API if appropriate environment variables are defined,
## or copying the files directly to a sibling mattermost-server directory.
ifneq ($(and $(MM_SERVICESETTINGS_SITEURL),$(MM_ADMIN_USERNAME),$(MM_ADMIN_PASSWORD),$(CURL)),)
@echo "Installing plugin via API"
$(eval TOKEN := $(shell curl -i --post301 --location $(MM_SERVICESETTINGS_SITEURL) -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))
@curl -s --post301 --location $(MM_SERVICESETTINGS_SITEURL) -H "Authorization: Bearer $(TOKEN)" -X POST $(MM_SERVICESETTINGS_SITEURL)/api/v4/plugins -F "plugin=@dist/$(BUNDLE_NAME)" -F "force=true" > /dev/null && \
curl -s --post301 --location $(MM_SERVICESETTINGS_SITEURL) -H "Authorization: Bearer $(TOKEN)" -X POST $(MM_SERVICESETTINGS_SITEURL)/api/v4/plugins/$(PLUGIN_ID)/enable > /dev/null && \
echo "OK." || echo "Sorry, something went wrong."
else ifneq ($(wildcard ../mattermost-server/.*),)
@echo "Installing plugin via filesystem. Server restart and manual plugin enabling required"
mkdir -p ../mattermost-server/plugins
tar -C ../mattermost-server/plugins -zxvf dist/$(BUNDLE_NAME)
else
@echo "No supported deployment method available. Install plugin manually."
endif
## Runs any lints and unit tests defined for the server and webapp, if they exist.
.PHONY: test
test: webapp/.npminstall
ifneq ($(HAS_SERVER),)
$(GO) test -v $(GO_TEST_FLAGS) ./server/...
endif
ifneq ($(HAS_WEBAPP),)
cd webapp && $(NPM) run fix && $(NPM) run test;
endif
## Creates a coverage report for the server code.
.PHONY: coverage
coverage: webapp/.npminstall
ifneq ($(HAS_SERVER),)
$(GO) test $(GO_TEST_FLAGS) -coverprofile=server/coverage.txt ./server/...
$(GO) tool cover -html=server/coverage.txt
endif
## Extract strings for translation from the source code.
.PHONY: i18n-extract
i18n-extract:
ifneq ($(HAS_WEBAPP),)
ifeq ($(HAS_MM_UTILITIES),)
@echo "You must clone github.com/mattermost/mattermost-utilities repo in .. to use this command"
else
cd $(MM_UTILITIES_DIR) && npm install && npm run babel && node mmjstool/build/index.js i18n extract-webapp --webapp-dir $(PWD)/webapp
endif
endif
## Clean removes all build artifacts.
.PHONY: clean
clean:
rm -fr dist/
ifneq ($(HAS_SERVER),)
rm -fr server/dist
endif
ifneq ($(HAS_WEBAPP),)
rm -fr webapp/.npminstall
rm -fr webapp/dist
rm -fr webapp/node_modules
endif
rm -fr build/bin/
vendor: glide.lock
go get github.com/Masterminds/glide
$(shell go env GOPATH)/bin/glide install
# Help documentatin à la https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
help:
@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
# mattermost-plugin-memes [![CircleCI](https://circleci.com/gh/mattermost/mattermost-plugin-memes.svg?style=svg)](https://circleci.com/gh/mattermost/mattermost-plugin-memes)
# Mattermost Memes Plugin
[![Build Status](https://img.shields.io/circleci/project/github/mattermost/mattermost-plugin-memes/master.svg)](https://circleci.com/gh/mattermost/mattermost-plugin-memes)
[![Code Coverage](https://img.shields.io/codecov/c/github/mattermost/mattermost-plugin-memes/master.svg)](https://codecov.io/gh/mattermost/mattermost-plugin-memes)
This plugin will create a slash command that you can use to create memes!
......@@ -6,7 +10,7 @@ This plugin will create a slash command that you can use to create memes!
`/meme memes. memes everywhere`
For more information like avaliable memes or command syntax type `/meme ` and press enter.
For more information like available memes or command syntax type `/meme ` and press enter.
## Installation
......@@ -26,4 +30,4 @@ If you want to create a fully bundled plugin that will run on a local server, yo
## Releasing
To make a release, update the version number in plugin.yaml, and create a release via the GitHub interface. Travis will upload the distributables for you.
To make a release, update the version number in plugin.yaml, and create a release via the GitHub interface. CircleCI will upload the distributables for you.
# Include custom targets and environment variables here
memelibrary: $(shell find server/memelibrary/assets)
env GO111MODULE=off $(GO) get github.com/jteeuwen/go-bindata/...
$(GOPATH)/bin/go-bindata -o server/memelibrary/assets.go -pkg memelibrary -prefix server/memelibrary/assets/ -ignore '(^|/)\..*' server/memelibrary/assets/...
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/mattermost/mattermost-server/model"
"github.com/pkg/errors"
)
const pluginIDGoFileTemplate = `package main
var manifest = struct {
ID string
Version string
}{
ID: "%s",
Version: "%s",
}
`
const pluginIDJSFileTemplate = `export const id = '%s';
export const version = '%s';
`
func main() {
if len(os.Args) <= 1 {
panic("no cmd specified")
}
manifest, err := findManifest()
if err != nil {
panic("failed to find manifest: " + err.Error())
}
cmd := os.Args[1]
switch cmd {
case "id":
dumpPluginID(manifest)
case "version":
dumpPluginVersion(manifest)
case "has_server":
if manifest.HasServer() {
fmt.Printf("true")
}
case "has_webapp":
if manifest.HasWebapp() {
fmt.Printf("true")
}
case "apply":
if err := applyManifest(manifest); err != nil {
panic("failed to apply manifest: " + err.Error())
}
default:
panic("unrecognized command: " + cmd)
}
}
func findManifest() (*model.Manifest, error) {
_, manifestFilePath, err := model.FindManifest(".")
if err != nil {
return nil, errors.Wrap(err, "failed to find manifest in current working directory")
}
manifestFile, err := os.Open(manifestFilePath)
if err != nil {
return nil, errors.Wrapf(err, "failed to open %s", manifestFilePath)
}
defer manifestFile.Close()
// Re-decode the manifest, disallowing unknown fields. When we write the manifest back out,
// we don't want to accidentally clobber anything we won't preserve.
var manifest model.Manifest
decoder := json.NewDecoder(manifestFile)
decoder.DisallowUnknownFields()
if err = decoder.Decode(&manifest); err != nil {
return nil, errors.Wrap(err, "failed to parse manifest")
}
return &manifest, nil
}
// dumpPluginId writes the plugin id from the given manifest to standard out
func dumpPluginID(manifest *model.Manifest) {
fmt.Printf("%s", manifest.Id)
}
// dumpPluginVersion writes the plugin version from the given manifest to standard out
func dumpPluginVersion(manifest *model.Manifest) {
fmt.Printf("%s", manifest.Version)
}
// applyManifest propagates the plugin_id into the server and webapp folders, as necessary
func applyManifest(manifest *model.Manifest) error {
if manifest.HasServer() {
if err := ioutil.WriteFile(
"server/manifest.go",
[]byte(fmt.Sprintf(pluginIDGoFileTemplate, manifest.Id, manifest.Version)),
0644,
); err != nil {
return errors.Wrap(err, "failed to write server/manifest.go")
}
}
if manifest.HasWebapp() {
if err := ioutil.WriteFile(
"webapp/src/manifest.js",
[]byte(fmt.Sprintf(pluginIDJSFileTemplate, manifest.Id, manifest.Version)),
0644,
); err != nil {
return errors.Wrap(err, "failed to open webapp/src/manifest.js")
}
}
return nil
}
# Ensure that go is installed. Note that this is independent of whether or not a server is being
# built, since the build script itself uses go.
ifeq ($(GO),)
$(error "go is not available: see https://golang.org/doc/install")
endif
# Ensure that the build tools are compiled. Go's caching makes this quick.
$(shell cd build/manifest && $(GO) build -o ../bin/manifest)
# Extract the plugin id from the manifest.
PLUGIN_ID ?= $(shell build/bin/manifest id)
ifeq ($(PLUGIN_ID),)
$(error "Cannot parse id from $(MANIFEST_FILE)")
endif
# Extract the plugin version from the manifest.
PLUGIN_VERSION ?= $(shell build/bin/manifest version)
ifeq ($(PLUGIN_VERSION),)
$(error "Cannot parse version from $(MANIFEST_FILE)")
endif
# Determine if a server is defined in the manifest.
HAS_SERVER ?= $(shell build/bin/manifest has_server)
# Determine if a webapp is defined in the manifest.
HAS_WEBAPP ?= $(shell build/bin/manifest has_webapp)
# Determine if a /public folder is in use
HAS_PUBLIC ?= $(wildcard public/.)
# Determine if the mattermost-utilities repo is present
HAS_MM_UTILITIES ?= $(wildcard $(MM_UTILITIES_DIR)/.)
# Store the current path for later use
PWD ?= $(shell pwd)
# Ensure that npm (and thus node) is installed.
ifneq ($(HAS_WEBAPP),)
ifeq ($(NPM),)
$(error "npm is not available: see https://www.npmjs.com/get-npm")
endif
endif
hash: a3877dc6f3d4f71229131fe319d16d3feeb34c75df413df9426951a726dc64a6
updated: 2018-07-15T21:26:16.18042093-04:00
imports:
- name: github.com/golang/freetype
version: e2365dfdc4a05e4b8299a783240d4a7d5a65d4e4
subpackages:
- truetype
- raster
- name: github.com/golang/protobuf
version: 14aad3d5ea4c323bcd7a2137e735da24a76e814c
subpackages:
- proto
- ptypes
- ptypes/any
- ptypes/duration
- ptypes/timestamp
- name: github.com/gorilla/context
version: 08b5f424b9271eedf6f9f0ce86cb9396ed337a42
- name: github.com/gorilla/mux
version: e3702bed27f0d39777b0b37b664b6280e8ef8fbf
- name: github.com/gorilla/websocket
version: 5ed622c449da6d44c3c8329331ff47a9e5844f71
- name: github.com/hashicorp/go-hclog
version: ff2cf002a8dd750586d91dddd4470c341f981fe1
- name: github.com/hashicorp/go-plugin
version: e8d22c780116115ae5624720c9af0c97afe4f551
- name: github.com/hashicorp/yamux
version: 3520598351bb3500a49ae9563f5539666ae0a27c
- name: github.com/kballard/go-shellquote
version: 95032a82bc518f77982ea72343cc1ade730072f0
- name: github.com/mattermost/mattermost-server
version: 62c64594ccaa0e634023b358758f2a6bf04164ad
subpackages:
- model
- plugin
- mlog
- utils/jsonutils
- utils/markdown
- name: github.com/mitchellh/go-testing-interface
version: a61a99592b77c9ba629d254a693acffaeb4b7e28
- name: github.com/nicksnyder/go-i18n
version: f6ac3d9cf0c4b6a32527779e992ebde26bd3d948
subpackages:
- i18n
- i18n/bundle
- i18n/language
- i18n/translation
- name: github.com/oklog/run
version: 6934b124db28979da51d3470dadfa34d73d72652
- name: github.com/pborman/uuid
version: c65b2f87fee37d1c7854c9164a450713c28d50cd
- name: github.com/pelletier/go-toml
version: 603baefff989777996bf283da430d693e78eba3a
- name: github.com/pkg/errors
version: 816c9085562cd7ee03e7f8188a1cfd942858cded
- name: go.uber.org/atomic
version: 1ea20fb1cbb1cc08cbd0d913a96dead89aa18289
- name: go.uber.org/multierr
version: 3c4937480c32f4c13a875a1829af76c98ca3d40a
- name: go.uber.org/zap
version: 7e7e266a8dbce911a49554b945538c5b950196b8
subpackages:
- zapcore
- internal/bufferpool
- buffer
- internal/color
- internal/exit
- name: golang.org/x/crypto
version: a49355c7e3f8fe157a85be2f77e6e269a0f89602
subpackages:
- bcrypt
- blowfish
- name: golang.org/x/image
version: c73c2afc3b812cdd6385de5a50616511c4a3d458
subpackages:
- font
- math/fixed
- name: golang.org/x/net
version: d0887baf81f4598189d4e12a37c6da86f0bba4d0
subpackages:
- context
- http2
- trace
- http/httpguts
- http2/hpack
- idna
- internal/timeseries
- name: golang.org/x/sys
version: ac767d655b305d4e9612f5f6e33120b9176c4ad4
subpackages:
- unix
- name: golang.org/x/text
version: 0605a8320aceb4207a5fb3521281e17ec2075476
subpackages:
- secure/bidirule
- unicode/bidi
- unicode/norm
- transform
- name: google.golang.org/genproto
version: e92b116572682a5b432ddd840aeaba2a559eeff1
subpackages:
- googleapis/rpc/status
- name: google.golang.org/grpc
version: ce6ee6b031cb9e88a81e8d4d502d5b3eafb27f98
subpackages:
- credentials
- health
- health/grpc_health_v1
- balancer
- balancer/roundrobin
- codes
- connectivity