Skip to content

Commit aed0781

Browse files
committed
feat: add requires resource setting (2.1.0)
Resources can declare types that must exist (ex: homebrew, xcode-tools on macOS). Unlike dependencies, which only order resources already in the config, the CLI adds a default config for missing required types. Sent in the initialize response. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016QnBk1brshJrHGEkhMFR29
1 parent 166a163 commit aed0781

9 files changed

Lines changed: 44 additions & 4 deletions

File tree

‎CLAUDE.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ Resources are configured via `ResourceSettings<T>` returned by `getSettings()`:
135135
schema, resolves equality/filtering/sensitive paths). Nothing outside that file should special case stateful,
136136
array or nested parameters.
137137
- `isSensitive`: Marks resource as sensitive (prevents auto-import, hides values)
138-
- `dependencies`: Resource IDs this resource depends on
138+
- `dependencies`: Resource IDs this resource depends on (soft: only orders resources present in the config)
139+
- `requires`: Resource IDs that must exist (ex: `homebrew`, `xcode-tools` on macOS). The CLI adds a default config for
140+
missing ones. Sent in the initialize response (not yet in the `@codifycli/schemas` `ResourceDefinition` type)
139141
- `canDestroy`: Whether resource can be destroyed (default: true)
140142

141143
## Implementation Notes

‎package-lock.json‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codifycli/plugin-core",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "TypeScript library for building Codify plugins to manage system resources (applications, CLI tools, settings) through infrastructure-as-code",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

‎src/plugin/plugin.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,4 +646,25 @@ describe('Plugin tests', () => {
646646
expect(() => Plugin.create('testPlugin', [self])).toThrowError(/supersededBy/);
647647
})
648648
})
649+
describe('requires', () => {
650+
it('Returns requires in the resource definitions (empty when not set)', async () => {
651+
const needsBrew = new class extends TestResource {
652+
getSettings(): ResourceSettings<TestConfig> {
653+
return { id: 'needs-brew', operatingSystems: [OS.Darwin], requires: ['homebrew'] };
654+
}
655+
}
656+
657+
const standalone = new class extends TestResource {
658+
getSettings(): ResourceSettings<TestConfig> {
659+
return { id: 'standalone', operatingSystems: [OS.Darwin] };
660+
}
661+
}
662+
663+
const testPlugin = Plugin.create('testPlugin', [needsBrew, standalone]);
664+
const { resourceDefinitions } = await testPlugin.initialize({});
665+
666+
expect((resourceDefinitions.find((d) => d.type === 'needs-brew') as any)?.requires).toEqual(['homebrew']);
667+
expect((resourceDefinitions.find((d) => d.type === 'standalone') as any)?.requires).toEqual([]);
668+
})
669+
})
649670
});

‎src/plugin/plugin.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ export class Plugin {
9090

9191
return {
9292
dependencies: r.dependencies,
93+
// Not in the @codifycli/schemas ResourceDefinition type yet. Older CLIs ignore it (the schema allows extra keys).
94+
requires: r.requires,
9395
type: r.typeId,
9496
sensitiveParameters,
9597
operatingSystems: r.settings.operatingSystems,

‎src/resource/parsed-resource-settings.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class ParsedResourceSettings<T extends StringIndexedObject> implements Om
3030

3131
removeStatefulParametersBeforeDestroy?: boolean | undefined;
3232
dependencies?: string[] | undefined;
33+
requires?: string[] | undefined;
3334
transformation?: InputTransformation;
3435

3536
operatingSystems!: Array<OS>;

‎src/resource/resource-controller.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export class ResourceController<T extends StringIndexedObject> {
2424

2525
readonly typeId: string;
2626
readonly dependencies: string[];
27+
readonly requires: string[];
2728

2829
protected ajv?: Ajv;
2930
protected schemaValidator?: ValidateFunction;
@@ -36,6 +37,7 @@ export class ResourceController<T extends StringIndexedObject> {
3637

3738
this.typeId = this.settings.id;
3839
this.dependencies = this.settings.dependencies ?? [];
40+
this.requires = this.settings.requires ?? [];
3941
this.parsedSettings = new ParsedResourceSettings<T>(this.settings);
4042

4143
if (this.parsedSettings.schema) {

‎src/resource/resource-settings.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ export interface ResourceSettings<T extends StringIndexedObject> {
109109
*/
110110
dependencies?: string[];
111111

112+
/**
113+
* Type ids of resources that must exist for this resource to work (ex: `homebrew` for a resource installed with brew,
114+
* `xcode-tools` for a resource that runs git, make or a compiler on macOS). Unlike {@link dependencies}, which only
115+
* orders resources that are already in the config, the CLI adds a default config for any required type that is
116+
* missing. Required resources are always applied first.
117+
*
118+
* Evaluated on the machine the plugin runs on, so OS specific requirements can be expressed with
119+
* `Utils.isMacOS() ? ['xcode-tools'] : []`. Defaults to `[]` (nothing is required).
120+
*/
121+
requires?: string[];
122+
112123
/**
113124
* Options for configuring parameters operations including overriding the equals function, adding default values,
114125
* applying input transformations, marking values sensitive and attaching stateful parameters.

‎src/resource/resource.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export abstract class Resource<T extends StringIndexedObject> {
3030
* - removeStatefulParametersBeforeDestory: Call the delete methods of stateful parameters before destorying
3131
* the base resource. Defaults to false.
3232
* - dependencies: Specify the ids of any resources that this resource depends on
33+
* - requires: Specify the ids of resources that must exist. The CLI adds them if they are missing from the config
3334
* - parameterSettings: Parameter specific settings. Use this to define custom equals functions, default values
3435
* and input transformations
3536
* - inputTransformation: Transform the input value.

0 commit comments

Comments
 (0)