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 3 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
26 changes: 22 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,24 @@ export abstract class BaseSourceAdapter implements SourceAdapter {
}
}

private calculateName(rootMetadata: MetadataXml): string {
if (!this.type.folderType) {
return rootMetadata.fullName;
}
const grandparentType = this.registry.getTypeByName(this.type.folderType);
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
}`;
}
if (grandparentType.folderType === this.type.id) {
return rootMetadata.fullName;
}

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