Skip to content

Commit b84742b

Browse files
committed
更新 BI 插件,添加插件定义和运行时支持,修复插件加载逻辑
1 parent 8b56206 commit b84742b

File tree

4 files changed

+59
-30
lines changed

4 files changed

+59
-30
lines changed

examples/plugin-bi/src/index.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PluginDefinition, PluginContextData } from '@objectstack/spec';
12

23
export class BiEngine {
34
constructor() {
@@ -14,18 +15,25 @@ export class BiEngine {
1415
}
1516
}
1617

17-
/**
18-
* Plugin Lifecycle Hook
19-
* (Simulated interface)
20-
*/
21-
export async function onEnable(context: any) {
22-
console.log('[BI Plugin] Enabling BI Plugin...');
23-
24-
// Register Service
25-
const engine = new BiEngine();
26-
if (context.services) {
27-
context.services.register('bi.engine', engine);
18+
const plugin: PluginDefinition = {
19+
id: 'com.objectstack.bi',
20+
version: '1.0.0',
21+
22+
onEnable: async (context: PluginContextData) => {
23+
const logger = context.logger || console;
24+
logger.info('[BI Plugin] Enabling BI Plugin...');
25+
26+
// Register Service
27+
const engine = new BiEngine();
28+
29+
// Access runtime capabilities not in strict schema
30+
const runtime = context as any;
31+
if (runtime.services) {
32+
runtime.services.register('bi.engine', engine);
33+
}
34+
35+
logger.info('[BI Plugin] Services registered.');
2836
}
37+
};
2938

30-
console.log('[BI Plugin] Services registered.');
31-
}
39+
export default plugin;

examples/server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@objectstack/spec": "workspace:*",
1111
"@objectstack/example-crm": "workspace:*",
1212
"@objectstack/example-todo": "workspace:*",
13+
"@objectstack/plugin-bi": "workspace:*",
1314
"hono": "^4.0.0",
1415
"@hono/node-server": "^1.0.0",
1516
"zod": "^3.0.0"

examples/server/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ app.use('*', cors());
1515

1616
// 2. Load Plugins (CRM, Todo)
1717
console.log('--- Starting ObjectStack Server ---');
18-
loadPlugins();
18+
await loadPlugins();
1919
console.log('--- Plugins Loaded ---');
2020

2121
// 3. Define Unified Routes

examples/server/src/loader.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,66 @@
11
import { SchemaRegistry } from './kernel/registry';
22
import { AppSchema, ManifestSchema, App, ObjectStackManifest } from '@objectstack/spec';
33

4-
// In a real monorepo scenario, we might use path aliases or require.resolve
5-
// Here we use relative paths to demonstrate loading from the sibling packages
4+
// Import from packages
65
// @ts-ignore
7-
import CrmApp from '../../crm/objectstack.config';
6+
import CrmApp from '@objectstack/example-crm/objectstack.config';
87
// @ts-ignore
9-
import TodoApp from '../../todo/objectstack.config';
8+
import TodoApp from '@objectstack/example-todo/objectstack.config';
109
// @ts-ignore
11-
import BiPlugin from '../../plugin-bi/objectstack.config';
10+
import BiPluginManifest from '@objectstack/plugin-bi/objectstack.config';
11+
import BiPluginRuntime from '@objectstack/plugin-bi';
1212

13-
export function loadPlugins() {
14-
const packages: any[] = [CrmApp, TodoApp, BiPlugin];
13+
export async function loadPlugins() {
14+
const packages: any[] = [CrmApp, TodoApp, BiPluginManifest];
1515

1616
for (const pkg of packages) {
1717
if (!pkg) continue;
1818

1919
// Check if it's a Manifest (Plugin/Package)
2020
if (pkg.type === 'plugin') {
2121
const manifest = pkg as ObjectStackManifest;
22-
console.log(`[Loader] Loading Plugin: ${manifest.id}`);
22+
console.log(`[Loader] Loading Plugin Manifest: ${manifest.id}`);
2323

2424
try {
25-
const parsedPlugin = ManifestSchema.parse(manifest);
26-
SchemaRegistry.registerPlugin(parsedPlugin);
25+
const parsedManifest = ManifestSchema.parse(manifest);
26+
SchemaRegistry.registerPlugin(parsedManifest);
2727

28-
if (parsedPlugin.contributes?.kinds) {
29-
for (const kind of parsedPlugin.contributes.kinds) {
28+
// 1. Register Contributions (Static)
29+
if (parsedManifest.contributes?.kinds) {
30+
for (const kind of parsedManifest.contributes.kinds) {
3031
SchemaRegistry.registerKind(kind);
3132
}
3233
}
3334

34-
// SIMULATION: Simulate the scanner loading a file matching the new Kind
35-
// In a real system, this would be done by a file watcher detecting **/*.dataset.json
36-
if (parsedPlugin.id === 'com.objectstack.bi') {
35+
// 2. Load Runtime (Dynamic Simulation)
36+
// In a real engine, this would use `import(manifest.extensions.runtime.entry)`
37+
if (manifest.id === 'com.objectstack.bi' && BiPluginRuntime) {
38+
const pluginDef = BiPluginRuntime.default || BiPluginRuntime;
39+
40+
if (pluginDef.onEnable) {
41+
console.log(`[Loader] Executing Plugin Runtime: ${manifest.id}`);
42+
await pluginDef.onEnable({
43+
logger: console,
44+
os: {
45+
// Mock System API
46+
registerService: (id: string, svc: any) => console.log(`[OS] Service Registered: ${id}`)
47+
},
48+
ql: {}, // Mock ObjectQL
49+
services: {
50+
register: (id: string, svc: any) => console.log(`[Services] Registered ${id}`)
51+
}
52+
});
53+
}
54+
}
55+
56+
// 3. Simulate File Scanning (Mock)
57+
if (parsedManifest.id === 'com.objectstack.bi') {
3758
SchemaRegistry.registerItem('bi.dataset', {
3859
name: 'quarterly_sales',
3960
label: 'Quarterly Sales Data',
4061
source: 'sql_warehouse',
4162
query: 'SELECT * FROM sales WHERE quarter = "Q4"'
4263
}, 'name');
43-
console.log('[Loader] Simulated loading: quarterly_sales (bi.dataset)');
4464
}
4565
} catch (e) {
4666
console.error(`[Loader] Failed to load plugin ${manifest.id}`, e);

0 commit comments

Comments
 (0)