Skip to content

Metadata format support #94

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

Merged
merged 6 commits into from
Aug 10, 2020
Merged
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
1 change: 1 addition & 0 deletions src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const messages = {
error_missing_adapter: "Missing adapter '%s' for metadata type '%s'",
error_missing_transformer: "Missing transformer '%s' for metadata type '%s'",
error_missing_metadata_xml: "%s: Metadata xml file missing for '%s'",
error_unsupported_content_metadata_xml: "%s: Unsupported content xml file for '%s'",
error_missing_type_definition: "Missing metadata type definition in registry for id '%s'",
error_no_metadata_xml_ignore: 'Metadata xml file %s is forceignored but is required for %s',
error_no_source_ignore: '%s types require source to be present and %s is forceignored.',
Expand Down
40 changes: 39 additions & 1 deletion src/metadata-registry/adapters/baseSourceAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as registryData from '../data/registry.json';
import { RegistryError, UnexpectedForceIgnore } from '../../errors';
import { parentName } from '../../utils/path';
import { ForceIgnore } from '../forceIgnore';
import { dirname, basename } from 'path';
import { dirname, basename, sep } from 'path';
import { NodeFSTreeContainer } from '../treeContainers';
import { SourceComponent } from '../sourceComponent';
import { MetadataType, SourcePath } from '../../common';
Expand All @@ -26,6 +26,7 @@ export abstract class BaseSourceAdapter implements SourceAdapter {
* folder, including its root metadata xml file.
*/
protected ownFolder = false;
protected metadataWithContent = true;

constructor(
type: MetadataType,
Expand All @@ -47,6 +48,9 @@ export abstract class BaseSourceAdapter implements SourceAdapter {
throw new RegistryError('error_missing_metadata_xml', [path, this.type.name]);
}
rootMetadata = parseMetadataXml(rootMetadataPath);
if (!rootMetadata) {
throw new RegistryError('error_unsupported_content_metadata_xml', [path, this.type.name]);
}
}
if (this.forceIgnore.denies(rootMetadata.path)) {
throw new UnexpectedForceIgnore('error_no_metadata_xml_ignore', [rootMetadata.path, path]);
Expand All @@ -68,6 +72,13 @@ export abstract class BaseSourceAdapter implements SourceAdapter {
return this.populate(component, path);
}

/**
* Control whether metadata and content metadata files are allowed for an adapter.
*/
public allowMetadataWithContent(): boolean {
return this.metadataWithContent;
}

/**
* Determine the related root metadata xml when the path given to `getComponent` isn't one.
*
Expand Down Expand Up @@ -107,7 +118,34 @@ export abstract class BaseSourceAdapter implements SourceAdapter {
} else {
isRootMetadataXml = true;
}
} else if (!this.allowMetadataWithContent()) {
return this.parseAsContentMetadataXml(path);
}
return isRootMetadataXml ? metaXml : undefined;
}

/**
* If the path given to `getComponent` serves as the sole definition (metadata and content)
* for a component, parse the name and return it. This allows matching files in metadata
* format such as:
*
* .../tabs/MyTab.tab
*
* @param path File path of a metadata component
*/
private parseAsContentMetadataXml(path: SourcePath): MetadataXml {
const parentPath = dirname(path);
const parts = parentPath.split(sep);
const typeFolderIndex = parts.lastIndexOf(this.type.directoryName);
const allowedIndex = this.type.inFolder ? parts.length - 2 : parts.length - 1;

if (typeFolderIndex !== allowedIndex) {
return undefined;
}

const match = basename(path).match(/(.+)\.(.+)/);
if (match && this.type.suffix === match[2]) {
return { fullName: match[1], suffix: match[2], path: path };
}
}
}
1 change: 1 addition & 0 deletions src/metadata-registry/adapters/decomposedSourceAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { SourceComponent } from '../sourceComponent';
*/
export class DecomposedSourceAdapter extends MixedContentSourceAdapter {
protected ownFolder = true;
protected metadataWithContent = false;

/**
* If the trigger turns out to be part of a child component, `populate` will build
Expand Down
2 changes: 2 additions & 0 deletions src/metadata-registry/adapters/defaultSourceAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { SourceComponent } from '../sourceComponent';
*```
*/
export class DefaultSourceAdapter extends BaseSourceAdapter {
protected metadataWithContent = false;

/* istanbul ignore next */
protected getRootMetadataXmlPath(trigger: string): SourcePath {
// istanbul ignored for code coverage since this return won't ever be hit,
Expand Down
31 changes: 25 additions & 6 deletions src/metadata-registry/registryAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class RegistryAccess {
}
}

const component = this.resolveComponent(pathForFetch);
const component = this.resolveComponent(pathForFetch, true);
return component ? [component] : [];
}

Expand All @@ -112,8 +112,8 @@ export class RegistryAccess {
const fsPath = join(dir, file);
if (this.tree.isDirectory(fsPath)) {
dirQueue.push(fsPath);
} else if (parseMetadataXml(fsPath)) {
const component = this.resolveComponent(fsPath);
} else if (parseMetadataXml(fsPath) || this.parseAsContentMetadataXml(fsPath)) {
const component = this.resolveComponent(fsPath, false);
if (component) {
components.push(component);
// don't traverse further if not in a root type directory. performance optimization
Expand All @@ -134,15 +134,34 @@ export class RegistryAccess {
return components;
}

private resolveComponent(fsPath: SourcePath): SourceComponent {
if (parseMetadataXml(fsPath) && this.forceIgnore.denies(fsPath)) {
/**
* Any file with a registered suffix is potentially a content metadata file.
*
* @param path File path of a potential content metadata file
*/
private parseAsContentMetadataXml(path: SourcePath): boolean {
return this.registry.suffixes.hasOwnProperty(extName(path));
}

private resolveComponent(fsPath: SourcePath, isResolvingSource: boolean): SourceComponent {
if (
(parseMetadataXml(fsPath) || this.parseAsContentMetadataXml(fsPath)) &&
this.forceIgnore.denies(fsPath)
) {
// don't fetch the component if the metadata xml is denied
return;
}
const type = this.resolveType(fsPath);
if (type) {
const adapter = this.sourceAdapterFactory.getAdapter(type, this.forceIgnore);
return adapter.getComponent(fsPath);
// short circuit the component resolution unless this is a resolve for a
// source path or allowed content-only path, otherwise the adapter
// knows how to handle it
const shouldResolve =
isResolvingSource ||
!this.parseAsContentMetadataXml(fsPath) ||
!adapter.allowMetadataWithContent();
return shouldResolve ? adapter.getComponent(fsPath) : undefined;
}
throw new TypeInferenceError('error_could_not_infer_type', fsPath);
}
Expand Down
5 changes: 5 additions & 0 deletions src/metadata-registry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export interface SourceAdapter {
* @param fsPath Path to resolve
*/
getComponent(fsPath: SourcePath): SourceComponent;

/**
* Whether the adapter allows content-only metadata definitions.
*/
allowMetadataWithContent(): boolean;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions test/metadata-registry/adapters/defaultSourceAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@ describe('DefaultSourceAdapter', () => {
})
);
});

it('should return a SourceComponent when given a content-only metadata file', () => {
const path = join('path', 'to', 'keanus', 'My_Test.keanu');
const type = mockRegistry.types.keanureeves;
const adapter = new DefaultSourceAdapter(type, mockRegistry);
expect(adapter.getComponent(path)).to.deep.equal(
new SourceComponent({
name: 'My_Test',
type,
xml: path,
})
);
});
});
61 changes: 60 additions & 1 deletion test/metadata-registry/registryAccess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { assert, expect } from 'chai';
import { RegistryAccess, SourceComponent, VirtualTreeContainer } from '../../src/metadata-registry';
import { nls } from '../../src/i18n';
import { mockRegistry, kathy, keanu, taraji, tina, simon } from '../mock/registry';
import { mockRegistry, kathy, keanu, taraji, tina, simon, sean } from '../mock/registry';
import { join, basename, dirname } from 'path';
import { TypeInferenceError } from '../../src/errors';
import { RegistryTestUtil } from './registryTestUtil';
Expand Down Expand Up @@ -111,6 +111,46 @@ describe('RegistryAccess', () => {
expect(access.getComponentsFromPath(path)).to.deep.equal([taraji.TARAJI_COMPONENT]);
});

it('Should determine type for path content files', () => {
const path = keanu.KEANU_SOURCE_PATHS[0];
const access = testUtil.createRegistryAccess([
{
dirPath: dirname(path),
children: keanu.KEANU_SOURCE_NAMES,
},
]);
testUtil.stubAdapters([
{
type: mockRegistry.types.keanureeves,
componentMappings: [{ path, component: keanu.KEANU_CONTENT_COMPONENT }],
allowContent: false,
},
]);
expect(access.getComponentsFromPath(path)).to.deep.equal([keanu.KEANU_CONTENT_COMPONENT]);
});

it('Should determine type for inFolder path content files', () => {
const path = sean.SEAN_FOLDER;
const access = testUtil.createRegistryAccess([
{
dirPath: path,
children: sean.SEAN_NAMES,
},
]);
const componentMappings = sean.SEAN_PATHS.map((p: string, i: number) => ({
path: p,
component: sean.SEAN_COMPONENTS[i],
}));
testUtil.stubAdapters([
{
type: mockRegistry.types.seanconnerys,
componentMappings,
allowContent: false,
},
]);
expect(access.getComponentsFromPath(path)).to.deep.equal(sean.SEAN_COMPONENTS);
});

it('Should not mistake folder component of a mixed content type as that type', () => {
// this test has coveage on non-mixedContent types as well by nature of the execution path
const path = tina.TINA_FOLDER_XML;
Expand Down Expand Up @@ -162,6 +202,25 @@ describe('RegistryAccess', () => {
]);
expect(access.getComponentsFromPath(path).length).to.equal(0);
});

it('Should not return a component if path to content metadata xml is forceignored', () => {
const path = keanu.KEANU_XML_PATHS[0];
const access = testUtil.createRegistryAccess([
{
dirPath: dirname(path),
children: [basename(path)],
},
]);
testUtil.stubForceIgnore({ seed: path, deny: [path] });
testUtil.stubAdapters([
{
type: mockRegistry.types.keanureeves,
// should not be returned
componentMappings: [{ path, component: keanu.KEANU_COMPONENT }],
},
]);
expect(access.getComponentsFromPath(path).length).to.equal(0);
});
});

describe('Directory Paths', () => {
Expand Down
2 changes: 2 additions & 0 deletions test/metadata-registry/registryTestUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class RegistryTestUtil {
config: {
type: MetadataType;
componentMappings: { path: SourcePath; component: SourceComponent }[];
allowContent?: boolean;
}[]
): void {
const getAdapterStub = this.env.stub(SourceAdapterFactory.prototype, 'getAdapter');
Expand All @@ -42,6 +43,7 @@ export class RegistryTestUtil {
}
getAdapterStub.withArgs(entry.type).returns({
getComponent: (path: SourcePath) => componentMap[path],
allowMetadataWithContent: () => entry.allowContent,
});
}
}
Expand Down
11 changes: 10 additions & 1 deletion test/mock/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ export const mockRegistry = {
},
},
},
seanconnerys: {
id: 'seanconnerys',
directoryName: 'seans',
inFolder: true,
name: 'SeanConnery',
suffix: 'sean',
},
},
suffixes: {
kathy: 'kathybates',
Expand All @@ -94,6 +101,7 @@ export const mockRegistry = {
tinafeyFolder: 'tinafeyfolder',
genewilder: 'gene',
reginaking: 'regina',
sean: 'seanconnerys',
},
mixedContent: {
dwaynes: 'dwaynejohnson',
Expand Down Expand Up @@ -121,7 +129,8 @@ import * as taraji from './tarajiConstants';
import * as tina from './tinaConstants';
import * as gene from './geneConstants';
import * as regina from './reginaConstants';
export { kathy, keanu, simon, taraji, tina, gene, regina };
import * as sean from './seanConstants';
export { kathy, keanu, simon, taraji, tina, gene, regina, sean };

// Mixed content
export const DWAYNE_DIR = join('path', 'to', 'dwaynes');
Expand Down
5 changes: 5 additions & 0 deletions test/mock/registry/keanuConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ export const KEANU_COMPONENT = new SourceComponent({
xml: KEANU_XML_PATHS[0],
content: KEANU_SOURCE_PATHS[0],
});
export const KEANU_CONTENT_COMPONENT = new SourceComponent({
name: 'a',
type,
xml: KEANU_SOURCE_PATHS[0],
});
34 changes: 34 additions & 0 deletions test/mock/registry/seanConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 { join } from 'path';
import { mockRegistry } from '.';
import { SourceComponent } from '../../../src/metadata-registry';

// Constants for a type that uses the BaseSourceAdapter and is inFolder
const type = mockRegistry.types.seanconnerys;

export const SEAN_DIR = join('path', 'to', 'seans');
export const SEAN_FOLDER = join(SEAN_DIR, 'A_Folder');
export const SEAN_NAMES = ['a.sean', 'b.sean', 'c.sean'];
export const SEAN_PATHS = SEAN_NAMES.map((p) => join(SEAN_FOLDER, p));
export const SEAN_COMPONENTS: SourceComponent[] = [
Copy link
Contributor

Choose a reason for hiding this comment

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

i like my components shaken, not stirred

new SourceComponent({
name: `A_Folder/a`,
type,
xml: SEAN_PATHS[0],
}),
new SourceComponent({
name: 'A_Folder/b',
type,
xml: SEAN_PATHS[1],
}),
new SourceComponent({
name: 'A_Folder/c',
type,
xml: SEAN_PATHS[2],
}),
];