Skip to content
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
12 changes: 12 additions & 0 deletions __tests__/ios/fcm/GoogleService-InfoCopied.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const fs = require("fs-extra");
const path = require("path");

const testProjectPath = path.join(__dirname, "../../../test-app");
const iosPath = path.join(testProjectPath, "ios");
const googleServicesFile = path.join(iosPath, "GoogleService-Info.plist");

test("GoogleService-Info.plist is copied at the expected location in the iOS project", async () => {
const googleServicesFileExists = fs.existsSync(googleServicesFile);

expect(googleServicesFileExists).toBe(true);
});
129 changes: 79 additions & 50 deletions plugin/src/ios/withGoogleServicesJsonFile.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,97 @@
import { withXcodeProject, IOSConfig, ConfigPlugin } from '@expo/config-plugins';
import {
withXcodeProject,
IOSConfig,
ConfigPlugin,
} from '@expo/config-plugins';

import { FileManagement } from './../helpers/utils/fileManagement';
import type { CustomerIOPluginOptionsIOS } from '../types/cio-types';
import { isFcmPushProvider } from './utils';

export const withGoogleServicesJsonFile: ConfigPlugin<CustomerIOPluginOptionsIOS> = (
config,
cioProps
) => {
export const withGoogleServicesJsonFile: ConfigPlugin<
CustomerIOPluginOptionsIOS
> = (config, cioProps) => {
return withXcodeProject(config, async (props) => {

const pushProvider = cioProps.pushNotification?.provider ?? 'apn';
const useFcm = pushProvider === 'fcm';
const useFcm = isFcmPushProvider(cioProps);
if (!useFcm) {
// Nothing to do, for providers other than FCM, the Google services JSON file isn't needed
return props;
// Nothing to do, for providers other than FCM, the Google services JSON file isn't needed
return props;
}

console.log(
'Only specify Customer.io ios.pushNotification.googleServicesFile config if you are not already including' +
' GoogleService-Info.plist as part of Firebase integration'
);

// googleServicesFile
const iosPath = props.modRequest.platformProjectRoot;
const googleServicesFile = cioProps.pushNotification?.googleServicesFile;
if (!FileManagement.exists(`${iosPath}/GoogleService-Info.plist`)) {
if (googleServicesFile && FileManagement.exists(googleServicesFile)) {
try {
FileManagement.copyFile(
googleServicesFile,
`${iosPath}/GoogleService-Info.plist`
);
const appName = props.modRequest.projectName;

if (FileManagement.exists(`${iosPath}/GoogleService-Info.plist`)) {
console.log(
`File already exists: ${iosPath}/GoogleService-Info.plist. Skipping...`
);
return props;
}

if (
FileManagement.exists(`${iosPath}/${appName}/GoogleService-Info.plist`)
) {
// This is where RN Firebase potentially copies GoogleService-Info.plist
// Do not copy if it's already done by Firebase to avoid conflict in Resources
console.log(
`File already exists: ${iosPath}/${appName}/GoogleService-Info.plist. Skipping...`
);
return props;
}

if (googleServicesFile && FileManagement.exists(googleServicesFile)) {
if (config.ios?.googleServicesFile) {
console.warn(
'Specifying both Expo ios.googleServicesFile and Customer.io ios.pushNotification.googleServicesFile can cause a conflict' +
' duplicating GoogleService-Info.plist in the iOS project resources. Please remove Customer.io ios.pushNotification.googleServicesFile'
);
}

addFileToXcodeProject(props.modResults, "GoogleService-Info.plist");
} catch (e) {
console.error(
`There was an error copying your GoogleService-Info.plist file. You can copy it manually into ${iosPath}/GoogleService-Info.plist`
);
}
} else {
console.error(
`The Google Services file provided in ${googleServicesFile} doesn't seem to exist. You can copy it manually into ${iosPath}/GoogleService-Info.plist`
);
}
} else {
console.log(
`File already exists: ${iosPath}/GoogleService-Info.plist. Skipping...`
);
}

try {
FileManagement.copyFile(
googleServicesFile,
`${iosPath}/GoogleService-Info.plist`
);

addFileToXcodeProject(props.modResults, 'GoogleService-Info.plist');
} catch (e) {
console.error(
`There was an error copying your GoogleService-Info.plist file. You can copy it manually into ${iosPath}/GoogleService-Info.plist`
);
}
} else {
console.error(
`The Google Services file provided in ${googleServicesFile} doesn't seem to exist. You can copy it manually into ${iosPath}/GoogleService-Info.plist`
);
}

return props;
});
};

function addFileToXcodeProject(project: any, fileName: string) {
const groupName = "Resources";
const filepath = fileName;

if (!IOSConfig.XcodeUtils.ensureGroupRecursively(project, groupName)) {
console.error(`Error copying GoogleService-Info.plist. Failed to find or create '${groupName}' group in Xcode.`);
return;
}

// Add GoogleService-Info.plist to the Xcode project
IOSConfig.XcodeUtils.addResourceFileToGroup({
project,
filepath,
groupName,
isBuildFile: true,
});
}
const groupName = 'Resources';
const filepath = fileName;

if (!IOSConfig.XcodeUtils.ensureGroupRecursively(project, groupName)) {
console.error(
`Error copying GoogleService-Info.plist. Failed to find or create '${groupName}' group in Xcode.`
);
return;
}

// Add GoogleService-Info.plist to the Xcode project
IOSConfig.XcodeUtils.addResourceFileToGroup({
project,
filepath,
groupName,
isBuildFile: true,
});
}