Skip to content

Commit

Permalink
fix(xcode): handle targets with only path
Browse files Browse the repository at this point in the history
  • Loading branch information
Murat committed Mar 24, 2024
1 parent a9da787 commit e08a5b4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 19 deletions.
48 changes: 48 additions & 0 deletions src/__tests__/unit/tasks/xcodeTask.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,54 @@ describe('xcodeTask', () => {
expect(infoContent).toContain('DirectionalGamepad');
expect(infoContent).toContain('MKDirectionsModeBus');
});
it('should add capabilities to project with path of target', async () => {
const pbxFilePath = getPbxProjectPath();
mockFs.writeFileSync(pbxFilePath, mockPbxProjTemplate);

const proj = xcode.project(pbxFilePath);
proj.parseSync();

const targetName = 'ReactNativeCliTemplates';
const mock = jest.spyOn(xcode.project.prototype, 'getPBXGroupByKey');
mock.mockReturnValueOnce({
name: undefined,
path: 'path/' + targetName,
children: [],
});

const task: XcodeTaskType = {
type: 'xcode',
actions: [
{
addCapability: 'groups',
target: 'main',
groups: ['group.test'],
},
],
};
await xcodeTask({
configPath: 'path/to/config',
task: task,
content: proj,
packageName: 'test-package',
});
const content = proj.writeSync();
expect(content).toMatch(
/\{.*?\bReactNativeCliTemplates\.entitlements.*?}/s
);
const entitlementsContent = mockFs.readFileSync(
path.join(
getProjectPath(),
'ios',
targetName,
targetName + '.entitlements'
)
);
expect(entitlementsContent).toContain(
'com.apple.security.application-groups'
);
mock.mockRestore();
});
it('should add resource to root', async () => {
const pbxFilePath = getPbxProjectPath();
mockFs.writeFileSync(pbxFilePath, mockPbxProjTemplate);
Expand Down
32 changes: 17 additions & 15 deletions src/tasks/xcode/xcodeTask.addCapability.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import color from 'picocolors';
import { XcodeProjectType } from 'xcode';
import { Constants } from '../../constants';
Expand All @@ -10,7 +11,7 @@ import { addGCCapability } from './xcodeTask.addCapability.gc';
import { addGroupsCapability } from './xcodeTask.addCapability.groups';
import { addKSCapability } from './xcodeTask.addCapability.ks';
import { addMapsCapability } from './xcodeTask.addCapability.maps';
import { patchXcodeProject } from './xcodeTask.helpers';
import { patchXcodeProject, unquote } from './xcodeTask.helpers';

export function applyAddCapability(
content: XcodeProjectType,
Expand All @@ -36,15 +37,16 @@ export function applyAddCapability(
break;
}
const groupObj = content.getPBXGroupByKey(group);
const filename = groupObj.name + '.entitlements';
const groupName = groupObj.name || path.basename(groupObj.path);
const filename = groupName + '.entitlements';
destination += `/${filename}`;
const isAdded = groupObj.children.some(x => x.comment == filename);
const isAdded = groupObj.children.some(x => unquote(x.comment) == filename);
if (!isAdded) {
const releasePatch = patchXcodeProject({
push: (array, item) => array.unshift(item),
});
try {
content.addFile(`${groupObj.name}/${filename}`, group, {
content.addFile(`${groupName}/${filename}`, group, {
target: nativeTarget.uuid,
lastKnownFileType: 'text.plist.entitlements',
});
Expand All @@ -53,15 +55,15 @@ export function applyAddCapability(
}
content.updateBuildProperty(
'CODE_SIGN_ENTITLEMENTS',
`${groupObj.name}/${filename}`,
`${groupName}/${filename}`,
null,
groupObj.name
groupName
);
content.updateBuildProperty(
'CODE_SIGN_ENTITLEMENTS',
`${groupObj.name}/${filename}`,
`${groupName}/${filename}`,
null,
'"' + groupObj.name + '"'
'"' + groupName + '"'
);
}
switch (addCapability) {
Expand All @@ -76,49 +78,49 @@ export function applyAddCapability(
addCommonCapability({
destination,
filename,
targetName: groupObj.name,
targetName: groupName,
capability: addCapability,
});
break;
case 'groups':
addGroupsCapability({
destination,
filename,
targetName: groupObj.name,
targetName: groupName,
groups: action.groups,
});
break;
case 'background-mode':
addBMCapability({
targetName: groupObj.name,
targetName: groupName,
modes: action.modes,
});
break;
case 'game-controllers':
addGCCapability({
targetName: groupObj.name,
targetName: groupName,
controllers: action.controllers,
});
break;
case 'maps':
addMapsCapability({
targetName: groupObj.name,
targetName: groupName,
routing: action.routing,
});
break;
case 'keychain-sharing':
addKSCapability({
destination,
filename,
targetName: groupObj.name,
targetName: groupName,
groups: action.groups,
});
break;
}

logMessage(
`added ${color.yellow(addCapability)} capability to the ${color.yellow(
groupObj.name
groupName
)} target`
);

Expand Down
2 changes: 1 addition & 1 deletion src/tasks/xcode/xcodeTask.addConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export async function applyAddConfiguration(

const groupObj = content.getPBXGroupByKey(group);
const fileRefExists = groupObj.children.find(
x => x.comment == fileName
x => unquote(x.comment) == fileName
);
if (fileRefExists) {
logMessageGray(
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/xcode/xcodeTask.addFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { logMessage, logMessageGray } from '../../prompter';
import { XcodeAddFile } from '../../types/mod.types';
import { getText } from '../../variables';
import { applyFsModification } from '../fsTask';
import { patchXcodeProject } from './xcodeTask.helpers';
import { patchXcodeProject, unquote } from './xcodeTask.helpers';

export async function applyAddFile(
content: XcodeProjectType,
Expand Down Expand Up @@ -44,7 +44,7 @@ export async function applyAddFile(
}
destination += `/${fileName}`;
const groupObj = content.getPBXGroupByKey(group);
if (groupObj.children.some(x => x.comment == action.addFile)) {
if (groupObj.children.some(x => unquote(x.comment) == action.addFile)) {
logMessageGray(
`skipped adding resource, ${color.yellow(
action.addFile
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/xcode/xcodeTask.addTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
normalizeBundleId,
patchXcodeHasFile,
patchXcodeProject,
unquote,
} from './xcodeTask.helpers';

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

const groupObj = content.getPBXGroupByKey(mainGroup);
if (groupObj.children.some(x => x.comment == targetName)) {
if (groupObj.children.some(x => unquote(x.comment) == targetName)) {
logMessageGray(
`skipped adding target, ${color.yellow(targetName)} is already exists`
);
Expand Down

0 comments on commit e08a5b4

Please sign in to comment.