Skip to content

Commit 5bc58a7

Browse files
committed
refactor: cocoapods override logic
1 parent 9c8ef41 commit 5bc58a7

9 files changed

+41
-27
lines changed

lib/definitions/platform.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ interface INodeModulesBuilder {
5252
interface IPrepareNodeModulesData {
5353
platformData: IPlatformData;
5454
projectData: IProjectData;
55-
forcePluginNativePrepare: boolean;
5655
}
5756

5857
interface INodeModulesDependenciesBuilder {

lib/definitions/plugins.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ interface IPreparePluginNativeCodeData {
2121
pluginData: IPluginData;
2222
platform: string;
2323
projectData: IProjectData;
24-
forcePluginNativePrepare: boolean;
2524
}
2625

2726
interface IPackageJsonDepedenciesResult {

lib/services/cocoapods-service.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ ${versionResolutionHint}`);
9393
finalPodfileContent = this.$cocoaPodsPlatformManager.addPlatformSection(projectData, podfilePlatformData, finalPodfileContent);
9494
}
9595

96-
if (this.isMainPodFile(podfilePath, projectData, platformData) && projectData.nsConfig && projectData.nsConfig.overridePods) {
97-
finalPodfileContent = this.overridePodsFromFile(finalPodfileContent, projectData, platformData);
98-
}
99-
10096
finalPodfileContent = `${finalPodfileContent.trim()}${EOL}${EOL}${podfileContent.trim()}${EOL}`;
10197
this.saveProjectPodfile(projectData, finalPodfileContent, nativeProjectPath);
10298
}
@@ -234,14 +230,13 @@ ${versionResolutionHint}`);
234230
}
235231

236232
private buildPodfileContent(pluginPodFilePath: string, pluginName: string, projectData: IProjectData, platformData: IPlatformData): { podfileContent: string, replacedFunctions: IRubyFunction[], podfilePlatformData: IPodfilePlatformData } {
237-
const mainPodfilePath = this.getMainPodFilePath(projectData, platformData);
238233
const pluginPodfileContent = this.$fs.readText(pluginPodFilePath);
239234
const data = this.replaceHookContent(CocoaPodsService.PODFILE_POST_INSTALL_SECTION_NAME, pluginPodfileContent, pluginName);
240235
const cocoapodsData = this.$cocoaPodsPlatformManager.replacePlatformRow(data.replacedContent, pluginPodFilePath);
241236
const podfilePlatformData = cocoapodsData.podfilePlatformData;
242237
let replacedContent = cocoapodsData.replacedContent;
243238

244-
if (projectData.nsConfig && projectData.nsConfig.overridePods && mainPodfilePath !== pluginPodFilePath) {
239+
if (projectData.nsConfig && projectData.nsConfig.overridePods && !this.isMainPodFile(pluginPodFilePath, projectData, platformData)) {
245240
replacedContent = this.overridePodsFromFile(replacedContent, projectData, platformData);
246241
}
247242

lib/services/ios-project-service.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,6 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
495495
await this.prepareResources(pluginPlatformsFolderPath, pluginData, projectData);
496496
await this.prepareFrameworks(pluginPlatformsFolderPath, pluginData, projectData);
497497
await this.prepareStaticLibs(pluginPlatformsFolderPath, pluginData, projectData);
498-
const platformData = this.getPlatformData(projectData);
499-
await this.$cocoapodsService.applyPodfileToProject(pluginData.name, this.$cocoapodsService.getPluginPodfilePath(pluginData), projectData, platformData);
500498
}
501499

502500
public async removePluginNativeCode(pluginData: IPluginData, projectData: IProjectData): Promise<void> {
@@ -512,8 +510,10 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
512510

513511
public async handleNativeDependenciesChange(projectData: IProjectData, opts: IRelease): Promise<void> {
514512
const platformData = this.getPlatformData(projectData);
515-
513+
const pluginsData = await this.getAllInstalledPlugins(projectData);
516514
this.setProductBundleIdentifier(projectData);
515+
516+
await this.applyPluginsCocoaPods(pluginsData, projectData, platformData);
517517
await this.$cocoapodsService.applyPodfileFromAppResources(projectData, platformData);
518518

519519
const projectPodfilePath = this.$cocoapodsService.getProjectPodfilePath(platformData.projectRoot);
@@ -528,7 +528,7 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
528528

529529
const pbxProjPath = this.getPbxProjPath(projectData);
530530
this.$iOSExtensionsService.removeExtensions({ pbxProjPath });
531-
await this.addExtensions(projectData);
531+
await this.addExtensions(projectData, pluginsData);
532532
}
533533

534534
public beforePrepareAllPlugins(): Promise<void> {
@@ -641,18 +641,17 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
641641
this.savePbxProj(project, projectData);
642642
}
643643

644-
private async addExtensions(projectData: IProjectData): Promise<void> {
644+
private async addExtensions(projectData: IProjectData, pluginsData: IPluginData[]): Promise<void> {
645645
const resorcesExtensionsPath = path.join(
646646
projectData.getAppResourcesDirectoryPath(),
647647
this.getPlatformData(projectData).normalizedPlatformName, constants.NATIVE_EXTENSION_FOLDER
648648
);
649649
const platformData = this.getPlatformData(projectData);
650650
const pbxProjPath = this.getPbxProjPath(projectData);
651651
const addedExtensionsFromResources = await this.$iOSExtensionsService.addExtensionsFromPath({ extensionsFolderPath: resorcesExtensionsPath, projectData, platformData, pbxProjPath });
652-
const plugins = await this.getAllInstalledPlugins(projectData);
653652
let addedExtensionsFromPlugins = false;
654-
for (const pluginIndex in plugins) {
655-
const pluginData = plugins[pluginIndex];
653+
for (const pluginIndex in pluginsData) {
654+
const pluginData = pluginsData[pluginIndex];
656655
const pluginPlatformsFolderPath = pluginData.pluginPlatformsFolderPath(IOSProjectService.IOS_PLATFORM_NAME);
657656

658657
const extensionPath = path.join(pluginPlatformsFolderPath, constants.NATIVE_EXTENSION_FOLDER);
@@ -828,6 +827,15 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
828827
this.$logger.warn("[WARNING]: The CFBundleIdentifier key inside the 'Info.plist' will be overriden by the 'id' inside 'package.json'.");
829828
}
830829
}
830+
831+
private async applyPluginsCocoaPods(pluginsData: IPluginData[], projectData: IProjectData, platformData: IPlatformData) {
832+
for (const pluginIndex in pluginsData) {
833+
const pluginData = pluginsData[pluginIndex];
834+
if (this.$fs.exists(pluginData.pluginPlatformsFolderPath(platformData.normalizedPlatformName))) {
835+
await this.$cocoapodsService.applyPodfileToProject(pluginData.name, this.$cocoapodsService.getPluginPodfilePath(pluginData), projectData, platformData);
836+
}
837+
}
838+
}
831839
}
832840

833841
$injector.register("iOSProjectService", IOSProjectService);

lib/services/platform/prepare-native-platform-service.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class PrepareNativePlatformService implements IPrepareNativePlatformServi
2424
const hasConfigChange = !changesInfo || changesInfo.configChanged;
2525
const hasChangesRequirePrepare = !changesInfo || changesInfo.changesRequirePrepare;
2626

27-
const hasChanges = hasNativeModulesChange || hasConfigChange || hasChangesRequirePrepare || changesInfo.nsConfigChanged;
27+
const hasChanges = hasNativeModulesChange || hasConfigChange || hasChangesRequirePrepare;
2828

2929
if (changesInfo.hasChanges) {
3030
await this.cleanProject(platformData, { release });
@@ -36,8 +36,8 @@ export class PrepareNativePlatformService implements IPrepareNativePlatformServi
3636
await platformData.platformProjectService.prepareProject(projectData, prepareData);
3737
}
3838

39-
if (hasNativeModulesChange || changesInfo.nsConfigChanged) {
40-
await this.$nodeModulesBuilder.prepareNodeModules({platformData, projectData, forcePluginNativePrepare: changesInfo.nsConfigChanged});
39+
if (hasNativeModulesChange) {
40+
await this.$nodeModulesBuilder.prepareNodeModules({platformData, projectData});
4141
}
4242

4343
if (hasNativeModulesChange || hasConfigChange) {

lib/services/plugins-service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class PluginsService implements IPluginsService {
114114
}
115115
}
116116

117-
public async preparePluginNativeCode({pluginData, platform, projectData, forcePluginNativePrepare}: IPreparePluginNativeCodeData): Promise<void> {
117+
public async preparePluginNativeCode({pluginData, platform, projectData}: IPreparePluginNativeCodeData): Promise<void> {
118118
const platformData = this.$platformsDataService.getPlatformData(platform, projectData);
119119
pluginData.pluginPlatformsFolderPath = (_platform: string) => path.join(pluginData.fullPath, "platforms", _platform.toLowerCase());
120120

@@ -126,7 +126,7 @@ export class PluginsService implements IPluginsService {
126126
const oldPluginNativeHashes = allPluginsNativeHashes[pluginData.name];
127127
const currentPluginNativeHashes = await this.getPluginNativeHashes(pluginPlatformsFolderPath);
128128

129-
if (forcePluginNativePrepare || !oldPluginNativeHashes || this.$filesHashService.hasChangesInShasums(oldPluginNativeHashes, currentPluginNativeHashes)) {
129+
if (!oldPluginNativeHashes || this.$filesHashService.hasChangesInShasums(oldPluginNativeHashes, currentPluginNativeHashes)) {
130130
await platformData.platformProjectService.preparePluginNativeCode(pluginData, projectData);
131131
this.setPluginNativeHashes({
132132
pathToPluginsBuildFile,

lib/tools/node-modules/node-modules-builder.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export class NodeModulesBuilder implements INodeModulesBuilder {
55
private $pluginsService: IPluginsService
66
) { }
77

8-
public async prepareNodeModules({platformData , projectData, forcePluginNativePrepare}: IPrepareNodeModulesData): Promise<void> {
8+
public async prepareNodeModules({platformData , projectData}: IPrepareNodeModulesData): Promise<void> {
99
const dependencies = this.$nodeModulesDependenciesBuilder.getProductionDependencies(projectData.projectDir);
1010
if (_.isEmpty(dependencies)) {
1111
return;
@@ -19,7 +19,7 @@ export class NodeModulesBuilder implements INodeModulesBuilder {
1919
if (isPlugin) {
2020
this.$logger.debug(`Successfully prepared plugin ${dependency.name} for ${platformData.normalizedPlatformName.toLowerCase()}.`);
2121
const pluginData = this.$pluginsService.convertToPluginData(dependency, projectData.projectDir);
22-
await this.$pluginsService.preparePluginNativeCode({pluginData, platform: platformData.normalizedPlatformName.toLowerCase(), projectData, forcePluginNativePrepare});
22+
await this.$pluginsService.preparePluginNativeCode({pluginData, platform: platformData.normalizedPlatformName.toLowerCase(), projectData});
2323
}
2424
}
2525
}

test/ios-project-service.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,18 @@ describe("Cocoapods support", () => {
318318
const samplePluginData = {
319319
pluginPlatformsFolderPath(platform: string): string {
320320
return samplePluginPlatformsFolderPath;
321-
}
321+
},
322+
name: "somePlugin"
322323
};
323324
const projectData: IProjectData = testInjector.resolve("projectData");
325+
const pluginsService = testInjector.resolve("pluginsService");
326+
pluginsService.getAllInstalledPlugins = () => {
327+
return [samplePluginData];
328+
};
329+
const cocoapodsService = testInjector.resolve("cocoapodsService");
330+
cocoapodsService.executePodInstall = async () => { /* */ };
324331

325-
await iOSProjectService.preparePluginNativeCode(samplePluginData, projectData);
332+
await iOSProjectService.handleNativeDependenciesChange(projectData);
326333

327334
const projectPodfilePath = join(platformsFolderPath, "Podfile");
328335
assert.isTrue(fs.exists(projectPodfilePath));
@@ -403,8 +410,14 @@ describe("Cocoapods support", () => {
403410
fullPath: "fullPath"
404411
};
405412
const projectData: IProjectData = testInjector.resolve("projectData");
413+
const pluginsService = testInjector.resolve("pluginsService");
414+
pluginsService.getAllInstalledPlugins = () => {
415+
return [samplePluginData];
416+
};
417+
const cocoapodsService = testInjector.resolve("cocoapodsService");
418+
cocoapodsService.executePodInstall = async () => { /* */ };
406419

407-
await iOSProjectService.preparePluginNativeCode(samplePluginData, projectData);
420+
await iOSProjectService.handleNativeDependenciesChange(projectData);
408421

409422
const projectPodfilePath = join(platformsFolderPath, "Podfile");
410423
assert.isTrue(fs.exists(projectPodfilePath));

test/plugins-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ describe("Plugins service", () => {
570570
`\n@#[line:1,col:39].` +
571571
`\n@#[line:1,col:39].`;
572572
mockBeginCommand(testInjector, expectedErrorMessage);
573-
await pluginsService.preparePluginNativeCode({pluginData: pluginsService.convertToPluginData(pluginJsonData, projectData.projectDir), platform: "android", projectData, forcePluginNativePrepare: false});
573+
await pluginsService.preparePluginNativeCode({pluginData: pluginsService.convertToPluginData(pluginJsonData, projectData.projectDir), platform: "android", projectData});
574574
});
575575
});
576576

0 commit comments

Comments
 (0)