Skip to content

Commit e08a5b4

Browse files
author
Murat
committed
fix(xcode): handle targets with only path
1 parent a9da787 commit e08a5b4

File tree

5 files changed

+70
-19
lines changed

5 files changed

+70
-19
lines changed

src/__tests__/unit/tasks/xcodeTask.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,54 @@ describe('xcodeTask', () => {
329329
expect(infoContent).toContain('DirectionalGamepad');
330330
expect(infoContent).toContain('MKDirectionsModeBus');
331331
});
332+
it('should add capabilities to project with path of target', async () => {
333+
const pbxFilePath = getPbxProjectPath();
334+
mockFs.writeFileSync(pbxFilePath, mockPbxProjTemplate);
335+
336+
const proj = xcode.project(pbxFilePath);
337+
proj.parseSync();
338+
339+
const targetName = 'ReactNativeCliTemplates';
340+
const mock = jest.spyOn(xcode.project.prototype, 'getPBXGroupByKey');
341+
mock.mockReturnValueOnce({
342+
name: undefined,
343+
path: 'path/' + targetName,
344+
children: [],
345+
});
346+
347+
const task: XcodeTaskType = {
348+
type: 'xcode',
349+
actions: [
350+
{
351+
addCapability: 'groups',
352+
target: 'main',
353+
groups: ['group.test'],
354+
},
355+
],
356+
};
357+
await xcodeTask({
358+
configPath: 'path/to/config',
359+
task: task,
360+
content: proj,
361+
packageName: 'test-package',
362+
});
363+
const content = proj.writeSync();
364+
expect(content).toMatch(
365+
/\{.*?\bReactNativeCliTemplates\.entitlements.*?}/s
366+
);
367+
const entitlementsContent = mockFs.readFileSync(
368+
path.join(
369+
getProjectPath(),
370+
'ios',
371+
targetName,
372+
targetName + '.entitlements'
373+
)
374+
);
375+
expect(entitlementsContent).toContain(
376+
'com.apple.security.application-groups'
377+
);
378+
mock.mockRestore();
379+
});
332380
it('should add resource to root', async () => {
333381
const pbxFilePath = getPbxProjectPath();
334382
mockFs.writeFileSync(pbxFilePath, mockPbxProjTemplate);

src/tasks/xcode/xcodeTask.addCapability.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path';
12
import color from 'picocolors';
23
import { XcodeProjectType } from 'xcode';
34
import { Constants } from '../../constants';
@@ -10,7 +11,7 @@ import { addGCCapability } from './xcodeTask.addCapability.gc';
1011
import { addGroupsCapability } from './xcodeTask.addCapability.groups';
1112
import { addKSCapability } from './xcodeTask.addCapability.ks';
1213
import { addMapsCapability } from './xcodeTask.addCapability.maps';
13-
import { patchXcodeProject } from './xcodeTask.helpers';
14+
import { patchXcodeProject, unquote } from './xcodeTask.helpers';
1415

1516
export function applyAddCapability(
1617
content: XcodeProjectType,
@@ -36,15 +37,16 @@ export function applyAddCapability(
3637
break;
3738
}
3839
const groupObj = content.getPBXGroupByKey(group);
39-
const filename = groupObj.name + '.entitlements';
40+
const groupName = groupObj.name || path.basename(groupObj.path);
41+
const filename = groupName + '.entitlements';
4042
destination += `/${filename}`;
41-
const isAdded = groupObj.children.some(x => x.comment == filename);
43+
const isAdded = groupObj.children.some(x => unquote(x.comment) == filename);
4244
if (!isAdded) {
4345
const releasePatch = patchXcodeProject({
4446
push: (array, item) => array.unshift(item),
4547
});
4648
try {
47-
content.addFile(`${groupObj.name}/${filename}`, group, {
49+
content.addFile(`${groupName}/${filename}`, group, {
4850
target: nativeTarget.uuid,
4951
lastKnownFileType: 'text.plist.entitlements',
5052
});
@@ -53,15 +55,15 @@ export function applyAddCapability(
5355
}
5456
content.updateBuildProperty(
5557
'CODE_SIGN_ENTITLEMENTS',
56-
`${groupObj.name}/${filename}`,
58+
`${groupName}/${filename}`,
5759
null,
58-
groupObj.name
60+
groupName
5961
);
6062
content.updateBuildProperty(
6163
'CODE_SIGN_ENTITLEMENTS',
62-
`${groupObj.name}/${filename}`,
64+
`${groupName}/${filename}`,
6365
null,
64-
'"' + groupObj.name + '"'
66+
'"' + groupName + '"'
6567
);
6668
}
6769
switch (addCapability) {
@@ -76,49 +78,49 @@ export function applyAddCapability(
7678
addCommonCapability({
7779
destination,
7880
filename,
79-
targetName: groupObj.name,
81+
targetName: groupName,
8082
capability: addCapability,
8183
});
8284
break;
8385
case 'groups':
8486
addGroupsCapability({
8587
destination,
8688
filename,
87-
targetName: groupObj.name,
89+
targetName: groupName,
8890
groups: action.groups,
8991
});
9092
break;
9193
case 'background-mode':
9294
addBMCapability({
93-
targetName: groupObj.name,
95+
targetName: groupName,
9496
modes: action.modes,
9597
});
9698
break;
9799
case 'game-controllers':
98100
addGCCapability({
99-
targetName: groupObj.name,
101+
targetName: groupName,
100102
controllers: action.controllers,
101103
});
102104
break;
103105
case 'maps':
104106
addMapsCapability({
105-
targetName: groupObj.name,
107+
targetName: groupName,
106108
routing: action.routing,
107109
});
108110
break;
109111
case 'keychain-sharing':
110112
addKSCapability({
111113
destination,
112114
filename,
113-
targetName: groupObj.name,
115+
targetName: groupName,
114116
groups: action.groups,
115117
});
116118
break;
117119
}
118120

119121
logMessage(
120122
`added ${color.yellow(addCapability)} capability to the ${color.yellow(
121-
groupObj.name
123+
groupName
122124
)} target`
123125
);
124126

src/tasks/xcode/xcodeTask.addConfiguration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export async function applyAddConfiguration(
8282

8383
const groupObj = content.getPBXGroupByKey(group);
8484
const fileRefExists = groupObj.children.find(
85-
x => x.comment == fileName
85+
x => unquote(x.comment) == fileName
8686
);
8787
if (fileRefExists) {
8888
logMessageGray(

src/tasks/xcode/xcodeTask.addFile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { logMessage, logMessageGray } from '../../prompter';
66
import { XcodeAddFile } from '../../types/mod.types';
77
import { getText } from '../../variables';
88
import { applyFsModification } from '../fsTask';
9-
import { patchXcodeProject } from './xcodeTask.helpers';
9+
import { patchXcodeProject, unquote } from './xcodeTask.helpers';
1010

1111
export async function applyAddFile(
1212
content: XcodeProjectType,
@@ -44,7 +44,7 @@ export async function applyAddFile(
4444
}
4545
destination += `/${fileName}`;
4646
const groupObj = content.getPBXGroupByKey(group);
47-
if (groupObj.children.some(x => x.comment == action.addFile)) {
47+
if (groupObj.children.some(x => unquote(x.comment) == action.addFile)) {
4848
logMessageGray(
4949
`skipped adding resource, ${color.yellow(
5050
action.addFile

src/tasks/xcode/xcodeTask.addTarget.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
normalizeBundleId,
1414
patchXcodeHasFile,
1515
patchXcodeProject,
16+
unquote,
1617
} from './xcodeTask.helpers';
1718

1819
export async function applyAddTarget(
@@ -32,7 +33,7 @@ export async function applyAddTarget(
3233
const mainGroup = content.getFirstProject().firstProject.mainGroup;
3334

3435
const groupObj = content.getPBXGroupByKey(mainGroup);
35-
if (groupObj.children.some(x => x.comment == targetName)) {
36+
if (groupObj.children.some(x => unquote(x.comment) == targetName)) {
3637
logMessageGray(
3738
`skipped adding target, ${color.yellow(targetName)} is already exists`
3839
);

0 commit comments

Comments
 (0)