Skip to content

Commit

Permalink
fix: decomposing - very messy
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Aug 30, 2024
1 parent 4b24bd8 commit f2e9353
Showing 1 changed file with 76 additions and 17 deletions.
93 changes: 76 additions & 17 deletions src/convert/transformers/filePerChildTypeMetadataTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { dirname, join } from 'node:path';
import fs from 'node:fs';
import { AnyJson, JsonMap, ensureString } from '@salesforce/ts-types';
import { AnyJson, ensureString, JsonMap } from '@salesforce/ts-types';
import { Messages } from '@salesforce/core';
import { calculateRelativePath } from '../../utils/path';
import { ForceIgnore } from '../../resolve/forceIgnore';
Expand Down Expand Up @@ -75,18 +75,22 @@ export class FilePerChildTypeMetadataTransformer extends BaseMetadataTransformer
const writeInfosForChildren = composedMetadata
.filter(hasChildTypeId)
.map(addChildType)
.map((c) => {
if (c.childType.directoryName) {
// merge all child types that will end up in the same file
return toInfoContainer(mergeWith)(component)(c.childType)(c.tagValue as JsonMap);
} else {
return toInfoContainer(mergeWith)(component)(c.childType)(c.tagValue as JsonMap);
}
})
.filter((c) => !c.childType.directoryName)
.map((c) => toInfoContainer(mergeWith)(component)(c.childType)(c.tagValue as JsonMap))
.filter(forceIgnoreAllowsComponent(forceIgnore))
.map(handleUnaddressableChildAlone(composedMetadata.length)(parentXmlObject)(stateSetter))
.flatMap(getChildWriteInfos(stateSetter)(childrenOfMergeComponent));

const toBeCombined = composedMetadata
.filter(hasChildTypeId)
.map(addChildType)
.filter((c) => c.childType.directoryName)
.map((c) => toInfoContainer(mergeWith)(component)(c.childType)(c.tagValue as JsonMap))
.filter(forceIgnoreAllowsComponent(forceIgnore))
.map(handleUnaddressableChildAlone(composedMetadata.length)(parentXmlObject)(stateSetter));
const combined = getAndCombineChildWriteInfos(toBeCombined, stateSetter, childrenOfMergeComponent);
writeInfosForChildren.push(...combined);

const writeInfoForParent = mergeWith
? getWriteInfosFromMerge(mergeWith)(stateSetter)(parentXmlObject)(component)
: getWriteInfosWithoutMerge(this.defaultDirectory)(parentXmlObject)(component);
Expand Down Expand Up @@ -301,14 +305,69 @@ type InfoContainer = {
mergeWith?: SourceComponent;
};

// const toInfoContainer =
// (mergeWith: SourceComponent | undefined) =>
// (parent: SourceComponent) =>
// (c: MetadataType) =>
// (tagValue: JsonMap[]): InfoContainer => {
// const entryName = childType.directoryName
// ? tagValue[childType.uniqueIdElement]?.split('.')?.at(0) ?? parent.name
// : parent.name;
const getAndCombineChildWriteInfos = (
containers: InfoContainer[],
stateSetter: StateSetter,
childrenOfMergeComponent: ComponentSet
): WriteInfo[] => {
const nameWriteInfoMap = new Map<string, InfoContainer[]>();
containers.map((c) =>
nameWriteInfoMap.has(c.entryName)
? nameWriteInfoMap.get(c.entryName)!.push(c)
: nameWriteInfoMap.set(c.entryName, [c])
);
const result: WriteInfo[] = [];

nameWriteInfoMap.forEach((info) => {
const source = new JsToXml({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
[info[0].parentComponent.type.name]: Object.assign(
{},
...info.map((i) => ({ [i.childComponent.type.name]: i.value }))
),
});
// if there's nothing to merge with, push write operation now to default location
if (!info[0].mergeWith) {
result.push({ source, output: getDefaultOutput(info[0].childComponent) });
return;
}
// if the merge parent has a child that can be merged with, push write
// operation now and mark it as merged in the state
if (childrenOfMergeComponent.has(info[0].childComponent)) {
const mergeChild = childrenOfMergeComponent.getSourceComponents(info[0].childComponent).first();
if (!mergeChild?.xml) {
throw messages.createError('error_parsing_xml', [
info[0].childComponent.fullName,
info[0].childComponent.type.name,
]);
}
stateSetter(info[0].childComponent, { foundMerge: true });
result.push({ source, output: mergeChild.xml });
return;
}
// If we have a parent and the child is unaddressable without the parent, keep them
// together on the file system, meaning a new child will not be written to the default dir.
if (info[0].childComponent.type.unaddressableWithoutParent && typeof info[0].mergeWith?.xml === 'string') {
// get output path from parent
result.push({
source,
output: join(
dirname(info[0].mergeWith.xml),
`${info[0].entryName}.${ensureString(info[0].childComponent.type.suffix)}${META_XML_SUFFIX}`
),
});
return;
}
// we didn't find a merge, so we add it to the state for later processing
stateSetter(info[0].childComponent, {
writeInfo: { source, output: getDefaultOutput(info[0].childComponent) },
});
return [];
});

return result;
};

/** returns a data structure with lots of context information in it */
const toInfoContainer =
(mergeWith: SourceComponent | undefined) =>
Expand Down

2 comments on commit f2e9353

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: f2e9353 Previous: 87322d5 Ratio
eda-componentSetCreate-linux 228 ms 235 ms 0.97
eda-sourceToMdapi-linux 2247 ms 2312 ms 0.97
eda-sourceToZip-linux 1893 ms 1874 ms 1.01
eda-mdapiToSource-linux 2954 ms 2844 ms 1.04
lotsOfClasses-componentSetCreate-linux 438 ms 420 ms 1.04
lotsOfClasses-sourceToMdapi-linux 3842 ms 3597 ms 1.07
lotsOfClasses-sourceToZip-linux 3213 ms 3100 ms 1.04
lotsOfClasses-mdapiToSource-linux 3639 ms 3510 ms 1.04
lotsOfClassesOneDir-componentSetCreate-linux 761 ms 725 ms 1.05
lotsOfClassesOneDir-sourceToMdapi-linux 6618 ms 6374 ms 1.04
lotsOfClassesOneDir-sourceToZip-linux 5803 ms 5469 ms 1.06
lotsOfClassesOneDir-mdapiToSource-linux 6583 ms 6342 ms 1.04

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: f2e9353 Previous: 87322d5 Ratio
eda-componentSetCreate-win32 689 ms 600 ms 1.15
eda-sourceToMdapi-win32 4488 ms 4238 ms 1.06
eda-sourceToZip-win32 3137 ms 3060 ms 1.03
eda-mdapiToSource-win32 6024 ms 5716 ms 1.05
lotsOfClasses-componentSetCreate-win32 1153 ms 1205 ms 0.96
lotsOfClasses-sourceToMdapi-win32 7786 ms 7700 ms 1.01
lotsOfClasses-sourceToZip-win32 5199 ms 4978 ms 1.04
lotsOfClasses-mdapiToSource-win32 8053 ms 7698 ms 1.05
lotsOfClassesOneDir-componentSetCreate-win32 2121 ms 2117 ms 1.00
lotsOfClassesOneDir-sourceToMdapi-win32 14164 ms 13856 ms 1.02
lotsOfClassesOneDir-sourceToZip-win32 9385 ms 9080 ms 1.03
lotsOfClassesOneDir-mdapiToSource-win32 14442 ms 14023 ms 1.03

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.