From 2d89c2c37af8f884d063634823f32606efa2924d Mon Sep 17 00:00:00 2001 From: ElenaDiachenko Date: Wed, 9 Oct 2024 15:58:17 +0300 Subject: [PATCH 1/3] fix/choosing_apple_device --- packages/sdk-apple/src/runner.ts | 62 +++++++++++++++++--------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/packages/sdk-apple/src/runner.ts b/packages/sdk-apple/src/runner.ts index 37772a811..b038b4d0e 100644 --- a/packages/sdk-apple/src/runner.ts +++ b/packages/sdk-apple/src/runner.ts @@ -69,18 +69,35 @@ export const getIosDeviceToRunOn = async (c: Context) => { if (!c.platform) return; - const { device } = c.program.opts(); + const { device, target } = c.program.opts(); let devicesArr: AppleDevice[] = []; - if (device === true) { + if (device) { devicesArr = await getAppleDevices(false, true); } else { - devicesArr = await getAppleDevices(true, false); + devicesArr = await getAppleDevices(false, false); } + const run = (selectedDevice: AppleDevice) => { + logDebug(`Selected device: ${JSON.stringify(selectedDevice, null, 3)}`); + c.runtime.targetUDID = selectedDevice.udid; + if (selectedDevice.udid) { + p = `--udid ${selectedDevice.udid}`; + } else { + p = `--device ${selectedDevice.name}`; + } + + return p; + }; let p; + const isAvailableDevice = (device: string | boolean) => { + if (device !== true) { + return devicesArr.find((d) => d.name === device) ? true : false; + } + return true; + }; - if (device === true) { - if (devicesArr.length === 1) { + if (device) { + if (devicesArr.length === 1 && !target && isAvailableDevice(device)) { logSuccess( `Found one device connected! Device name: ${chalk().bold.white( devicesArr[0].name @@ -92,20 +109,8 @@ export const getIosDeviceToRunOn = async (c: Context) => { } else { p = `--device ${devicesArr[0].name}`; } - } else if (devicesArr.length > 1) { - const run = (selectedDevice: AppleDevice) => { - logDebug(`Selected device: ${JSON.stringify(selectedDevice, null, 3)}`); - c.runtime.targetUDID = selectedDevice.udid; - if (selectedDevice.udid) { - p = `--udid ${selectedDevice.udid}`; - } else { - p = `--device ${selectedDevice.name}`; - } - - return p; - }; - - if (c.runtime.target) { + } else if (devicesArr.length && (target || devicesArr.find((d) => d.isDevice))) { + if (c.runtime.target && device === true) { const selectedDevice = devicesArr.find((d) => d.name === c.runtime.target); if (selectedDevice) { return run(selectedDevice); @@ -133,8 +138,6 @@ export const getIosDeviceToRunOn = async (c: Context) => { } else { return Promise.reject(`No ${c.platform} devices connected!`); } - } else if (device) { - p = `--device ${device}`; } else if (c.runtime.isTargetTrue) { const devices = devicesArr.map((v) => ({ name: `${v.name} | ${v.icon} | v: ${chalk().green(v.version)} | udid: ${chalk().grey(v.udid)}${ @@ -143,16 +146,19 @@ export const getIosDeviceToRunOn = async (c: Context) => { value: v, })); - const { sim } = await inquirerPrompt({ - name: 'sim', - message: 'Select the simulator you want to launch on', + const { chosenTarget } = await inquirerPrompt({ + name: 'chosenTarget', + message: 'Select the simulator or device you want to launch on', type: 'list', choices: devices, }); - - c.runtime.target = sim.name; - if (c.runtime.target) { - p = `--simulator ${c.runtime.target.replace(/(\s+)/g, '\\$1')}`; + if (!chosenTarget.isDevice) { + c.runtime.target = chosenTarget.name; + if (c.runtime.target) { + p = `--simulator ${c.runtime.target.replace(/(\s+)/g, '\\$1')}`; + } + } else { + return run(chosenTarget); } } else if (c.runtime.target || devicesArr.length > 0) { // check if the default sim is available From c4fa6b062b8e11059e5922f8c55d6eb2a350eabe Mon Sep 17 00:00:00 2001 From: ElenaDiachenko Date: Sat, 12 Oct 2024 17:48:55 +0300 Subject: [PATCH 2/3] fix --- packages/sdk-apple/src/runner.ts | 109 +++++++++++++++++++++---------- 1 file changed, 75 insertions(+), 34 deletions(-) diff --git a/packages/sdk-apple/src/runner.ts b/packages/sdk-apple/src/runner.ts index b038b4d0e..44fbeb77b 100644 --- a/packages/sdk-apple/src/runner.ts +++ b/packages/sdk-apple/src/runner.ts @@ -1,4 +1,5 @@ import child_process, { ExecFileOptions } from 'child_process'; +import _ from 'lodash'; import path from 'path'; import { fsExistsSync, @@ -22,6 +23,7 @@ import { logRaw, inquirerPrompt, CoreEnvVars, + logInfo, } from '@rnv/core'; import { getAppleDevices } from './deviceManager'; @@ -89,17 +91,18 @@ export const getIosDeviceToRunOn = async (c: Context) => { }; let p; - const isAvailableDevice = (device: string | boolean) => { - if (device !== true) { - return devicesArr.find((d) => d.name === device) ? true : false; - } - return true; - }; - if (device) { - if (devicesArr.length === 1 && !target && isAvailableDevice(device)) { + const { isValidTarget, isValidDevice } = await _getAvailableTarget({ + target, + device, + devicesArr, + }); + + if (device || isValidTarget) { + const availableDevices = devicesArr.filter((d) => d.isDevice); + if (availableDevices.length === 1 && ((device && !target && isValidDevice) || isValidTarget)) { logSuccess( - `Found one device connected! Device name: ${chalk().bold.white( + `Found device connected! Device name: ${chalk().bold.white( devicesArr[0].name )} udid: ${chalk().bold.white(devicesArr[0].udid)}` ); @@ -109,8 +112,8 @@ export const getIosDeviceToRunOn = async (c: Context) => { } else { p = `--device ${devicesArr[0].name}`; } - } else if (devicesArr.length && (target || devicesArr.find((d) => d.isDevice))) { - if (c.runtime.target && device === true) { + } else if (availableDevices.length) { + if (c.runtime.target) { const selectedDevice = devicesArr.find((d) => d.name === c.runtime.target); if (selectedDevice) { return run(selectedDevice); @@ -118,7 +121,7 @@ export const getIosDeviceToRunOn = async (c: Context) => { logWarning(`Could not find device ${c.runtime.target}`); } - const devices = devicesArr.map((v) => ({ + const devices = availableDevices.map((v) => ({ name: `${v.name} | ${v.icon} | v: ${chalk().green(v.version)} | udid: ${chalk().grey(v.udid)}${ v.isDevice ? chalk().red(' (device)') : '' }`, @@ -161,26 +164,23 @@ export const getIosDeviceToRunOn = async (c: Context) => { return run(chosenTarget); } } else if (c.runtime.target || devicesArr.length > 0) { - // check if the default sim is available - const desiredSim = devicesArr.find((d) => d.name === c.runtime.target && !d.isDevice); + let desiredSim = devicesArr.find((d) => d.name === c.runtime.target); if (!desiredSim) { - const { sim } = await inquirerPrompt({ - name: 'sim', + const { currentTarget } = await inquirerPrompt({ + name: 'currentTarget', message: !c.runtime.target - ? `No global or project default simulator defined. Please select a supported simulator to use` - : `We couldn't find ${c.runtime.target} as a simulator supported by the current version of your Xcode. Please select another sim`, + ? `No global or project default simulator or device defined. Please select a supported simulator to use` + : `We couldn't find ${c.runtime.target} as a target supported by the current version of your Xcode or as a connected device. Please select another simulator or device`, type: 'list', - choices: devicesArr - .filter((d) => !d.isDevice) - .map((v) => ({ - name: `${v.name} | ${v.icon} | v: ${chalk().green(v.version)} | udid: ${chalk().grey(v.udid)}${ - v.isDevice ? chalk().red(' (device)') : '' - }`, - value: v, - })), + choices: devicesArr.map((v) => ({ + name: `${v.name} | ${v.icon} | v: ${chalk().green(v.version)} | udid: ${chalk().grey(v.udid)}${ + v.isDevice ? chalk().red(' (device)') : '' + }`, + value: v, + })), }); - + desiredSim = currentTarget; const localOverridden = !!c.files.project.configLocal?.defaultTargets?.[c.platform]; const actionLocalUpdate = `Update ${chalk().green('project')} default target for platform ${c.platform}`; @@ -194,15 +194,15 @@ export const getIosDeviceToRunOn = async (c: Context) => { type: 'list', name: 'chosenAction', choices: [actionLocalUpdate, actionGlobalUpdate, actionNoUpdate], - warningMessage: `Your default target for platform ${c.platform} is set to ${c.runtime.target}. This seems to not be supported by Xcode anymore`, + warningMessage: `Your default target for platform ${c.platform} is set to ${c.runtime.target}.`, }); - c.runtime.target = sim.name; + c.runtime.target = currentTarget.name; if (chosenAction === actionLocalUpdate || (chosenAction === actionGlobalUpdate && localOverridden)) { const configLocal = c.files.project.configLocal || {}; if (!configLocal.defaultTargets) configLocal.defaultTargets = {}; - configLocal.defaultTargets[c.platform] = sim.name; + configLocal.defaultTargets[c.platform] = currentTarget.name; c.files.project.configLocal = configLocal; writeFileSync(c.paths.project.configLocal, configLocal); @@ -212,22 +212,63 @@ export const getIosDeviceToRunOn = async (c: Context) => { const configGlobal = c.files.workspace.config; if (configGlobal) { if (!configGlobal.defaultTargets) configGlobal.defaultTargets = {}; - configGlobal.defaultTargets[c.platform] = sim.name; + configGlobal.defaultTargets[c.platform] = currentTarget.name; c.files.workspace.config = configGlobal; writeFileSync(c.paths.workspace.config, configGlobal); } } } + if (!desiredSim?.isDevice) { + const target = c.runtime.target?.replace(/(\s+)/g, '\\$1'); - const target = c.runtime.target?.replace(/(\s+)/g, '\\$1'); - - p = `--simulator ${target}`; + p = `--simulator ${target}`; + } else { + return run(desiredSim); + } } return p; }; +const _getAvailableTarget = async ({ + target, + device, + devicesArr, +}: { + target: string | undefined | boolean; + device: string | undefined | boolean; + devicesArr: AppleDevice[]; +}) => { + let isValidTarget = false; + let isValidDevice = false; + + if (_.isString(target)) { + const targetMatch = devicesArr.filter((d) => d.isDevice).find((d) => d.name === target); + isValidTarget = !!targetMatch || (await _isValidIP(target)); + } + if (_.isString(device)) { + const deviceMatch = devicesArr.find((d) => d.isDevice && d.name === device); + isValidDevice = !!deviceMatch || (await _isValidIP(device)); + } + if (device === true) { + isValidDevice = true; + } + + return { isValidTarget, isValidDevice }; +}; + +const _isValidIP = async (ip: string) => { + try { + await executeAsync(`ping -c 1 ${ip}`, { silent: true }); + logInfo(`Target IP ${ip} is a valid IP address`); + return true; + } catch (error) { + logInfo(`Target IP ${ip} is not a valid IP address`); + return false; + } +}; + export const runXcodeProject = async (runDeviceArguments?: string) => { const c = getContext(); logDefault('runXcodeProject', `targetArgs:${runDeviceArguments}`); From e1093206b85c94679fba25972afd95e868ef02c9 Mon Sep 17 00:00:00 2001 From: ElenaDiachenko Date: Wed, 16 Oct 2024 16:18:39 +0300 Subject: [PATCH 3/3] fix --- packages/sdk-apple/src/runner.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/sdk-apple/src/runner.ts b/packages/sdk-apple/src/runner.ts index 44fbeb77b..9788fa626 100644 --- a/packages/sdk-apple/src/runner.ts +++ b/packages/sdk-apple/src/runner.ts @@ -72,12 +72,7 @@ export const getIosDeviceToRunOn = async (c: Context) => { if (!c.platform) return; const { device, target } = c.program.opts(); - let devicesArr: AppleDevice[] = []; - if (device) { - devicesArr = await getAppleDevices(false, true); - } else { - devicesArr = await getAppleDevices(false, false); - } + const devicesArr = await getAppleDevices(false, !!device); const run = (selectedDevice: AppleDevice) => { logDebug(`Selected device: ${JSON.stringify(selectedDevice, null, 3)}`); c.runtime.targetUDID = selectedDevice.udid;