async function changeEntityEnumLG()

in experimental/generation/generator/packages/library/src/mergeAssets.ts [475:552]


async function changeEntityEnumLG(oldPath: string, oldFileList: string[], newPath: string, newFileList: string[], mergedPath: string, filename: string, feedback: Feedback): Promise<void> {
    const oldFilePath = oldFileList.filter(file => file.match(filename))[0]
    if (await isOldUnchanged(oldFileList, filename)) {
        await copySingleFile(newPath, mergedPath, filename, newFileList, feedback)
    } else {
        const oldText = await fs.readFile(oldFilePath, 'utf8')
        const oldStatements = oldText.split(os.EOL)
        const oldTemplates = Templates.parseText(oldText)

        const newFilePath = newFileList.filter(file => file.endsWith(filename))[0]
        const newText = await fs.readFile(newFilePath, 'utf8')
        const newStatements = newText.split(os.EOL)
        const newTemplates = Templates.parseText(newText)

        let mergedStatements: string[] = []

        const recordPart: object[] = []

        for (const oldTemplate of oldTemplates) {
            const oldBody = oldTemplate.templateBodyParseTree
            if (oldBody === undefined) {
                continue
            }
            if (oldBody instanceof SwitchCaseBodyContext) {
                for (const newTemplate of newTemplates) {
                    if (newTemplate.name !== oldTemplate.name) {
                        continue
                    }
                    const newBody = newTemplate.templateBodyParseTree
                    if (newBody instanceof SwitchCaseBodyContext) {
                        const newSwitchStatements: string[] = []
                        const newEnumValueMap = new Map<string, number>()
                        const oldEnumEntitySet = new Set<string>()
                        const newRules = newBody.switchCaseTemplateBody().switchCaseRule()
                        for (const rule of newRules) {
                            const state = rule.switchCaseStat()
                            // get enumEntity and its following statements
                            if (state.text.match('\s*-\s*CASE:')) {
                                const enumEntity = state.text.replace('-CASE:${', '').replace('}', '')
                                const start = state.start.line + newTemplate.sourceRange.range.start.line
                                newEnumValueMap.set(enumEntity, start)
                            }
                        }
                        const {startIndex, endIndex} = parseLGTemplate(oldTemplate, oldBody, oldStatements, newStatements, newEnumValueMap, oldEnumEntitySet, newSwitchStatements)
                        const statementInfo = {
                            start: startIndex, end: endIndex, newSStatements: newSwitchStatements
                        }
                        recordPart.push(statementInfo)
                    }
                }
            }
        }

        if (recordPart.length !== 0) {
            let startSplit = 0
            const arrList: [string[]] = [[]]
            for (const obj of recordPart) {
                const arr = oldStatements.slice(startSplit, obj['start'])
                arrList.push(arr)
                arrList.push(obj['newSStatements'])
                startSplit = obj['end']
            }

            if (startSplit !== oldStatements.length) {
                const arr = oldStatements.slice(startSplit)
                arrList.push(arr)
            }

            for (const arr of arrList) {
                mergedStatements = mergedStatements.concat(arr)
            }
            const val = mergedStatements.join(os.EOL)
            await writeToFile(oldPath, mergedPath, filename, oldFileList, val, feedback)
        } else {
            await writeToFile(oldPath, mergedPath, filename, oldFileList, oldText, feedback)
        }
    }
}