Skip to content

Commit

Permalink
Add required behaviors to behavior headers (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H authored Jul 26, 2023
1 parent e2faafa commit 20fe7af
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
50 changes: 50 additions & 0 deletions scripts/generate-extensions-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const args = require('minimist')(process.argv.slice(2));
/** @typedef {import('./types').ExtensionHeader} ExtensionHeader */
/** @typedef {import('./types').ExtensionWithFileInfo} ExtensionWithFileInfo */
/** @typedef {import('./types').ExtensionTier} ExtensionTier */
/** @typedef {import('./types').Extension} Extension */
/** @typedef {import('./types').EventsBasedBehavior} EventsBasedBehavior */

const extensionsBasePath = path.join(__dirname, '..', 'extensions');
const reviewedExtensionsTier = 'reviewed';
Expand Down Expand Up @@ -78,6 +80,50 @@ const readExtensionsFromFolder = async (folderPath, tier) => {
);
};

/**
* Find all required behaviors including transitive ones.
* @param {Extension} extension
* @param {EventsBasedBehavior} behavior
* @param {Array<string>} requiredBehaviorTypes
*/
const findAllRequiredBehaviorTypes = (
extension,
behavior,
requiredBehaviorTypes = []
) => {
for (const propertyDescriptor of behavior.propertyDescriptors) {
if (propertyDescriptor.type == 'Behavior') {
const requiredBehaviorType = propertyDescriptor.extraInformation[0];
const extensionPrefix = extension.name + '::';
if (!requiredBehaviorTypes.includes(requiredBehaviorType)) {
requiredBehaviorTypes.push(requiredBehaviorType);

if (requiredBehaviorType.startsWith(extensionPrefix)) {
const behaviorName = requiredBehaviorType.substring(
extensionPrefix.length
);
const requiredBehavior = extension.eventsBasedBehaviors.find(
(behavior) => behavior.name === behaviorName
);
if (!requiredBehavior) {
throw new Error(
'Required behavior: ' +
requiredBehaviorTypes +
' is missing in the extension.'
);
}
findAllRequiredBehaviorTypes(
extension,
requiredBehavior,
requiredBehaviorTypes
);
}
}
}
}
return requiredBehaviorTypes;
};

(async () => {
try {
shell.mkdir('-p', distBasePath);
Expand Down Expand Up @@ -217,6 +263,10 @@ const readExtensionsFromFolder = async (folderPath, tier) => {
fullName: behavior.fullName,
description: behavior.description,
objectType: behavior.objectType,
allRequiredBehaviorTypes: findAllRequiredBehaviorTypes(
extension,
behavior
),
}
)
.filter(Boolean)
Expand Down
2 changes: 1 addition & 1 deletion scripts/lib/rules/DotsInSentences.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @typedef {import("../../types").Extension} Extension */
/** @typedef {import("../../types").EventsFunction} EventsFunction */
/** @typedef {import("../../types").EventsBasedBehaviors} EventsBasedBehaviors */
/** @typedef {import("../../types").EventsBasedBehavior} EventsBasedBehaviors */
/** @typedef {import("../../types").Parameter} Parameter */
/** @typedef {import("./rule").ErrorLogger} ErrorLogger */

Expand Down
2 changes: 1 addition & 1 deletion scripts/lib/rules/FilledOutDescriptions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @typedef {import("../../types").Extension} Extension */
/** @typedef {import("../../types").EventsFunction} EventsFunction */
/** @typedef {import("../../types").EventsBasedBehaviors} EventsBasedBehaviors */
/** @typedef {import("../../types").EventsBasedBehavior} EventsBasedBehaviors */
/** @typedef {import("../../types").EventsBasedObjects} EventsBasedObjects */
/** @typedef {import("../../types").Parameter} Parameter */

Expand Down
14 changes: 12 additions & 2 deletions scripts/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export interface BehaviorShortHeader
extends RegistryItem,
BehaviorAndShortHeaderFields {
extensionName: string;
/**
* All required behaviors including transitive ones.
*/
allRequiredBehaviorTypes: Array<string>;
}

interface ObjectAndShortHeaderFields {
Expand Down Expand Up @@ -148,13 +152,19 @@ export interface EventsFunction {
objectGroups: string[];
}

export interface EventsBasedBehaviors {
export interface PropertyDescriptor {
type: 'Number' | 'String' | 'Boolean' | 'Choice' | 'Color' | 'Behavior';
extraInformation: string[];
}

export interface EventsBasedBehavior {
description: string;
fullName: string;
name: string;
objectType: string;
private?: boolean;
eventsFunctions: EventsFunction[];
propertyDescriptors: PropertyDescriptor[];
}

export interface EventsBasedObjects {
Expand All @@ -170,7 +180,7 @@ export interface Extension
ExtensionAndHeaderFields {
tags: string | string[];
eventsFunctions: EventsFunction[];
eventsBasedBehaviors: EventsBasedBehaviors[];
eventsBasedBehaviors: EventsBasedBehavior[];
eventsBasedObjects?: EventsBasedObjects[];
}

Expand Down

0 comments on commit 20fe7af

Please sign in to comment.