Skip to content

Commit 2ea9fd6

Browse files
cipolleschimeta-codesync[bot]
authored andcommitted
Add function to generate artifact for codegen (#53676)
Summary: Pull Request resolved: #53676 ## Context When configuring an app to build with SwiftPM from source, there is a sequence of operations we need to run in order to prepare the project correctly. ## Changed Add a function that generates codegen artifacts. Those are required by React Native and by the app to build properly. ## Changelog: [Internal] - Reviewed By: cortinico Differential Revision: D81778439 fbshipit-source-id: 7fe5cc0b612d2d9324d68fe04ec556e986ec14af
1 parent 7a86ee0 commit 2ea9fd6

2 files changed

Lines changed: 147 additions & 0 deletions

File tree

packages/react-native/scripts/swiftpm/__tests__/prepare-app-utils-test.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
configureAppForSwift,
1515
createHardlinks,
1616
findXcodeProjectDirectory,
17+
generateCodegenArtifacts,
1718
runIosPrebuild,
1819
runPodDeintegrate,
1920
setBuildFromSource,
@@ -31,6 +32,9 @@ jest.mock('../headers-utils');
3132
// Mock prepare-app-dependencies-headers module
3233
jest.mock('../prepare-app-dependencies-headers');
3334

35+
// Mock codegen executor module
36+
jest.mock('../../codegen/generate-artifacts-executor');
37+
3438
// Mock path module for absolute paths
3539
jest.mock('path', () => {
3640
const actualPath = jest.requireActual('path');
@@ -239,6 +243,127 @@ describe('createHardlinks', () => {
239243
});
240244
});
241245

246+
describe('generateCodegenArtifacts', () => {
247+
let mockCodegenExecutor;
248+
let mockConsoleLog;
249+
250+
beforeEach(() => {
251+
// Setup mocks
252+
mockCodegenExecutor = require('../../codegen/generate-artifacts-executor');
253+
mockConsoleLog = console.log;
254+
255+
// Clear and reset all mocks completely
256+
jest.clearAllMocks();
257+
jest.resetAllMocks();
258+
259+
// Set up fresh mock implementations
260+
mockCodegenExecutor.execute = jest.fn();
261+
});
262+
263+
it('should run codegen successfully', async () => {
264+
// Setup
265+
const reactNativePath = '/path/to/react-native';
266+
const appPath = '/path/to/app';
267+
const appIosPath = '/path/to/app/ios';
268+
269+
mockCodegenExecutor.execute.mockImplementation(() => {});
270+
271+
// Execute
272+
await generateCodegenArtifacts(reactNativePath, appPath, appIosPath);
273+
274+
// Assert
275+
expect(mockCodegenExecutor.execute).toHaveBeenCalledWith(
276+
appPath,
277+
'ios',
278+
appIosPath,
279+
'app',
280+
);
281+
expect(mockCodegenExecutor.execute).toHaveBeenCalledTimes(1);
282+
283+
// Verify console output
284+
expect(mockConsoleLog).toHaveBeenCalledWith(
285+
'Generating codegen artifacts...',
286+
);
287+
expect(mockConsoleLog).toHaveBeenCalledWith(
288+
'✓ Codegen artifacts generated',
289+
);
290+
});
291+
292+
it('should throw error when codegen execution fails', async () => {
293+
// Setup
294+
const reactNativePath = '/path/to/react-native';
295+
const appPath = '/path/to/app';
296+
const appIosPath = '/path/to/app/ios';
297+
const originalError = new Error('Codegen failed to generate artifacts');
298+
299+
mockCodegenExecutor.execute.mockImplementation(() => {
300+
throw originalError;
301+
});
302+
303+
// Execute & Assert
304+
await expect(
305+
generateCodegenArtifacts(reactNativePath, appPath, appIosPath),
306+
).rejects.toThrow(
307+
'Codegen generation failed: Codegen failed to generate artifacts',
308+
);
309+
310+
expect(mockCodegenExecutor.execute).toHaveBeenCalledWith(
311+
appPath,
312+
'ios',
313+
appIosPath,
314+
'app',
315+
);
316+
expect(mockCodegenExecutor.execute).toHaveBeenCalledTimes(1);
317+
318+
expect(mockConsoleLog).toHaveBeenCalledWith(
319+
'Generating codegen artifacts...',
320+
);
321+
expect(mockConsoleLog).not.toHaveBeenCalledWith(
322+
'✓ Codegen artifacts generated',
323+
);
324+
});
325+
326+
it('should handle different paths correctly', async () => {
327+
// Setup
328+
const reactNativePath = '/Users/developer/react-native';
329+
const appPath = '/Users/developer/MyApp';
330+
const appIosPath = '/Users/developer/MyApp/ios';
331+
332+
mockCodegenExecutor.execute.mockImplementation(() => {});
333+
334+
// Execute
335+
await generateCodegenArtifacts(reactNativePath, appPath, appIosPath);
336+
337+
// Assert
338+
expect(mockCodegenExecutor.execute).toHaveBeenCalledWith(
339+
appPath,
340+
'ios',
341+
appIosPath,
342+
'app',
343+
);
344+
});
345+
346+
it('should handle paths with spaces correctly', async () => {
347+
// Setup
348+
const reactNativePath = '/path/to/react native';
349+
const appPath = '/path/to/my app';
350+
const appIosPath = '/path/to/my app/ios folder';
351+
352+
mockCodegenExecutor.execute.mockImplementation(() => {});
353+
354+
// Execute
355+
await generateCodegenArtifacts(reactNativePath, appPath, appIosPath);
356+
357+
// Assert
358+
expect(mockCodegenExecutor.execute).toHaveBeenCalledWith(
359+
appPath,
360+
'ios',
361+
appIosPath,
362+
'app',
363+
);
364+
});
365+
});
366+
242367
// Mock console methods - disable React Native's strict console checking
243368
const originalConsole = global.console;
244369

packages/react-native/scripts/swiftpm/prepare-app-utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @format
99
*/
1010

11+
const codegenExecutor = require('../codegen/generate-artifacts-executor');
1112
const {symlinkThirdPartyDependenciesHeaders} = require('./headers-utils');
1213
const {
1314
symlinkReactNativeHeaders,
@@ -236,11 +237,32 @@ async function createHardlinks(
236237
}
237238
}
238239

240+
/**
241+
* Generate codegen artifacts using the executor
242+
*/
243+
async function generateCodegenArtifacts(
244+
reactNativePath /*: string */,
245+
appPath /*: string */,
246+
appIosPath /*: string */,
247+
) /*: Promise<void> */ {
248+
try {
249+
console.log('Generating codegen artifacts...');
250+
251+
// Use the codegen executor directly
252+
codegenExecutor.execute(appPath, 'ios', appIosPath, 'app');
253+
254+
console.log('✓ Codegen artifacts generated');
255+
} catch (error) {
256+
throw new Error(`Codegen generation failed: ${error.message}`);
257+
}
258+
}
259+
239260
module.exports = {
240261
findXcodeProjectDirectory,
241262
runPodDeintegrate,
242263
runIosPrebuild,
243264
configureAppForSwift,
244265
setBuildFromSource,
245266
createHardlinks,
267+
generateCodegenArtifacts,
246268
};

0 commit comments

Comments
 (0)