Skip to content

Commit

Permalink
Merge pull request #1782 from flexn-io/fix/defaultTargets_updating
Browse files Browse the repository at this point in the history
fix/defaultTargets_updating
  • Loading branch information
pauliusguzas authored Nov 26, 2024
2 parents b50b46d + 21e8a8b commit d2b4189
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ export default createTask({
copyFileSync(oldGlobalConfigPath, paths.workspace.config);
} else {
logInfo(`${paths.workspace.dir}/${RnvFileName.renative} file missing! Creating one for you...`);
writeFileSync(paths.workspace.config, '{}');
const defaultWorkspaceCnf = {
sdks: {
ANDROID_SDK: '/Users/<USER>/Library/Android/sdk',
ANDROID_NDK: '/Users/<USER>/Library/Android/sdk/ndk-bundle',
IOS_SDK: 'No need. Just install Xcode',
TIZEN_SDK: '/Users/<USER>/tizen-studio',
WEBOS_SDK: '/Users/<USER>/Library/webOS_TV_SDK',
KAIOS_SDK: '/Applications/Kaiosrt.app',
},
};
writeFileSync(paths.workspace.config, JSON.stringify(defaultWorkspaceCnf, null, 2));
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/sdk-android/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import {
} from './deviceManager';
import { ANDROID_COLORS, ANDROID_STRINGS, ANDROID_STYLES, CLI_ANDROID_ADB } from './constants';
import { runReactNativeAndroid, packageReactNativeAndroid, generateEnvVarsFile } from '@rnv/sdk-react-native';
import { getEntryFile } from '@rnv/sdk-utils';
import { getEntryFile, updateDefaultTargets } from '@rnv/sdk-utils';
import { Context, getContext } from './getContext';

export const packageAndroid = async () => {
Expand Down Expand Up @@ -120,6 +120,10 @@ export const getAndroidDeviceToRunOn = async () => {
choices,
});
if (chosenTarget) {
// update defaultTarget
if (!target) {
await updateDefaultTargets(c, chosenTarget);
}
const dev = activeDevices.find((d) => d.name === chosenTarget);
if (dev) return dev;

Expand Down
28 changes: 15 additions & 13 deletions packages/sdk-apple/src/__tests__/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { inquirerPrompt, getContext, createRnvContext, logSuccess } from '@rnv/c
import type { PromptParams } from '@rnv/core';
import { getIosDeviceToRunOn } from '../runner';
import { getAppleDevices } from '../deviceManager';
import { updateDefaultTargets } from '@rnv/sdk-utils';

const simJson = [
{
Expand Down Expand Up @@ -33,6 +34,7 @@ const devicesJson = [
];

jest.mock('@rnv/core');
jest.mock('@rnv/sdk-utils');
jest.mock('../deviceManager');
jest.mock('chalk', () => ({
bold: {
Expand Down Expand Up @@ -97,22 +99,17 @@ describe('getIosDeviceToRunOn', () => {
[name as string]: true,
};
}

if (type === 'list') {
// Testing the addition of global/project value should be handled in another UT
if (choices?.includes("Don't update")) {
const choiceIndex = choices.findIndex((c) => c === "Don't update");
return {
[name as string]:
(choices![choiceIndex] as { name: string; value: any }).value || choices![choiceIndex],
};
}
// By default first value returned (aka the first simulator from the list in this case)
return {
[name as string]: (choices![0] as { name: string; value: any }).value || choices![0],
};
}
});
jest.mocked(updateDefaultTargets).mockImplementation(async (ctx, currentTarget) => {
if (!ctx.platform) return;
ctx.runtime.target = currentTarget;
});
// WHEN
const deviceArgs = await getIosDeviceToRunOn(ctx);
//THEN
Expand All @@ -139,14 +136,19 @@ describe('getIosDeviceToRunOn', () => {
ctx.files.workspace.config = {};
ctx.runtime.target = 'iPhone 14';
jest.mocked(getAppleDevices).mockResolvedValueOnce(simJson);
jest.mocked(inquirerPrompt).mockImplementation(async ({ type, name, choices }: PromptParams) => {
if (type === 'list' && choices?.includes('Update global default target for platform ios')) {
return { [name as string]: 'Update global default target for platform ios' };
}
jest.mocked(inquirerPrompt).mockImplementation(async ({ name, choices }: PromptParams) => {
return {
[name as string]: (choices![0] as { name: string; value: any }).value || choices![0],
};
});
jest.mocked(updateDefaultTargets).mockImplementation(async (ctx, currentTarget) => {
if (!ctx.platform) return;
const configGlobal = ctx.files.workspace.config || {};
configGlobal.defaultTargets = configGlobal.defaultTargets || {};
configGlobal.defaultTargets[ctx.platform] = currentTarget;
ctx.files.workspace.config = configGlobal;
ctx.runtime.target = currentTarget;
});
// WHEN
const deviceArgs = await getIosDeviceToRunOn(ctx);
// THEN
Expand Down
39 changes: 2 additions & 37 deletions packages/sdk-apple/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
import { registerDevice } from './fastlane';
import { Context, getContext } from './getContext';
import { parsePrivacyManifest } from './privacyManifestParser';
import { updateDefaultTargets } from '@rnv/sdk-utils';

export const packageBundleForXcode = () => {
return packageReactNativeIOS();
Expand Down Expand Up @@ -175,43 +176,7 @@ export const getIosDeviceToRunOn = async (c: Context) => {
})),
});
desiredSim = currentTarget;
const localOverridden = !!c.files.project.configLocal?.defaultTargets?.[c.platform];

const actionLocalUpdate = `Update ${chalk().green('project')} default target for platform ${c.platform}`;
const actionGlobalUpdate = `Update ${chalk().green('global')}${
localOverridden ? ` and ${chalk().green('project')}` : ''
} default target for platform ${c.platform}`;
const actionNoUpdate = "Don't update";

const { chosenAction } = await inquirerPrompt({
message: 'What to do next?',
type: 'list',
name: 'chosenAction',
choices: [actionLocalUpdate, actionGlobalUpdate, actionNoUpdate],
warningMessage: `Your default target for platform ${c.platform} is set to ${c.runtime.target}.`,
});

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] = currentTarget.name;

c.files.project.configLocal = configLocal;
writeFileSync(c.paths.project.configLocal, configLocal);
}

if (chosenAction === actionGlobalUpdate) {
const configGlobal = c.files.workspace.config;
if (configGlobal) {
if (!configGlobal.defaultTargets) configGlobal.defaultTargets = {};
configGlobal.defaultTargets[c.platform] = currentTarget.name;

c.files.workspace.config = configGlobal;
writeFileSync(c.paths.workspace.config, configGlobal);
}
}
await updateDefaultTargets(c, currentTarget.name);
}
if (!desiredSim?.isDevice) {
const target = c.runtime.target?.replace(/(\s+)/g, '\\$1');
Expand Down
5 changes: 5 additions & 0 deletions packages/sdk-tizen/src/__tests__/deviceManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const ERROR_MSG = {
};

jest.mock('@rnv/core');
jest.mock('@rnv/sdk-utils');

beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -135,6 +136,7 @@ describe('launchTizenTarget', () => {
value: '111.111.111:11111',
},
],
default: 'emulatorTizen',
});
expect(executeAsync).toHaveBeenCalledWith(
'tizen-emulator launch --name emulatorTizen',
Expand Down Expand Up @@ -163,6 +165,7 @@ describe('launchTizenTarget', () => {
name: 'chosenEmulator',
type: 'list',
message: 'which emulator would you like to launch?',
default: 'emulatorTizen',
choices: [
{
key: 'emulatorTizen',
Expand Down Expand Up @@ -202,6 +205,7 @@ describe('launchTizenTarget', () => {
{ key: 'emulatorTizen', name: 'emulatorTizen', value: 'emulatorTizen' },
{ key: '111.111.111.111:11111', name: '111.111.111.111:11111', value: '111.111.111.111:11111' },
],
default: 'emulatorTizen',
});
expect(result).toBe(true);
});
Expand Down Expand Up @@ -242,6 +246,7 @@ describe('launchTizenTarget', () => {
value: '111.111.111.111:11111',
},
],
default: 'emulatorTizen',
});
expect(executeAsync).toHaveBeenCalledWith(
'tizen-emulator launch --name emulatorTizen',
Expand Down
60 changes: 42 additions & 18 deletions packages/sdk-tizen/src/deviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { CLI_SDB_TIZEN, CLI_TIZEN, CLI_TIZEN_EMULATOR } from './constants';

import { TizenDevice, TizenSecurityConfig } from './types';
import { updateDefaultTargets } from '@rnv/sdk-utils';

const xml2js = require('xml2js');

Expand Down Expand Up @@ -72,7 +73,11 @@ const formatXMLObject = (
return {};
};

export const launchTizenTarget = async (name: string | true, hideDevices?: boolean): Promise<boolean> => {
export const launchTizenTarget = async (
name: string | true,
hideDevices?: boolean,
updateDefault = false
): Promise<boolean> => {
const c = getContext();
logDefault(`launchTizenTarget:${name}`);
if (name === true) {
Expand All @@ -81,7 +86,7 @@ export const launchTizenTarget = async (name: string | true, hideDevices?: boole
const devices_lines = devices.split('\n');
const devicesArr = devices_lines.slice(1).map((line: string) => line.split(' ')[0]); // devices array with only their ip

const allDownloadedEmulators = emulators.split('\n'); // all tizen, tizenwatch and tizenmobile emulators
const allDownloadedEmulators = emulators.split('\n').map((em) => em.trim()); // all tizen, tizenwatch and tizenmobile emulators

const specificEmulators = await getEmulatorType(allDownloadedEmulators, c.platform as string);
const specificDevices = await getDeviceType(devicesArr, c.platform as string);
Expand All @@ -104,9 +109,19 @@ export const launchTizenTarget = async (name: string | true, hideDevices?: boole
? 'which emulator would you like to launch?'
: 'which emulator or device would you like to launch?',
choices,
default: choices.find((it) => it.key.includes('samsung'))?.key || choices[0].key,
});

if (
chosenEmulator &&
(c.files.project.configLocal?.defaultTargets?.[c.platform!] ||
c.files.workspace.config?.defaultTargets?.[c.platform!] !== chosenEmulator)
) {
// update defaultTarget in .rnv/renative.json
await updateDefaultTargets(c, chosenEmulator);
}
name = chosenEmulator;
c.runtime.target = chosenEmulator;
}
if (name && typeof name === 'string') {
const ipRegex = /^(?:\d{1,3}\.){3}\d{1,3}:\d{1,5}$/;
Expand All @@ -118,11 +133,16 @@ export const launchTizenTarget = async (name: string | true, hideDevices?: boole
return new Promise(() => logInfo('Device is launched.'));
}
try {
await executeAsync(
`${c.cli[CLI_TIZEN_EMULATOR]} launch --name ${name}`,
ExecOptionsPresets.SPINNER_FULL_ERROR_SUMMARY
);
return true;
if (updateDefault) {
await runTizenSimOrDevice();
return true;
} else {
await executeAsync(
`${c.cli[CLI_TIZEN_EMULATOR]} launch --name ${name}`,
ExecOptionsPresets.SPINNER_FULL_ERROR_SUMMARY
);
return true;
}
} catch (e) {
if (typeof e === 'string') {
if (e.includes(ERROR_MSG.UNKNOWN_VM)) {
Expand Down Expand Up @@ -428,7 +448,7 @@ export const runTizenSimOrDevice = async () => {
if (!tId) return Promise.reject(`Tizen platform requires "id" filed in platforms.tizen`);
const askForEmulator = async () => {
if (!target) {
launchTizenTarget(true);
launchTizenTarget(true, undefined, true);
return;
}
const { startEmulator } = await inquirerPrompt({
Expand All @@ -439,7 +459,7 @@ export const runTizenSimOrDevice = async () => {

if (startEmulator) {
isRunningEmulator = true;
const defaultTarget = c.files.workspace.config?.defaultTargets?.[platform];
const defaultTarget = c.runtime.target;
if (!defaultTarget) {
logError('No default target found for tizen. please provide one using -t option');
return;
Expand Down Expand Up @@ -482,10 +502,10 @@ Please create one and then edit the default target from ${c.paths.workspace.dir}
hasDevice = true;
} catch (e) {
if (typeof e === 'string' && e.includes('No device matching')) {
if (target) {
if (c.runtime.target) {
isRunningEmulator = true;
await launchTizenTarget(target);
hasDevice = await _waitForEmulatorToBeReady(target);
await launchTizenTarget(c.runtime.target);
hasDevice = await _waitForEmulatorToBeReady(c.runtime.target);
} else {
return Promise.reject('Not target specified. (-t)');
}
Expand All @@ -512,16 +532,16 @@ Please create one and then edit the default target from ${c.paths.workspace.dir}
logError(err);
}

if (!target) {
if (!c.runtime.target) {
return Promise.reject('Not target specified. (-t)');
}

if (hasDevice) {
await launchTizenTarget(true);
} else {
isRunningEmulator = true;
await launchTizenTarget(target);
hasDevice = await _waitForEmulatorToBeReady(target);
await launchTizenTarget(c.runtime.target);
hasDevice = await _waitForEmulatorToBeReady(c.runtime.target);
}
}

Expand All @@ -534,7 +554,10 @@ Please create one and then edit the default target from ${c.paths.workspace.dir}

if (platform !== 'tizenwatch' && platform !== 'tizenmobile' && hasDevice) {
// change id for for emulator because tizen 8+ fails to run app with
await execCLI(CLI_TIZEN, `run -p ${isRunningEmulator ? tId.split('.')[0] : tId} -t ${deviceID}`);
await execCLI(
CLI_TIZEN,
`run -p ${isRunningEmulator && !deviceID.includes('samsung') ? tId.split('.')[0] : tId} -t ${deviceID}`
);
} else if ((platform === 'tizenwatch' || platform === 'tizenmobile') && hasDevice) {
const packageID = tId.split('.');
await execCLI(CLI_TIZEN, `run -p ${packageID[0]} -t ${deviceID}`);
Expand Down Expand Up @@ -566,8 +589,9 @@ Please create one and then edit the default target from ${c.paths.workspace.dir}
// try to launch it, see if it's a emulator that's not started yet
isRunningEmulator = true;
await launchTizenTarget(target);
await _waitForEmulatorToBeReady(target);
deviceID = target;
const currentTarget = c.runtime.target || target;
await _waitForEmulatorToBeReady(currentTarget);
deviceID = currentTarget;
return continueLaunching();
} catch (e) {
return askForEmulator();
Expand Down
45 changes: 44 additions & 1 deletion packages/sdk-utils/src/target.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getContext, inquirerPrompt } from '@rnv/core';
import { RnvContext, chalk, getContext, inquirerPrompt, writeFileSync } from '@rnv/core';

export const getTargetWithOptionalPrompt = async () => {
const ctx = getContext();
Expand Down Expand Up @@ -31,3 +31,46 @@ export const getTargetWithOptionalPrompt = async () => {
}
return target;
};

export const updateDefaultTargets = async (c: RnvContext, currentTarget: string) => {
if (!c.platform) return;
const localOverridden = !!c.files.project.configLocal?.defaultTargets?.[c.platform];
const defaultTarget = c.runtime.target;
const actionLocalUpdate = `Update ${chalk().green('project')} default target for platform ${c.platform}`;
const actionGlobalUpdate = `Update ${chalk().green('global')}${
localOverridden ? ` and ${chalk().green('project')}` : ''
} default target for platform ${c.platform}`;
const actionNoUpdate = "Don't update";

const { chosenAction } = await inquirerPrompt({
message: 'What to do next?',
type: 'list',
name: 'chosenAction',
choices: [actionLocalUpdate, actionGlobalUpdate, actionNoUpdate],
warningMessage: `Your default target for platform ${c.platform} is ${
!defaultTarget ? 'not defined' : `set to ${defaultTarget}`
}.`,
});

c.runtime.target = currentTarget;

if (chosenAction === actionLocalUpdate || (chosenAction === actionGlobalUpdate && localOverridden)) {
const configLocal = c.files.project.configLocal || {};
if (!configLocal.defaultTargets) configLocal.defaultTargets = {};
configLocal.defaultTargets[c.platform] = currentTarget;

c.files.project.configLocal = configLocal;
writeFileSync(c.paths.project.configLocal, JSON.stringify(configLocal, null, 2));
}

if (chosenAction === actionGlobalUpdate) {
const configGlobal = c.files.workspace.config;
if (configGlobal) {
if (!configGlobal.defaultTargets) configGlobal.defaultTargets = {};
configGlobal.defaultTargets[c.platform] = currentTarget;

c.files.workspace.config = configGlobal;
writeFileSync(c.paths.workspace.config, JSON.stringify(configGlobal, null, 2));
}
}
};
Loading

0 comments on commit d2b4189

Please sign in to comment.