Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sm/territory2 #433

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/collections/componentSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type RetrieveSetOptions = Omit<MetadataApiRetrieveOptions, 'components'>;

/**
* A collection containing no duplicate metadata members (`fullName` and `type` pairs). `ComponentSets`
* are a convinient way of constructing a unique collection of components to perform operations such as
* are a convenient way of constructing a unique collection of components to perform operations such as
* deploying and retrieving.
*
* Multiple {@link SourceComponent}s can be present in the set and correspond to the same member.
Expand Down
10 changes: 5 additions & 5 deletions src/registry/registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -1397,21 +1397,21 @@
"name": "Territory2Model",
"suffix": "territory2Model",
"directoryName": "territory2Models",
"inFolder": false
"folderType": "territory2model"
},
"territory2rule": {
"id": "territory2rule",
"name": "Territory2Rule",
"suffix": "territory2Rule",
"directoryName": "territory2Models",
"inFolder": false
"directoryName": "rules",
"folderType": "territory2model"
},
"territory2": {
"id": "territory2",
"name": "Territory2",
"suffix": "territory2",
"directoryName": "territory2Models",
"inFolder": false
"directoryName": "territories",
"folderType": "territory2model"
},
"campaigninfluencemodel": {
"id": "campaigninfluencemodel",
Expand Down
30 changes: 26 additions & 4 deletions src/resolve/adapters/baseSourceAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ export abstract class BaseSourceAdapter implements SourceAdapter {

let component: SourceComponent;
if (rootMetadata) {
const componentName = this.type.folderType
? `${parentName(rootMetadata.path)}/${rootMetadata.fullName}`
: rootMetadata.fullName;
component = new SourceComponent(
{
name: componentName,
name: this.calculateName(rootMetadata),
type: this.type,
xml: rootMetadata.path,
parentType: this.type.folderType
? this.registry.getTypeByName(this.type.folderType)
: undefined,
},
this.tree,
this.forceIgnore
Expand Down Expand Up @@ -146,6 +146,28 @@ export abstract class BaseSourceAdapter implements SourceAdapter {
}
}

private calculateName(rootMetadata: MetadataXml): string {
// not using folders? then name is fullname
if (!this.type.folderType) {
return rootMetadata.fullName;
}
const grandparentType = this.registry.getTypeByName(this.type.folderType);

// type is in a nested inside another type (ex: Territory2Model). So the names are modelName.ruleName or modelName.territoryName
if (grandparentType.folderType && grandparentType.folderType !== this.type.id) {
const splits = rootMetadata.path.split(sep);
Copy link
Contributor

Choose a reason for hiding this comment

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

Using path sep can be tricky because if the path is from a server response it will necessarily be a forward slash, even on Windows OS. If this is used for a retrieve on Windows I'd expect it to not work as expected. In order for it to work we first have to normalize the path, then split.

return `${splits[splits.indexOf(grandparentType.directoryName) + 1]}.${
rootMetadata.fullName
}`;
}
// this is the top level of nested types (ex: in a Territory2Model, the Territory2Model)
if (grandparentType.folderType === this.type.id) {
return rootMetadata.fullName;
}
// other folderType scenarios (report, dashboard, emailTemplate, etc) where the parent is of a different type
return `${parentName(rootMetadata.path)}/${rootMetadata.fullName}`;
}
mshanemc marked this conversation as resolved.
Show resolved Hide resolved

/**
* Determine the related root metadata xml when the path given to `getComponent` isn't one.
*
Expand Down
49 changes: 30 additions & 19 deletions src/resolve/sourceComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { join, basename } from 'path';
import { join, basename, sep } from 'path';
import { parse } from 'fast-xml-parser';
import { ForceIgnore } from './forceIgnore';
import { NodeFSTreeContainer, TreeContainer, VirtualTreeContainer } from './treeContainers';
Expand All @@ -22,6 +22,7 @@ export type ComponentProperties = {
xml?: string;
content?: string;
parent?: SourceComponent;
parentType?: MetadataType;
};

/**
Expand All @@ -32,6 +33,7 @@ export class SourceComponent implements MetadataComponent {
public readonly type: MetadataType;
public readonly xml?: string;
public readonly parent?: SourceComponent;
public parentType?: MetadataType;
public content?: string;
private _tree: TreeContainer;
private forceIgnore: ForceIgnore;
Expand All @@ -47,6 +49,7 @@ export class SourceComponent implements MetadataComponent {
this.xml = props.xml;
this.parent = props.parent;
this.content = props.content;
this.parentType = props.parentType;
this._tree = tree;
this.forceIgnore = forceIgnore;
}
Expand Down Expand Up @@ -98,24 +101,9 @@ export class SourceComponent implements MetadataComponent {
}

public getPackageRelativePath(fsPath: string, format: SfdxFileFormat): string {
const { directoryName, suffix, inFolder, folderType } = this.type;
// if there isn't a suffix, assume this is a mixed content component that must
// reside in the directoryName of its type. trimUntil maintains the folder structure
// the file resides in for the new destination.
let relativePath: string;
if (!suffix) {
relativePath = trimUntil(fsPath, directoryName);
} else if (folderType || inFolder) {
const folderName = this.fullName.split('/')[0];
relativePath = join(directoryName, folderName, basename(fsPath));
} else {
relativePath = join(directoryName, basename(fsPath));
}

if (format === 'source') {
return join(DEFAULT_PACKAGE_ROOT_SFDX, relativePath);
}
return relativePath;
return format === 'source'
? join(DEFAULT_PACKAGE_ROOT_SFDX, this.calculateRelativePath(fsPath))
: this.calculateRelativePath(fsPath);
}

/**
Expand All @@ -129,6 +117,29 @@ export class SourceComponent implements MetadataComponent {
this.markedForDelete = asDeletion;
}

private calculateRelativePath(fsPath: string): string {
const { directoryName, suffix, inFolder, folderType } = this.type;
// if there isn't a suffix, assume this is a mixed content component that must
// reside in the directoryName of its type. trimUntil maintains the folder structure
// the file resides in for the new destination.
if (!suffix) {
return trimUntil(fsPath, directoryName);
}
// legacy version of folderType
if (inFolder) {
return join(directoryName, this.fullName.split('/')[0], basename(fsPath));
}
if (folderType) {
// types like Territory2Model have child types inside them. We have to preserve those folder structures
if (this.parentType?.folderType && this.parentType?.folderType !== this.type.id) {
const fsPathSplits = fsPath.split(sep);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll echo the same comment from above about using path.sep to split file paths. For this to work we have to normalize the path first unless we are absolutely sure this path can only come from the file system of the OS.

return fsPathSplits.slice(fsPathSplits.indexOf(this.parentType.directoryName)).join(sep);
}
return join(directoryName, this.fullName.split('/')[0], basename(fsPath));
}
return join(directoryName, basename(fsPath));
}

private parse<T = JsonMap>(contents: string): T {
// include tag attributes and don't parse text node as number
const parsed = parse(contents.toString(), {
Expand Down
39 changes: 39 additions & 0 deletions test/convert/transformers/defaultMetadataTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
matchingContentFile,
mockRegistry,
mockRegistryData,
nestedTypes,
} from '../../mock/registry';
import { DefaultMetadataTransformer } from '../../../src/convert/transformers/defaultMetadataTransformer';
import { WriteInfo } from '../../../src/convert';
Expand Down Expand Up @@ -122,6 +123,44 @@ describe('DefaultMetadataTransformer', () => {

expect(await transformer.toMetadataFormat(component)).to.deep.equal(expectedInfos);
});

it('should handle nested components (parent)', async () => {
// ex: territory2Models/someModel/
const component = nestedTypes.NESTED_PARENT_COMPONENT;
const expectedInfos: WriteInfo[] = [
{
output: join(
component.type.directoryName,
component.fullName,
`${component.fullName}.${component.type.suffix}`
),
source: component.tree.stream(component.xml),
},
];

expect(await transformer.toMetadataFormat(component)).to.deep.equal(expectedInfos);
});

it('should handle nested components (children)', async () => {
// ex: territory2Models/someModel/rules/someRule.Territory2Rule-meta.xml
const component = nestedTypes.NESTED_CHILD_COMPONENT;
const parentComponent = nestedTypes.NESTED_PARENT_COMPONENT;

const expectedInfos: WriteInfo[] = [
{
output: join(
component.parentType.directoryName,
parentComponent.fullName,
component.type.directoryName,
`${nestedTypes.CHILD_COMPONENT_NAME}.${component.type.suffix}`
),
source: component.tree.stream(component.xml),
},
];

console.log(expectedInfos);
expect(await transformer.toMetadataFormat(component)).to.deep.equal(expectedInfos);
});
});

describe('toSourceFormat', () => {
Expand Down
1 change: 1 addition & 0 deletions test/mock/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export {
decomposed,
nonDecomposed,
decomposedtoplevel,
nestedTypes,
} from './type-constants';
export { mockRegistry, mockRegistryData } from './mockRegistry';
20 changes: 20 additions & 0 deletions test/mock/registry/mockRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ import { RegistryAccess } from '../../../src';
*/
export const mockRegistryData = {
types: {
/**
* nested folder types
*
* e.g. territoryRules goes inside a folder for each territory2Model
*/
nestedparent: {
id: 'nestedparent',
directoryName: 'nestedParents',
name: 'NestedParent',
suffix: 'nestedParent',
folderType: 'nestedparent',
},
nestedchild: {
id: 'nestedchild',
directoryName: 'nestedChildren',
name: 'NestedChild',
suffix: 'nestedChild',
folderType: 'nestedparent',
},

/**
* Metadata with no content and is contained in a folder type component
*
Expand Down
2 changes: 2 additions & 0 deletions test/mock/registry/type-constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as mixedContentSingleFile from './mixedContentSingleFileConstants';
import * as decomposed from './decomposedConstants';
import * as decomposedtoplevel from './decomposedTopLevelConstants';
import * as nonDecomposed from './nonDecomposedConstants';
import * as nestedTypes from './nestedTypesConstants';
export {
xmlInFolder,
document,
Expand All @@ -25,4 +26,5 @@ export {
decomposed,
decomposedtoplevel,
nonDecomposed,
nestedTypes,
};
63 changes: 63 additions & 0 deletions test/mock/registry/type-constants/nestedTypesConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { mockRegistryData } from '../mockRegistry';
import { join } from 'path';
import { SourceComponent } from '../../../../src';

const parentType = mockRegistryData.types.nestedparent;
const childType = mockRegistryData.types.nestedchild;

export const PARENT_COMPONENT_NAME = 'parentName';
export const PARENT_TYPE_DIRECTORY = join('path', 'to', parentType.directoryName);
export const PARENT_CONTENT_PATH = join(PARENT_TYPE_DIRECTORY, PARENT_COMPONENT_NAME);
export const PARENT_XML_NAME = `${PARENT_COMPONENT_NAME}.${parentType.suffix}-meta.xml`;
export const PARENT_XML_PATH = join(PARENT_CONTENT_PATH, PARENT_XML_NAME);

export const CHILD_COMPONENT_NAME = 'childName';
// /Territory2Models/someModel/rules
export const CHILD_TYPE_DIRECTORY = join(PARENT_CONTENT_PATH, childType.directoryName);
export const CHILD_XML_NAME = `${CHILD_COMPONENT_NAME}.${childType.suffix}-meta.xml`;
export const CHILD_XML_PATH = join(CHILD_TYPE_DIRECTORY, CHILD_XML_NAME);

export const NESTED_PARENT_COMPONENT = SourceComponent.createVirtualComponent(
{
name: PARENT_COMPONENT_NAME,
type: parentType,
xml: PARENT_XML_PATH,
parentType,
},
[]
);

export const NESTED_CHILD_COMPONENT = SourceComponent.createVirtualComponent(
{
name: `${PARENT_COMPONENT_NAME}.${CHILD_COMPONENT_NAME}`,
type: childType,
xml: CHILD_XML_PATH,
parentType,
},
[]
);

// export const NESTED_CHILD_COMPONENT = SourceComponent.createVirtualComponent(
// {
// name: 'testRule',
// type: childType,
// xml: XML_PATH,
// content: CONTENT_PATH,
// },
// [
// {
// dirPath: TYPE_DIRECTORY,
// children: [COMPONENT_NAME],
// },
// {
// dirPath: CONTENT_PATH,
// children: [],
// },
// ]
// );
24 changes: 24 additions & 0 deletions test/resolve/adapters/baseSourceAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
mixedContentSingleFile,
decomposed,
matchingContentFile,
nestedTypes,
} from '../../mock/registry';
import { BaseSourceAdapter, DefaultSourceAdapter } from '../../../src/resolve/adapters';
import { META_XML_SUFFIX } from '../../../src/common';
Expand Down Expand Up @@ -96,4 +97,27 @@ describe('BaseSourceAdapter', () => {
const adapter = new DefaultSourceAdapter(type, mockRegistry);
expect(adapter.getComponent(path)).to.be.undefined;
});

describe('handling nested types (Territory2Model)', () => {
// mocha was throwing errors about private property _tree not matching
const sourceComponentKeys = ['type', 'name', 'xml', 'parent', 'parentType', 'content'];

it('should resolve the parent name and type', () => {
const component = nestedTypes.NESTED_PARENT_COMPONENT;
const adapter = new DefaultSourceAdapter(component.type, mockRegistry);
const componentFromAdapter = adapter.getComponent(component.xml);
sourceComponentKeys.map((prop: keyof SourceComponent) =>
expect(componentFromAdapter[prop]).to.deep.equal(component[prop])
);
});

it('should resolve the child name and type AND parentType', () => {
const component = nestedTypes.NESTED_CHILD_COMPONENT;
const adapter = new DefaultSourceAdapter(component.type, mockRegistry);
const componentFromAdapter = adapter.getComponent(component.xml);
sourceComponentKeys.map((prop: keyof SourceComponent) =>
expect(componentFromAdapter[prop]).to.deep.equal(component[prop])
);
});
});
});
Loading