-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathmetadata-filtering-service.ts
204 lines (190 loc) · 6.32 KB
/
metadata-filtering-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import * as path from "path";
import * as os from "os";
import { MetadataFilteringConstants } from "../constants";
import { IPluginsService } from "../definitions/plugins";
import { IPlatformsDataService } from "../definitions/platform";
import { IProjectData } from "../definitions/project";
import {
IMetadataFilteringService,
INativeApiUsageConfiguration,
INativeApiUsagePluginConfiguration,
} from "../definitions/metadata-filtering-service";
import { IFileSystem } from "../common/declarations";
import { injector } from "../common/yok";
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,
platform
);
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 (nativeApiConfiguration["whitelist-plugins-usages"]) {
const plugins = this.$pluginsService.getAllProductionPlugins(
projectData,
platform
);
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.blacklist || [];
if (itemsToAdd.length) {
blacklistedItems.push(
`// Added from: ${pathToPluginsMetadataConfig}`
);
blacklistedItems.push(...itemsToAdd);
blacklistedItems.push(
`// Finished part from ${pathToPluginsMetadataConfig}${os.EOL}`
);
}
}
}
}
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
): INativeApiUsageConfiguration {
let config: INativeApiUsageConfiguration = 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);