From 0b5fac54ef9e199189189576efcd9828d2da9458 Mon Sep 17 00:00:00 2001 From: HaseenaSainul Date: Fri, 20 Oct 2023 10:44:18 -0400 Subject: [PATCH] additionalDeclarationTemplates combined to additionalMethodTemplates --- languages/cpp/language.config.json | 2 +- .../cpp/templates/modules/include/module.h | 2 +- .../cpp/templates/modules/src/module_impl.h | 2 +- languages/javascript/language.config.json | 3 +- .../templates/codeblocks/module.mjs | 4 +- src/macrofier/engine.mjs | 82 ++++++++----------- src/macrofier/index.mjs | 2 - src/sdk/index.mjs | 1 - 8 files changed, 41 insertions(+), 57 deletions(-) diff --git a/languages/cpp/language.config.json b/languages/cpp/language.config.json index 786bf191..debc539d 100644 --- a/languages/cpp/language.config.json +++ b/languages/cpp/language.config.json @@ -29,5 +29,5 @@ }, "langVersion" : "c++17", "additionalSchemaTemplates": [ "json-types" ], - "additionalDeclarationTemplates": [ "declarations-override" ] + "additionalMethodTemplates": [ "declarations", "declarations-override" ] } diff --git a/languages/cpp/templates/modules/include/module.h b/languages/cpp/templates/modules/include/module.h index 6ee534e4..de58e77a 100644 --- a/languages/cpp/templates/modules/include/module.h +++ b/languages/cpp/templates/modules/include/module.h @@ -36,7 +36,7 @@ namespace ${info.Title} { virtual ~I${info.Title}() = default; // Methods & Events - /* ${DECLARATIONS} */ + /* ${METHODS:declarations} */ };${end.if.methods} diff --git a/languages/cpp/templates/modules/src/module_impl.h b/languages/cpp/templates/modules/src/module_impl.h index 8051139e..33e6e005 100644 --- a/languages/cpp/templates/modules/src/module_impl.h +++ b/languages/cpp/templates/modules/src/module_impl.h @@ -40,7 +40,7 @@ namespace ${info.Title} { ~${info.Title}Impl() override = default; // Methods & Events - /* ${DECLARATIONS:declarations-override} */ + /* ${METHODS:declarations-override} */ };${end.if.methods} }//namespace ${info.Title} diff --git a/languages/javascript/language.config.json b/languages/javascript/language.config.json index 4044bffc..529ac99a 100644 --- a/languages/javascript/language.config.json +++ b/languages/javascript/language.config.json @@ -23,5 +23,6 @@ "number": "number", "string": "string", "object": "object" - } + }, + "additionalMethodTemplates": [ "declarations" ] } \ No newline at end of file diff --git a/languages/javascript/templates/codeblocks/module.mjs b/languages/javascript/templates/codeblocks/module.mjs index cb5ca256..def593af 100644 --- a/languages/javascript/templates/codeblocks/module.mjs +++ b/languages/javascript/templates/codeblocks/module.mjs @@ -4,8 +4,8 @@ ${if.events} type Event = ${events} ${end.if.events} /* ${SCHEMAS} */ - /* ${DECLARATIONS} */ + /* ${METHODS:declarations} */ /* ${PROVIDERS} */ -} \ No newline at end of file +} diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index a0bbab03..b1d2dd89 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -495,7 +495,6 @@ const generateMacros = (obj, templates, languages, options = {}) => { schemas: {}, types: {}, enums: {}, - declarations: {}, methods: {}, events: {}, methodList: '', @@ -518,21 +517,23 @@ const generateMacros = (obj, templates, languages, options = {}) => { const examples = generateExamples(obj, templates, languages) const allMethodsArray = generateMethods(obj, examples, templates) - Array.from(new Set(['declarations'].concat(config.additionalDeclarationTemplates))).filter(dir => dir).forEach(dir => { - const declarationsArray = allMethodsArray.filter(m => m.declaration[dir] && (!config.excludeDeclarations || (!options.hideExcluded || !m.excluded))) - macros.declarations[dir] = declarationsArray.length ? getTemplate('/sections/declarations', templates).replace(/\$\{declaration\.list\}/g, declarationsArray.map(m => m.declaration[dir]).join('\n')) : '' - }) - Array.from(new Set(['methods'].concat(config.additionalMethodTemplates))).filter(dir => dir).forEach(dir => { - const methodsArray = allMethodsArray.filter(m => m.body[dir] && !m.event && (!options.hideExcluded || !m.excluded)) - macros.methods[dir] = methodsArray.length ? getTemplate('/sections/methods', templates).replace(/\$\{method.list\}/g, methodsArray.map(m => m.body[dir]).join('\n')) : '' - const eventsArray = allMethodsArray.filter(m => m.body[dir] && m.event && (!options.hideExcluded || !m.excluded)) - macros.events[dir] = eventsArray.length ? getTemplate('/sections/events', templates).replace(/\$\{event.list\}/g, eventsArray.map(m => m.body[dir]).join('\n')) : '' + if (dir.includes('declarations')) { + const declarationsArray = allMethodsArray.filter(m => m.declaration[dir] && (!config.excludeDeclarations || (!options.hideExcluded || !m.excluded))) + macros.methods[dir] = declarationsArray.length ? getTemplate('/sections/declarations', templates).replace(/\$\{declaration\.list\}/g, declarationsArray.map(m => m.declaration[dir]).join('\n')) : '' + } + else if (dir.includes('methods')) { + const methodsArray = allMethodsArray.filter(m => m.body[dir] && !m.event && (!options.hideExcluded || !m.excluded)) + macros.methods[dir] = methodsArray.length ? getTemplate('/sections/methods', templates).replace(/\$\{method.list\}/g, methodsArray.map(m => m.body[dir]).join('\n')) : '' + + const eventsArray = allMethodsArray.filter(m => m.body[dir] && m.event && (!options.hideExcluded || !m.excluded)) + macros.events[dir] = eventsArray.length ? getTemplate('/sections/events', templates).replace(/\$\{event.list\}/g, eventsArray.map(m => m.body[dir]).join('\n')) : '' - if (dir === 'methods') { - macros.methodList = methodsArray.filter(m => m.body).map(m => m.name) - macros.eventList = eventsArray.map(m => makeEventName(m)) + if (dir === 'methods') { + macros.methodList = methodsArray.filter(m => m.body).map(m => m.name) + macros.eventList = eventsArray.map(m => makeEventName(m)) + } } }) @@ -594,9 +595,9 @@ const insertMacros = (fContents = '', macros = {}) => { fContents = fContents.replace(/\$\{if\.types\}(.*?)\$\{end\.if\.types\}/gms, macros.types.types.trim() ? '$1' : '') fContents = fContents.replace(/\$\{if\.schemas\}(.*?)\$\{end\.if\.schemas\}/gms, macros.schemas.types.trim() ? '$1' : '') fContents = fContents.replace(/\$\{if\.enums\}(.*?)\$\{end\.if\.enums\}/gms, macros.enums.types.trim() ? '$1' : '') - fContents = fContents.replace(/\$\{if\.declarations\}(.*?)\$\{end\.if\.declarations\}/gms, (macros.declarations.declarations.trim() || macros.enums.types.trim()) || macros.types.types.trim()? '$1' : '') + fContents = fContents.replace(/\$\{if\.declarations\}(.*?)\$\{end\.if\.declarations\}/gms, (macros.methods.declarations && macros.methods.declarations.trim() || macros.enums.types.trim()) || macros.types.types.trim()? '$1' : '') - fContents = fContents.replace(/\$\{if\.methods\}(.*?)\$\{end\.if\.methods\}/gms, (macros.methods.methods.trim() || macros.events.methods.trim()) ? '$1' : '') + fContents = fContents.replace(/\$\{if\.methods\}(.*?)\$\{end\.if\.methods\}/gms, macros.methods.methods.trim() || macros.events.methods.trim() ? '$1' : '') fContents = fContents.replace(/\$\{if\.implementations\}(.*?)\$\{end\.if\.implementations\}/gms, (macros.methods.methods.trim() || macros.events.methods.trim() || macros.schemas.types.trim()) ? '$1' : '') fContents = fContents.replace(/\$\{module\.list\}/g, macros.module) @@ -606,16 +607,6 @@ const insertMacros = (fContents = '', macros = {}) => { fContents = fContents.replace(/\$\{if\.modules\}(.*?)\$\{end\.if\.modules\}/gms, (macros.methods.methods.trim() || macros.events.methods.trim()) ? '$1' : '') - // Output the originally supported non-configurable declarations macros - fContents = fContents.replace(/[ \t]*\/\* \$\{DECLARATIONS\} \*\/[ \t]*\n/, macros.declarations.declarations) - // Output all declarations with all dynamically configured templates - Array.from(new Set(['declarations'].concat(config.additionalDeclarationTemplates))).filter(dir => dir).forEach(dir => { - ['DECLARATIONS'].forEach(type => { - const regex = new RegExp('[ \\t]*\\/\\* \\$\\{' + type + '\\:' + dir + '\\} \\*\\/[ \\t]*\\n', 'g') - fContents = fContents.replace(regex, macros[type.toLowerCase()][dir]) - }) - }) - // Output the originally supported non-configurable methods & events macros fContents = fContents.replace(/[ \t]*\/\* \$\{METHODS\} \*\/[ \t]*\n/, macros.methods.methods) fContents = fContents.replace(/[ \t]*\/\* \$\{METHOD_LIST\} \*\/[ \t]*\n/, macros.methodList.join(',\n')) @@ -623,7 +614,7 @@ const insertMacros = (fContents = '', macros = {}) => { fContents = fContents.replace(/[ \t]*\/\* \$\{EVENT_LIST\} \*\/[ \t]*\n/, macros.eventList.join(',')) fContents = fContents.replace(/[ \t]*\/\* \$\{EVENTS_ENUM\} \*\/[ \t]*\n/, macros.eventsEnum) - // Output all methods & events with all dynamically configured templates + // Output all declarations, methods & events with all dynamically configured templates Array.from(new Set(['methods'].concat(config.additionalMethodTemplates))).filter(dir => dir).forEach(dir => { ['METHODS', 'EVENTS'].forEach(type => { const regex = new RegExp('[ \\t]*\\/\\* \\$\\{' + type + '\\:' + dir + '\\} \\*\\/[ \\t]*\\n', 'g') @@ -1105,17 +1096,15 @@ function generateMethodResult(type, templates) { declaration: {}, } - Array.from(new Set(['declarations'].concat(config.additionalDeclarationTemplates))).filter(dir => dir).forEach(dir => { - const template = getTemplate(('/' + dir + '/' + type), templates) - if (template) { - result.declaration[dir] = template - } - }) - Array.from(new Set(['methods'].concat(config.additionalMethodTemplates))).filter(dir => dir).forEach(dir => { const template = getTemplate(('/' + dir + '/' + type), templates) if (template) { - result.body[dir] = template + if (dir.includes('declarations')) { + result.declaration[dir] = template + } + else if (dir.includes('methods')) { + result.body[dir] = template + } } }) return result @@ -1137,21 +1126,19 @@ function generateMethods(json = {}, examples = {}, templates = {}) { event: isEventMethod(methodObj) } - // Generate declarations for both dynamic and static configured templates - Array.from(new Set(['declarations'].concat(config.additionalDeclarationTemplates))).filter(dir => dir).forEach(dir => { - const template = getTemplateForDeclaration(methodObj, templates, dir) - if (template && template.length) { - let javascript = insertMethodMacros(template, methodObj, json, templates, examples) - result.declaration[dir] = javascript - } - }) - // Generate implementation of methods/events for both dynamic and static configured templates Array.from(new Set(['methods'].concat(config.additionalMethodTemplates))).filter(dir => dir).forEach(dir => { - const template = getTemplateForMethod(methodObj, templates, dir) - if (template && template.length) { - let javascript = insertMethodMacros(template, methodObj, json, templates, examples) - result.body[dir] = javascript + if (dir.includes('declarations')) { + const template = getTemplateForDeclaration(methodObj, templates, dir) + if (template && template.length) { + result.declaration[dir] = insertMethodMacros(template, methodObj, json, templates, examples) + } + } + else if (dir.includes('methods')) { + const template = getTemplateForMethod(methodObj, templates, dir) + if (template && template.length) { + result.body[dir] = insertMethodMacros(template, methodObj, json, templates, examples) + } } }) @@ -1173,7 +1160,6 @@ function generateMethods(json = {}, examples = {}, templates = {}) { } results.sort((a, b) => a.name.localeCompare(b.name)) - return results } diff --git a/src/macrofier/index.mjs b/src/macrofier/index.mjs index 2b24c9d5..04cdd9fb 100644 --- a/src/macrofier/index.mjs +++ b/src/macrofier/index.mjs @@ -52,7 +52,6 @@ const macrofy = async ( allocatedPrimitiveProxies, convertTuplesToArraysOrObjects, additionalSchemaTemplates, - additionalDeclarationTemplates, additionalMethodTemplates, excludeDeclarations, aggregateFiles, @@ -96,7 +95,6 @@ const macrofy = async ( primitives, allocatedPrimitiveProxies, additionalSchemaTemplates, - additionalDeclarationTemplates, additionalMethodTemplates, excludeDeclarations, operators diff --git a/src/sdk/index.mjs b/src/sdk/index.mjs index 562af9e0..763b6c9e 100755 --- a/src/sdk/index.mjs +++ b/src/sdk/index.mjs @@ -68,7 +68,6 @@ const run = async ({ unwrapResultObjects: config.unwrapResultObjects, allocatedPrimitiveProxies: config.allocatedPrimitiveProxies, additionalSchemaTemplates: config.additionalSchemaTemplates, - additionalDeclarationTemplates: config.additionalDeclarationTemplates, additionalMethodTemplates: config.additionalMethodTemplates, excludeDeclarations: config.excludeDeclarations, staticModuleNames: staticModuleNames,