Skip to content

Commit

Permalink
[eas-cli] VCS Context Fields to replace helper functions (#2086)
Browse files Browse the repository at this point in the history
* [eas-cli] Add context fields for vcs clients

* [eas-cli] Replace vcs client helper with new context field

* [eas-cli] Pass instance of vcs client in helpers instead of re-calling vcs helper

* update CHANGELOG.md
  • Loading branch information
Josh-McFarlin committed Oct 18, 2023
1 parent 5ed155e commit 743ae7c
Show file tree
Hide file tree
Showing 78 changed files with 560 additions and 232 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This is the log of notable changes to EAS CLI and related packages.
### 🧹 Chores

- Add `requiredPackageManager` to metadata. ([#2067](https://github.com/expo/eas-cli/pull/2067) by [@kadikraman](https://github.com/kadikraman))
- Move `getVcsClient` into command context. ([#2086](https://github.com/expo/eas-cli/pull/2086) by [@Josh-McFarlin](https://github.com/Josh-McFarlin))
- Display Apple device creation date when listing devices. ([#2092](https://github.com/expo/eas-cli/pull/2092) by [@radoslawkrzemien](https://github.com/radoslawkrzemien))

## [5.4.0](https://github.com/expo/eas-cli/releases/tag/v5.4.0) - 2023-09-28
Expand Down
7 changes: 3 additions & 4 deletions packages/eas-cli/src/branch/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { getVcsClient } from '../vcs';
import { Client } from '../vcs/vcs';

export async function getDefaultBranchNameAsync(): Promise<string> {
export async function getDefaultBranchNameAsync(vcsClient: Client): Promise<string> {
return (
(await getVcsClient().getBranchNameAsync()) ||
`branch-${Math.random().toString(36).substring(2, 4)}`
(await vcsClient.getBranchNameAsync()) || `branch-${Math.random().toString(36).substring(2, 4)}`
);
}

Expand Down
9 changes: 7 additions & 2 deletions packages/eas-cli/src/build/android/__tests__/version-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import os from 'os';
import path from 'path';

import { getAppBuildGradleAsync, resolveConfigValue } from '../../../project/android/gradleUtils';
import { getVcsClient } from '../../../vcs';
import {
BumpStrategy,
bumpVersionAsync,
Expand All @@ -18,6 +19,8 @@ import {
const fsReal = jest.requireActual('fs').promises as typeof fs;
jest.mock('fs');

const vcsClient = getVcsClient();

afterAll(async () => {
// do not remove the following line
// this fixes a weird error with tempy in @expo/image-utils
Expand Down Expand Up @@ -191,7 +194,8 @@ describe(maybeResolveVersionsAsync, () => {
const { appVersion, appBuildVersion } = await maybeResolveVersionsAsync(
'/app',
exp,
{} as BuildProfile<Platform.ANDROID>
{} as BuildProfile<Platform.ANDROID>,
vcsClient
);
expect(appVersion).toBe('3.0.0');
expect(appBuildVersion).toBe('123');
Expand All @@ -203,7 +207,8 @@ describe(maybeResolveVersionsAsync, () => {
const { appVersion, appBuildVersion } = await maybeResolveVersionsAsync(
'/app',
exp,
{} as BuildProfile<Platform.ANDROID>
{} as BuildProfile<Platform.ANDROID>,
vcsClient
);
expect(appVersion).toBe('5.0.0');
expect(appBuildVersion).toBe('126');
Expand Down
16 changes: 14 additions & 2 deletions packages/eas-cli/src/build/android/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,22 @@ This means that it will most likely produce an AAB and you will not be able to i
await checkGoogleServicesFileAsync(ctx);
await validatePNGsForManagedProjectAsync(ctx);

const gradleContext = await resolveGradleBuildContextAsync(ctx.projectDir, buildProfile);
const gradleContext = await resolveGradleBuildContextAsync(
ctx.projectDir,
buildProfile,
ctx.vcsClient
);

if (ctx.workflow === Workflow.MANAGED) {
await ensureApplicationIdIsDefinedForManagedProjectAsync(ctx);
}

const applicationId = await getApplicationIdAsync(ctx.projectDir, ctx.exp, gradleContext);
const applicationId = await getApplicationIdAsync(
ctx.projectDir,
ctx.exp,
ctx.vcsClient,
gradleContext
);
const versionCodeOverride =
ctx.easJsonCliConfig?.appVersionSource === AppVersionSource.REMOTE
? await resolveRemoteVersionCodeAsync(ctx.graphqlClient, {
Expand All @@ -74,6 +83,7 @@ This means that it will most likely produce an AAB and you will not be able to i
exp: ctx.exp,
applicationId,
buildProfile,
vcsClient: ctx.vcsClient,
})
: undefined;

Expand All @@ -97,6 +107,7 @@ export async function prepareAndroidBuildAsync(
? false
: ctx.buildProfile.autoIncrement,
projectId: ctx.projectId,
vcsClient: ctx.vcsClient,
});
},
prepareJobAsync: async (
Expand Down Expand Up @@ -136,6 +147,7 @@ async function ensureAndroidCredentialsAsync(
const androidApplicationIdentifier = await getApplicationIdAsync(
ctx.projectDir,
ctx.exp,
ctx.vcsClient,
ctx.android.gradleContext
);
const provider = new AndroidCredentialsProvider(ctx.credentialsCtx, {
Expand Down
3 changes: 1 addition & 2 deletions packages/eas-cli/src/build/android/prepareJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import slash from 'slash';
import { AndroidCredentials } from '../../credentials/android/AndroidCredentialsProvider';
import { getCustomBuildConfigPath } from '../../project/customBuildConfig';
import { getUsername } from '../../project/projectUtils';
import { getVcsClient } from '../../vcs';
import { BuildContext } from '../context';

interface JobData {
Expand All @@ -34,7 +33,7 @@ export async function prepareJobAsync(
const username = getUsername(ctx.exp, ctx.user);
const buildProfile: BuildProfile<Platform.ANDROID> = ctx.buildProfile;
const projectRootDirectory =
slash(path.relative(await getVcsClient().getRootPathAsync(), ctx.projectDir)) || '.';
slash(path.relative(await ctx.vcsClient.getRootPathAsync(), ctx.projectDir)) || '.';
const { credentials } = jobData;
const buildCredentials = credentials
? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Log from '../../log';
import { isExpoUpdatesInstalled } from '../../project/projectUtils';
import { resolveWorkflowAsync } from '../../project/workflow';
import { syncUpdatesConfigurationAsync } from '../../update/android/UpdatesModule';
import { Client } from '../../vcs/vcs';
import { BumpStrategy, bumpVersionAsync, bumpVersionInAppJsonAsync } from './version';

export async function syncProjectConfigurationAsync(
Expand All @@ -20,14 +21,16 @@ export async function syncProjectConfigurationAsync(
exp,
localAutoIncrement,
projectId,
vcsClient,
}: {
projectDir: string;
exp: ExpoConfig;
localAutoIncrement?: AndroidVersionAutoIncrement;
projectId: string;
vcsClient: Client;
}
): Promise<void> {
const workflow = await resolveWorkflowAsync(projectDir, Platform.ANDROID);
const workflow = await resolveWorkflowAsync(projectDir, Platform.ANDROID, vcsClient);
const versionBumpStrategy = resolveVersionBumpStrategy(localAutoIncrement ?? false);

if (workflow === Workflow.GENERIC) {
Expand Down
10 changes: 7 additions & 3 deletions packages/eas-cli/src/build/android/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '../../project/android/gradleUtils';
import { getNextVersionCode } from '../../project/android/versions';
import { resolveWorkflowAsync } from '../../project/workflow';
import { Client } from '../../vcs/vcs';
import { updateAppJsonConfigAsync } from '../utils/appJson';
import { bumpAppVersionAsync, ensureStaticConfigExists } from '../utils/version';

Expand Down Expand Up @@ -96,9 +97,10 @@ export async function bumpVersionInAppJsonAsync({
export async function maybeResolveVersionsAsync(
projectDir: string,
exp: ExpoConfig,
buildProfile: BuildProfile<Platform.ANDROID>
buildProfile: BuildProfile<Platform.ANDROID>,
vcsClient: Client
): Promise<{ appVersion?: string; appBuildVersion?: string }> {
const workflow = await resolveWorkflowAsync(projectDir, Platform.ANDROID);
const workflow = await resolveWorkflowAsync(projectDir, Platform.ANDROID, vcsClient);
if (workflow === Workflow.GENERIC) {
const buildGradle = await getAppBuildGradleAsync(projectDir);
try {
Expand Down Expand Up @@ -183,12 +185,14 @@ export async function resolveRemoteVersionCodeAsync(
exp,
applicationId,
buildProfile,
vcsClient,
}: {
projectDir: string;
projectId: string;
exp: ExpoConfig;
applicationId: string;
buildProfile: BuildProfile<Platform.ANDROID>;
vcsClient: Client;
}
): Promise<string> {
const remoteVersions = await AppVersionQuery.latestVersionAsync(
Expand All @@ -198,7 +202,7 @@ export async function resolveRemoteVersionCodeAsync(
applicationId
);

const localVersions = await maybeResolveVersionsAsync(projectDir, exp, buildProfile);
const localVersions = await maybeResolveVersionsAsync(projectDir, exp, buildProfile, vcsClient);
let currentBuildVersion: string;
if (remoteVersions?.buildVersion) {
currentBuildVersion = remoteVersions.buildVersion;
Expand Down
8 changes: 4 additions & 4 deletions packages/eas-cli/src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import { formatBytes } from '../utils/files';
import { printJsonOnlyOutput } from '../utils/json';
import { createProgressTracker } from '../utils/progress';
import { sleepAsync } from '../utils/promise';
import { getVcsClient } from '../vcs';
import { BuildContext } from './context';
import {
EasBuildDownForMaintenanceError,
Expand Down Expand Up @@ -125,9 +124,10 @@ export async function prepareBuildRequestForPlatformAsync<
}
);

if (await getVcsClient().isCommitRequiredAsync()) {
if (await ctx.vcsClient.isCommitRequiredAsync()) {
Log.addNewLineIfNone();
await reviewAndCommitChangesAsync(
ctx.vcsClient,
`[EAS Build] Run EAS Build for ${requestedPlatformDisplayNames[ctx.platform as Platform]}`,
{ nonInteractive: ctx.nonInteractive }
);
Expand All @@ -137,7 +137,7 @@ export async function prepareBuildRequestForPlatformAsync<
if (ctx.localBuildOptions.localBuildMode === LocalBuildMode.LOCAL_BUILD_PLUGIN) {
projectArchive = {
type: ArchiveSourceType.PATH,
path: (await makeProjectTarballAsync()).path,
path: (await makeProjectTarballAsync(ctx.vcsClient)).path,
};
} else if (ctx.localBuildOptions.localBuildMode === LocalBuildMode.INTERNAL) {
projectArchive = {
Expand Down Expand Up @@ -245,7 +245,7 @@ async function uploadProjectAsync<TPlatform extends Platform>(
'https://expo.fyi/eas-build-archive'
)}`
);
const projectTarball = await makeProjectTarballAsync();
const projectTarball = await makeProjectTarballAsync(ctx.vcsClient);

if (projectTarball.size > 1024 * 1024 * 100) {
Log.warn(
Expand Down
25 changes: 15 additions & 10 deletions packages/eas-cli/src/build/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import fs from 'fs-extra';
import Log, { learnMore } from '../log';
import { resolveWorkflowAsync } from '../project/workflow';
import { easCliVersion } from '../utils/easCli';
import { getVcsClient } from '../vcs';
import { Client } from '../vcs/vcs';
import { maybeBailOnRepoStatusAsync, reviewAndCommitChangesAsync } from './utils/repository';

interface ConfigureParams {
projectDir: string;
nonInteractive: boolean;
vcsClient: Client;
}

export async function easJsonExistsAsync(projectDir: string): Promise<boolean> {
Expand All @@ -36,14 +37,18 @@ export async function ensureProjectConfiguredAsync(
return true;
}

async function configureAsync({ projectDir, nonInteractive }: ConfigureParams): Promise<void> {
await maybeBailOnRepoStatusAsync();
async function configureAsync({
projectDir,
nonInteractive,
vcsClient,
}: ConfigureParams): Promise<void> {
await maybeBailOnRepoStatusAsync(vcsClient);

await createEasJsonAsync(projectDir);
await createEasJsonAsync(projectDir, vcsClient);

if (await getVcsClient().isCommitRequiredAsync()) {
if (await vcsClient.isCommitRequiredAsync()) {
Log.newLine();
await reviewAndCommitChangesAsync('Configure EAS Build', {
await reviewAndCommitChangesAsync(vcsClient, 'Configure EAS Build', {
nonInteractive,
});
}
Expand Down Expand Up @@ -92,20 +97,20 @@ const EAS_JSON_BARE_DEFAULT: EasJson = {
},
};

async function createEasJsonAsync(projectDir: string): Promise<void> {
async function createEasJsonAsync(projectDir: string, vcsClient: Client): Promise<void> {
const easJsonPath = EasJsonAccessor.formatEasJsonPath(projectDir);

const hasAndroidNativeProject =
(await resolveWorkflowAsync(projectDir, Platform.ANDROID)) === Workflow.GENERIC;
(await resolveWorkflowAsync(projectDir, Platform.ANDROID, vcsClient)) === Workflow.GENERIC;
const hasIosNativeProject =
(await resolveWorkflowAsync(projectDir, Platform.IOS)) === Workflow.GENERIC;
(await resolveWorkflowAsync(projectDir, Platform.IOS, vcsClient)) === Workflow.GENERIC;
const easJson =
hasAndroidNativeProject || hasIosNativeProject
? EAS_JSON_BARE_DEFAULT
: EAS_JSON_MANAGED_DEFAULT;

await fs.writeFile(easJsonPath, `${JSON.stringify(easJson, null, 2)}\n`);
await getVcsClient().trackFileAsync(easJsonPath);
await vcsClient.trackFileAsync(easJsonPath);
Log.withTick(
`Generated ${chalk.bold('eas.json')}. ${learnMore(
'https://docs.expo.dev/build-reference/eas-json/'
Expand Down
2 changes: 2 additions & 0 deletions packages/eas-cli/src/build/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { GradleBuildContext } from '../project/android/gradle';
import { CustomBuildConfigMetadata } from '../project/customBuildConfig';
import { XcodeBuildContext } from '../project/ios/scheme';
import { Actor } from '../user/User';
import { Client } from '../vcs/vcs';
import { LocalBuildOptions } from './local';

export type CommonContext<T extends Platform> = Omit<BuildContext<T>, 'android' | 'ios'>;
Expand Down Expand Up @@ -58,4 +59,5 @@ export interface BuildContext<T extends Platform> {
ios: T extends Platform.IOS ? IosBuildContext : undefined;
developmentClient: boolean;
requiredPackageManager: NodePackageManager['name'] | null;
vcsClient: Client;
}
7 changes: 6 additions & 1 deletion packages/eas-cli/src/build/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { CustomBuildConfigMetadata } from '../project/customBuildConfig';
import { getOwnerAccountForProjectIdAsync } from '../project/projectUtils';
import { resolveWorkflowAsync } from '../project/workflow';
import { Actor } from '../user/User';
import { Client } from '../vcs/vcs';
import { createAndroidContextAsync } from './android/build';
import { BuildContext, CommonContext } from './context';
import { createIosContextAsync } from './ios/build';
Expand All @@ -35,6 +36,7 @@ export async function createBuildContextAsync<T extends Platform>({
actor,
graphqlClient,
analytics,
vcsClient,
getDynamicPrivateProjectConfigAsync,
customBuildConfigMetadata,
}: {
Expand All @@ -52,13 +54,14 @@ export async function createBuildContextAsync<T extends Platform>({
actor: Actor;
graphqlClient: ExpoGraphqlClient;
analytics: Analytics;
vcsClient: Client;
getDynamicPrivateProjectConfigAsync: DynamicConfigContextFn;
customBuildConfigMetadata?: CustomBuildConfigMetadata;
}): Promise<BuildContext<T>> {
const { exp, projectId } = await getDynamicPrivateProjectConfigAsync({ env: buildProfile.env });
const projectName = exp.slug;
const account = await getOwnerAccountForProjectIdAsync(graphqlClient, projectId);
const workflow = await resolveWorkflowAsync(projectDir, platform);
const workflow = await resolveWorkflowAsync(projectDir, platform, vcsClient);
const accountId = account.id;
const runFromCI = getenv.boolish('CI', false);
const developmentClient =
Expand All @@ -79,6 +82,7 @@ export async function createBuildContextAsync<T extends Platform>({
analytics,
env: buildProfile.env,
easJsonCliConfig,
vcsClient,
});

const devClientProperties = getDevClientEventProperties({
Expand Down Expand Up @@ -125,6 +129,7 @@ export async function createBuildContextAsync<T extends Platform>({
user: actor,
graphqlClient,
analytics,
vcsClient,
workflow,
message,
runFromCI,
Expand Down
Loading

0 comments on commit 743ae7c

Please sign in to comment.