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

feat: add a wildcard expansion to metadata full names when building the CS #1063

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Changes from 1 commit
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
35 changes: 26 additions & 9 deletions src/collections/componentSetBuilder.ts
Original file line number Diff line number Diff line change
@@ -99,23 +99,40 @@ export class ComponentSetBuilder {
const registry = new RegistryAccess();
const compSetFilter = new ComponentSet();
componentSet ??= new ComponentSet();
const directoryPaths = metadata.directoryPaths;

// Build a Set of metadata entries
metadata.metadataEntries.forEach((rawEntry) => {
const splitEntry = rawEntry.split(':').map((entry) => entry.trim());
// The registry will throw if it doesn't know what this type is.
registry.getTypeByName(splitEntry[0]);
const entry = {
type: splitEntry[0],
fullName: splitEntry.length === 1 ? '*' : splitEntry[1],
};
// Add to the filtered ComponentSet for resolved source paths,
// and the unfiltered ComponentSet to build the correct manifest.
compSetFilter.add(entry);
componentSet?.add(entry);

// if there's a better way to detect
if (splitEntry[1]?.includes('*')) {
// get all components of the type, and then filter by the regex of the fullName
ComponentSet.fromSource({
fsPaths: directoryPaths,
include: new ComponentSet([{ type: splitEntry[0], fullName: ComponentSet.WILDCARD }]),
})
.getSourceComponents()
.toArray()
.filter((cs) => Boolean(cs.fullName.match(new RegExp(splitEntry[1].replace('*', '.*')))))
.map((match) => {
compSetFilter.add(match);
componentSet?.add(match);
});
} else {
const entry = {
type: splitEntry[0],
fullName: splitEntry.length === 1 ? '*' : splitEntry[1],
};
// Add to the filtered ComponentSet for resolved source paths,
// and the unfiltered ComponentSet to build the correct manifest.
compSetFilter.add(entry);
componentSet?.add(entry);
}
});

const directoryPaths = metadata.directoryPaths;
logger.debug(`Searching for matching metadata in directories: ${directoryPaths.join(', ')}`);
const resolvedComponents = ComponentSet.fromSource({ fsPaths: directoryPaths, include: compSetFilter });
componentSet.forceIgnoredPaths = resolvedComponents.forceIgnoredPaths;
44 changes: 44 additions & 0 deletions test/collections/componentSetBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -26,6 +26,21 @@ describe('ComponentSetBuilder', () => {
content: 'MyClass.cls',
xml: 'MyClass.cls-meta.xml',
};

const apexClassWildcardMatch = {
type: 'ApexClass',
fullName: 'MyClassIsAwesome',
content: 'MyClassIsAwesome.cls',
xml: 'MyClassIsAwesome.cls-meta.xml',
};

const apexClassWildcardNoMatch = {
type: 'ApexClass',
fullName: 'MyTableIsAwesome',
content: 'MyTableIsAwesome.cls',
xml: 'MyTableIsAwesome.cls-meta.xml',
};

const customObjectComponent = {
type: 'CustomObject',
fullName: 'MyCustomObject__c',
@@ -275,6 +290,35 @@ describe('ComponentSetBuilder', () => {
expect(compSet.has({ type: 'CustomObject', fullName: '*' })).to.equal(true);
});

it('should create ComponentSet from partial-match fullName (ApexClass:Prop*)', async () => {
componentSet.add(apexClassComponent);
componentSet.add(apexClassWildcardMatch);
fromSourceStub.returns(componentSet);
const packageDir1 = path.resolve('force-app');

const compSet = await ComponentSetBuilder.build({
sourcepath: undefined,
manifest: undefined,
metadata: {
metadataEntries: ['ApexClass:MyClas*'],
directoryPaths: [packageDir1],
},
});
expect(fromSourceStub.calledTwice).to.equal(true);
const fromSourceArgs = fromSourceStub.firstCall.args[0] as FromSourceOptions;
expect(fromSourceArgs).to.have.deep.property('fsPaths', [packageDir1]);
const filter = new ComponentSet();
filter.add({ type: 'ApexClass', fullName: 'MyClass' });
filter.add({ type: 'ApexClass', fullName: 'MyClassIsAwesome' });
filter.add({ type: 'ApexClass', fullName: 'MyTableIsAwesome' });
assert(fromSourceArgs.include instanceof ComponentSet, 'include should be a ComponentSet');
expect(fromSourceArgs.include.getSourceComponents()).to.deep.equal(filter.getSourceComponents());
expect(compSet.size).to.equal(2);
expect(compSet.has(apexClassComponent)).to.equal(true);
expect(compSet.has(apexClassWildcardMatch)).to.equal(true);
expect(compSet.has(apexClassWildcardNoMatch)).to.equal(false);
});

it('should create ComponentSet from metadata and multiple package directories', async () => {
componentSet.add(apexClassComponent);
const apexClassComponent2 = { type: 'ApexClass', fullName: 'MyClass2' };