Skip to content

Commit

Permalink
Revert "Revert "[build-tools] Update expo-updates runtime version res…
Browse files Browse the repository at this point in the history
…olution (#354)" (#355)" (#356)
  • Loading branch information
wschurman authored Mar 7, 2024
1 parent 39e063a commit f64b01f
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function configureEASUpdateIfInstalledFunction(): BuildFunction {
}

await configureEASUpdateAsync({
expoUpdatesPackageVersion,
job,
workingDirectory: stepCtx.workingDirectory,
logger: stepCtx.logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe(configureEASUpdateAsync, () => {

it('aborts if updates.url (app config) is set but updates.channel (eas.json) is not', async () => {
await configureEASUpdateAsync({
expoUpdatesPackageVersion: '1.0',
job: { platform: Platform.IOS } as unknown as Job,
workingDirectory: '/app',
logger: createLogger({
Expand All @@ -43,6 +44,7 @@ describe(configureEASUpdateAsync, () => {

it('configures for EAS if updates.channel (eas.json) and updates.url (app config) are set', async () => {
await configureEASUpdateAsync({
expoUpdatesPackageVersion: '1.0',
job: {
updates: {
channel: 'main',
Expand All @@ -69,6 +71,7 @@ describe(configureEASUpdateAsync, () => {

it('configures for EAS if the updates.channel and releaseChannel are both set', async () => {
await configureEASUpdateAsync({
expoUpdatesPackageVersion: '1.0',
job: {
updates: { channel: 'main' },
releaseChannel: 'default',
Expand Down
15 changes: 12 additions & 3 deletions packages/build-tools/src/steps/utils/expoUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Job, Platform } from '@expo/eas-build-job';
import { bunyan } from '@expo/logger';
import { ExpoConfig } from '@expo/config';
import { getRuntimeVersionNullableAsync } from '@expo/config-plugins/build/utils/Updates';

import { resolveRuntimeVersionAsync } from '../../utils/resolveRuntimeVersionAsync';

import {
iosGetNativelyDefinedChannelAsync,
Expand All @@ -20,6 +21,7 @@ export async function configureEASUpdateAsync({
logger,
inputs,
appConfig,
expoUpdatesPackageVersion,
}: {
job: Job;
workingDirectory: string;
Expand All @@ -29,11 +31,18 @@ export async function configureEASUpdateAsync({
channel?: string;
};
appConfig: ExpoConfig;
expoUpdatesPackageVersion: string;
}): Promise<void> {
const runtimeVersion =
inputs.channel ??
inputs.runtimeVersion ??
job.version?.runtimeVersion ??
(await getRuntimeVersionNullableAsync(workingDirectory, appConfig, job.platform));
(await resolveRuntimeVersionAsync({
expoUpdatesPackageVersion,
projectDir: workingDirectory,
exp: appConfig,
platform: job.platform,
logger,
}));

const jobOrInputChannel = inputs.channel ?? job.updates?.channel;

Expand Down
31 changes: 22 additions & 9 deletions packages/build-tools/src/utils/expoUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import assert from 'assert';

import { Platform, Job } from '@expo/eas-build-job';
import { getRuntimeVersionNullableAsync } from '@expo/config-plugins/build/utils/Updates';
import semver from 'semver';

import {
Expand All @@ -23,6 +22,7 @@ import {
import { BuildContext } from '../context';

import getExpoUpdatesPackageVersionIfInstalledAsync from './getExpoUpdatesPackageVersionIfInstalledAsync';
import { resolveRuntimeVersionAsync } from './resolveRuntimeVersionAsync';

export async function setRuntimeVersionNativelyAsync(
ctx: BuildContext<Job>,
Expand Down Expand Up @@ -139,20 +139,22 @@ export async function configureEASExpoUpdatesAsync(ctx: BuildContext<Job>): Prom
}

export async function configureExpoUpdatesIfInstalledAsync(ctx: BuildContext<Job>): Promise<void> {
const expoUpdatesVersion = await getExpoUpdatesPackageVersionIfInstalledAsync(
const expoUpdatesPackageVersion = await getExpoUpdatesPackageVersionIfInstalledAsync(
ctx.getReactNativeProjectDirectory()
);
if (expoUpdatesVersion === null) {
if (expoUpdatesPackageVersion === null) {
return;
}

const appConfigRuntimeVersion =
ctx.job.version?.runtimeVersion ??
(await getRuntimeVersionNullableAsync(
ctx.getReactNativeProjectDirectory(),
ctx.appConfig,
ctx.job.platform
));
(await resolveRuntimeVersionAsync({
projectDir: ctx.getReactNativeProjectDirectory(),
exp: ctx.appConfig,
platform: ctx.job.platform,
logger: ctx.logger,
expoUpdatesPackageVersion,
}));
if (ctx.metadata?.runtimeVersion && ctx.metadata?.runtimeVersion !== appConfigRuntimeVersion) {
ctx.markBuildPhaseHasWarnings();
ctx.logger.warn(
Expand Down Expand Up @@ -199,7 +201,7 @@ export async function configureExpoUpdatesIfInstalledAsync(ctx: BuildContext<Job
ctx.markBuildPhaseHasWarnings();
}
}
} else if (shouldConfigureClassicUpdatesReleaseChannelAsFallback(expoUpdatesVersion)) {
} else if (shouldConfigureClassicUpdatesReleaseChannelAsFallback(expoUpdatesPackageVersion)) {
await configureClassicExpoUpdatesAsync(ctx);
}

Expand Down Expand Up @@ -261,3 +263,14 @@ export function shouldConfigureClassicUpdatesReleaseChannelAsFallback(
// of the expo-updates package published for SDK 50 was 0.19.0
return semver.lt(expoUpdatesPackageVersion, '0.19.0');
}

export function isModernExpoUpdatesCLIWithRuntimeVersionCommandSupported(
expoUpdatesPackageVersion: string
): boolean {
if (expoUpdatesPackageVersion.includes('canary')) {
return true;
}

// TODO(wschurman): add semver check once we know the SDK51 version of expo-updates that supports this
return false;
}
32 changes: 32 additions & 0 deletions packages/build-tools/src/utils/expoUpdatesCli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import spawnAsync from '@expo/spawn-async';
import resolveFrom, { silent as silentResolveFrom } from 'resolve-from';

export class ExpoUpdatesCLIModuleNotFoundError extends Error {}
export class ExpoUpdatesCLIInvalidCommandError extends Error {}

export async function expoUpdatesCommandAsync(projectDir: string, args: string[]): Promise<string> {
let expoUpdatesCli;
try {
expoUpdatesCli =
silentResolveFrom(projectDir, 'expo-updates/bin/cli') ??
resolveFrom(projectDir, 'expo-updates/bin/cli.js');
} catch (e: any) {
if (e.code === 'MODULE_NOT_FOUND') {
throw new ExpoUpdatesCLIModuleNotFoundError(
`The \`expo-updates\` package was not found. Follow the installation directions at https://docs.expo.dev/bare/installing-expo-modules/`
);
}
throw e;
}

try {
return (await spawnAsync(expoUpdatesCli, args)).stdout;
} catch (e: any) {
if (e.stderr && typeof e.stderr === 'string' && e.stderr.includes('Invalid command')) {
throw new ExpoUpdatesCLIInvalidCommandError(
`The command specified by ${args} was not valid in the \`expo-updates\` CLI.`
);
}
throw e;
}
}
46 changes: 46 additions & 0 deletions packages/build-tools/src/utils/resolveRuntimeVersionAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ExpoConfig } from '@expo/config';
import { Updates } from '@expo/config-plugins';
import { bunyan } from '@expo/logger';

import { ExpoUpdatesCLIModuleNotFoundError, expoUpdatesCommandAsync } from './expoUpdatesCli';
import { isModernExpoUpdatesCLIWithRuntimeVersionCommandSupported } from './expoUpdates';

export async function resolveRuntimeVersionAsync({
exp,
platform,
projectDir,
logger,
expoUpdatesPackageVersion,
}: {
exp: ExpoConfig;
platform: 'ios' | 'android';
projectDir: string;
logger: bunyan;
expoUpdatesPackageVersion: string;
}): Promise<string | null> {
if (!isModernExpoUpdatesCLIWithRuntimeVersionCommandSupported(expoUpdatesPackageVersion)) {
// fall back to the previous behavior (using the @expo/config-plugins eas-cli dependency rather
// than the versioned @expo/config-plugins dependency in the project)
return await Updates.getRuntimeVersionNullableAsync(projectDir, exp, platform);
}

try {
const resolvedRuntimeVersionJSONResult = await expoUpdatesCommandAsync(projectDir, [
'runtimeversion:resolve',
'--platform',
platform,
]);
const runtimeVersionResult = JSON.parse(resolvedRuntimeVersionJSONResult);
if (runtimeVersionResult.fingerprintSources) {
logger.debug(`Resolved fingeprint runtime version for platform "${platform}". Sources:`);
logger.debug(runtimeVersionResult.fingerprintSources);
}
return runtimeVersionResult.runtimeVersion ?? null;
} catch (e: any) {
// if expo-updates is not installed, there's no need for a runtime version in the build
if (e instanceof ExpoUpdatesCLIModuleNotFoundError) {
return null;
}
throw e;
}
}

0 comments on commit f64b01f

Please sign in to comment.