Skip to content

Commit

Permalink
allow specifying skill subscriptions in file-system hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispatrick committed Oct 17, 2023
1 parent 9e0c45d commit b2ebebc
Showing 1 changed file with 56 additions and 21 deletions.
77 changes: 56 additions & 21 deletions lib/events/register_skill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const handler: MappingEventHandler<

skill.version = await version(ctx, skill);
skill.apiVersion = apiVersion(ctx);
await inlineDatalogResources(p, skill);
await inlineDatalogResources(p, skill, skillYaml.skill);
if (isContainer(ctx)) {
await createArtifact(skill, ctx, registry);
} else {
Expand Down Expand Up @@ -377,27 +377,36 @@ export function fullImageName(
}${digest ? `@${digest}` : ""}`;
}

export async function inlineDatalogResources(
type DatalogSubscription = {
name: string;
query: string;
limit?: number;
}

async function getDatalogSubscriptionFileMatches(
p: project.Project,
skill: AtomistSkillInput,
): Promise<void> {
const datalogSubscriptions = [];
datalogSubscriptions.push(
...(await project.withGlobMatches<{
name: string;
query: string;
limit?: number;
}>(p, "datalog/subscription/*.edn", async file => {
const filePath = p.path(file);
const fileName = path.basename(filePath);
const extName = path.extname(fileName);
return {
query: (await fs.readFile(filePath)).toString(),
name: fileName.replace(extName, ""),
};
})),
);
(skill.datalogSubscriptions || []).forEach(d => {
matchPath: string
): Promise<Array<DatalogSubscription>> {
return (await project.withGlobMatches<{
name: string;
query: string;
limit?: number;
}>(p, matchPath, async file => {
const filePath = p.path(file);
const fileName = path.basename(filePath);
const extName = path.extname(fileName);
return {
query: (await fs.readFile(filePath)).toString(),
name: fileName.replace(extName, ""),
};
}))
}

function updateSubscriptions(
datalogSubscriptions: Array<DatalogSubscription>,
updates: Array<DatalogSubscription>
): void {
updates.forEach(d => {
const eds = datalogSubscriptions.find(ds => d.name === ds.name);
if (eds) {
eds.query = d.query;
Expand All @@ -406,6 +415,32 @@ export async function inlineDatalogResources(
datalogSubscriptions.push(d);
}
});
}

export async function inlineDatalogResources(
p: project.Project,
skill: AtomistSkillInput,
skillYaml: any,
): Promise<void> {
const datalogSubscriptions = new Array<DatalogSubscription>();

// common subscriptions
datalogSubscriptions.push(
...(await getDatalogSubscriptionFileMatches(p, "datalog/subscription/*.edn")),
);

// subscription groups
for (const subscriptionGroup of (skillYaml.datalogSubscriptionGroups || [])) {
const groupMatches = await getDatalogSubscriptionFileMatches(p, `datalog/subscription/${subscriptionGroup}/*.edn`)
updateSubscriptions(datalogSubscriptions, groupMatches)
}

// skill-specific subscriptions on-disk
const skillMatches = await getDatalogSubscriptionFileMatches(p, `datalog/subscription/${skill.name}/*.edn`)
updateSubscriptions(datalogSubscriptions, skillMatches)

// subscriptions defined in yaml
updateSubscriptions(datalogSubscriptions, skill.datalogSubscriptions || [])
skill.datalogSubscriptions = datalogSubscriptions;

const schemata = [...(skill.schemata || [])];
Expand Down

0 comments on commit b2ebebc

Please sign in to comment.