Skip to content

Commit

Permalink
Fix and updated the plugin (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
saturninoabril authored May 27, 2020
1 parent 6817b74 commit a67a69e
Show file tree
Hide file tree
Showing 35 changed files with 9,319 additions and 4,447 deletions.
29 changes: 15 additions & 14 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@ orbs:

workflows:
version: 2
nightly:
triggers:
- schedule:
cron: "0 0 * * *"
filters:
branches:
only:
- master
jobs:
- plugin-ci/lint
- plugin-ci/test
- plugin-ci/build
ci:
jobs:
- plugin-ci/lint:
filters:
tags:
only: /^v.*/
- plugin-ci/test:
- plugin-ci/coverage:
filters:
tags:
only: /^v.*/
Expand All @@ -26,18 +38,7 @@ workflows:
context: plugin-ci
requires:
- plugin-ci/lint
- plugin-ci/test
- plugin-ci/build
- plugin-ci/deploy-release:
filters:
tags:
only: /^v.*/
branches:
ignore: /.*/
context: plugin-ci
requires:
- plugin-ci/lint
- plugin-ci/test
- plugin-ci/coverage
- plugin-ci/build
- plugin-ci/deploy-release-github:
filters:
Expand All @@ -48,5 +49,5 @@ workflows:
context: matterbuild-github-token
requires:
- plugin-ci/lint
- plugin-ci/test
- plugin-ci/coverage
- plugin-ci/build
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
dist
.vs.json
.vs.json

.DS_Store
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @saturninoabril
85 changes: 54 additions & 31 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ 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
Expand All @@ -14,6 +17,11 @@ include build/setup.mk

BUNDLE_NAME ?= $(PLUGIN_ID)-$(PLUGIN_VERSION).tar.gz

# Include custom makefile, if present
ifneq ($(wildcard build/custom.mk),)
include build/custom.mk
endif

## Checks the code style, tests, builds and bundles the plugin.
all: check-style test dist

Expand All @@ -36,7 +44,7 @@ endif
gofmt:
ifneq ($(HAS_SERVER),)
@echo Running gofmt
@for package in $$(go list ./server/...); do \
@for package in $$(go list ./...); do \
echo "Checking "$$package; \
files=$$(go list -f '{{range .GoFiles}}{{$$.Dir}}/{{.}} {{end}}' $$package); \
if [ "$$files" ]; then \
Expand All @@ -56,21 +64,29 @@ endif
govet:
ifneq ($(HAS_SERVER),)
@echo Running govet
@# Workaroung because you can't install binaries without adding them to go.mod
@# 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 ./server/...
$(GO) vet -vettool=$(GOPATH)/bin/shadow ./server/...
$(GO) vet ./...
$(GO) vet -vettool=$(GOPATH)/bin/shadow ./...
@echo Govet success
endif

## 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 -o dist/plugin-linux-amd64;
cd server && env GOOS=darwin GOARCH=amd64 $(GO) build -o dist/plugin-darwin-amd64;
cd server && env GOOS=windows GOARCH=amd64 $(GO) build -o dist/plugin-windows-amd64.exe;
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.
Expand All @@ -87,6 +103,14 @@ ifneq ($(HAS_WEBAPP),)
cd webapp && $(NPM) run build;
endif

## Builds the webapp in debug mode, if it exists.
.PHONY: webapp-debug
webapp-debug: webapp/.npminstall
ifneq ($(HAS_WEBAPP),)
cd webapp && \
$(NPM) run debug;
endif

## Generates a tar bundle of the plugin for install.
.PHONY: bundle
bundle:
Expand Down Expand Up @@ -116,64 +140,63 @@ endif
dist: apply server webapp bundle

## Installs the plugin to a (development) server.
## It uses the API if appropriate environment variables are defined,
## and otherwise falls back to trying to copy the plugin to a sibling mattermost-server directory.
.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 -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 -H "Authorization: Bearer $(TOKEN)" -X POST $(MM_SERVICESETTINGS_SITEURL)/api/v4/plugins -F "plugin=@dist/$(BUNDLE_NAME)" -F "force=true" > /dev/null && \
curl -s -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
./build/bin/deploy $(PLUGIN_ID) dist/$(BUNDLE_NAME)

.PHONY: debug-deploy
debug-deploy: debug-dist deploy

.PHONY: debug-dist
debug-dist: apply server webapp-debug bundle

## 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 -race -v ./server/...
$(GO) test -v $(GO_TEST_FLAGS) ./server/...
endif
ifneq ($(HAS_WEBAPP),)
cd webapp && $(NPM) run fix;
cd webapp && $(NPM) run fix && $(NPM) run test;
endif

## Creates a coverage report for the server code.
.PHONY: coverage
coverage: server/.depensure webapp/.npminstall
coverage: webapp/.npminstall
ifneq ($(HAS_SERVER),)
$(GO) test -race -coverprofile=server/coverage.txt ./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.
## Extract strings for translation from the source code.
.PHONY: i18n-extract
i18n-extract:
i18n-extract:
ifneq ($(HAS_WEBAPP),)
@[[ -d $(MM_UTILITIES_DIR) ]] || echo "You must clone github.com/mattermost/mattermost-utilities repo in .. to use this command"
@[[ -d $(MM_UTILITIES_DIR) ]] && cd $(MM_UTILITIES_DIR) && npm install && npm run babel && node mmjstool/build/index.js i18n extract-webapp --webapp-dir ../mattermost-plugin-demo/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/coverage.txt
rm -fr server/dist
endif
ifneq ($(HAS_WEBAPP),)
rm -fr webapp/.npminstall
rm -fr webapp/junit.xml
rm -fr webapp/dist
rm -fr webapp/node_modules
endif
rm -fr build/bin/

# Help documentatin à la https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
# Help documentation à 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
27 changes: 8 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
## Mattermost Autotranslation Plugin (beta) [![CircleCI](https://circleci.com/gh/mattermost/mattermost-plugin-autotranslate.svg?style=svg)](https://circleci.com/gh/mattermost/mattermost-plugin-autotranslate)

Autotranslation plugin for Mattermost.
**Maintainer:** [@saturninoabril](https://github.com/saturninoabril)

Message autotranslation is powered by Amazon Translate which supports translation between English and any of the following languages: Arabic, Chinese (Simplified), Chinese (Traditional), Czech, French, German, Italian, Japanese, Portuguese, Russian, Spanish, and Turkish.
### Autotranslation plugin for Mattermost.

Message autotranslation is powered by Amazon Translate which is a text translation service that uses advanced machine learning technologies to provide high-quality translation on demand. Amazon Translate can translate text between the languages listed in its [website](https://docs.aws.amazon.com/translate/latest/dg/what-is.html).

### Feature
* __Translate__ option available at dropdown menu of each regular post.
* __Slash commands__ to change user settings using `/autotranslate` slash command
* __Check user info__ by issuing `/autotranslate info` to see current user setting
* __Turn on/off__ translation by issuing `/autotranslate [on|off]`
* __Change source language__ translation by initiating `/autotranslate source [language code]` (see language codes below)
* __Change target language__ translation by initiating `/autotranslate target [language code]` (see language codes below)
* __Supported Languages and its codes__
* __auto__ : Automatic language detection
* __ar__ : Arabic
* __zh__ : Chinese
* __cs__ : Czech
* __fr__ : French
* __de__ : German
* __en__ : English
* __es__ : Spanish
* __it__ : Italian
* __ja__ : Japanese
* __pt__ : Portuguese
* __ru__ : Russian
* __tr__ : Turkish
* __Change source language__ translation by initiating `/autotranslate source [language code]`
* __Change target language__ translation by initiating `/autotranslate target [language code]`
* __Supported Languages and its codes__ can be found at [Amazon Translate website](https://docs.aws.amazon.com/translate/latest/dg/what-is.html).

### Installation

__Requires Mattermost 5.4 or higher__
__Requires Mattermost 5.22 or higher__

1. Install the plugin
1. Download the latest version of the plugin from the GitHub releases page
Expand Down
63 changes: 49 additions & 14 deletions build/manifest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,36 @@ import (
"github.com/pkg/errors"
)

const pluginIdGoFileTemplate = `package main
var manifest = struct {
Id string
Version string
}{
Id: "%s",
Version: "%s",
const pluginIDGoFileTemplate = `// This file is automatically generated. Do not modify it manually.
package main
import (
"strings"
"github.com/mattermost/mattermost-server/v5/model"
)
var manifest *model.Manifest
const manifestStr = ` + "`" + `
%s
` + "`" + `
func init() {
manifest = model.ManifestFromJson(strings.NewReader(manifestStr))
}
`

const pluginIdJsFileTemplate = `export const id = '%s';
export const version = '%s';
const pluginIDJSFileTemplate = `// This file is automatically generated. Do not modify it manually.
const manifest = JSON.parse(` + "`" + `
%s
` + "`" + `);
export default manifest;
export const id = manifest.id;
export const version = manifest.version;
`

func main() {
Expand All @@ -38,7 +55,7 @@ func main() {
cmd := os.Args[1]
switch cmd {
case "id":
dumpPluginId(manifest)
dumpPluginID(manifest)

case "version":
dumpPluginVersion(manifest)
Expand Down Expand Up @@ -87,7 +104,7 @@ func findManifest() (*model.Manifest, error) {
}

// dumpPluginId writes the plugin id from the given manifest to standard out
func dumpPluginId(manifest *model.Manifest) {
func dumpPluginID(manifest *model.Manifest) {
fmt.Printf("%s", manifest.Id)
}

Expand All @@ -99,19 +116,37 @@ func dumpPluginVersion(manifest *model.Manifest) {
// applyManifest propagates the plugin_id into the server and webapp folders, as necessary
func applyManifest(manifest *model.Manifest) error {
if manifest.HasServer() {
// generate JSON representation of Manifest.
manifestBytes, err := json.MarshalIndent(manifest, "", " ")
if err != nil {
return err
}
manifestStr := string(manifestBytes)

// write generated code to file by using Go file template.
if err := ioutil.WriteFile(
"server/manifest.go",
[]byte(fmt.Sprintf(pluginIdGoFileTemplate, manifest.Id, manifest.Version)),
[]byte(fmt.Sprintf(pluginIDGoFileTemplate, manifestStr)),
0644,
); err != nil {
return errors.Wrap(err, "failed to write server/manifest.go")
}
}

if manifest.HasWebapp() {
// generate JSON representation of Manifest.
// JSON is very similar and compatible with JS's object literals. so, what we do here
// is actually JS code generation.
manifestBytes, err := json.MarshalIndent(manifest, "", " ")
if err != nil {
return err
}
manifestStr := string(manifestBytes)

// write generated code to file by using JS file template.
if err := ioutil.WriteFile(
"webapp/src/manifest.js",
[]byte(fmt.Sprintf(pluginIdJsFileTemplate, manifest.Id, manifest.Version)),
[]byte(fmt.Sprintf(pluginIDJSFileTemplate, manifestStr)),
0644,
); err != nil {
return errors.Wrap(err, "failed to open webapp/src/manifest.js")
Expand Down
Loading

0 comments on commit a67a69e

Please sign in to comment.