Skip to content

Commit

Permalink
fix: decompose by any folder name
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Feb 8, 2024
1 parent 406a43a commit 083b7c5
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/convert/transformers/decomposedMetadataTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { dirname, join } from 'node:path';
import { JsonMap } from '@salesforce/ts-types';
import { AnyJson, JsonMap, getString, isJsonMap } from '@salesforce/ts-types';
import { ensureArray } from '@salesforce/kit';
import { Messages } from '@salesforce/core';
import { MetadataComponent, SourceComponent } from '../../resolve';
Expand Down Expand Up @@ -70,9 +70,10 @@ export class DecomposedMetadataTransformer extends BaseMetadataTransformer {
if (!childType) {
throw messages.createError('error_missing_child_type_definition', [type.name, childTypeId]);
}
const tagValues = ensureArray(tagValue);
for (const value of tagValues as [{ fullName: string; name: string }]) {
const entryName = value.fullName || value.name;
const tagValues = ensureArray(tagValue).filter(isJsonMap);
// iterate each array member if it's Object-like (ex: customField of a CustomObject)
for (const value of tagValues) {
const entryName = extractUniqueElementValue(value, childType.uniqueIdElement);
const childComponent: MetadataComponent = {
fullName: `${parentFullName}.${entryName}`,
type: childType,
Expand Down Expand Up @@ -196,7 +197,7 @@ export class DecomposedMetadataTransformer extends BaseMetadataTransformer {
forComponent: MetadataComponent,
props: Partial<Omit<DecompositionStateValue, 'origin'>> = {}
): void {
const key = `${forComponent.type.name}#${forComponent.fullName}`;
const key = getKey(forComponent);
const withOrigin = Object.assign({ origin: forComponent.parent ?? forComponent }, props);
this.context.decomposition.transactionState.set(key, {
...(this.context.decomposition.transactionState.get(key) ?? {}),
Expand All @@ -205,26 +206,37 @@ export class DecomposedMetadataTransformer extends BaseMetadataTransformer {
}

private getDecomposedState(forComponent: MetadataComponent): DecompositionStateValue | undefined {
const key = `${forComponent.type.name}#${forComponent.fullName}`;
return this.context.decomposition.transactionState.get(key);
return this.context.decomposition.transactionState.get(getKey(forComponent));
}
}

const getComposedMetadataEntries = async (
component: SourceComponent
): Promise<Array<[string, { fullname?: string; name?: string }]>> => {
const getKey = (component: MetadataComponent): string => `${component.type.name}#${component.fullName}`;

const getComposedMetadataEntries = async (component: SourceComponent): Promise<Array<[string, AnyJson]>> => {
const composedMetadata = (await component.parseXml())[component.type.name];
// composedMetadata might be undefined if you call toSourceFormat() from a non-source-backed Component
return composedMetadata ? Object.entries(composedMetadata) : [];
};

/** where the file goes if there's nothing to merge with */
const getDefaultOutput = (component: MetadataComponent): SourcePath => {
const { parent, fullName, type } = component;
const [baseName, childName] = fullName.split('.');
const [baseName, ...tail] = fullName.split('.');
// there could be a '.' inside the child name (ex: PermissionSet.FieldPermissions.field uses Obj__c.Field__c)
// we put folders for each object in (ex) FieldPermissions because of the dot
const childName = tail.length ? join(...tail) : undefined;
const baseComponent = (parent ?? component) as SourceComponent;
let output = `${childName ?? baseName}.${component.type.suffix}${META_XML_SUFFIX}`;
if (parent?.type.strategies?.decomposition === DecompositionStrategy.FolderPerType) {
output = join(type.directoryName, output);
}
const output = join(
parent?.type.strategies?.decomposition === DecompositionStrategy.FolderPerType ? type.directoryName : '',
`${childName ?? baseName}.${component.type.suffix}${META_XML_SUFFIX}`
);
return join(baseComponent.getPackageRelativePath(baseName, 'source'), output);
};

/** handle wide-open reading of values from elements inside any metadata xml file.
* Return the value of the matching element if supplied, or defaults `fullName` then `name` */
const extractUniqueElementValue = (xml: JsonMap, elementName?: string): string | undefined =>
elementName ? getString(xml, elementName) ?? getStandardElements(xml) : getStandardElements(xml);

const getStandardElements = (xml: JsonMap): string | undefined =>
getString(xml, 'fullName') ?? getString(xml, 'name') ?? undefined;

2 comments on commit 083b7c5

@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: 083b7c5 Previous: 24dc7ad Ratio
eda-componentSetCreate-linux 197 ms 192 ms 1.03
eda-sourceToMdapi-linux 1985 ms 2314 ms 0.86
eda-sourceToZip-linux 1770 ms 1730 ms 1.02
eda-mdapiToSource-linux 2994 ms 2931 ms 1.02
lotsOfClasses-componentSetCreate-linux 382 ms 372 ms 1.03
lotsOfClasses-sourceToMdapi-linux 3490 ms 3524 ms 0.99
lotsOfClasses-sourceToZip-linux 2947 ms 2886 ms 1.02
lotsOfClasses-mdapiToSource-linux 3332 ms 3257 ms 1.02
lotsOfClassesOneDir-componentSetCreate-linux 670 ms 642 ms 1.04
lotsOfClassesOneDir-sourceToMdapi-linux 6194 ms 6025 ms 1.03
lotsOfClassesOneDir-sourceToZip-linux 5346 ms 5202 ms 1.03
lotsOfClassesOneDir-mdapiToSource-linux 5956 ms 5990 ms 0.99

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: 083b7c5 Previous: 24dc7ad Ratio
eda-componentSetCreate-win32 450 ms 419 ms 1.07
eda-sourceToMdapi-win32 3526 ms 3564 ms 0.99
eda-sourceToZip-win32 2687 ms 2706 ms 0.99
eda-mdapiToSource-win32 5869 ms 5836 ms 1.01
lotsOfClasses-componentSetCreate-win32 888 ms 915 ms 0.97
lotsOfClasses-sourceToMdapi-win32 7727 ms 7643 ms 1.01
lotsOfClasses-sourceToZip-win32 4999 ms 4885 ms 1.02
lotsOfClasses-mdapiToSource-win32 7768 ms 7561 ms 1.03
lotsOfClassesOneDir-componentSetCreate-win32 1501 ms 1508 ms 1.00
lotsOfClassesOneDir-sourceToMdapi-win32 14149 ms 14066 ms 1.01
lotsOfClassesOneDir-sourceToZip-win32 8846 ms 8798 ms 1.01
lotsOfClassesOneDir-mdapiToSource-win32 14151 ms 13945 ms 1.01

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

Please sign in to comment.