-
-
Notifications
You must be signed in to change notification settings - Fork 197
feat: add support for metadata filtering #5224
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Describes service used to generate necessary files to filter the native metadata generation. | ||
*/ | ||
interface INativeApiUsageConfiguartion { | ||
/** | ||
* Defines if the content of plugins' native-api-usage files will be used and included in the whitelist content. | ||
*/ | ||
["whitelist-plugins-usages"]: boolean; | ||
|
||
/** | ||
* Defines APIs which will be inlcuded in the metadata. | ||
*/ | ||
whitelist: string[]; | ||
|
||
/** | ||
* Defines APIs which will be excluded from the metadata. | ||
*/ | ||
blacklist: string[]; | ||
} | ||
|
||
/** | ||
* Describes the content of plugin's native-api-usage.json file located in `<path to plugin>/platforms/<platform> directory. | ||
*/ | ||
interface INativeApiUsagePluginConfiguration { | ||
/** | ||
* Defines APIs which are used by the plugin and which should be whitelisted by the application using this plugin. | ||
*/ | ||
uses: string[]; | ||
} | ||
|
||
/** | ||
* Describes service used to generate neccesary files to filter the metadata generation. | ||
*/ | ||
interface IMetadataFilteringService { | ||
/** | ||
* Cleans old metadata filters and creates new ones for the current project and platform. | ||
* @param {IProjectData} projectData Information about the current project. | ||
* @param {string} platform The platform for which metadata should be generated. | ||
* @returns {void} | ||
*/ | ||
generateMetadataFilters(projectData: IProjectData, platform: string): void; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import * as path from "path"; | ||
import * as os from "os"; | ||
import { MetadataFilteringConstants } from "../constants"; | ||
|
||
export class MetadataFilteringService implements IMetadataFilteringService { | ||
constructor(private $fs: IFileSystem, | ||
private $pluginsService: IPluginsService, | ||
private $mobileHelper: Mobile.IMobileHelper, | ||
private $platformsDataService: IPlatformsDataService, | ||
private $logger: ILogger) { } | ||
|
||
public generateMetadataFilters(projectData: IProjectData, platform: string): void { | ||
this.generateWhitelist(projectData, platform); | ||
this.generateBlacklist(projectData, platform); | ||
} | ||
|
||
private generateWhitelist(projectData: IProjectData, platform: string): void { | ||
const platformsDirPath = this.getPlatformsDirPath(projectData, platform); | ||
const pathToWhitelistFile = path.join(platformsDirPath, MetadataFilteringConstants.WHITELIST_FILE_NAME); | ||
this.$fs.deleteFile(pathToWhitelistFile); | ||
|
||
const nativeApiConfiguration = this.getNativeApiConfigurationForPlatform(projectData, platform); | ||
if (nativeApiConfiguration) { | ||
const whitelistedItems: string[] = []; | ||
if (nativeApiConfiguration["whitelist-plugins-usages"]) { | ||
const plugins = this.$pluginsService.getAllProductionPlugins(projectData); | ||
for (const pluginData of plugins) { | ||
const pathToPlatformsDir = pluginData.pluginPlatformsFolderPath(platform); | ||
const pathToPluginsMetadataConfig = path.join(pathToPlatformsDir, MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME); | ||
if (this.$fs.exists(pathToPluginsMetadataConfig)) { | ||
const pluginConfig: INativeApiUsagePluginConfiguration = this.$fs.readJson(pathToPluginsMetadataConfig) || {}; | ||
this.$logger.trace(`Adding content of ${pathToPluginsMetadataConfig} to whitelisted items of metadata filtering: ${JSON.stringify(pluginConfig, null, 2)}`); | ||
const itemsToAdd = pluginConfig.uses || []; | ||
if (itemsToAdd.length) { | ||
whitelistedItems.push(`// Added from: ${pathToPluginsMetadataConfig}`); | ||
whitelistedItems.push(...itemsToAdd); | ||
whitelistedItems.push(`// Finished part from ${pathToPluginsMetadataConfig}${os.EOL}`); | ||
} | ||
} | ||
} | ||
} | ||
|
||
const applicationWhitelistedItems = nativeApiConfiguration.whitelist || []; | ||
if (applicationWhitelistedItems.length) { | ||
this.$logger.trace(`Adding content from application to whitelisted items of metadata filtering: ${JSON.stringify(applicationWhitelistedItems, null, 2)}`); | ||
|
||
whitelistedItems.push(`// Added from application`); | ||
whitelistedItems.push(...applicationWhitelistedItems); | ||
whitelistedItems.push(`// Finished part from application${os.EOL}`); | ||
} | ||
|
||
if (whitelistedItems.length) { | ||
this.$fs.writeFile(pathToWhitelistFile, whitelistedItems.join(os.EOL)); | ||
} | ||
} | ||
} | ||
|
||
private generateBlacklist(projectData: IProjectData, platform: string): void { | ||
const platformsDirPath = this.getPlatformsDirPath(projectData, platform); | ||
const pathToBlacklistFile = path.join(platformsDirPath, MetadataFilteringConstants.BLACKLIST_FILE_NAME); | ||
this.$fs.deleteFile(pathToBlacklistFile); | ||
|
||
const nativeApiConfiguration = this.getNativeApiConfigurationForPlatform(projectData, platform); | ||
if (nativeApiConfiguration) { | ||
const blacklistedItems: string[] = nativeApiConfiguration.blacklist || []; | ||
|
||
if (blacklistedItems.length) { | ||
this.$fs.writeFile(pathToBlacklistFile, blacklistedItems.join(os.EOL)); | ||
} | ||
} else { | ||
this.$logger.trace(`There's no application configuration for metadata filtering for platform ${platform}. Full metadata will be generated.`); | ||
} | ||
} | ||
|
||
private getNativeApiConfigurationForPlatform(projectData: IProjectData, platform: string): INativeApiUsageConfiguartion { | ||
let config: INativeApiUsageConfiguartion = null; | ||
const pathToApplicationConfigurationFile = this.getPathToApplicationConfigurationForPlatform(projectData, platform); | ||
if (this.$fs.exists(pathToApplicationConfigurationFile)) { | ||
config = this.$fs.readJson(pathToApplicationConfigurationFile); | ||
} | ||
|
||
return config; | ||
} | ||
|
||
private getPlatformsDirPath(projectData: IProjectData, platform: string): string { | ||
const platformData = this.$platformsDataService.getPlatformData(platform, projectData); | ||
return platformData.projectRoot; | ||
} | ||
|
||
private getPathToApplicationConfigurationForPlatform(projectData: IProjectData, platform: string): string { | ||
return path.join(projectData.appResourcesDirectoryPath, this.$mobileHelper.normalizePlatformName(platform), MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME); | ||
} | ||
} | ||
|
||
$injector.register("metadataFilteringService", MetadataFilteringService); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
import { MetadataFilteringService } from "../../lib/services/metadata-filtering-service"; | ||
import { Yok } from "../../lib/common/yok"; | ||
import { LoggerStub, FileSystemStub } from "../stubs"; | ||
import { assert } from "chai"; | ||
import * as path from "path"; | ||
import { MetadataFilteringConstants } from "../../lib/constants"; | ||
import { EOL } from "os"; | ||
|
||
describe("metadataFilteringService", () => { | ||
const platform = "platform"; | ||
const projectDir = "projectDir"; | ||
const projectRoot = path.join(projectDir, "platforms", platform); | ||
const projectData: any = { | ||
appResourcesDirectoryPath: path.join(projectDir, "App_Resources") | ||
}; | ||
const blacklistArray: string[] = ["blacklisted1", "blacklisted2"]; | ||
const whitelistArray: string[] = ["whitelisted1", "whitelisted2"]; | ||
const appResourcesNativeApiUsageFilePath = path.join(projectData.appResourcesDirectoryPath, platform, MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME); | ||
const pluginPlatformsDir = path.join("pluginDir", platform); | ||
const pluginNativeApiUsageFilePath = path.join(pluginPlatformsDir, MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME); | ||
const pluginsUses: string[] = ["pluginUses1", "pluginUses2"]; | ||
|
||
const createTestInjector = (input?: { hasPlugins: boolean }): IInjector => { | ||
const testInjector = new Yok(); | ||
testInjector.register("logger", LoggerStub); | ||
testInjector.register("fs", FileSystemStub); | ||
testInjector.register("pluginsService", { | ||
getAllProductionPlugins: (prjData: IProjectData, dependencies?: IDependencyData[]): IPluginData[] => { | ||
const plugins = !!(input && input.hasPlugins) ? [ | ||
<any>{ | ||
pluginPlatformsFolderPath: (pl: string) => pluginPlatformsDir | ||
} | ||
] : []; | ||
|
||
return plugins; | ||
} | ||
}); | ||
testInjector.register("mobileHelper", { | ||
normalizePlatformName: (pl: string) => pl | ||
}); | ||
testInjector.register("platformsDataService", { | ||
getPlatformData: (pl: string, prjData: IProjectData): IPlatformData => (<any>{ projectRoot }) | ||
}); | ||
return testInjector; | ||
}; | ||
|
||
describe("generateMetadataFilters", () => { | ||
const mockFs = (input: { testInjector: IInjector, readJsonData?: any, writeFileAction?: (filePath: string, data: string) => void, existingFiles?: any[] }): { fs: FileSystemStub, dataWritten: IDictionary<any> } => { | ||
const fs = input.testInjector.resolve<FileSystemStub>("fs"); | ||
const dataWritten: IDictionary<any> = {}; | ||
|
||
if (input.writeFileAction) { | ||
fs.writeFile = (filePath: string, data: string) => input.writeFileAction(filePath, data); | ||
} else { | ||
fs.writeFile = (filePath: string, data: string) => dataWritten[filePath] = data; | ||
} | ||
|
||
if (input.readJsonData) { | ||
fs.readJson = (filePath: string) => input.readJsonData[filePath]; | ||
} | ||
|
||
if (input.existingFiles) { | ||
fs.exists = (filePath: string) => input.existingFiles.indexOf(filePath) !== -1; | ||
} | ||
|
||
return { fs, dataWritten }; | ||
}; | ||
|
||
it("deletes previously generated files for metadata filtering", () => { | ||
const testInjector = createTestInjector(); | ||
const metadataFilteringService: IMetadataFilteringService = testInjector.resolve(MetadataFilteringService); | ||
const { fs } = mockFs({ | ||
testInjector, writeFileAction: (filePath: string, data: string) => { | ||
throw new Error(`No data should be written when the ${MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME} does not exist in App_Resource/<platform>`); | ||
} | ||
}); | ||
|
||
metadataFilteringService.generateMetadataFilters(projectData, platform); | ||
|
||
const expectedDeletedFiles = [ | ||
path.join(projectRoot, MetadataFilteringConstants.WHITELIST_FILE_NAME), | ||
path.join(projectRoot, MetadataFilteringConstants.BLACKLIST_FILE_NAME) | ||
]; | ||
assert.deepEqual(fs.deletedFiles, expectedDeletedFiles); | ||
}); | ||
|
||
it(`generates ${MetadataFilteringConstants.BLACKLIST_FILE_NAME} when the file ${MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME} exists in App_Resources/<platform>`, () => { | ||
const testInjector = createTestInjector(); | ||
const metadataFilteringService: IMetadataFilteringService = testInjector.resolve(MetadataFilteringService); | ||
const { dataWritten } = mockFs({ | ||
testInjector, | ||
existingFiles: [appResourcesNativeApiUsageFilePath], | ||
readJsonData: { [`${appResourcesNativeApiUsageFilePath}`]: { blacklist: blacklistArray } } | ||
}); | ||
|
||
metadataFilteringService.generateMetadataFilters(projectData, platform); | ||
|
||
assert.deepEqual(dataWritten, { [path.join(projectRoot, MetadataFilteringConstants.BLACKLIST_FILE_NAME)]: blacklistArray.join(EOL) }); | ||
}); | ||
|
||
const getExpectedWhitelistContent = (input: { applicationWhitelist?: string[], pluginWhitelist?: string[] }): string => { | ||
let finalContent = ""; | ||
if (input.pluginWhitelist) { | ||
finalContent += `// Added from: ${pluginNativeApiUsageFilePath}${EOL}${input.pluginWhitelist.join(EOL)}${EOL}// Finished part from ${pluginNativeApiUsageFilePath}${EOL}`; | ||
} | ||
|
||
if (input.applicationWhitelist) { | ||
if (finalContent !== "") { | ||
finalContent += EOL; | ||
} | ||
|
||
finalContent += `// Added from application${EOL}${input.applicationWhitelist.join(EOL)}${EOL}// Finished part from application${EOL}`; | ||
} | ||
|
||
return finalContent; | ||
}; | ||
|
||
it(`generates ${MetadataFilteringConstants.WHITELIST_FILE_NAME} when the file ${MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME} exists in App_Resources/<platform>`, () => { | ||
const testInjector = createTestInjector(); | ||
const metadataFilteringService: IMetadataFilteringService = testInjector.resolve(MetadataFilteringService); | ||
const { dataWritten } = mockFs({ | ||
testInjector, | ||
existingFiles: [appResourcesNativeApiUsageFilePath], | ||
readJsonData: { [`${appResourcesNativeApiUsageFilePath}`]: { whitelist: whitelistArray } }, | ||
}); | ||
|
||
metadataFilteringService.generateMetadataFilters(projectData, platform); | ||
assert.deepEqual(dataWritten, { [path.join(projectRoot, MetadataFilteringConstants.WHITELIST_FILE_NAME)]: getExpectedWhitelistContent({ applicationWhitelist: whitelistArray }) }); | ||
}); | ||
|
||
it(`generates ${MetadataFilteringConstants.WHITELIST_FILE_NAME} with content from plugins when the file ${MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME} exists in App_Resources/<platform> and whitelist-plugins-usages is true`, () => { | ||
const testInjector = createTestInjector({ hasPlugins: true }); | ||
const metadataFilteringService: IMetadataFilteringService = testInjector.resolve(MetadataFilteringService); | ||
const { dataWritten } = mockFs({ | ||
testInjector, | ||
existingFiles: [appResourcesNativeApiUsageFilePath, pluginNativeApiUsageFilePath], | ||
readJsonData: { | ||
[`${appResourcesNativeApiUsageFilePath}`]: { ["whitelist-plugins-usages"]: true }, | ||
[`${pluginNativeApiUsageFilePath}`]: { uses: whitelistArray } | ||
}, | ||
}); | ||
|
||
metadataFilteringService.generateMetadataFilters(projectData, platform); | ||
assert.deepEqual(dataWritten, { [path.join(projectRoot, MetadataFilteringConstants.WHITELIST_FILE_NAME)]: getExpectedWhitelistContent({ pluginWhitelist: whitelistArray }) }); | ||
}); | ||
|
||
it(`generates all files when both plugins and applications filters are included`, () => { | ||
const testInjector = createTestInjector({ hasPlugins: true }); | ||
const metadataFilteringService: IMetadataFilteringService = testInjector.resolve(MetadataFilteringService); | ||
const { dataWritten } = mockFs({ | ||
testInjector, | ||
existingFiles: [appResourcesNativeApiUsageFilePath, pluginNativeApiUsageFilePath], | ||
readJsonData: { | ||
[`${appResourcesNativeApiUsageFilePath}`]: { | ||
whitelist: whitelistArray, | ||
blacklist: blacklistArray, | ||
["whitelist-plugins-usages"]: true | ||
}, | ||
[`${pluginNativeApiUsageFilePath}`]: { uses: pluginsUses } | ||
}, | ||
}); | ||
|
||
metadataFilteringService.generateMetadataFilters(projectData, platform); | ||
const expectedWhitelist = getExpectedWhitelistContent({ pluginWhitelist: pluginsUses, applicationWhitelist: whitelistArray }); | ||
|
||
assert.deepEqual(dataWritten, { | ||
[path.join(projectRoot, MetadataFilteringConstants.WHITELIST_FILE_NAME)]: expectedWhitelist, | ||
[path.join(projectRoot, MetadataFilteringConstants.BLACKLIST_FILE_NAME)]: blacklistArray.join(EOL) | ||
}); | ||
}); | ||
|
||
it(`skips plugins ${MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME} files when whitelist-plugins-usages in App_Resources is false`, () => { | ||
const testInjector = createTestInjector({ hasPlugins: true }); | ||
const metadataFilteringService: IMetadataFilteringService = testInjector.resolve(MetadataFilteringService); | ||
const { dataWritten } = mockFs({ | ||
testInjector, | ||
existingFiles: [appResourcesNativeApiUsageFilePath, pluginNativeApiUsageFilePath], | ||
readJsonData: { | ||
[`${appResourcesNativeApiUsageFilePath}`]: { | ||
whitelist: whitelistArray, | ||
blacklist: blacklistArray, | ||
["whitelist-plugins-usages"]: false | ||
}, | ||
[`${pluginNativeApiUsageFilePath}`]: { uses: pluginsUses } | ||
}, | ||
}); | ||
|
||
metadataFilteringService.generateMetadataFilters(projectData, "platform"); | ||
const expectedWhitelist = getExpectedWhitelistContent({ applicationWhitelist: whitelistArray }); | ||
|
||
assert.deepEqual(dataWritten, { | ||
[path.join(projectRoot, MetadataFilteringConstants.WHITELIST_FILE_NAME)]: expectedWhitelist, | ||
[path.join(projectRoot, MetadataFilteringConstants.BLACKLIST_FILE_NAME)]: blacklistArray.join(EOL) | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.