Skip to content

Commit

Permalink
Added Entitlements Helper
Browse files Browse the repository at this point in the history
  • Loading branch information
larcho committed Nov 13, 2024
1 parent 99dd9af commit c89e9e0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
45 changes: 5 additions & 40 deletions packages/react-native/plugin/src/withBreezIOS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { addExtension } from "./addExtension";
import { withTargetPlist } from "./withExtensionInfoPlist";
import { warnOnce } from "./utils";
import { withTargetEntitlementsPlist } from "./withExtensionEntitlementsPlist";

const NOTIFICATION_SERVICE_PODS = (targetName: string) => `
target '${targetName}' do
Expand All @@ -19,40 +20,12 @@ target '${targetName}' do
end
`;

const NOTIFICATION_SERVICE_ENTITLEMENT = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
</dict>
</plist>
`;

type EASAppExtension = {
targetName: string;
bundleIdentifier: string;
entitlements: Record<string, Array<string>>;
};

function generateEntitlementsXML(
entitlements: Record<string, Array<string>>,
): string {
const dictEntries = Object.entries(entitlements)
.map(
([key, values]) => `
<key>${key}</key>
<array>
${values.map((value) => `<string>${value}</string>`).join("\n ")}
</array>`,
)
.join("\n");

return NOTIFICATION_SERVICE_ENTITLEMENT.replace(
"<dict>",
`<dict>\n${dictEntries}`,
);
}

export type NotificationServiceExtensionProps = {
apiKey: string;
keyService: string;
Expand Down Expand Up @@ -90,17 +63,6 @@ export function withNotificationServiceExtension(config: ExpoConfig, props: Noti
fs.mkdirSync(extensionDir);
}

const entitlementsPath = path.join(
extensionDir,
`${targetName}.entitlements`,
);
if (!fs.existsSync(entitlementsPath)) {
fs.writeFileSync(
entitlementsPath,
generateEntitlementsXML(entitlements),
);
}

// TODO: Add multiple sources
const swiftSource = path.join(
__dirname,
Expand All @@ -120,6 +82,10 @@ export function withNotificationServiceExtension(config: ExpoConfig, props: Noti
// Create Target Info Plist
config = withTargetPlist(config, { targetName, appGroup, ...props });

// Create Target Entitlements
config = withTargetEntitlementsPlist(config, { targetName, entitlements });

// Add the same entitlements to the main target
config = withEntitlementsPlist(config, (config) => {
config.modResults = { ...config.modResults, ...entitlements };
return config;
Expand All @@ -134,7 +100,6 @@ export function withNotificationServiceExtension(config: ExpoConfig, props: Noti
});

// Add required Pods for the extension
// TODO: Support dynamic Pods
config = withPodfile(config, (config) => {
const podFile = config.modResults;
if (podFile.contents.includes(targetName)) {
Expand Down
35 changes: 35 additions & 0 deletions packages/react-native/plugin/src/withExtensionEntitlementsPlist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as fs from "fs";
import * as path from "path";
import type { ExpoConfig } from "@expo/config";
import type { InfoPlist } from "expo/config-plugins";
import { withDangerousMod } from "expo/config-plugins";
import plist from "@expo/plist";

type EntitlementsProps = {
targetName: string;
entitlements: Record<string, Array<string>>;
};

export function withTargetEntitlementsPlist(config: ExpoConfig, {targetName, entitlements}: EntitlementsProps): ExpoConfig {
return withDangerousMod(config, [
"ios",
(config) => {
const extensionDir = path.join(
config.modRequest.platformProjectRoot,
targetName,
);
if (!fs.existsSync(extensionDir)) {
fs.mkdirSync(extensionDir);
}
const entitlementsPath = path.join(extensionDir, `${targetName}.entitlements`);

const entitlementsPlist: InfoPlist = {
...entitlements
};

fs.writeFileSync(entitlementsPath, plist.build(entitlementsPlist));

return config;
},
]);
}

0 comments on commit c89e9e0

Please sign in to comment.