Getting Started
Let’s start to create an HelloWorld extension for StarUML.
Create an extension
Section titled “Create an extension”First we need to create an extension folder. Open your extensions folder.
- MacOS:
~/Library/Application Support/StarUML/extensions/user - Windows:
C:\Users\<user>\AppData\Roaming\StarUML\extensions\user - Linux:
~/.config/StarUML/extensions/user
Create a new folder HelloWorld in your extensions folder. Each folder is an extension, and the extensions are loaded in the order of the folder names when StarUML is started.
Extension package layout
Section titled “Extension package layout”An extension may have following folder structure:
my-extension/├─ menus/├─ keymaps/├─ toolbox/├─ preferences/├─ quickedits/├─ metamodel.json├─ main.js└─ package.json/menusSee Menus./keymapsSee Keymaps./toolboxSee Toolbox./preferencesSee Preferences./quickedits,metamodel.json: Quick edits and metamodel extensions. The formats are the same as the built-in extensions.main.jspackage.json
The files are loaded in this order: metamodel.json, keymaps, menus, preferences, toolbox, quickedits, and then main.js. The JSON files in each folder are loaded in alphabetical order. A JSON file with a syntax error is skipped.
Create main.js
Section titled “Create main.js”Create main.js in the new extension folder. main.js is the entry point to be executed when StarUML is started. init() function will be called when the extension is loaded. init() is optional, not mandatory.
function init() { // ...}
exports.init = initmain.js is executed as a CommonJS module. In the module, you can use exports, module, __filename, __dirname, and the globals app, type and console.
- All scripts (extensions and Script Console) share one global context, so use local variables in your module instead of global variables.
- An error thrown in
init()is printed in Script Console, and the extension is regarded as loaded. - An error while evaluating the module (e.g. a syntax error) is printed in Script Console, and the extension is marked as Extension error in Extension Manager. The resources loaded before
main.js(menus, keymaps, …) remain loaded. - A script running longer than 30 seconds is stopped.
Application Context
Section titled “Application Context”To write an extension with Javascript, you need to use StarUML’s open APIs. You can use most of API functions via the application context object app. The app includes objects providing useful APIs for commands, preferences, factory, dialogs, etc. For the APIs available in extensions, see Scripting API.
Note
StarUML 8 runs extensions in an embedded JavaScript engine (QuickJS), not in Electron. Browser APIs (
window,document,alert, …) and Electron APIs are not available.
Add a command
Section titled “Add a command”A command is an execution unit which can be bound with a menu item as well as a keyboard shortcut. For more about command, see Commands.
In Hello World extension, we will add a command that showing “Hello, World!” message with id helloworld:show-message as bellow:
function handleShowMessage () { app.toast.info('Hello, world!')}
function init () { app.commands.register('helloworld:show-message', handleShowMessage, 'Show Message')}
exports.init = initYou can find and execute the command by Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
Add a menu item
Section titled “Add a menu item”We will add a menu item named Hello World under Tools menu. To add a menu item, we need to create a folder /menus and a menu JSON file helloworld.json in the folder.
staruml-helloworld/├─ menus/│ └─ helloworld.json└─ main.jsThe menu JSON file is as below:
{ "menu": [ { "id": "tools", "submenu": [ { "label": "Hello World", "id": "tool.helloworld", "command": "helloworld:show-message" } ] } ]}Add a shortcut
Section titled “Add a shortcut”We will also bind a keyboard shortcut Ctrl+Shift+H (Cmd+Shift+H for MacOS) to the command, so we need to create a folder /keymaps and a keymap JSON file helloworld.json in the folder.
staruml-helloworld/├─ menus/│ └─ helloworld.json├─ keymaps/│ └─ helloworld.json└─ main.jsThe keymap JSON file is as below:
{ "cmdctrl-shift-h": "helloworld:show-message"}Now when user selects the menu item or press Ctrl+Shift+H (Cmd+Shift+H for MacOS), the command helloworld:show-message will be executed to show a toast message “Hello, World!”.
Run and Debug
Section titled “Run and Debug”If you finished editing main.js, restart StarUML. StarUML 8 does not have the Debug > Reload menu, so you need to restart StarUML whenever you modified the extension.
It is useful to use Script Console to debug an extension. To open Script Console, select View > Script Console (Ctrl+Alt+J or Cmd+Alt+J). You can see all errors and logs (console.log) of extensions, and you can also execute any JavaScript code such as app.commands.execute('helloworld:show-message').
- An extension failed to load is marked as Extension error in Extension Manager (select Tools > Extension Manager and click Installed tab).
- To execute commands at startup, run StarUML with the environment variable
STARUML_COMMANDS(e.g.STARUML_COMMANDS=helloworld:show-message).
Define package.json
Section titled “Define package.json”If you consider to distribute your extension to other users, you need to create a package.json file containing metadata for the extension.
{ "name": "your_id.helloworld", "title": "HelloWorld", "description": "HelloWorld extension example.", "homepage": "https://github.com/staruml/staruml-helloworld", "version": "1.0.0", "keywords": ["example", "helloworld"], "author": { "name": "Minkyu Lee", "email": "niklaus.lee@gmail.com", "url": "https://github.com/niklauslee" }, "license": "MIT", "engines": { "staruml": ">=8.0.0" }}name: The extension ID. It should start with a lowercase letter or a digit, and contain only lowercase letters, digits,-,.and_. We recommend to use the same name as the extension folder.engines.staruml: The range of StarUML versions supported by the extension. The lowest version of the range should be 8.0.0 or later (e.g.^8.0.0,>=8.0.0). The extensions for StarUML 7 or earlier (e.g.>=3.0.0) are made for Electron (DOM, Node.js modules, …), so Extension Manager does not install them.
Restart StarUML and then check whether your extension is properly shown in Extension Manager or not. (select Tools > Extension Manager and click Installed tab).
Distribute your extension
Section titled “Distribute your extension”To allow other users to install your extension, there are several possible ways:
- Distribute as ZIP archive. Zip the extension folder
staruml-helloworldasstaruml-helloworld.zipand just unzip the file in other user’s the extensions folder explained above. - Distribute via Github URL. Users can install from Github URL. In Extension Manager, click Install From Url… and enter the Github URL (e.g.
https://github.com/staruml/staruml-helloworld) or the URL of a ZIP archive and press Install button. If the archive has only one top-level folder, the folder is installed as the extension. - Distribute via Extensions Registry. See Registering to Extension Registry.