Allow plugins to include assets in the bundle (#28)

This commit is contained in:
Hanzei 2019-03-18 20:09:23 +01:00 committed by GitHub
parent 81c67a92f3
commit cd8afb141d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View file

@ -4,6 +4,9 @@ NPM ?= $(shell command -v npm 2> /dev/null)
CURL ?= $(shell command -v curl 2> /dev/null) CURL ?= $(shell command -v curl 2> /dev/null)
MANIFEST_FILE ?= plugin.json 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. # Verify environment, and define PLUGIN_ID, PLUGIN_VERSION, HAS_SERVER and HAS_WEBAPP as needed.
include build/setup.mk include build/setup.mk
@ -94,6 +97,9 @@ bundle:
rm -rf dist/ rm -rf dist/
mkdir -p dist/$(PLUGIN_ID) mkdir -p dist/$(PLUGIN_ID)
cp $(MANIFEST_FILE) dist/$(PLUGIN_ID)/ cp $(MANIFEST_FILE) dist/$(PLUGIN_ID)/
ifneq ($(wildcard $(ASSETS_DIR)/.),)
cp -r $(ASSETS_DIR) dist/$(PLUGIN_ID)/
endif
ifneq ($(HAS_SERVER),) ifneq ($(HAS_SERVER),)
mkdir -p dist/$(PLUGIN_ID)/server/dist; mkdir -p dist/$(PLUGIN_ID)/server/dist;
cp -r server/dist/* dist/$(PLUGIN_ID)/server/dist/; cp -r server/dist/* dist/$(PLUGIN_ID)/server/dist/;

View file

@ -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? ### 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. 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")
}
```

0
assets/.gitkeep Normal file
View file