From f00b453f45a9232d54d3e16cd5b65452eb39a9dd Mon Sep 17 00:00:00 2001 From: Murat Date: Mon, 25 Mar 2024 02:56:40 +0300 Subject: [PATCH] fix(xcode): make addTarget name non optional --- src/__tests__/unit/tasks/xcodeTask.spec.ts | 7 ++++ src/schema/integrate.schema.json | 1 + src/schema/upgrade.schema.json | 1 + src/tasks/xcode/xcodeTask.addTarget.ts | 22 +++++++++---- src/tasks/xcode/xcodeTask.ts | 3 +- src/types/mod.types.ts | 3 +- .../guides/task-types/ios-tasks/xcode.md | 33 ++++++++++--------- 7 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src/__tests__/unit/tasks/xcodeTask.spec.ts b/src/__tests__/unit/tasks/xcodeTask.spec.ts index 3133710..a174e58 100644 --- a/src/__tests__/unit/tasks/xcodeTask.spec.ts +++ b/src/__tests__/unit/tasks/xcodeTask.spec.ts @@ -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(); @@ -80,6 +81,7 @@ describe('xcodeTask', () => { }, ], }; + await xcodeTask({ configPath: 'path/to/config', task: task, @@ -112,6 +114,7 @@ describe('xcodeTask', () => { type: 'xcode', actions: [ { + name: 'noti', addTarget: 'test', type: 'notification-service', }, @@ -134,6 +137,7 @@ describe('xcodeTask', () => { type: 'xcode', actions: [ { + name: 'noti', addTarget: 'test2', type: 'notification-service', }, @@ -167,6 +171,7 @@ describe('xcodeTask', () => { type: 'xcode', actions: [ { + name: 'noti', addTarget: 'test', type: 'notification-content', }, @@ -206,6 +211,7 @@ describe('xcodeTask', () => { type: 'xcode', actions: [ { + name: 'noti', addTarget: 'test', type: 'notification-content', }, @@ -231,6 +237,7 @@ describe('xcodeTask', () => { type: 'xcode', actions: [ { + name: 'noti', addTarget: 'test', type: 'notification-content', }, diff --git a/src/schema/integrate.schema.json b/src/schema/integrate.schema.json index 5bc49f7..56192f4 100644 --- a/src/schema/integrate.schema.json +++ b/src/schema/integrate.schema.json @@ -1630,6 +1630,7 @@ }, "required": [ "addTarget", + "name", "type" ], "type": "object" diff --git a/src/schema/upgrade.schema.json b/src/schema/upgrade.schema.json index d95214a..f951240 100644 --- a/src/schema/upgrade.schema.json +++ b/src/schema/upgrade.schema.json @@ -1630,6 +1630,7 @@ }, "required": [ "addTarget", + "name", "type" ], "type": "object" diff --git a/src/tasks/xcode/xcodeTask.addTarget.ts b/src/tasks/xcode/xcodeTask.addTarget.ts index 3d16e00..d24decc 100644 --- a/src/tasks/xcode/xcodeTask.addTarget.ts +++ b/src/tasks/xcode/xcodeTask.addTarget.ts @@ -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, @@ -18,17 +19,24 @@ import { export async function applyAddTarget( content: XcodeProjectType, - action: XcodeAddTarget + action: XcodeAddTarget, + packageName: string ): Promise { 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(action.name + '.target'); if (!targetName) targetName = action.addTarget; - if (action.name) variables.set(action.name + '.target', targetName); const mainGroup = content.getFirstProject().firstProject.mainGroup; diff --git a/src/tasks/xcode/xcodeTask.ts b/src/tasks/xcode/xcodeTask.ts index efb6f83..e4c9b43 100644 --- a/src/tasks/xcode/xcodeTask.ts +++ b/src/tasks/xcode/xcodeTask.ts @@ -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); diff --git a/src/types/mod.types.ts b/src/types/mod.types.ts index 89a121d..27a6c12 100644 --- a/src/types/mod.types.ts +++ b/src/types/mod.types.ts @@ -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 & { + name: string; addTarget: string; type: XcodeAddTargetType; message?: string; diff --git a/website/docs/for-developers/guides/task-types/ios-tasks/xcode.md b/website/docs/for-developers/guides/task-types/ios-tasks/xcode.md index bef3d6c..4a7798c 100644 --- a/website/docs/for-developers/guides/task-types/ios-tasks/xcode.md +++ b/website/docs/for-developers/guides/task-types/ios-tasks/xcode.md @@ -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