Skip to content

Commit

Permalink
additionalDeclarationTemplates combined to additionalMethodTemplates
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Oct 20, 2023
1 parent ae3cfef commit 4803635
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 57 deletions.
2 changes: 1 addition & 1 deletion languages/cpp/language.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
},
"langVersion" : "c++17",
"additionalSchemaTemplates": [ "json-types" ],
"additionalDeclarationTemplates": [ "declarations-override" ]
"additionalMethodTemplates": [ "declarations", "declarations-override" ]
}
2 changes: 1 addition & 1 deletion languages/cpp/templates/modules/include/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace ${info.Title} {
virtual ~I${info.Title}() = default;

// Methods & Events
/* ${DECLARATIONS} */
/* ${METHODS:declarations} */

};${end.if.methods}

Expand Down
2 changes: 1 addition & 1 deletion languages/cpp/templates/modules/src/module_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
3 changes: 2 additions & 1 deletion languages/javascript/language.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"number": "number",
"string": "string",
"object": "object"
}
},
"additionalMethodTemplates": [ "declarations" ]
}
4 changes: 2 additions & 2 deletions languages/javascript/templates/codeblocks/module.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ ${if.events} type Event = ${events}
${end.if.events}
/* ${SCHEMAS} */

/* ${DECLARATIONS} */
/* ${METHODS:declarations} */

/* ${PROVIDERS} */

}
}
84 changes: 36 additions & 48 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@ const generateMacros = (obj, templates, languages, options = {}) => {
schemas: {},
types: {},
enums: {},
declarations: {},
methods: {},
events: {},
methodList: '',
Expand All @@ -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))
}
}
})

Expand Down Expand Up @@ -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)
Expand All @@ -606,24 +607,14 @@ 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'))
fContents = fContents.replace(/[ \t]*\/\* \$\{EVENTS\} \*\/[ \t]*\n/, macros.events.methods)
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')
Expand Down Expand Up @@ -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
Expand All @@ -1137,21 +1126,21 @@ 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) {
let javascript = insertMethodMacros(template, methodObj, json, templates, examples)
result.declaration[dir] = javascript
}
}
else if (dir.includes('methods')) {
const template = getTemplateForMethod(methodObj, templates, dir)
if (template && template.length) {
let javascript = insertMethodMacros(template, methodObj, json, templates, examples)
result.body[dir] = javascript
}
}
})

Expand All @@ -1173,7 +1162,6 @@ function generateMethods(json = {}, examples = {}, templates = {}) {
}

results.sort((a, b) => a.name.localeCompare(b.name))

return results
}

Expand Down
2 changes: 0 additions & 2 deletions src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const macrofy = async (
allocatedPrimitiveProxies,
convertTuplesToArraysOrObjects,
additionalSchemaTemplates,
additionalDeclarationTemplates,
additionalMethodTemplates,
excludeDeclarations,
aggregateFiles,
Expand Down Expand Up @@ -96,7 +95,6 @@ const macrofy = async (
primitives,
allocatedPrimitiveProxies,
additionalSchemaTemplates,
additionalDeclarationTemplates,
additionalMethodTemplates,
excludeDeclarations,
operators
Expand Down
1 change: 0 additions & 1 deletion src/sdk/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4803635

Please sign in to comment.