Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adapt buildRun to properly open macOS apps #2232

Merged
merged 5 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {runOnDevice} from './runOnDevice';
import {runOnSimulator} from './runOnSimulator';
import {BuilderCommand} from '../../types';
import {supportedPlatforms} from '../../config/supportedPlatforms';
import openApp from './openApp';

export interface FlagsT extends BuildFlags {
simulator?: string;
Expand Down Expand Up @@ -127,7 +128,24 @@ const createRun =
);

if (platformName === 'macos') {
buildProject(xcodeProject, platformName, undefined, mode, scheme, args);
const buildOutput = await buildProject(
xcodeProject,
platformName,
undefined,
mode,
scheme,
args,
);

openApp({
buildOutput,
xcodeProject,
mode,
scheme,
target: args.target,
binaryPath: args.binaryPath,
});

return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {CLIError} from '@react-native-community/cli-tools';
import path from 'path';
import {BuildSettings} from './getBuildSettings';
import {ApplePlatform} from '../../types';

export async function getBuildPath(
buildSettings: BuildSettings,
platform: ApplePlatform = 'ios',
isCatalyst: boolean = false,
) {
const targetBuildDir = buildSettings.TARGET_BUILD_DIR;
Expand All @@ -24,6 +26,8 @@ export async function getBuildPath(

if (isCatalyst) {
return path.join(targetBuildDir, '-maccatalyst', executableFolderPath);
} else if (platform === 'macos') {
return path.join(targetBuildDir, fullProductName);
} else {
return path.join(targetBuildDir, executableFolderPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import chalk from 'chalk';
import {getBuildPath} from './getBuildPath';
import {getBuildSettings} from './getBuildSettings';
import path from 'path';
import {ApplePlatform} from '../../types';

function handleLaunchResult(
success: boolean,
Expand All @@ -19,13 +20,14 @@ function handleLaunchResult(
}

type Options = {
buildOutput: any;
buildOutput: string;
xcodeProject: IOSProjectInfo;
mode: string;
scheme: string;
target?: string;
udid: string;
binaryPath?: string;
platform?: ApplePlatform;
};

export default async function installApp({
szymonrybczak marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -36,6 +38,7 @@ export default async function installApp({
target,
udid,
binaryPath,
platform,
}: Options) {
let appPath = binaryPath;

Expand All @@ -52,11 +55,7 @@ export default async function installApp({
}

if (!appPath) {
appPath = await getBuildPath(buildSettings);
}

if (!buildSettings) {
throw new CLIError('Failed to get build settings for your project');
appPath = await getBuildPath(buildSettings, platform);
}

const targetBuildDir = buildSettings.TARGET_BUILD_DIR;
Expand Down
51 changes: 51 additions & 0 deletions packages/cli-platform-apple/src/commands/runCommand/openApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {CLIError, logger} from '@react-native-community/cli-tools';
import {IOSProjectInfo} from '@react-native-community/cli-types';
import chalk from 'chalk';
import {getBuildPath} from './getBuildPath';
import {getBuildSettings} from './getBuildSettings';
import execa from 'execa';

type Options = {
buildOutput: string;
xcodeProject: IOSProjectInfo;
mode: string;
scheme: string;
target?: string;
binaryPath?: string;
};

export default async function openApp({
buildOutput,
xcodeProject,
mode,
scheme,
target,
binaryPath,
}: Options) {
let appPath = binaryPath;

const buildSettings = await getBuildSettings(
xcodeProject,
mode,
buildOutput,
scheme,
target,
);

if (!buildSettings) {
throw new CLIError('Failed to get build settings for your project');
}

if (!appPath) {
appPath = await getBuildPath(buildSettings, 'macos');
}

logger.info(`Opening "${chalk.bold(appPath)}"`);

try {
await execa(`open ${appPath}`);
logger.success('Successfully launched the app');
} catch (e) {
logger.error('Failed to launch the app', e as string);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function runOnDevice(
throw new CLIError('Failed to get build settings for your project');
}

const appPath = await getBuildPath(buildSettings, true);
const appPath = await getBuildPath(buildSettings, platform, true);
const appProcess = child_process.spawn(`${appPath}/${scheme}`, [], {
detached: true,
stdio: 'ignore',
Expand Down Expand Up @@ -86,7 +86,7 @@ export async function runOnDevice(
throw new CLIError('Failed to get build settings for your project');
}

appPath = await getBuildPath(buildSettings);
appPath = await getBuildPath(buildSettings, platform);
} else {
appPath = args.binaryPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function runOnSimulator(
}

installApp({
buildOutput,
buildOutput: buildOutput ?? '',
xcodeProject,
mode,
scheme,
Expand Down
Loading