Skip to content

Commit

Permalink
build and install an iOS app (facebook#44465)
Browse files Browse the repository at this point in the history
Summary:

Allows us to `yarn build ios` the helloworld app.

Changelog: [Internal]

Differential Revision: D57067038
  • Loading branch information
blakef authored and facebook-github-bot committed May 15, 2024
1 parent 86dffb3 commit 641dc0b
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 259 deletions.
343 changes: 138 additions & 205 deletions packages/helloworld/cli.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,191 +9,36 @@
* @oncall react_native
*/

import type {Task} from '@react-native/core-cli-utils';
import type {ExecaPromise, Result} from 'execa';
import type {TaskSpec} from 'listr';

import {apple} from '@react-native/core-cli-utils';
import type {IOSDevice} from './lib/ios';

import {run} from './lib/cli';
import {pauseWatchman} from './lib/filesystem';
import {
bootSimulator,
getSimulatorDetails,
getXcodeBuildSettings,
launchApp,
launchSimulator,
} from './lib/ios';
import {app, apple} from '@react-native/core-cli-utils';
import chalk from 'chalk';
import {program} from 'commander';
import execa from 'execa';
import {readFileSync} from 'fs';
import Listr from 'listr';
import path from 'path';
import {Observable} from 'rxjs';

program.version(JSON.parse(readFileSync('./package.json', 'utf8')).version);

const FIRST = 1;

const bootstrap = program.command('bootstrap');

const cwd = {
ios: path.join(__dirname, 'ios'),
android: path.join(__dirname, 'android'),
root: __dirname,
};

type IOSDevice = {
lastBootedAt: Date,
dataPath: string,
dataPathSize: number,
logPath: string,
udid: string,
isAvailable: boolean,
availabilityError: string,
logPathSize: number,
deviceTypeIdentifier: string,
state: 'Shutdown' | 'Booted' | 'Creating',
name: string,
};

type ExecaPromiseMetaized = Promise<Result> & child_process$ChildProcess;

function observe(result: ExecaPromiseMetaized): Observable<string> {
return new Observable(observer => {
result.stderr.on('data', data =>
data
.toString('utf8')
.split('\n')
.filter(line => line.length > 0)
.forEach(line => observer.next('🟢 ' + line.trim())),
);
result.stdout.on('data', data =>
data
.toString('utf8')
.split('\n')
.filter(line => line.length > 0)
.forEach(line => observer.next('🟠 ' + line.trim())),
);
for (const event of ['close', 'end']) {
result.stdout.on(event, () => observer.complete());
}
result.stdout.on('error', error => observer.error(error));
return () => {
for (const out of [result.stderr, result.stdout]) {
out.destroy();
out.removeAllListeners();
}
};
});
}

function getXcodeBuildSettings(iosProjectFolder: string) {
const {stdout} = execa.sync(
'xcodebuild',
[
'-workspace',
'HelloWorld.xcworkspace',
'-scheme',
'HelloWorld',
'-configuration',
'Debug',
'-sdk',
'iphonesimulator',
'-showBuildSettings',
'-json',
],
{cwd: iosProjectFolder},
);
return JSON.parse(stdout);
}

async function getSimulatorDetails(nameOrUDID: string): Promise<IOSDevice> {
const {stdout} = execa.sync('xcrun', [
'simctl',
'list',
'devices',
'iPhone',
'available',
'--json',
]);
const json = JSON.parse(stdout);

const allAvailableDevices: IOSDevice[] = Object.values(json.devices)
.flatMap(devices => devices)
.filter(device => device.isAvailable)
.map(device => ({
...device,
lastBootedAt: new Date(device.lastBootedAt),
}));

if (nameOrUDID.length > 0 && nameOrUDID.toLowerCase() !== 'simulator') {
const namedDevice = allAvailableDevices.find(
device => device.udid === nameOrUDID || device.name === nameOrUDID,
);
if (namedDevice == null) {
const devices = allAvailableDevices
.map(device => `- ${device.name}: ${device.udid}`)
.join('\n - ');
throw new Error(
`Unable to find device with name or UDID: '${nameOrUDID}', found:\n\n${devices}`,
);
}
return namedDevice;
}

const allSimIPhones: IOSDevice[] = allAvailableDevices.filter(device =>
/SimDeviceType\.iPhone/.test(device.deviceTypeIdentifier),
);

// Pick anything that is booted, otherwise get your user to help out
const available = allSimIPhones.sort(
(a, b) => a.lastBootedAt.getTime() - b.lastBootedAt.getTime(),
);

if (available.length === 0) {
throw new Error(
'No simulator is available, please create on using the Simulator',
);
}

const booted = allSimIPhones
.filter(device => device.state === 'Booted')
.pop();

if (booted != null) {
return booted;
}

return allSimIPhones[0];
}

async function launchSimulator(udid: string): Promise<Result> {
// Boot something that's new
return execa('open', [
'-a',
'Simulator',
'--args',
'-CurrentDeviceUDID',
udid,
]);
}

type MixedTasks = Task<ExecaPromise> | Task<void>;
type Tasks = {
+[label: string]: MixedTasks,
};

function run(
tasks: Tasks,
exclude: {[label: string]: boolean} = {},
): Promise<void> {
let ordered: MixedTasks[] = [];
for (const [label, task] of Object.entries(tasks)) {
if (label in exclude) {
continue;
}
ordered.push(task);
}
ordered = ordered.sort((a, b) => a.order - b.order);

const spec: TaskSpec<void, Observable<string> | Promise<void> | void>[] =
ordered.map(task => ({
title: task.label,
task: () => {
const action = task.action();
if (action != null) {
return observe(action);
}
},
}));
return new Listr(spec).run();
}

bootstrap
.command('ios')
.description('Bootstrap iOS')
Expand All @@ -211,58 +56,146 @@ bootstrap

const build = program.command('build');

type BuildOptions = {
newArchitecture: boolean,
hermes: boolean,
onlyBuild: boolean,
device: string,
};

build
.command('ios')
.description('Builds & run your app for iOS')
.option('--new-architecture', 'Enable new architecture')
.option('--hermes', 'Use Hermes or point to a prebuilt tarball', true)
.option('--only-build', 'Build but do not run', false)
.option('--device', 'Any simulator or a specific device', 'simulator')
.action(async options => {
const device = await getSimulatorDetails(options.device);

if (!options.onlyBuild) {
await launchSimulator(device.udid);
.action(async (options: BuildOptions) => {
let device: IOSDevice;
try {
device = await getSimulatorDetails(options.device);
} catch (e) {
console.log(chalk.bold.red(e.message));
process.exit(1);
}

await run(
apple.build({
isWorkspace: true,
name: 'HelloWorld.xcworkspace',
mode: 'Debug',
scheme: 'HelloWorld',
cwd: cwd.ios,
destination: `id=${device.udid}`,
}),
);
if (device == null) {
return;
}

const settings = {
appPath: '',
bundleId: '',
bundleBuildDir: '',
bundleResourceDir: '',
};

await run({
buildSettings: {
order: 1,
label: 'Getting your build settings',
action: (): void => {
const xcode = getXcodeBuildSettings(cwd.ios)[0].buildSettings;
settings.appPath = path.join(
xcode.TARGET_BUILD_DIR,
xcode.EXECUTABLE_FOLDER_PATH,
);
settings.bundleId = xcode.PRODUCT_BUNDLE_IDENTIFIER;
await pauseWatchman(async () => {
await run({
buildSettings: {
order: FIRST,
label: 'Getting your build settings',
action: (): void => {
const xcode = getXcodeBuildSettings(cwd.ios)[0].buildSettings;
settings.appPath = path.join(
xcode.TARGET_BUILD_DIR,
xcode.EXECUTABLE_FOLDER_PATH,
);
settings.bundleId = xcode.PRODUCT_BUNDLE_IDENTIFIER;
settings.bundleBuildDir = xcode.CONFIGURATION_BUILD_DIR;
settings.bundleResourceDir = path.join(
xcode.CONFIGURATION_BUILD_DIR,
xcode.UNLOCALIZED_RESOURCES_FOLDER_PATH,
);
},
},
},
});

// Metro: src -> js
const jsBundlePath = path.join(
settings.bundleBuildDir,
'main.jsbundle.js',
);
// Hermes: js -> Hermes Byte Code
const binaryBundlePath = path.join(
settings.bundleResourceDir,
'main.jsbundle',
);

await run(
apple.build({
isWorkspace: true,
name: 'HelloWorld.xcworkspace',
mode: 'Debug',
scheme: 'HelloWorld',
cwd: cwd.ios,
env: {
FORCE_BUNDLING: 'true',
},
destination: `id=${device.udid}`,
}),
);

await run(
app.bundle({
mode: 'bundle',
cwd: cwd.root,
entryFile: 'index.js',
platform: 'ios',
outputJsBundle: jsBundlePath,
minify: false,
optimize: false,
outputSourceMap: settings.bundleResourceDir,
outputBundle: binaryBundlePath,
dev: true,
target: 'hermes',
hermes: {
hermesc: path.join(
cwd.ios,
'Pods',
'hermes-engine',
'build_host_hermesc',
'bin',
'hermesc',
),
},
}),
);
});

await run(
apple.ios.install({
if (!options.onlyBuild) {
const {install} = apple.ios.install({
cwd: cwd.ios,
device: device.udid,
appPath: settings.appPath,
bundleId: settings.bundleId,
}),
);
});

await new Listr([
{
title: 'Booting simulator',
task: (_: mixed, task) => {
if (device.state === 'Booted') {
task.skip('Simulator currently Booted');
} else {
return bootSimulator(device);
}
},
},
{
title: 'Launching simulator',
task: () => launchSimulator(device),
},
{
title: 'Installing app on simulator',
task: () => install.action(),
},
{
title: 'Launching app on simulator',
task: () => launchApp(device.udid, settings.bundleId),
},
]).run();
}
});

if (require.main === module) {
Expand Down

0 comments on commit 641dc0b

Please sign in to comment.