Skip to content

Commit 462ae34

Browse files
feat(0.79): generate codegen before pod install (#2600)
* feat(0.79): run codgen before `pod install` * chore: move logic to `runCodegen` fn * fix: mock `runCodegen` fn * fix: mock default module instead * fix: pass reactNativePath from config * fix: pass `reactNativePath` also in `init` command
1 parent 5adc9e7 commit 462ae34

File tree

8 files changed

+70
-6
lines changed

8 files changed

+70
-6
lines changed

packages/cli-config-apple/src/__tests__/pods.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jest.mock('@react-native-community/cli-tools', () => ({
1616
},
1717
}));
1818
jest.mock('../tools/installPods', () => jest.fn());
19+
jest.mock('../tools/runCodegen', () => jest.fn());
1920
const dependencyHash = 'd41d8cd98f00b204e9800998ecf8427e';
2021

2122
const packageJson = {

packages/cli-config-apple/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export {
33
getProjectConfig,
44
findPodfilePaths,
55
} from './config';
6-
6+
export {default as runCodegen} from './tools/runCodegen';
77
export {default as installPods} from './tools/installPods';
88
export {default as resolvePods} from './tools/pods';
99
export {default as findXcodeProject} from './config/findXcodeProject';

packages/cli-config-apple/src/tools/installPods.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface RunPodInstallOptions {
2222
newArchEnabled?: boolean;
2323
}
2424

25-
async function runPodInstall(loader: Ora, options?: RunPodInstallOptions) {
25+
async function runPodInstall(loader: Ora, options: RunPodInstallOptions) {
2626
const shouldHandleRepoUpdate = options?.shouldHandleRepoUpdate || true;
2727
try {
2828
loader.start(
@@ -156,8 +156,9 @@ async function installPods(loader?: Ora, options?: PodInstallOptions) {
156156
loader.info();
157157
await installCocoaPods(loader);
158158
}
159-
160-
await runPodInstall(loader, {newArchEnabled: options?.newArchEnabled});
159+
await runPodInstall(loader, {
160+
newArchEnabled: options?.newArchEnabled,
161+
});
161162
} finally {
162163
process.chdir('..');
163164
}

packages/cli-config-apple/src/tools/pods.ts

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
IOSDependencyConfig,
1414
} from '@react-native-community/cli-types';
1515
import {ApplePlatform} from '../types';
16+
import runCodegen from './runCodegen';
1617

1718
interface ResolvePodsOptions {
1819
forceInstall?: boolean;
@@ -89,9 +90,17 @@ async function install(
8990
cachedDependenciesHash: string | undefined,
9091
currentDependenciesHash: string,
9192
iosFolderPath: string,
93+
platform: string,
94+
root: string,
95+
reactNativePath: string,
9296
) {
9397
const loader = getLoader('Installing CocoaPods...');
9498
try {
99+
await runCodegen({
100+
root,
101+
platform,
102+
reactNativePath,
103+
});
95104
await installPods(loader, {
96105
skipBundleInstall: !!cachedDependenciesHash,
97106
iosFolderPath,
@@ -114,6 +123,7 @@ export default async function resolvePods(
114123
sourceDir: string,
115124
nativeDependencies: NativeDependencies,
116125
platformName: ApplePlatform,
126+
reactNativePath: string,
117127
options?: ResolvePodsOptions,
118128
) {
119129
const packageJson = getPackageJson(root);
@@ -154,6 +164,9 @@ export default async function resolvePods(
154164
cachedDependenciesHash,
155165
currentDependenciesHash,
156166
platformFolderPath,
167+
platformName,
168+
root,
169+
reactNativePath,
157170
);
158171
} else if (
159172
arePodsInstalled &&
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import execa from 'execa';
4+
5+
interface CodegenOptions {
6+
root: string;
7+
platform: string;
8+
reactNativePath: string;
9+
}
10+
11+
async function runCodegen(options: CodegenOptions): Promise<void> {
12+
if (fs.existsSync('build')) {
13+
fs.rmSync('build', {recursive: true});
14+
}
15+
16+
const codegenScript = path.join(
17+
options.reactNativePath,
18+
'scripts',
19+
'generate-codegen-artifacts.js',
20+
);
21+
22+
await execa('node', [
23+
codegenScript,
24+
'-p',
25+
options.root,
26+
'-o',
27+
process.cwd(),
28+
'-t',
29+
options.platform,
30+
]);
31+
}
32+
33+
export default runCodegen;

packages/cli-platform-apple/src/commands/buildCommand/createBuild.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const createBuild =
3434
platformConfig.sourceDir,
3535
ctx.dependencies,
3636
platformName,
37+
ctx.reactNativePath,
3738
{
3839
forceInstall: args.forcePods,
3940
newArchEnabled: isAppRunningNewArchitecture,

packages/cli-platform-apple/src/commands/runCommand/createRun.ts

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const createRun =
8989
platformConfig.sourceDir,
9090
ctx.dependencies,
9191
platformName,
92+
ctx.reactNativePath,
9293
{
9394
forceInstall: args.forcePods,
9495
newArchEnabled: isAppRunningNewArchitecture,

packages/cli/src/commands/init/init.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import DirectoryAlreadyExistsError from './errors/DirectoryAlreadyExistsError';
4141
import {createTemplateUri} from './version';
4242
import {TEMPLATE_COMMUNITY_REACT_NATIVE_VERSION} from './constants';
4343
import type {Options} from './types';
44+
import {runCodegen} from '@react-native-community/cli-config-apple';
4445

4546
const DEFAULT_VERSION = 'latest';
4647

@@ -280,11 +281,19 @@ async function createFromTemplate({
280281

281282
if (process.platform === 'darwin') {
282283
const installPodsValue = String(installCocoaPods);
284+
const reactNativePath = path.dirname(
285+
require.resolve('react-native', {paths: [projectDirectory]}),
286+
);
283287

284288
try {
285289
if (installPodsValue === 'true') {
286290
didInstallPods = true;
287-
await installPods(loader);
291+
await runCodegen({
292+
root: projectDirectory,
293+
platform: 'ios',
294+
reactNativePath,
295+
});
296+
await installPods(loader, {});
288297
loader.succeed();
289298
setEmptyHashForCachedDependencies(projectName);
290299
} else if (installPodsValue === 'undefined') {
@@ -298,7 +307,12 @@ async function createFromTemplate({
298307
didInstallPods = installCocoapods;
299308

300309
if (installCocoapods) {
301-
await installPods(loader, {newArchEnabled: true});
310+
await runCodegen({
311+
root: projectDirectory,
312+
platform: 'ios',
313+
reactNativePath,
314+
});
315+
await installPods(loader, {});
302316
loader.succeed();
303317
setEmptyHashForCachedDependencies(projectName);
304318
}

0 commit comments

Comments
 (0)