Skip to content
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

Make targetName configurable #9

Merged
merged 1 commit into from
Dec 13, 2024
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
4 changes: 4 additions & 0 deletions plugin/src/withAirship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export type AirshipIOSPluginProps = {
* The local path to a Notification Service Extension Info.plist.
*/
notificationServiceInfo?: string;
/**
* Optional. Defaults to NotificationServiceExtension if not provided.
*/
notificationServiceTargetName?: string;
/**
* Optional. The Apple Development Team ID used to configure the Notification Service Extension target.
*/
Expand Down
29 changes: 16 additions & 13 deletions plugin/src/withAirshipIOS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { basename, join } from 'path';
import { AirshipIOSPluginProps } from './withAirship';
import { mergeContents, MergeResults } from '@expo/config-plugins/build/utils/generateCode';

const NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME = "AirshipNotificationServiceExtension";
const DEFAULT_NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME = "NotificationServiceExtension";
const NOTIFICATION_SERVICE_FILE_NAME = "AirshipNotificationService.swift";
const NOTIFICATION_SERVICE_INFO_PLIST_FILE_NAME = "AirshipNotificationServiceExtension-Info.plist";

Expand Down Expand Up @@ -54,8 +54,8 @@ async function writeNotificationServiceFilesAsync(props: AirshipIOSPluginProps,

const pluginDir = require.resolve("airship-expo-plugin/package.json");
const sourceDir = join(pluginDir, "../plugin/NotificationServiceExtension/");

const extensionPath = join(projectRoot, "ios", NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME);
const targetName = props.notificationServiceTargetName ?? DEFAULT_NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME
const extensionPath = join(projectRoot, "ios", targetName);

if (!existsSync(extensionPath)) {
mkdirSync(extensionPath, { recursive: true });
Expand Down Expand Up @@ -91,19 +91,20 @@ async function writeNotificationServiceFilesAsync(props: AirshipIOSPluginProps,
};

const withExtensionTargetInXcodeProject: ConfigPlugin<AirshipIOSPluginProps> = (config, props) => {
const targetName = props.notificationServiceTargetName ?? DEFAULT_NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME

return withXcodeProject(config, newConfig => {
const xcodeProject = newConfig.modResults;

if (!!xcodeProject.pbxTargetByName(NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME)) {
console.log(NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME + " already exists in project. Skipping...");
if (!!xcodeProject.pbxTargetByName(targetName)) {
console.log(targetName + " already exists in project. Skipping...");
return newConfig;
}

// Create new PBXGroup for the extension
const extGroup = xcodeProject.addPbxGroup(
[NOTIFICATION_SERVICE_FILE_NAME, NOTIFICATION_SERVICE_INFO_PLIST_FILE_NAME],
NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME,
NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME
targetName,
targetName
);

// Add the new PBXGroup to the top level group. This makes the
Expand All @@ -126,10 +127,10 @@ const withExtensionTargetInXcodeProject: ConfigPlugin<AirshipIOSPluginProps> = (
// Add the Notification Service Extension Target
// This adds PBXTargetDependency and PBXContainerItemProxy
const notificationServiceExtensionTarget = xcodeProject.addTarget(
NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME,
targetName,
"app_extension",
NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME,
`${config.ios?.bundleIdentifier}.${NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME}`
targetName,
`${config.ios?.bundleIdentifier}.${targetName}`
);

// Add build phases to the new Target
Expand All @@ -156,7 +157,7 @@ const withExtensionTargetInXcodeProject: ConfigPlugin<AirshipIOSPluginProps> = (
const configurations = xcodeProject.pbxXCBuildConfigurationSection();
for (const key in configurations) {
if (typeof configurations[key].buildSettings !== "undefined"
&& configurations[key].buildSettings.PRODUCT_NAME == `"${NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME}"`
&& configurations[key].buildSettings.PRODUCT_NAME == `"${targetName}"`
) {
const buildSettingsObj = configurations[key].buildSettings;
buildSettingsObj.IPHONEOS_DEPLOYMENT_TARGET = "14.0";
Expand All @@ -174,9 +175,11 @@ const withExtensionTargetInXcodeProject: ConfigPlugin<AirshipIOSPluginProps> = (
};

const withAirshipServiceExtensionPod: ConfigPlugin<AirshipIOSPluginProps> = (config, props) => {
const targetName = props.notificationServiceTargetName ?? DEFAULT_NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME

return withPodfile(config, async (config) => {
const airshipServiceExtensionPodfileSnippet = `
target '${NOTIFICATION_SERVICE_EXTENSION_TARGET_NAME}' do
target '${targetName}' do
use_frameworks! :linkage => podfile_properties['ios.useFrameworks'].to_sym if podfile_properties['ios.useFrameworks']
use_frameworks! :linkage => ENV['USE_FRAMEWORKS'].to_sym if ENV['USE_FRAMEWORKS']
pod 'AirshipServiceExtension'
Expand Down
Loading