Skip to content

Commit 3c56fed

Browse files
committed
Cleaned up plugin structure + added temporary plugin logging + added Plugin plan call
1 parent 51156d2 commit 3c56fed

21 files changed

Lines changed: 325 additions & 57 deletions

File tree

codify-core/src/commands/plan/orchestrator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ export const PlanOrchestrator = {
99
const resourceDefinitions = await pluginCollection.getAllResourceDefinitions();
1010

1111
await ConfigCompiler.analyzeProject(project, resourceDefinitions);
12+
await pluginCollection.getPlan(project);
1213

13-
console.log(project.coreModule.configBlocks);
14+
// console.log(project.coreModule.configBlocks);
1415

1516
await pluginCollection.destroy();
1617
},

codify-core/src/config-compiler/parser/entities/resource.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class ResourceConfig implements ConfigBlock {
2323

2424
type: string;
2525
name?: string;
26+
pluginName?: string;
2627
parameters: Record<string, unknown>;
2728
dependencies: { match: string; parameterName: string; parameterValue: string; resource: ResourceConfig }[] = [];
2829

codify-core/src/config-compiler/semantic-analysis/config-semantic-analyzer.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const ConfigSemanticAnalyzer = {
2525
throw new Error(`Invalid resource type specified ${type}. Type is not found in any plugins`);
2626
}
2727

28-
const { parameters: parameterDefinitions } = definition;
28+
const { parameters: parameterDefinitions, pluginName } = definition;
2929
for (const [key, value] of Object.entries(parameters)) {
3030

3131
if (!parameterDefinitions.has(key)) {
@@ -42,6 +42,8 @@ export const ConfigSemanticAnalyzer = {
4242
throw new Error(`Invalid resource config ${type}. Allowed values are ${parameter.allowedValues} but ${value} was provided`)
4343
}
4444
}
45+
46+
configBlock.pluginName = pluginName;
4547
}
4648
},
4749

@@ -58,8 +60,8 @@ export const ConfigSemanticAnalyzer = {
5860
.map(([name, value]) => [name, String(value), String(value).matchAll(resourceReferenceRegex)] as const)
5961
.filter(([, _, match]) => match)
6062
.flatMap(([name, value, matches]) =>
61-
[...matches].map(match => [name, value, match[1]] as const
62-
));
63+
[...matches].map(match => [name, value, match[1]] as const)
64+
);
6365

6466
for (const [name, value, match] of referenceParameters) {
6567
const parts = match.split('.');
@@ -68,7 +70,7 @@ export const ConfigSemanticAnalyzer = {
6870
}
6971

7072
if (!resourceMap.has(parts[0])) {
71-
throw new Error(`Un-able to find resource being referenced. ${match}`);
73+
throw new Error(`Unable to find resource being referenced. ${match}`);
7274
}
7375

7476
// TODO: Check for circular dependencies

codify-core/src/entities/resource-definition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type ResourceDefinitions = Map<Name, ResourceDefinition>
2222
export class ResourceDefinition {
2323
name!: string;
2424
parameters!: Map<Name, ResourceParameterDefinition>;
25+
pluginName!: string;
2526

2627
constructor(props: RemoveMethods<ResourceDefinition>) {
2728
Object.assign(this, props);
@@ -36,7 +37,7 @@ export class ResourceDefinition {
3637
})
3738

3839
const parametersMap = new Map(entries);
39-
return new ResourceDefinition({ name: `${pluginName}_${json.name}`, parameters: parametersMap })
40+
return new ResourceDefinition({ name: `${pluginName}_${json.name}`, parameters: parametersMap, pluginName })
4041
}
4142

4243
throw new InternalError('Unable to parse resource definition');

codify-core/src/plugins/entities/plugin.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ResourceConfig } from '../../config-compiler/parser/entities/resource';
12
import { PluginIpcBridge } from '../ipc-bridge';
23
import { PluginData } from './plugin-data';
34

@@ -20,6 +21,11 @@ export class Plugin {
2021
return new Plugin(PluginData.create(directory, name, resourceDefinitions), ipcBridge);
2122
}
2223

24+
async generateResourcePlan(resource: ResourceConfig): Promise<unknown> {
25+
const plan = await this.ipcBridge.sendMessageForResult({ cmd: 'generateResourcePlan', data: resource });
26+
return plan;
27+
}
28+
2329
destroy() {
2430
this.ipcBridge.killPlugin();
2531
}

codify-core/src/plugins/ipc-bridge.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export class PluginIpcBridge {
2323
{ execArgv: ['-r', 'ts-node/register'], silent: true },
2424
);
2525

26+
process.stdout!.on('data', (message) => console.log(message.toString()));
27+
process.stderr!.on('data', (message) => console.log(message.toString()));
28+
29+
2630
return new PluginIpcBridge(process);
2731
}
2832

codify-core/src/plugins/plugin-collection.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { ParsedProject } from '../config-compiler/parser/entities';
22
import { ResourceDefinition } from '../entities/resource-definition';
33
import { Plugin } from './entities/plugin';
44
import { PluginResolver } from './resolver';
5+
import { ConfigBlockType } from '../config-compiler/language-definition';
6+
import { ResourceConfig } from '../config-compiler/parser/entities/resource';
57

68
type PluginName = string;
79

@@ -54,6 +56,28 @@ export class PluginCollection {
5456
return result;
5557
}
5658

59+
async getPlan(project: ParsedProject): Promise<void> {
60+
for (const config of project.coreModule.configBlocks) {
61+
if (config.configType !== ConfigBlockType.RESOURCE) {
62+
continue;
63+
}
64+
65+
const { pluginName } = (config as ResourceConfig);
66+
if (!pluginName) {
67+
continue;
68+
}
69+
70+
const plugin = this.plugins.get(pluginName);
71+
if (!plugin) {
72+
continue;
73+
}
74+
75+
// eslint-disable-next-line no-await-in-loop
76+
const result = await plugin.generateResourcePlan(config as ResourceConfig);
77+
console.log(result);
78+
}
79+
}
80+
5781
async destroy(): Promise<void> {
5882
for (const plugin of this.plugins.values()) {
5983
plugin.destroy();

plugins/homebrew/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"name": "homebrew",
33
"version": "1.0.0",
44
"description": "",
5-
"main": "index.js",
5+
"main": "src/index.ts",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "ts-node .",
89
"build": "tsc"
910
},
1011
"devDependencies": {

plugins/homebrew/resource-definitions/homebrew_installation.json renamed to plugins/homebrew/resource-definitions/main.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "installation",
2+
"name": "main",
33
"parameters": {
44
"directory": "string"
55
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Plugin } from "../library/entities/plugin";
2+
import {HomebrewMainResource} from "./resources";
3+
import {HomebrewOptionsResource} from "./resources/options";
4+
5+
export class HomebrewPlugin extends Plugin {
6+
name = 'homebrew'
7+
resources = [
8+
new HomebrewMainResource(),
9+
new HomebrewOptionsResource(),
10+
]
11+
}

0 commit comments

Comments
 (0)