Skip to content

Commit

Permalink
CPPSDK: support to generate imeplemtnation methods & events for both …
Browse files Browse the repository at this point in the history
…dynamic and static configured templates
  • Loading branch information
HaseenaSainul committed Oct 20, 2023
1 parent 750dbe4 commit 6b3a60f
Showing 1 changed file with 47 additions and 29 deletions.
76 changes: 47 additions & 29 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ const getTemplateTypeForMethod = (method, type, templates) => {
return getTemplate(path, templates)
}

const getTemplateForMethod = (method, templates) => {
return getTemplateTypeForMethod(method, 'methods', templates)
const getTemplateForMethod = (method, templates, templateDir) => {
return getTemplateTypeForMethod(method, templateDir, templates)
}

const getTemplateForDeclaration = (method, templates, templateDir) => {
Expand Down Expand Up @@ -495,7 +495,11 @@ const generateMacros = (obj, templates, languages, options = {}) => {
schemas: {},
types: {},
enums: {},
declarations: {}
declarations: {},
methods: {},
events: {},
methodList: '',
eventList: ''
}

Array.from(new Set(['types'].concat(config.additionalSchemaTemplates))).forEach(dir => {
Expand All @@ -507,26 +511,32 @@ const generateMacros = (obj, templates, languages, options = {}) => {
})

state.typeTemplateDir = 'types'
const examples = generateExamples(obj, templates, languages)
const allMethodsArray = generateMethods(obj, examples, templates)
const methodsArray = allMethodsArray.filter(m => m.body && !m.event && (!options.hideExcluded || !m.excluded))
const eventsArray = allMethodsArray.filter(m => m.body && m.event && (!options.hideExcluded || !m.excluded))

const imports = generateImports(obj, templates, { destination: (options.destination ? options.destination : '') })
const initialization = generateInitialization(obj, templates)
const eventsEnum = generateEvents(obj, templates)

const methods = methodsArray.length ? getTemplate('/sections/methods', templates).replace(/\$\{method.list\}/g, methodsArray.map(m => m.body).join('\n')) : ''
const methodList = methodsArray.filter(m => m.body).map(m => m.name)
const examples = generateExamples(obj, templates, languages)
const allMethodsArray = generateMethods(obj, examples, templates)

Array.from(new Set(['declarations'].concat(config.additionalDeclarationTemplates))).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))).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 === 'methods') {
macros.methodList = methodsArray.filter(m => m.body).map(m => m.name)
macros.eventList = eventsArray.map(m => makeEventName(m))
}
})

const providerInterfaces = generateProviderInterfaces(obj, templates)
const events = eventsArray.length ? getTemplate('/sections/events', templates).replace(/\$\{event.list\}/g, eventsArray.map(m => m.body).join('\n')) : ''
const eventList = eventsArray.map(m => makeEventName(m))
const defaults = generateDefaults(obj, templates)

const suffix = options.destination ? options.destination.split('.').pop().trim() : ''
Expand All @@ -538,11 +548,7 @@ const generateMacros = (obj, templates, languages, options = {}) => {
Object.assign(macros, {
imports,
initialization,
events,
eventList,
eventsEnum,
methods,
methodList,
defaults,
examples,
providerInterfaces,
Expand Down Expand Up @@ -590,15 +596,15 @@ const insertMacros = (fContents = '', macros = {}) => {
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\.methods\}(.*?)\$\{end\.if\.methods\}/gms, (macros.methods.trim() || macros.events.trim()) ? '$1' : '')
fContents = fContents.replace(/\$\{if\.implementations\}(.*?)\$\{end\.if\.implementations\}/gms, (macros.methods.trim() || macros.events.trim() || macros.schemas.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\.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)
fContents = fContents.replace(/\$\{module\.includes\}/g, macros.moduleInclude)
fContents = fContents.replace(/\$\{module\.includes\.private\}/g, macros.moduleIncludePrivate)
fContents = fContents.replace(/\$\{module\.init\}/g, macros.moduleInit)

fContents = fContents.replace(/\$\{if\.modules\}(.*?)\$\{end\.if\.modules\}/gms, (macros.methods.trim() || macros.events.trim()) ? '$1' : '')
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)
Expand All @@ -610,12 +616,21 @@ const insertMacros = (fContents = '', macros = {}) => {
})
})

fContents = fContents.replace(/[ \t]*\/\* \$\{METHODS\} \*\/[ \t]*\n/, macros.methods)
// 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)
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
Array.from(new Set(['methods'].concat(config.additionalMethodTemplates))).forEach(dir => {
['METHODS', 'EVENTS'].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 schema macros
fContents = fContents.replace(/[ \t]*\/\* \$\{SCHEMAS\} \*\/[ \t]*\n/, macros.schemas.types)
fContents = fContents.replace(/[ \t]*\/\* \$\{TYPES\} \*\/[ \t]*\n/, macros.types.types)
Expand Down Expand Up @@ -1093,27 +1108,30 @@ function generateMethods(json = {}, examples = {}, templates = {}) {
const results = reduce((acc, methodObj, i, arr) => {
const result = {
name: methodObj.name,
body: '',
body: {},
declaration: {},
excluded: methodObj.tags.find(t => t.name === 'exclude-from-sdk'),
event: isEventMethod(methodObj)
}

let template = getTemplateForMethod(methodObj, templates);
if (template && template.length) {
let javascript = insertMethodMacros(template, methodObj, json, templates, examples)
result.body = javascript
}

// Generate declarations for both dynamic and static configured templates
Array.from(new Set(['declarations'].concat(config.additionalDeclarationTemplates))).forEach(dir => {
template = getTemplateForDeclaration(methodObj, templates, 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))).forEach(dir => {
const template = getTemplateForMethod(methodObj, templates, dir)
if (template && template.length) {
let javascript = insertMethodMacros(template, methodObj, json, templates, examples)
result.body[dir] = javascript
}
})

acc.push(result)

return acc
Expand Down

0 comments on commit 6b3a60f

Please sign in to comment.