From cd8afb141d6bb0e33ab41c9c9a0052c23ccae845 Mon Sep 17 00:00:00 2001 From: Hanzei <16541325+hanzei@users.noreply.github.com> Date: Mon, 18 Mar 2019 20:09:23 +0100 Subject: [PATCH] Allow plugins to include assets in the bundle (#28) --- Makefile | 6 ++++++ README.md | 20 ++++++++++++++++++++ assets/.gitkeep | 0 3 files changed, 26 insertions(+) create mode 100644 assets/.gitkeep diff --git a/Makefile b/Makefile index 326070d..9c3f70c 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,9 @@ NPM ?= $(shell command -v npm 2> /dev/null) CURL ?= $(shell command -v curl 2> /dev/null) MANIFEST_FILE ?= plugin.json +# You can include assets this directory into the bundle. This can be e.g. used to include profile pictures. +ASSETS_DIR ?= assets + # Verify environment, and define PLUGIN_ID, PLUGIN_VERSION, HAS_SERVER and HAS_WEBAPP as needed. include build/setup.mk @@ -94,6 +97,9 @@ 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_SERVER),) mkdir -p dist/$(PLUGIN_ID)/server/dist; cp -r server/dist/* dist/$(PLUGIN_ID)/server/dist/; diff --git a/README.md b/README.md index 2d381e4..7f04213 100644 --- a/README.md +++ b/README.md @@ -45,3 +45,23 @@ In production, deploy and upload your plugin via the [System Console](https://ab ### How do I make a server-only or web app-only plugin? Simply delete the `server` or `webapp` folders and remove the corresponding sections from `plugin.json`. The build scripts will skip the missing portions automatically. + +### How do I include assets in the plugin bundle? + +Place them into the `assets` directory. To use an asset at runtime, build the path to your asset and open as a regular file: + +```go +bundlePath, err := p.API.GetBundlePath() +if err != nil { + return errors.Wrap(err, "failed to get bundle path") +} + +profileImage, err := ioutil.ReadFile(filepath.Join(bundlePath, "assets", "profile_image.png")) +if err != nil { + return errors.Wrap(err, "failed to read profile image") +} + +if appErr := p.API.SetProfileImage(userID, profileImage); appErr != nil { + return errors.Wrap(err, "failed to set profile image") +} +``` \ No newline at end of file diff --git a/assets/.gitkeep b/assets/.gitkeep new file mode 100644 index 0000000..e69de29