Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
streamer45 committed Apr 10, 2020
1 parent edb0c4c commit 0554ae5
Show file tree
Hide file tree
Showing 20 changed files with 6,239 additions and 1,318 deletions.
39 changes: 21 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ include build/setup.mk

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

# Include custom makefile, if pressent
# Include custom makefile, if present
ifneq ($(wildcard build/custom.mk),)
include build/custom.mk
endif
Expand Down Expand Up @@ -119,6 +119,13 @@ ifneq ($(HAS_WEBAPP),)
cd webapp && $(NPM) run build;
endif

## Builds the webapp in debug mode, if it exists.
webapp-debug: mp3-wasm 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 @@ -148,27 +155,21 @@ 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 --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
./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
test: mp3-wasm webapp/.npminstall
ifneq ($(HAS_SERVER),)
$(GO) test -v $(GO_TEST_FLAGS) ./server/...
endif
Expand Down Expand Up @@ -203,16 +204,18 @@ clean:
rm -fr public/encoder.wasm
rm -rf public/recorder.worker.js
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
rm -rf webapp/src/client/recorder.js
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
122 changes: 122 additions & 0 deletions build/deploy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// main handles deployment of the plugin to a development server using either the Client4 API
// or by copying the plugin bundle into a sibling mattermost-server/plugin directory.
package main

import (
"fmt"
"log"
"os"
"path/filepath"

"github.com/mattermost/mattermost-server/v5/model"
"github.com/mholt/archiver/v3"
"github.com/pkg/errors"
)

func main() {
err := deploy()
if err != nil {
fmt.Printf("Failed to deploy: %s\n", err.Error())
fmt.Println()
fmt.Println("Usage:")
fmt.Println(" deploy <plugin id> <bundle path>")
os.Exit(1)
}
}

// deploy handles deployment of the plugin to a development server.
func deploy() error {
if len(os.Args) < 3 {
return errors.New("invalid number of arguments")
}

pluginID := os.Args[1]
bundlePath := os.Args[2]

siteURL := os.Getenv("MM_SERVICESETTINGS_SITEURL")
adminToken := os.Getenv("MM_ADMIN_TOKEN")
adminUsername := os.Getenv("MM_ADMIN_USERNAME")
adminPassword := os.Getenv("MM_ADMIN_PASSWORD")
copyTargetDirectory, _ := filepath.Abs("../mattermost-server")

if siteURL != "" {
client := model.NewAPIv4Client(siteURL)

if adminToken != "" {
log.Printf("Authenticating using token against %s.", siteURL)
client.SetToken(adminToken)

return uploadPlugin(client, pluginID, bundlePath)
}

if adminUsername != "" && adminPassword != "" {
client := model.NewAPIv4Client(siteURL)
log.Printf("Authenticating as %s against %s.", adminUsername, siteURL)
_, resp := client.Login(adminUsername, adminPassword)
if resp.Error != nil {
return errors.Wrapf(resp.Error, "failed to login as %s", adminUsername)
}

return uploadPlugin(client, pluginID, bundlePath)
}
}

_, err := os.Stat(copyTargetDirectory)
if os.IsNotExist(err) {
return errors.New("no supported deployment method available, please install plugin manually")
} else if err != nil {
return errors.Wrapf(err, "failed to stat %s", copyTargetDirectory)
}

log.Printf("Installing plugin to mattermost-server found in %s.", copyTargetDirectory)
log.Print("Server restart required to load updated plugin.")
return copyPlugin(pluginID, copyTargetDirectory, bundlePath)
}

// uploadPlugin attempts to upload and enable a plugin via the Client4 API.
// It will fail if plugin uploads are disabled.
func uploadPlugin(client *model.Client4, pluginID, bundlePath string) error {
pluginBundle, err := os.Open(bundlePath)
if err != nil {
return errors.Wrapf(err, "failed to open %s", bundlePath)
}
defer pluginBundle.Close()

log.Print("Uploading plugin via API.")
_, resp := client.UploadPluginForced(pluginBundle)
if resp.Error != nil {
return fmt.Errorf("Failed to upload plugin bundle: %s", resp.Error.Error())
}

log.Print("Enabling plugin.")
_, resp = client.EnablePlugin(pluginID)
if resp.Error != nil {
return fmt.Errorf("Failed to enable plugin: %s", resp.Error.Error())
}

return nil
}

// copyPlugin attempts to install a plugin by copying it to a sibling ../mattermost-server/plugin
// directory. A server restart is required before the plugin will start.
func copyPlugin(pluginID, targetPath, bundlePath string) error {
targetPath = filepath.Join(targetPath, "plugins")

err := os.MkdirAll(targetPath, 0777)
if err != nil {
return errors.Wrapf(err, "failed to create %s", targetPath)
}

existingPluginPath := filepath.Join(targetPath, pluginID)
err = os.RemoveAll(existingPluginPath)
if err != nil {
return errors.Wrapf(err, "failed to remove existing existing plugin directory %s", existingPluginPath)
}

err = archiver.Unarchive(bundlePath, targetPath)
if err != nil {
return errors.Wrapf(err, "failed to unarchive %s into %s", bundlePath, targetPath)
}

return nil
}
59 changes: 47 additions & 12 deletions build/manifest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,40 @@ import (
"io/ioutil"
"os"

"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/pkg/errors"
)

const pluginIDGoFileTemplate = `package main
const pluginIDGoFileTemplate = `// This file is automatically generated. Do not modify it manually.
var manifest = struct {
ID string
Version string
}{
ID: "%s",
Version: "%s",
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 Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions build/setup.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ endif
# Ensure that the build tools are compiled. Go's caching makes this quick.
$(shell cd build/manifest && $(GO) build -o ../bin/manifest)

# Ensure that the deployment tools are compiled. Go's caching makes this quick.
$(shell cd build/deploy && $(GO) build -o ../bin/deploy)

# Extract the plugin id from the manifest.
PLUGIN_ID ?= $(shell build/bin/manifest id)
ifeq ($(PLUGIN_ID),)
Expand Down
22 changes: 9 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
module github.com/streamer45/mattermost-plugin-voice
module github.com/mattermost/mattermost-plugin-starter-template

go 1.12

require (
github.com/blang/semver v3.6.1+incompatible
github.com/blang/semver v3.5.1+incompatible
github.com/go-ldap/ldap v3.0.3+incompatible // indirect
github.com/hashicorp/go-hclog v0.9.2 // indirect
github.com/hashicorp/go-plugin v1.0.1 // indirect
github.com/lib/pq v1.1.1 // indirect
github.com/mattermost/go-i18n v1.11.0 // indirect
github.com/mattermost/mattermost-server v5.12.0+incompatible
github.com/pelletier/go-toml v1.4.0 // indirect
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0
github.com/mattermost/mattermost-server v5.11.1+incompatible
github.com/mattermost/mattermost-server/v5 v5.20.0
github.com/mholt/archiver/v3 v3.3.0
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect
)

// Workaround for https://github.com/golang/go/issues/30831 and fallout.
replace github.com/golang/lint => github.com/golang/lint v0.0.0-20190227174305-8f45f776aaf1
Loading

0 comments on commit 0554ae5

Please sign in to comment.