Skip to content

Commit

Permalink
fix(xcode): make addTarget name non optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Murat committed Mar 24, 2024
1 parent e08a5b4 commit f00b453
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 24 deletions.
7 changes: 7 additions & 0 deletions src/__tests__/unit/tasks/xcodeTask.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('xcodeTask', () => {
it('should add notification service to project', async () => {
const pbxFilePath = getPbxProjectPath();
mockFs.writeFileSync(pbxFilePath, mockPbxProjTemplate);
mockPrompter.text.mockReset().mockImplementation(() => 'test');

const proj = xcode.project(pbxFilePath);
proj.parseSync();
Expand All @@ -80,6 +81,7 @@ describe('xcodeTask', () => {
},
],
};

await xcodeTask({
configPath: 'path/to/config',
task: task,
Expand Down Expand Up @@ -112,6 +114,7 @@ describe('xcodeTask', () => {
type: 'xcode',
actions: [
{
name: 'noti',
addTarget: 'test',
type: 'notification-service',
},
Expand All @@ -134,6 +137,7 @@ describe('xcodeTask', () => {
type: 'xcode',
actions: [
{
name: 'noti',
addTarget: 'test2',
type: 'notification-service',
},
Expand Down Expand Up @@ -167,6 +171,7 @@ describe('xcodeTask', () => {
type: 'xcode',
actions: [
{
name: 'noti',
addTarget: 'test',
type: 'notification-content',
},
Expand Down Expand Up @@ -206,6 +211,7 @@ describe('xcodeTask', () => {
type: 'xcode',
actions: [
{
name: 'noti',
addTarget: 'test',
type: 'notification-content',
},
Expand All @@ -231,6 +237,7 @@ describe('xcodeTask', () => {
type: 'xcode',
actions: [
{
name: 'noti',
addTarget: 'test',
type: 'notification-content',
},
Expand Down
1 change: 1 addition & 0 deletions src/schema/integrate.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,7 @@
},
"required": [
"addTarget",
"name",
"type"
],
"type": "object"
Expand Down
1 change: 1 addition & 0 deletions src/schema/upgrade.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,7 @@
},
"required": [
"addTarget",
"name",
"type"
],
"type": "object"
Expand Down
22 changes: 15 additions & 7 deletions src/tasks/xcode/xcodeTask.addTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import path from 'path';
import color from 'picocolors';
import { XcodeProjectType } from 'xcode';
import { Constants } from '../../constants';
import { logMessage, logMessageGray, text } from '../../prompter';
import { logMessage, logMessageGray } from '../../prompter';
import { notificationContentFiles } from '../../scaffold/notification-content';
import { notificationServiceFiles } from '../../scaffold/notification-service';
import { XcodeAddTarget } from '../../types/mod.types';
import { getProjectPath } from '../../utils/getProjectPath';
import { runPrompt } from '../../utils/runPrompt';
import { getText, variables } from '../../variables';
import {
normalizeBundleId,
Expand All @@ -18,17 +19,24 @@ import {

export async function applyAddTarget(
content: XcodeProjectType,
action: XcodeAddTarget
action: XcodeAddTarget,
packageName: string
): Promise<XcodeProjectType> {
const { type } = action;
action.addTarget = getText(action.addTarget);

let targetName = await text(action.message || 'Enter new target name:', {
defaultValue: action.addTarget,
placeholder: action.addTarget,
});
await runPrompt(
{
name: action.name + '.target',
text: action.message || 'Enter new target name:',
type: 'text',
defaultValue: action.addTarget,
placeholder: action.addTarget,
},
packageName
);
let targetName = variables.get<string>(action.name + '.target');
if (!targetName) targetName = action.addTarget;
if (action.name) variables.set(action.name + '.target', targetName);

const mainGroup = content.getFirstProject().firstProject.mainGroup;

Expand Down
3 changes: 2 additions & 1 deletion src/tasks/xcode/xcodeTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ async function applyXcodeModification(
packageName: string
) {
if ('addFile' in action) return applyAddFile(content, action, packageName);
if ('addTarget' in action) return applyAddTarget(content, action);
if ('addTarget' in action)
return applyAddTarget(content, action, packageName);
if ('addCapability' in action) return applyAddCapability(content, action);
if ('setDeploymentVersion' in action)
return applySetDeploymentVersion(content, action);
Expand Down
3 changes: 2 additions & 1 deletion src/types/mod.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ export type XcodeAddFile = ActionBase & {
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
target?: 'root' | 'main' | string;
};
export type XcodeAddTarget = ActionBase & {
export type XcodeAddTarget = Omit<ActionBase, 'name'> & {
name: string;
addTarget: string;
type: XcodeAddTargetType;
message?: string;
Expand Down
33 changes: 18 additions & 15 deletions website/docs/for-developers/guides/task-types/ios-tasks/xcode.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,24 @@ Specifies the target group within the iOS project where the resource should be a
| type | "notification-service" or "notification-content" | Specifies target type to be added. "notification-service" adds Notification Service Extension and "notification-content" adds Notification Content Extension. |
| message | string | Specifies the message when requesting the target name from the user. |

> **Note**: Specify `name` field for this action to expose the `name.target` variable which will hold the name of the target which was entered by the user.
>
> For example:
> ```yaml
> # add a notification service extension
> # with the default name `MyNotificationService`.
> # User can change this when running this action!
> - addTarget: MyNotificationService
> name: notificationsv # Give it a name
> type: notification-service
>
> # set extension version to same as main target
> - setDeploymentVersion: $[IOS_DEPLOYMENT_VERSION]
> target: $[notificationsv.target] # use the name here
> ```
:::info
`name` must be specified for this action or you will get a validation error.
This action will expose the `name.target` variable which will hold the name of the target which was entered by the user.

For example:
```yaml
# add a notification service extension
# with the default name `MyNotificationService`.
# User can change this when running this action!
- addTarget: MyNotificationService
name: notificationsv # Give it a name
type: notification-service

# set extension version to same as main target
- setDeploymentVersion: $[IOS_DEPLOYMENT_VERSION]
target: $[notificationsv.target] # use the name here
```
:::
### Add new capability to a target
Expand Down

0 comments on commit f00b453

Please sign in to comment.