From 8cf303c943d0f191a87d1a5c14bb9e72124ab46a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0lker=20G=C3=B6ktu=C4=9F=20=C3=96zt=C3=BCrk?= Date: Sat, 21 Dec 2019 01:01:40 +0300 Subject: [PATCH] build/manifest: print whole manifest variable as it is (#73) while generating manifest files for both JS and Go sources. fixes #72. --- build/manifest/main.go | 51 +++++++++++++++++++++++++++++-------- go.sum | 2 +- server/manifest.go | 42 +++++++++++++++++++++++++----- webapp/src/index.js | 4 +-- webapp/src/manifest.js | 31 ++++++++++++++++++++-- webapp/src/manifest.test.js | 9 ++++++- 6 files changed, 117 insertions(+), 22 deletions(-) diff --git a/build/manifest/main.go b/build/manifest/main.go index 5db5aaa..a5416d1 100644 --- a/build/manifest/main.go +++ b/build/manifest/main.go @@ -14,19 +14,32 @@ const pluginIDGoFileTemplate = `// This file is automatically generated. Do not package main -var manifest = struct { - ID string - Version string -}{ - ID: "%s", - Version: "%s", +import ( + "strings" + + "github.com/mattermost/mattermost-server/model" +) + +var manifest *model.Manifest + +const manifestStr = ` + "`" + ` +%s +` + "`" + ` + +func init() { + manifest = model.ManifestFromJson(strings.NewReader(manifestStr)) } ` const pluginIDJSFileTemplate = `// This file is automatically generated. Do not modify it manually. -export const id = '%s'; -export const version = '%s'; +const manifest = JSON.parse(` + "`" + ` +%s +` + "`" + `); + +export default manifest; +export const id = manifest.id; +export const version = manifest.version; ` func main() { @@ -103,9 +116,17 @@ 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") @@ -113,9 +134,19 @@ func applyManifest(manifest *model.Manifest) error { } 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") diff --git a/go.sum b/go.sum index 37931b2..ff98531 100644 --- a/go.sum +++ b/go.sum @@ -111,6 +111,7 @@ github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= @@ -219,7 +220,6 @@ github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOq github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q= github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= -github.com/mholt/archiver v3.1.1+incompatible h1:1dCVxuqs0dJseYEhi5pl7MYPH9zDa1wBi7mF09cbNkU= github.com/mholt/archiver/v3 v3.3.0 h1:vWjhY8SQp5yzM9P6OJ/eZEkmi3UAbRrxCq48MxjAzig= github.com/mholt/archiver/v3 v3.3.0/go.mod h1:YnQtqsp+94Rwd0D/rk5cnLrxusUBUXg+08Ebtr1Mqao= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= diff --git a/server/manifest.go b/server/manifest.go index 471775b..0f06783 100644 --- a/server/manifest.go +++ b/server/manifest.go @@ -2,10 +2,40 @@ package main -var manifest = struct { - ID string - Version string -}{ - ID: "com.mattermost.plugin-starter-template", - Version: "0.1.0", +import ( + "strings" + + "github.com/mattermost/mattermost-server/model" +) + +var manifest *model.Manifest + +const manifestStr = ` +{ + "id": "com.mattermost.plugin-starter-template", + "name": "Plugin Starter Template", + "description": "This plugin serves as a starting point for writing a Mattermost plugin.", + "version": "0.1.0", + "min_server_version": "5.12.0", + "server": { + "executables": { + "linux-amd64": "server/dist/plugin-linux-amd64", + "darwin-amd64": "server/dist/plugin-darwin-amd64", + "windows-amd64": "server/dist/plugin-windows-amd64.exe" + }, + "executable": "" + }, + "webapp": { + "bundle_path": "webapp/dist/main.js" + }, + "settings_schema": { + "header": "", + "footer": "", + "settings": [] + } +} +` + +func init() { + manifest = model.ManifestFromJson(strings.NewReader(manifestStr)) } diff --git a/webapp/src/index.js b/webapp/src/index.js index 59a5581..a79327a 100644 --- a/webapp/src/index.js +++ b/webapp/src/index.js @@ -1,4 +1,4 @@ -import {id as pluginId} from './manifest'; +import manifest from './manifest'; export default class Plugin { // eslint-disable-next-line no-unused-vars @@ -7,4 +7,4 @@ export default class Plugin { } } -window.registerPlugin(pluginId, new Plugin()); +window.registerPlugin(manifest.id, new Plugin()); diff --git a/webapp/src/manifest.js b/webapp/src/manifest.js index 52d1e5b..ae6fdfc 100644 --- a/webapp/src/manifest.js +++ b/webapp/src/manifest.js @@ -1,4 +1,31 @@ // This file is automatically generated. Do not modify it manually. -export const id = 'com.mattermost.plugin-starter-template'; -export const version = '0.1.0'; +const manifest = JSON.parse(` +{ + "id": "com.mattermost.plugin-starter-template", + "name": "Plugin Starter Template", + "description": "This plugin serves as a starting point for writing a Mattermost plugin.", + "version": "0.1.0", + "min_server_version": "5.12.0", + "server": { + "executables": { + "linux-amd64": "server/dist/plugin-linux-amd64", + "darwin-amd64": "server/dist/plugin-darwin-amd64", + "windows-amd64": "server/dist/plugin-windows-amd64.exe" + }, + "executable": "" + }, + "webapp": { + "bundle_path": "webapp/dist/main.js" + }, + "settings_schema": { + "header": "", + "footer": "", + "settings": [] + } +} +`); + +export default manifest; +export const id = manifest.id; +export const version = manifest.version; diff --git a/webapp/src/manifest.test.js b/webapp/src/manifest.test.js index 0abde8e..9774e3f 100644 --- a/webapp/src/manifest.test.js +++ b/webapp/src/manifest.test.js @@ -1,5 +1,12 @@ -import {id, version} from './manifest'; +import manifest, {id, version} from './manifest'; +test('Plugin manifest, id and version are defined', () => { + expect(manifest).toBeDefined(); + expect(manifest.id).toBeDefined(); + expect(manifest.version).toBeDefined(); +}); + +// To ease migration, verify separate export of id and version. test('Plugin id and version are defined', () => { expect(id).toBeDefined(); expect(version).toBeDefined();