Skip to content

Commit 5a320b6

Browse files
committed
[eas-cli] refactor capability syncing
1 parent 886f645 commit 5a320b6

File tree

2 files changed

+172
-201
lines changed

2 files changed

+172
-201
lines changed

packages/eas-cli/src/credentials/ios/appstore/bundleIdCapabilities.ts

Lines changed: 10 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
CapabilityType,
55
CapabilityTypeOption,
66
} from '@expo/apple-utils';
7-
import { JSONObject, JSONValue } from '@expo/json-file';
7+
import { JSONObject } from '@expo/json-file';
88
import getenv from 'getenv';
99
import { inspect } from 'util';
1010

@@ -87,102 +87,6 @@ interface CapabilitiesRequest {
8787
option: any;
8888
}
8989

90-
function shouldSkipPushNotificationsCapabilityUpdate(
91-
existing: BundleIdCapability,
92-
additionalOptions: {
93-
usesBroadcastPushNotifications: boolean;
94-
}
95-
): boolean {
96-
// For push notifications, we should always update the capability if
97-
// - settings are not defined in the existing capability, but usesBroadcastPushNotifications is enabled (we want to add settings for this capability)
98-
// - settings are defined in the existing capability, but usesBroadcastPushNotifications is disabled (we want to remove settings for this capability)
99-
const noSettingsAttributes = existing.attributes.settings == null;
100-
return !noSettingsAttributes === additionalOptions.usesBroadcastPushNotifications;
101-
}
102-
103-
function shouldSkipIcloudCapabilityUpdate(
104-
existing: BundleIdCapability,
105-
newOption: CapabilityTypeOption
106-
): boolean {
107-
// For iCloud capabilities, we should skip if:
108-
// - the capability is already enabled and has the correct settings
109-
// - we want to enable it and it's already enabled with settings
110-
const existingEnabled = 'enabled' in existing.attributes && existing.attributes.enabled === true;
111-
const newEnabled = newOption === CapabilityTypeOption.ON;
112-
113-
// If both are enabled and the existing one has settings, skip the update
114-
// the settings are defined for only a few capabilities: https://developer.apple.com/documentation/appstoreconnectapi/capabilitysetting
115-
if (existingEnabled && newEnabled && existing.attributes.settings) {
116-
return true;
117-
}
118-
119-
// If the states don't match, we need to update
120-
return existingEnabled === newEnabled;
121-
}
122-
123-
function shouldPerformRemoteCapabilitySetup(
124-
existingRemote: BundleIdCapability | null,
125-
staticCapabilityInfo: CapabilityClassifier,
126-
entitlementValue: JSONValue,
127-
entitlements: JSONObject,
128-
additionalOptions: {
129-
usesBroadcastPushNotifications: boolean;
130-
}
131-
): 'enable' | 'disable' | 'skip' {
132-
if (!existingRemote) {
133-
if (entitlementValue === false) {
134-
// the value represents a disabled capability (boolean false)
135-
// e.g. 'com.apple.developer.networking.wifi-info': false
136-
// the remote capability is *already disabled*, so we should skip it
137-
return 'skip';
138-
}
139-
// a new capability not present remotely, so we want to create it
140-
return 'enable';
141-
}
142-
if (existingRemote && entitlementValue === false) {
143-
return 'disable';
144-
}
145-
146-
// Only skip if the existing capability is a simple boolean value,
147-
// if it has more complex settings then we should update it (except for push notifications, iCloud and perhaps more).
148-
// If the `existing.attributes.settings` object is defined, then we can determine that it has extra configuration.
149-
// For push notifications, we should always update the capability if
150-
// - settings are not defined in the existing capability, but usesBroadcastPushNotifications is enabled (we want to add settings for this capability)
151-
// - settings are defined in the existing capability, but usesBroadcastPushNotifications is disabled (we want to remove settings for this capability)
152-
153-
const isPushNotificationsCapability =
154-
staticCapabilityInfo.capability === CapabilityType.PUSH_NOTIFICATIONS;
155-
156-
if (isPushNotificationsCapability) {
157-
return shouldSkipPushNotificationsCapabilityUpdate(existingRemote, additionalOptions)
158-
? 'skip'
159-
: 'enable';
160-
}
161-
162-
const newValue = staticCapabilityInfo.getOptions(
163-
entitlementValue,
164-
entitlements,
165-
additionalOptions
166-
);
167-
168-
const needsSpecialHandling =
169-
staticCapabilityInfo.capability === CapabilityType.ICLOUD ||
170-
staticCapabilityInfo.capability === CapabilityType.APPLE_ID_AUTH;
171-
if (needsSpecialHandling) {
172-
return shouldSkipIcloudCapabilityUpdate(existingRemote, newValue) ? 'skip' : 'enable';
173-
}
174-
175-
if (
176-
staticCapabilityInfo.capability === CapabilityType.DATA_PROTECTION &&
177-
existingRemote.attributes.settings?.[0].key === 'DATA_PROTECTION_PERMISSION_LEVEL'
178-
) {
179-
const oldValue = existingRemote.attributes.settings[0]?.options?.[0]?.key;
180-
return oldValue === newValue ? 'skip' : 'enable';
181-
}
182-
183-
return existingRemote.attributes.settings === null ? 'skip' : 'enable';
184-
}
185-
18690
function getCapabilitiesToEnable(
18791
currentRemoteCapabilities: BundleIdCapability[],
18892
entitlements: JSONObject,
@@ -216,27 +120,25 @@ function getCapabilitiesToEnable(
216120
);
217121
const existingRemote = existingIndex > -1 ? remainingCapabilities[existingIndex] : null;
218122

219-
const operation = shouldPerformRemoteCapabilitySetup(
123+
const operation = staticCapabilityInfo.getSyncOperation({
220124
existingRemote,
221-
staticCapabilityInfo,
222-
value,
223125
entitlements,
224-
additionalOptions
225-
);
126+
entitlementValue: value,
127+
additionalOptions,
128+
});
129+
const { op } = operation;
226130

227131
if (Log.isDebug) {
228-
Log.log(`Will ${operation} remote capability: ${key} (${staticCapabilityInfo.name}.`);
132+
Log.log(`Will ${op} remote capability: ${key} (${staticCapabilityInfo.name}.`);
229133
}
230-
if (operation === 'enable') {
134+
if (op === 'enable') {
231135
enabledCapabilityNames.push(staticCapabilityInfo.name);
232136

233-
const option = staticCapabilityInfo.getOptions(value, entitlements, additionalOptions);
234-
235137
request.push({
236138
capabilityType: staticCapabilityInfo.capability,
237-
option,
139+
option: operation.option,
238140
});
239-
} else if (operation === 'skip') {
141+
} else if (op === 'skip') {
240142
// Remove the item from the list of capabilities so we don't disable it in the next step.
241143
remainingCapabilities.splice(existingIndex, 1);
242144
}

0 commit comments

Comments
 (0)