Skip to content

Commit 4f8c7dc

Browse files
authored
refactor: move saving cache to buildApp; show that fingerprint was updated (#559)
* refactor: move saving cache to buildApp; show that fingerprint was updated * update only if pods did install * changeset
1 parent 97e2e67 commit 4f8c7dc

File tree

6 files changed

+78
-61
lines changed

6 files changed

+78
-61
lines changed

.changeset/short-bags-hang.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@rock-js/platform-apple-helpers': patch
3+
'@rock-js/tools': patch
4+
---
5+
6+
refactor: move saving cache to buildApp; show that fingerprint was updated

packages/platform-apple-helpers/src/lib/commands/build/createBuild.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,12 @@ export const createBuild = async ({
5353
: 'device'
5454
: 'simulator';
5555

56-
async function getArtifactName({ silent }: { silent?: boolean } = {}) {
57-
return await formatArtifactName({
58-
platform: 'ios',
59-
traits: [deviceOrSimulator, args.configuration ?? 'Debug'],
60-
root: projectRoot,
61-
fingerprintOptions,
62-
silent,
63-
});
64-
}
65-
66-
let artifactName = await getArtifactName();
56+
const artifactName = await formatArtifactName({
57+
platform: 'ios',
58+
traits: [deviceOrSimulator, args.configuration ?? 'Debug'],
59+
root: projectRoot,
60+
fingerprintOptions,
61+
});
6762

6863
const binaryPath = await getBinaryPath({
6964
platformName,
@@ -90,26 +85,22 @@ export const createBuild = async ({
9085
}
9186

9287
try {
93-
const { appPath, didInstallPods, ...buildAppResult } = await buildApp({
88+
const { appPath, ...buildAppResult } = await buildApp({
9489
projectRoot,
9590
projectConfig,
9691
platformName,
9792
args,
9893
reactNativePath,
9994
brownfield,
95+
artifactName,
96+
deviceOrSimulator,
97+
fingerprintOptions,
10098
});
10199
logger.log(`Build available at: ${colorLink(relativeToCwd(appPath))}`);
102100

103101
xcodeProject = buildAppResult.xcodeProject;
104102
sourceDir = buildAppResult.sourceDir;
105103
scheme = buildAppResult.scheme;
106-
107-
// After installing pods the fingerprint likely changes.
108-
// We update the artifact name to reflect the new fingerprint and store proper entry in the local cache.
109-
if (didInstallPods) {
110-
artifactName = await getArtifactName({ silent: true });
111-
}
112-
saveLocalBuildCache(artifactName, appPath);
113104
} catch (error) {
114105
const message = `Failed to create ${args.archive ? 'archive' : 'build'}`;
115106
throw new RockError(message, { cause: error });

packages/platform-apple-helpers/src/lib/commands/run/createRun.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
logger,
1010
promptSelect,
1111
RockError,
12-
saveLocalBuildCache,
1312
} from '@rock-js/tools';
1413
import type {
1514
ApplePlatform,
@@ -53,17 +52,12 @@ export const createRun = async ({
5352
: 'device'
5453
: 'simulator';
5554

56-
async function getArtifactName({ silent }: { silent?: boolean } = {}) {
57-
return await formatArtifactName({
58-
platform: 'ios',
59-
traits: [deviceOrSimulator, args.configuration ?? 'Debug'],
60-
root: projectRoot,
61-
fingerprintOptions,
62-
silent,
63-
});
64-
}
65-
66-
let artifactName = await getArtifactName();
55+
const artifactName = await formatArtifactName({
56+
platform: 'ios',
57+
traits: [deviceOrSimulator, args.configuration ?? 'Debug'],
58+
root: projectRoot,
59+
fingerprintOptions,
60+
});
6761

6862
const binaryPath = await getBinaryPath({
6963
platformName: 'ios',
@@ -94,6 +88,9 @@ export const createRun = async ({
9488
deviceName,
9589
reactNativePath,
9690
binaryPath,
91+
artifactName,
92+
deviceOrSimulator,
93+
fingerprintOptions,
9794
});
9895
await runOnMac(appPath);
9996
return;
@@ -107,6 +104,9 @@ export const createRun = async ({
107104
deviceName,
108105
reactNativePath,
109106
binaryPath,
107+
artifactName,
108+
deviceOrSimulator,
109+
fingerprintOptions,
110110
});
111111
if (scheme) {
112112
await runOnMacCatalyst(appPath, scheme);
@@ -140,7 +140,7 @@ ${devices
140140
}
141141
cacheRecentDevice(device, platformName);
142142
if (device.type === 'simulator') {
143-
const [, { appPath, infoPlistPath, didInstallPods }] = await Promise.all([
143+
const [, { appPath, infoPlistPath }] = await Promise.all([
144144
launchSimulator(device),
145145
buildApp({
146146
args,
@@ -150,14 +150,11 @@ ${devices
150150
projectRoot,
151151
reactNativePath,
152152
binaryPath,
153+
artifactName,
154+
deviceOrSimulator,
155+
fingerprintOptions,
153156
}),
154157
]);
155-
// After installing pods the fingerprint likely changes.
156-
// We update the artifact name to reflect the new fingerprint and store proper entry in the local cache.
157-
if (didInstallPods) {
158-
artifactName = await getArtifactName({ silent: true });
159-
}
160-
saveLocalBuildCache(artifactName, appPath);
161158
await runOnSimulator(device, appPath, infoPlistPath);
162159
} else if (device.type === 'device') {
163160
const { appPath, bundleIdentifier } = await buildApp({
@@ -168,6 +165,9 @@ ${devices
168165
projectRoot,
169166
reactNativePath,
170167
binaryPath,
168+
artifactName,
169+
deviceOrSimulator,
170+
fingerprintOptions,
171171
});
172172
await runOnDevice(
173173
device,
@@ -207,7 +207,7 @@ ${devices
207207
}
208208
}
209209
for (const bootedDevice of bootedDevices) {
210-
const [, { appPath, infoPlistPath, bundleIdentifier, didInstallPods }] =
210+
const [, { appPath, infoPlistPath, bundleIdentifier }] =
211211
await Promise.all([
212212
launchSimulator(bootedDevice),
213213
buildApp({
@@ -218,14 +218,11 @@ ${devices
218218
projectRoot,
219219
reactNativePath,
220220
binaryPath,
221+
artifactName,
222+
deviceOrSimulator,
223+
fingerprintOptions,
221224
}),
222225
]);
223-
// After installing pods the fingerprint likely changes.
224-
// We update the artifact name to reflect the new fingerprint and store proper entry in the local cache.
225-
if (didInstallPods) {
226-
artifactName = await getArtifactName({ silent: true });
227-
}
228-
saveLocalBuildCache(artifactName, appPath);
229226
if (bootedDevice.type === 'simulator') {
230227
await runOnSimulator(bootedDevice, appPath, infoPlistPath);
231228
} else {

packages/platform-apple-helpers/src/lib/utils/buildApp.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import path from 'node:path';
22
import type { IOSProjectConfig } from '@react-native-community/cli-types';
3-
import { getInfoPlist, RockError } from '@rock-js/tools';
3+
import {
4+
type FingerprintSources,
5+
formatArtifactName,
6+
getInfoPlist,
7+
RockError,
8+
saveLocalBuildCache,
9+
} from '@rock-js/tools';
410
import type { BuildFlags } from '../commands/build/buildOptions.js';
511
import { buildProject } from '../commands/build/buildProject.js';
612
import { getBuildSettings } from '../commands/run/getBuildSettings.js';
@@ -24,6 +30,9 @@ export async function buildApp({
2430
reactNativePath,
2531
binaryPath,
2632
brownfield,
33+
artifactName,
34+
fingerprintOptions,
35+
deviceOrSimulator,
2736
}: {
2837
args: RunFlags | BuildFlags;
2938
projectConfig: ProjectConfig;
@@ -35,6 +44,9 @@ export async function buildApp({
3544
reactNativePath: string;
3645
binaryPath?: string;
3746
brownfield?: boolean;
47+
artifactName: string;
48+
fingerprintOptions: FingerprintSources;
49+
deviceOrSimulator: string;
3850
}) {
3951
if (binaryPath) {
4052
// @todo Info.plist is hardcoded when reading from binaryPath
@@ -49,15 +61,13 @@ export async function buildApp({
4961
scheme: args.scheme,
5062
xcodeProject: projectConfig.xcodeProject,
5163
sourceDir: projectConfig.sourceDir,
52-
didInstallPods: false,
5364
};
5465
}
55-
56-
let didInstallPods = false;
66+
let artifactNameToSave = artifactName;
5767
let { xcodeProject, sourceDir } = projectConfig;
5868

5969
if (args.installPods) {
60-
await installPodsIfNeeded(
70+
const didInstallPods = await installPodsIfNeeded(
6171
projectRoot,
6272
platformName,
6373
sourceDir,
@@ -77,7 +87,18 @@ export async function buildApp({
7787
xcodeProject = newProjectConfig.xcodeProject;
7888
sourceDir = newProjectConfig.sourceDir;
7989
}
80-
didInstallPods = true;
90+
91+
if (didInstallPods) {
92+
// After installing pods the fingerprint likely changes.
93+
// We update the artifact name to reflect the new fingerprint and store proper entry in the local cache.
94+
artifactNameToSave = await formatArtifactName({
95+
platform: 'ios',
96+
traits: [deviceOrSimulator, args.configuration ?? 'Debug'],
97+
root: projectRoot,
98+
fingerprintOptions,
99+
type: 'update',
100+
});
101+
}
81102
}
82103

83104
const info = await getInfo(xcodeProject, sourceDir);
@@ -119,14 +140,15 @@ export async function buildApp({
119140
buildFolder: args.buildFolder,
120141
});
121142

143+
saveLocalBuildCache(artifactNameToSave, buildSettings.appPath);
144+
122145
return {
123146
appPath: buildSettings.appPath,
124147
infoPlistPath: buildSettings.infoPlistPath,
125148
scheme: scheme,
126149
xcodeProject,
127150
sourceDir,
128151
bundleIdentifier: buildSettings.bundleIdentifier,
129-
didInstallPods,
130152
};
131153
}
132154

packages/platform-apple-helpers/src/lib/utils/pods.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ export async function installPodsIfNeeded(
5656
cacheKey,
5757
calculateCurrentHash({ podfilePath, podsPath, nativeDependencies }),
5858
);
59+
return true;
5960
}
61+
return false;
6062
}
6163

6264
const calculateCurrentHash = ({

packages/tools/src/lib/build-cache/common.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { color } from '../color.js';
44
import type { FingerprintSources } from '../fingerprint/index.js';
55
import { nativeFingerprint } from '../fingerprint/index.js';
66
import { isInteractive } from '../isInteractive.js';
7-
import logger from '../logger.js';
87
import { getCacheRootPath } from '../project.js';
98
import { spinner } from '../prompts.js';
109

@@ -105,14 +104,14 @@ export async function formatArtifactName({
105104
root,
106105
fingerprintOptions,
107106
raw,
108-
silent,
107+
type,
109108
}: {
110109
platform?: 'ios' | 'android';
111110
traits?: string[];
112111
root: string;
113112
fingerprintOptions: FingerprintSources;
114113
raw?: boolean;
115-
silent?: boolean;
114+
type?: 'create' | 'update';
116115
}): Promise<string> {
117116
if (!platform || !traits) {
118117
return '';
@@ -126,18 +125,18 @@ export async function formatArtifactName({
126125
return `rock-${platform}-${traits.join('-')}-${hash}`;
127126
}
128127

129-
const loader = spinner({ silent });
130-
loader.start('Calculating project fingerprint');
128+
const startMessage = type === 'update' ? 'Updating' : 'Calculating';
129+
const stopMessage = type === 'update' ? 'Updated' : 'Calculated';
130+
131+
const loader = spinner();
132+
loader.start(`${startMessage} project fingerprint`);
131133
const { hash } = await nativeFingerprint(root, {
132134
...fingerprintOptions,
133135
platform,
134136
});
135137
loader.stop(
136-
`Calculated project fingerprint: ${color.bold(color.magenta(hash))}`,
138+
`${stopMessage} project fingerprint: ${color.bold(color.magenta(hash))}`,
137139
);
138-
if (silent) {
139-
logger.debug(`Calculated project fingerprint: ${hash}`);
140-
}
141140
return `rock-${platform}-${traits.join('-')}-${hash}`;
142141
}
143142

0 commit comments

Comments
 (0)