diff --git a/apps/1test/src/electron/engine.ts b/apps/1test/src/electron/engine.ts index e57122725b..a9d4631a6d 100644 --- a/apps/1test/src/electron/engine.ts +++ b/apps/1test/src/electron/engine.ts @@ -10,9 +10,11 @@ const gitPlugin = new GitPlugin() engine.register(appManager) engine.register(fsPlugin) engine.register(gitPlugin) -//appManager.activatePlugin('fs') -ipcMain.on('engine:activatePlugin', (event, arg) => { +ipcMain.handle('engine:activatePlugin', async (event, arg) => { console.log('engine:activatePlugin', arg) - appManager.activatePlugin(arg) + if(await appManager.isActive(arg)){ + return true + } + return await appManager.activatePlugin(arg) }) diff --git a/apps/1test/src/electron/fsPlugin.ts b/apps/1test/src/electron/fsPlugin.ts index 7b3cc133ea..6098eb0a89 100644 --- a/apps/1test/src/electron/fsPlugin.ts +++ b/apps/1test/src/electron/fsPlugin.ts @@ -1,10 +1,9 @@ import { PluginClient } from "@remixproject/plugin"; -import { createClient } from "./electronPluginClient" +import { createClient } from "./lib/electronPluginClient" import { Engine, PluginManager, Plugin } from '@remixproject/engine'; import fs from 'fs/promises' import { Stats } from "fs"; - const profile = { displayName: 'fs', name: 'fs', @@ -21,6 +20,7 @@ export class FSPlugin extends Plugin { console.log('fsPlugin onActivation') this.client = new FSPluginClient() } + } class FSPluginClient extends PluginClient { diff --git a/apps/1test/src/electron/gitPlugin.ts b/apps/1test/src/electron/gitPlugin.ts index 36c51d1038..ea4ab2510a 100644 --- a/apps/1test/src/electron/gitPlugin.ts +++ b/apps/1test/src/electron/gitPlugin.ts @@ -2,7 +2,7 @@ import { Plugin } from "@remixproject/engine"; import { PluginClient } from "@remixproject/plugin"; import { Profile } from "@remixproject/plugin-utils"; import { spawn } from "child_process"; -import { createClient } from "./electronPluginClient"; +import { createClient } from "./lib/electronPluginClient"; const profile: Profile = { name: 'git', @@ -17,9 +17,9 @@ export class GitPlugin extends Plugin { } onActivation(): void { - console.log('GitPlugin onActivation') this.client = new GitPluginClient() } + } class GitPluginClient extends PluginClient { diff --git a/apps/1test/src/electron/electronPluginClient.ts b/apps/1test/src/electron/lib/electronPluginClient.ts similarity index 77% rename from apps/1test/src/electron/electronPluginClient.ts rename to apps/1test/src/electron/lib/electronPluginClient.ts index 96b74b5c82..1df3c35c53 100644 --- a/apps/1test/src/electron/electronPluginClient.ts +++ b/apps/1test/src/electron/lib/electronPluginClient.ts @@ -2,26 +2,22 @@ import { ClientConnector, connectClient, applyApi, Client, PluginClient } from ' import type { Message, Api, ApiMap } from '@remixproject/plugin-utils' import { IRemixApi } from '@remixproject/plugin-api' import { ipcMain } from 'electron' -import { mainWindow } from '..' +import { mainWindow } from '../..' export class ElectronPluginClientConnector implements ClientConnector { constructor(public IPCName: string) { - console.log('ElectronPluginClientConnector constructor', IPCName) } /** Send a message to the engine */ send(message: Partial) { - console.log('ElectronPluginConnector send', message) mainWindow.webContents.send(this.IPCName + ':send', message) } /** Listen to message from the engine */ on(cb: (message: Partial) => void) { - console.log('ElectronPluginConnector on', cb) ipcMain.on(this.IPCName + ':on', (event, message) => { - console.log('ElectronPluginConnector on message received', message) cb(message) }) } diff --git a/apps/1test/src/global.d.ts b/apps/1test/src/global.d.ts index 7cafe0862a..01eb0b442b 100644 --- a/apps/1test/src/global.d.ts +++ b/apps/1test/src/global.d.ts @@ -1,5 +1,5 @@ export interface IElectronAPI { - activatePlugin: (name: string) => void + activatePlugin: (name: string) => Promise receiveFromFS: (cb: any) => void sendToFS: (message: Partial) => void receiveFromGit: (cb: any) => void diff --git a/apps/1test/src/preload.ts b/apps/1test/src/preload.ts index e66ec2ef79..079b2f7ef9 100644 --- a/apps/1test/src/preload.ts +++ b/apps/1test/src/preload.ts @@ -6,11 +6,10 @@ console.log('preload.ts') contextBridge.exposeInMainWorld('api', { activatePlugin: (name: string) => { - console.log('activatePlugin', name) - ipcRenderer.send('engine:activatePlugin', name) + return ipcRenderer.invoke('engine:activatePlugin', name) }, receiveFromFS: (cb:any) => ipcRenderer.on('fs:send', cb), - sendToFS: (message: Partial) => ipcRenderer.send('fs:on', message), + sendToFS: (message: Partial) => ipcRenderer.send('fs:on', message), receiveFromGit: (cb:any) => ipcRenderer.on('git:send', cb), sendToGit: (message: Partial) => ipcRenderer.send('git:on', message) }) \ No newline at end of file diff --git a/apps/1test/src/remix/fsPlugin.ts b/apps/1test/src/remix/fsPlugin.ts new file mode 100644 index 0000000000..43f778ec12 --- /dev/null +++ b/apps/1test/src/remix/fsPlugin.ts @@ -0,0 +1,19 @@ +import { Engine, PluginManager, Plugin, PluginConnector } from '@remixproject/engine'; +import { Message, Profile } from '@remixproject/plugin-utils'; +import { ElectronPluginConnector } from './lib/electronPluginConnector'; + +export class fsPlugin extends ElectronPluginConnector { + + constructor(){ + super({ + displayName: 'fs', + name: 'fs', + description: 'fs', + }, { + sendAPI: window.api.sendToFS, + receiveAPI: window.api.receiveFromFS + }) + this.methods = ['readdir', 'readFile', 'writeFile', 'mkdir', 'rmdir', 'unlink', 'rename', 'stat', 'exists'] + } + +} \ No newline at end of file diff --git a/apps/1test/src/remix/gitPlugin.ts b/apps/1test/src/remix/gitPlugin.ts new file mode 100644 index 0000000000..d8e2c08964 --- /dev/null +++ b/apps/1test/src/remix/gitPlugin.ts @@ -0,0 +1,17 @@ +import { Engine, PluginManager, Plugin, PluginConnector } from '@remixproject/engine'; +import { Message, Profile } from '@remixproject/plugin-utils'; +import { ElectronPluginConnector } from './lib/electronPluginConnector'; + +export class gitPlugin extends ElectronPluginConnector { + constructor(){ + super({ + displayName: 'git', + name: 'git', + description: 'git', + },{ + sendAPI: window.api.sendToGit, + receiveAPI: window.api.receiveFromGit + }) + this.methods = ['log', 'status', 'add', 'commit', 'push', 'pull', 'clone', 'checkout', 'branch', 'merge', 'reset', 'revert', 'diff', 'stash', 'apply', 'cherryPick', 'rebase', 'tag', 'fetch', 'remote', 'config', 'show', 'init', 'help', 'version'] + } +} \ No newline at end of file diff --git a/apps/1test/src/remix/lib/electronPluginConnector.ts b/apps/1test/src/remix/lib/electronPluginConnector.ts index 9d0f6b11bb..54bdef652b 100644 --- a/apps/1test/src/remix/lib/electronPluginConnector.ts +++ b/apps/1test/src/remix/lib/electronPluginConnector.ts @@ -2,8 +2,9 @@ import type { ExternalProfile, Profile, Message, PluginOptions } from '@remixpro import { Plugin } from '@remixproject/engine'; -export interface PluginConnectorOptions extends PluginOptions { - engine?:string +export interface ElectronPluginConnectorOptions extends PluginOptions { + sendAPI?: (message: Partial) => void + receiveAPI?: (cb: (event:any, message: Partial) => void) => void } @@ -11,24 +12,40 @@ export abstract class ElectronPluginConnector extends Plugin { protected loaded: boolean protected id = 0 protected pendingRequest: Record void> = {} - protected options: PluginConnectorOptions + protected options: ElectronPluginConnectorOptions profile: Profile - constructor(profile: Profile) { + constructor(profile: Profile, options: ElectronPluginConnectorOptions = {}) { super(profile) + this.loaded = false + if(!options.sendAPI || !options.receiveAPI) throw new Error('ElectronPluginConnector requires sendAPI and receiveAPI') + this.options = options + + options.receiveAPI((event: any, message: any) => { + this.getMessage(message) + }) } /** * Send a message to the external plugin * @param message the message passed to the plugin */ - protected abstract send(message: Partial): void + protected send(message: Partial): void { + if(this.loaded) + this.options.sendAPI(message) + } /** * Open connection with the plugin - * @param url The transformed url the plugin should connect to + * @param name The name of the plugin should connect to */ - protected abstract connect(name: string): any | Promise + protected async connect(name: string) { + if(await window.api.activatePlugin(name) && !this.loaded){ + this.handshake() + } + } /** Close connection with the plugin */ - protected abstract disconnect(): any | Promise + protected disconnect(): any | Promise { + + } async activate() { await this.connect(this.profile.name) @@ -42,7 +59,7 @@ export abstract class ElectronPluginConnector extends Plugin { } /** Set options for an external plugin */ - setOptions(options: Partial = {}) { + setOptions(options: Partial = {}) { super.setOptions(options) } @@ -61,27 +78,23 @@ export abstract class ElectronPluginConnector extends Plugin { /** Perform handshake with the client if not loaded yet */ protected async handshake() { - console.log('ElectronPluginConnector handshake', this.loaded) if (!this.loaded) { this.loaded = true let methods: string[]; try { - console.log('ElectronPluginConnector handshake calling plugin method') - methods = await this.callPluginMethod('handshake', [this.profile.name, this.options?.engine]) - console.log('ElectronPluginConnector handshake methods', methods) + methods = await this.callPluginMethod('handshake', [this.profile.name]) } catch (err) { - console.error('ElectronPluginConnector handshake error', err) this.loaded = false throw err; } + this.emit('loaded', this.name) if (methods) { this.profile.methods = methods this.call('manager', 'updateProfile', this.profile) } } else { // If there is a broken connection we want send back the handshake to the plugin client - console.log('ElectronPluginConnector handshake already loaded') - return this.callPluginMethod('handshake', [this.profile.name, this.options?.engine]) + return this.callPluginMethod('handshake', [this.profile.name]) } } @@ -90,7 +103,6 @@ export abstract class ElectronPluginConnector extends Plugin { * @param message The message sent by the client */ protected async getMessage(message: Message) { - console.log('ElectronPluginConnector getMessage', message) // Check for handshake request from the client if (message.action === 'request' && message.key === 'handshake') { return this.handshake() diff --git a/apps/1test/src/remix/lib/fsPlugin.ts b/apps/1test/src/remix/lib/fsPlugin.ts deleted file mode 100644 index 660e7c7c2b..0000000000 --- a/apps/1test/src/remix/lib/fsPlugin.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Engine, PluginManager, Plugin, PluginConnector } from '@remixproject/engine'; -import { Message, Profile } from '@remixproject/plugin-utils'; -import { ElectronPluginConnector } from './electronPluginConnector'; - -export class fsPlugin extends ElectronPluginConnector { - constructor(){ - super({ - displayName: 'fs', - name: 'fs', - description: 'fs', - }) - } - - onActivation(): void { - console.log('fsPlugin onActivation') - //window.api.activatePlugin('fs') - } - - protected connect(name: string) { - console.log('fsPlugin connect', name) - window.api.activatePlugin(name) - window.api.receiveFromFS((event: any, message: any) => { - console.log('fsPlugin fsClientSend message received', message) - this.getMessage(message) - }) - } - - protected send(message: Partial): void { - console.log('fsPlugin send', message) - window.api.sendToFS(message) - } - - protected disconnect() { - console.log('fsPlugin disconnect') - } -} \ No newline at end of file diff --git a/apps/1test/src/remix/lib/gitPlugin.ts b/apps/1test/src/remix/lib/gitPlugin.ts deleted file mode 100644 index 08a2f05105..0000000000 --- a/apps/1test/src/remix/lib/gitPlugin.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Engine, PluginManager, Plugin, PluginConnector } from '@remixproject/engine'; -import { Message, Profile } from '@remixproject/plugin-utils'; -import { ElectronPluginConnector } from './electronPluginConnector'; - -export class gitPlugin extends ElectronPluginConnector { - constructor(){ - super({ - displayName: 'git', - name: 'git', - description: 'git', - }) - } - - onActivation(): void { - console.log('git onActivation') - //window.api.activatePlugin('fs') - } - - protected connect(name: string) { - console.log('git connect', name) - window.api.activatePlugin(name) - window.api.receiveFromGit((event: any, message: any) => { - console.log('git fsClientSend message received', message) - this.getMessage(message) - }) - } - - protected send(message: Partial): void { - console.log('git send', message) - window.api.sendToGit(message) - } - - protected disconnect() { - console.log('git disconnect') - } -} \ No newline at end of file diff --git a/apps/1test/src/renderer.ts b/apps/1test/src/renderer.ts index 015e0ffea2..d06b9c1ffc 100644 --- a/apps/1test/src/renderer.ts +++ b/apps/1test/src/renderer.ts @@ -1,13 +1,26 @@ -import { Engine, PluginManager, Plugin, PluginConnector } from '@remixproject/engine'; -import { Profile } from '@remixproject/plugin-utils'; -import { fsPlugin } from './remix/lib/fsPlugin'; -import { gitPlugin } from './remix/lib/gitPlugin'; +import { Engine, PluginManager } from '@remixproject/engine'; +import { fsPlugin } from './remix/fsPlugin'; +import { gitPlugin } from './remix/gitPlugin'; +class MyAppManager extends PluginManager { + onActivation(): void { + this.on('fs', 'loaded', async () => { + const files = await this.call('fs', 'readdir', './src') + console.log('files', files) + }) + + this.on('git', 'loaded', async () => { + const log = await this.call('git', 'log', './src') + console.log('log', log) + }) + } +} + const engine = new Engine() -const appManager = new PluginManager() +const appManager = new MyAppManager() const fs = new fsPlugin() const git = new gitPlugin() engine.register(appManager) @@ -16,10 +29,9 @@ engine.register(git) appManager.activatePlugin('fs') appManager.activatePlugin('git') + setTimeout(async () => { - const files = await appManager.call('fs', 'readdir', './') + const files = await appManager.call('fs', 'readdir', './src') console.log('files', files) - const log = await appManager.call('git', 'log', './') - console.log('log', log) -}, 5000) +}, 1000)