使用Theia——创建扩展包
创建Theia扩展包
本例中,我们将添加一个菜单项“Say hello”用来显示一个通知“Hello world!”。本文将指导你完成所有必要的步骤。
Theia的架构
Theia应用程序由所谓的扩展包(extensions)构成。一个扩展包提供一组特定功能的小部件、命令和处理程序等。Theia本身提供了一些扩展包,如编辑器、终端、项目视图等。每一个扩展包都属于它们各自的npm包。
必要条件
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash nvm install 10
以及yarn:
npm install -g yarn
还需要确保已安装python 2.x,可通过python --version来检查。
项目结构
npm install -g yo generator-theia-extension mkdir theia-hello-world-extension cd theia-hello-world-extension yo theia-extension hello-world-extension
我们来看下生成的代码。根目录下的package.json文件中定义了workspaces、依赖项lerna以及一些用来给浏览器或electron构件本地包的scripts。
{ "private": true, "scripts": { "prepare": "lerna run prepare", "rebuild:browser": "theia rebuild:browser", "rebuild:electron": "theia rebuild:electron" }, "devDependencies": { "lerna": "2.4.0" }, "workspaces": [ "hello-world-extension", "browser-app", "electron-app" ] }
同时也生成了lerna.json文件,用来配置lerna:
{ "lerna": "2.4.0", "version": "0.1.0", "useWorkspaces": true, "npmClient": "yarn", "command": { "run": { "stream": true } } }
实现扩展包
{ "name": "hello-world-extension", "keywords": [ "theia-extension" ], "version": "0.1.0", "files": [ "lib", "src" ], "dependencies": { "@theia/core": "latest" }, "devDependencies": { "rimraf": "latest", "typescript": "latest" }, "scripts": { "prepare": "yarn run clean && yarn run build", "clean": "rimraf lib", "build": "tsc", "watch": "tsc -w" }, "theiaExtensions": [ { "frontend": "lib/browser/hello-world-frontend-module" } ] }
export default new ContainerModule(bind => { // add your contribution bindings here bind(CommandContribution).to(HelloWorldCommandContribution); bind(MenuContribution).to(HelloWorldMenuContribution); });
Command被定义为一种简单的数据结构,其中包含id和label。id中包含了在command contribution中注册的处理程序,用来实现command的功能。代码生成器已经添加了一个command和一个处理程序,用于显示一个“Hello World!”的消息。
export const HelloWorldCommand = { id: 'HelloWorld.command', label: "Shows a message" }; @injectable() export class HelloWorldCommandContribution implements CommandContribution { constructor( @inject(MessageService) private readonly messageService: MessageService, ) { } registerCommands(registry: CommandRegistry): void { registry.registerCommand(HelloWorldCommand, { execute: () => this.messageService.info('Hello World!') }); } } ...
... @injectable() export class HelloWorldMenuContribution implements MenuContribution { registerMenus(menus: MenuModelRegistry): void { menus.registerMenuAction(CommonMenus.EDIT_FIND, { commandId: HelloWorldCommand.id, label: 'Say Hello' }); } }
在浏览器中运行扩展包
现在来看看我们的扩展包是如何工作的。为此,生成器在browser-app目录中创建了一个package.json文件,定义了一个含有几个扩展包的Theia浏览器应用程序,其中包含了我们的hello-world-extension。目录中剩余的其它文件都是由yarn在构建过程中通过theia-cli工具自动生成的,如scripts部分中所定义的。
{ "name": "browser-app", "version": "0.1.0", "dependencies": { "@theia/core": "latest", "@theia/filesystem": "latest", "@theia/workspace": "latest", "@theia/preferences": "latest", "@theia/navigator": "latest", "@theia/process": "latest", "@theia/terminal": "latest", "@theia/editor": "latest", "@theia/languages": "latest", "@theia/markers": "latest", "@theia/monaco": "latest", "@theia/typescript": "latest", "@theia/messages": "latest", "hello-world-extension": "0.1.0" }, "devDependencies": { "@theia/cli": "latest" }, "scripts": { "prepare": "theia build", "start": "theia start", "watch": "theia build --watch" }, "theia": { "target": "browser" } }
现在所有构建和运行应用程序需要的部分都准备好了,要运行它,输入下面的命令:
cd browser-app
yarn start <path to workspace>
然后在浏览器中输入http://localhost:3000,在打开的应用程序中选择Edit>Say Hello,你将会看到 “Hello World!”的消息弹出。
在Electron中运行扩展包
{ "name": "electron-app", ... "theia": { "target": "electron" } }
在运行electron应用程序之前,你需要重新构建一些本地的模块:
yarn rebuild:electron cd electron-app yarn start <path to workspace>
部署扩展包
如果你想公开你的扩展包,我们建议你将它发布到npm。你可以通过在扩展包的目录中调用yarn publish来完成,当然前提是你需要一个有效的账户。