Skip to content

Commit

Permalink
Allow eas/prebuild in custom jobs (#492)
Browse files Browse the repository at this point in the history
# Why

Users may want to prebuild in custom jobs to run native unit tests.

# How

In essence, the problem with `BuildJob` vs `Job` in this function was about missing `platform`. We can infer it from `process.platform`.

# Test Plan

TS is ok with the changes.
  • Loading branch information
sjchmiela authored Jan 14, 2025
1 parent 9a804e6 commit b221cb6
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions packages/build-tools/src/steps/functions/prebuild.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import assert from 'assert';

import { Platform } from '@expo/config';
import { BuildJob } from '@expo/eas-build-job';
import { BuildFunction, BuildStepInput, BuildStepInputValueTypeName } from '@expo/steps';
import spawn from '@expo/turtle-spawn';

import { PackageManager, resolvePackageManager } from '../../utils/packageManager';

import { installNodeModules } from './installNodeModules';

type PrebuildOptions = {
clean?: boolean;
};

export function createPrebuildBuildFunction(): BuildFunction {
return new BuildFunction({
namespace: 'eas',
Expand All @@ -30,16 +23,22 @@ export function createPrebuildBuildFunction(): BuildFunction {
allowedValueTypeName: BuildStepInputValueTypeName.STRING,
required: false,
}),
BuildStepInput.createProvider({
id: 'platform',
allowedValueTypeName: BuildStepInputValueTypeName.STRING,
required: false,
}),
],
fn: async (stepCtx, { inputs, env }) => {
const { logger } = stepCtx;
const appleTeamId = inputs.apple_team_id.value as string | undefined;
const packageManager = resolvePackageManager(stepCtx.workingDirectory);
const defaultPlatform = process.platform === 'darwin' ? 'ios' : 'android';

assert(stepCtx.global.staticContext.job, 'Job is not defined');
const job = stepCtx.global.staticContext.job;
assert(job.platform, 'Prebuild command is not supported in generic jobs.');
const prebuildCommandArgs = getPrebuildCommandArgs(job, {
const prebuildCommandArgs = getPrebuildCommandArgs({
platform: job.platform ?? defaultPlatform,
customPrebuildCommand: job.platform ? job.experimental?.prebuildCommand : undefined,
clean: inputs.clean.value as boolean,
});
const argsWithExpo = ['expo', ...prebuildCommandArgs];
Expand Down Expand Up @@ -68,22 +67,36 @@ export function createPrebuildBuildFunction(): BuildFunction {
});
}

function getPrebuildCommandArgs(job: BuildJob, { clean }: PrebuildOptions): string[] {
if (job.experimental?.prebuildCommand) {
return sanitizeUserDefinedPrebuildCommand(job.experimental.prebuildCommand, job.platform, {
function getPrebuildCommandArgs({
platform,
customPrebuildCommand,
clean,
}: {
platform: Platform;
customPrebuildCommand?: string;
clean: boolean;
}): string[] {
if (customPrebuildCommand) {
return sanitizeUserDefinedPrebuildCommand({
customPrebuildCommand,
platform,
clean,
});
}
return ['prebuild', '--no-install', '--platform', job.platform, ...(clean ? ['--clean'] : [])];
return ['prebuild', '--no-install', '--platform', platform, ...(clean ? ['--clean'] : [])];
}

// TODO: deprecate prebuildCommand in eas.json
function sanitizeUserDefinedPrebuildCommand(
userDefinedPrebuildCommand: string,
platform: Platform,
{ clean }: PrebuildOptions
): string[] {
let prebuildCommand = userDefinedPrebuildCommand;
function sanitizeUserDefinedPrebuildCommand({
customPrebuildCommand,
platform,
clean,
}: {
customPrebuildCommand: string;
platform: Platform;
clean: boolean;
}): string[] {
let prebuildCommand = customPrebuildCommand;
if (!prebuildCommand.match(/(?:--platform| -p)/)) {
prebuildCommand = `${prebuildCommand} --platform ${platform}`;
}
Expand Down

0 comments on commit b221cb6

Please sign in to comment.