|
| 1 | +import { escapeRegex, trimExtension } from "../../util/commonUtil.js"; |
| 2 | +import { IChange } from "../../model/types.js"; |
| 3 | +import { posix as path } from "path"; |
| 4 | +import { MergeCommand } from "./command.js"; |
| 5 | + |
| 6 | +export default class I18nPropertiesMergeCommand extends MergeCommand { |
| 7 | + |
| 8 | + private mergePaths: RegExp[]; |
| 9 | + private copyPaths: RegExp[]; |
| 10 | + |
| 11 | + constructor(private i18nPath: string, private prefix: string, manifestChanges: ReadonlyArray<IChange>) { |
| 12 | + super(); |
| 13 | + const { mergePaths, copyPaths } = this.analyzeAppVariantManifestChanges(manifestChanges); |
| 14 | + this.mergePaths = mergePaths; |
| 15 | + this.copyPaths = copyPaths; |
| 16 | + } |
| 17 | + |
| 18 | + accept = (filename: string) => filename.endsWith(".properties"); |
| 19 | + |
| 20 | + |
| 21 | + private analyzeAppVariantManifestChanges(manifestChanges: ReadonlyArray<IChange>) { |
| 22 | + // check which files need to be copied and which files need to be merged and copied |
| 23 | + // this is necessary because lrep does not support multiple enhanceWith with multiple locations |
| 24 | + const TRANSLATION_REGEX_PATTERN = "((_[a-z]{2,3})?(_[a-zA-Z]{2,3}(_[a-zA-Z]{2,20})?)?)\.properties$"; |
| 25 | + const mergePaths = new Set<RegExp>(); |
| 26 | + const copyPaths = new Set<RegExp>(); |
| 27 | + manifestChanges.forEach((change) => { |
| 28 | + const i18nPathWithExtension = change.content?.bundleUrl || change.texts?.i18n; |
| 29 | + if (i18nPathWithExtension) { |
| 30 | + // build regex to match specific + language related files |
| 31 | + const i18nPath = trimExtension(i18nPathWithExtension); |
| 32 | + const regex = new RegExp("^" + escapeRegex(i18nPath) + TRANSLATION_REGEX_PATTERN); |
| 33 | + if (change.changeType.includes("addNewModelEnhanceWith")) { |
| 34 | + copyPaths.add(regex); |
| 35 | + } else { |
| 36 | + mergePaths.add(regex); |
| 37 | + } |
| 38 | + } |
| 39 | + }); |
| 40 | + return { mergePaths: Array.from(mergePaths), copyPaths: Array.from(copyPaths) }; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + async execute(files: Map<string, string>, filename: string, appVariantContent: string): Promise<void> { |
| 45 | + // merge/copy logic |
| 46 | + // check if file matches with regex in merge/copy |
| 47 | + const mergePathMatch = this.mergePaths.map(path => filename.match(path)).find(match => match); |
| 48 | + const copyPathMatch = this.copyPaths.map(path => filename.match(path)).find(match => match); |
| 49 | + if (mergePathMatch) { |
| 50 | + this.mergePropertiesFiles(files, this.i18nPath, appVariantContent, mergePathMatch[1]); |
| 51 | + } |
| 52 | + if (copyPathMatch) { |
| 53 | + files.set(path.join(this.prefix, filename), appVariantContent); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Filters out specific lines from the given string. |
| 59 | + * Removes lines matching: |
| 60 | + * - __ldi.translation.uuid\s*=\s*(.*) |
| 61 | + * - ABAP_TRANSLATION |
| 62 | + * - SAPUI5 TRANSLATION-KEY |
| 63 | + */ |
| 64 | + private filterTranslationMetaLines(content: string): string { |
| 65 | + const lines = content.split('\n'); |
| 66 | + const filtered = lines.filter( |
| 67 | + line => |
| 68 | + !/^# __ldi\.translation\.uuid\s*=/.test(line) && |
| 69 | + !line.startsWith("# ABAP_TRANSLATION") && |
| 70 | + !line.startsWith("# SAPUI5 TRANSLATION-KEY") |
| 71 | + ); |
| 72 | + return filtered.join('\n'); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Merge/Append base property file with property file from app variant |
| 77 | + * FIXME Currently merge could duplicate keys which causes undefined |
| 78 | + * behavior => Existing keys which are in merge content must be removed => |
| 79 | + * Actually only descriptor texts are relevant for merge which always have |
| 80 | + * app variant Id as prefix => If we filter on them we do not need to remove |
| 81 | + * existing overwritten keys (as there should be none) |
| 82 | + */ |
| 83 | + private mergePropertiesFiles(files: Map<string, string>, i18nPath: string, appVariantFileContent: string, language: string = "") { |
| 84 | + const baseAppI18nPath = i18nPath + language + ".properties"; |
| 85 | + const baseAppFileContent = files.get(baseAppI18nPath); |
| 86 | + const filteredBaseContent = baseAppFileContent ? this.filterTranslationMetaLines(baseAppFileContent) : ""; |
| 87 | + const content = filteredBaseContent |
| 88 | + ? `${filteredBaseContent}\n\n#App variant specific text file\n\n${appVariantFileContent}` |
| 89 | + : appVariantFileContent; |
| 90 | + files.set(baseAppI18nPath, content); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments