Skip to content

Commit

Permalink
Plugins: Use types on typescript for plugins
Browse files Browse the repository at this point in the history
* Create typescript types and parse the json string directly in ts-bindings
* Use files directly instead of directories for typescript types.
* Rust: Set render options in a box for parser
* Fix linting issues on typescript
  • Loading branch information
AmmarAbouZor committed Dec 16, 2024
1 parent 598a838 commit fa45a79
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub enum PluginState {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RenderOptions {
Parser(ParserRenderOptions),
Parser(Box<ParserRenderOptions>),
ByteSource,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl PluginParser {
Ok(PluginInfo {
version,
config_schemas,
render_options: RenderOptions::Parser(render_options),
render_options: RenderOptions::Parser(Box::new(render_options)),
})
}

Expand Down
34 changes: 18 additions & 16 deletions application/apps/rustcore/ts-bindings/src/api/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IFilter } from 'platform/types/filter';
import { ShellProfile } from 'platform/types/shells';
import { SomeipStatistic } from 'platform/types/observe/parser/someip';
import { StatisticInfo } from 'platform/types/observe/parser/dlt';
import { PluginEntity } from 'platform/types/plugins';

export class Jobs extends Base {
public static async create(): Promise<Jobs> {
Expand Down Expand Up @@ -247,16 +248,15 @@ export class Jobs extends Base {
return job;
}

//TODO AAZ: There is no type conversion currently.
//This first prototype should deliver the json values as string to
//show them in the UI
public getAllPlugins(): CancelablePromise<string> {
public getAllPlugins(): CancelablePromise<PluginEntity[]> {
const sequence = this.sequence();
const job: CancelablePromise<string> = this.execute(
(res: string): string | Error => {
return typeof res === 'string'
? res
: new Error(`getAllPlugins should return string while prototyping`);
const job: CancelablePromise<PluginEntity[]> = this.execute(
(pluginsJson: string): PluginEntity[] | Error => {
try {
return JSON.parse(pluginsJson);
} catch (e) {
return new Error(error(e));
}
},
this.native.getAllPlugins(sequence),
sequence,
Expand All @@ -265,13 +265,15 @@ export class Jobs extends Base {
return job;
}

public getActivePlugins(): CancelablePromise<string> {
public getActivePlugins(): CancelablePromise<PluginEntity[]> {
const sequence = this.sequence();
const job: CancelablePromise<string> = this.execute(
(res: string): string | Error => {
return typeof res === 'string'
? res
: new Error(`getActivePlugins should return string while prototyping`);
const job: CancelablePromise<PluginEntity[]> = this.execute(
(pluginsJson: string): PluginEntity[] | Error => {
try {
return JSON.parse(pluginsJson);
} catch (e) {
return new Error(error(e));
}
},
this.native.getActivePlugins(sequence),
sequence,
Expand All @@ -283,7 +285,7 @@ export class Jobs extends Base {
public reloadPlugins(): CancelablePromise<void> {
const sequence = this.sequence();
const job: CancelablePromise<void> = this.execute(
(res: void): void => {},
(): void => {},
this.native.reloadPlugins(sequence),
sequence,
'reloadPlugins',
Expand Down
9 changes: 5 additions & 4 deletions application/client/src/app/service/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@ import { SetupService, Interface, Implementation, register } from '@platform/ent
import { services } from '@register/services';

import * as Requests from '@platform/ipc/request/index';
import { PluginEntity } from '@platform/types/plugins';

@SetupService(services['plugins'])
export class Service extends Implementation {
public allPlugins(): Promise<string> {
public allPlugins(): Promise<PluginEntity[]> {
return new Promise((resolve, reject) => {
Requests.IpcRequest.send(
Requests.Plugins.ListAll.Response,
new Requests.Plugins.ListAll.Request(),
)
.then((response: Requests.Plugins.ListAll.Response) => {
resolve(response.pluginsJson);
resolve(response.plugins);
})
.catch(reject);
});
}

public activePlugins(): Promise<string> {
public activePlugins(): Promise<PluginEntity[]> {
return new Promise((reslove, reject) => {
Requests.IpcRequest.send(
Requests.Plugins.ListActive.Response,
new Requests.Plugins.ListActive.Request(),
)
.then((response: Requests.Plugins.ListActive.Response) => {
reslove(response.pluginsJson);
reslove(response.plugins);
})
.catch(reject);
});
Expand Down
8 changes: 3 additions & 5 deletions application/client/src/app/ui/tabs/plugins/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ export class PluginsManager extends ChangesDetector implements AfterContentInit
loadPlugins(): void {
this.ilc()
.services.system.plugins.allPlugins()
.then((pluginsJson) => {
const plugins = JSON.parse(pluginsJson);
.then((plugins) => {
const plugins_pretty = JSON.stringify(plugins, null, 2);

this.allPlugins = plugins_pretty;
Expand All @@ -52,9 +51,8 @@ export class PluginsManager extends ChangesDetector implements AfterContentInit

this.ilc()
.services.system.plugins.activePlugins()
.then((activePluginsJson) => {
const plugins = JSON.parse(activePluginsJson);
const plugins_pretty = JSON.stringify(plugins, null, 2);
.then((activePlugins) => {
const plugins_pretty = JSON.stringify(activePlugins, null, 2);
this.activePlugins = plugins_pretty;
this.detectChanges();
})
Expand Down
4 changes: 2 additions & 2 deletions application/holder/src/service/unbound/plugins/list_active.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const handler = Requests.InjectLogger<
return new CancelablePromise((reslove, reject) => {
unbound.jobs
.getActivePlugins()
.then((pluginsJson) => {
reslove(new Requests.Plugins.ListActive.Response({ pluginsJson }));
.then((plugins) => {
reslove(new Requests.Plugins.ListActive.Response({ plugins }));
})
.catch(reject);
});
Expand Down
4 changes: 2 additions & 2 deletions application/holder/src/service/unbound/plugins/list_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const handler = Requests.InjectLogger<
return new CancelablePromise((resolve, reject) => {
unbound.jobs
.getAllPlugins()
.then((pluginsJson) => {
resolve(new Requests.Plugins.ListAll.Response({ pluginsJson }));
.then((plugins) => {
resolve(new Requests.Plugins.ListAll.Response({ plugins }));
})
.catch(reject);
});
Expand Down
10 changes: 5 additions & 5 deletions application/platform/ipc/request/plugins/list_active.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { Define, Interface, SignatureRequirement } from '../declarations';

import * as validator from '../../../env/obj';

import { PluginEntity } from '../../../types/plugins';

@Define({ name: 'ListActivePluginsRequest' })
export class Request extends SignatureRequirement {}
export interface Request extends Interface {}

@Define({ name: 'ListActivePluginsResponse' })
export class Response extends SignatureRequirement {
public pluginsJson: string;
public error?: string;
public plugins: PluginEntity[];

constructor(input: { pluginsJson?: string; error?: string }) {
constructor(input: { plugins: PluginEntity[] }) {
super();
validator.isObject(input);
this.pluginsJson = validator.getAsNotEmptyStringOrAsUndefined(input, 'pluginsJson');
this.error = validator.getAsNotEmptyStringOrAsUndefined(input, 'error');
this.plugins = validator.getAsArray(input, 'plugins');
}
}

Expand Down
10 changes: 5 additions & 5 deletions application/platform/ipc/request/plugins/list_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { Define, Interface, SignatureRequirement } from '../declarations';

import * as validator from '../../../env/obj';

import { PluginEntity } from '../../../types/plugins';

@Define({ name: 'ListAllPluginsRequest' })
export class Request extends SignatureRequirement {}
export interface Request extends Interface {}

@Define({ name: 'ListAllPluginsRespond' })
export class Response extends SignatureRequirement {
public pluginsJson: string;
public error?: string;
public plugins: PluginEntity[];

constructor(input: { pluginsJson?: string; error?: string }) {
constructor(input: { plugins: PluginEntity[] }) {
super();
validator.isObject(input);
this.pluginsJson = validator.getAsNotEmptyStringOrAsUndefined(input, 'pluginsJson');
this.error = validator.getAsNotEmptyStringOrAsUndefined(input, 'error');
this.plugins = validator.getAsArray(input, 'plugins');
}
}

Expand Down
63 changes: 63 additions & 0 deletions application/platform/types/plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
export interface PluginEntity {
dir_path: string;
plugin_type: PluginType;
state: PluginState;
metadata: PluginMetadata | null;
}

export enum PluginType {
Parser = 'Parser',
ByteSource = 'ByteSource',
}

export type PluginState =
| { type: 'Active'; state: ActiveState }
| { type: 'Invalid'; state: InvalidState };

export interface ActiveState {
wasm_file_path: string;
api_version: Version;
plugin_version: Version;
config_schemas: ConfigSchema[];
render_options: RenderOptions;
}

export interface InvalidState {
error_msg: string;
}

export interface Version {
major: number;
minor: number;
patch: number;
}

export type ConfigSchemaType =
| { type: 'Boolean' }
| { type: 'Number' }
| { type: 'Float' }
| { type: 'Text' }
| { type: 'Path' }
| { type: 'Dropdown'; options: string[] };

export interface ConfigSchema {
id: string;
title: string;
description: string;
input_type: ConfigSchemaType;
}

export type RenderOptions =
| { type: PluginType.Parser; options: ParserRenderOptions }
| { type: PluginType.ByteSource };

export interface ParserRenderOptions {
Parser: {
headers: null;
};
}

export interface PluginMetadata {
name: string;
description: string;
}
2 changes: 0 additions & 2 deletions application/platform/types/plugins/entity.ts

This file was deleted.

2 changes: 0 additions & 2 deletions application/platform/types/plugins/index.ts

This file was deleted.

0 comments on commit fa45a79

Please sign in to comment.