implement changes in codegen to put files in the right folders

This commit is contained in:
Riccardo Cipolleschi
2025-08-04 16:42:14 +01:00
parent dee403e807
commit ef25e5b4e5
5 changed files with 126 additions and 9 deletions
@@ -0,0 +1,43 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
'use strict';
const {TEMPLATES_FOLDER_PATH} = require('./constants');
const {
codegenLog,
} = require('./utils');
const fs = require('fs');
const path = require('path');
const PACKAGE_SWIFT_TEMPLATE_PATH = path.join(
TEMPLATES_FOLDER_PATH,
'Package.swift.template',
);
function generatePackageSwift(
projectRoot /*: string */,
outputDir /*: string */,
reactNativePath /*: string */,
) /*: string */ {
const fullOutputPath = path.join(projectRoot, outputDir);
fs.mkdirSync(outputDir, {recursive: true});
// Generate PAckage.swift File
codegenLog('Generating Package.swift');
const templateH = fs
.readFileSync(PACKAGE_SWIFT_TEMPLATE_PATH, 'utf8')
.replace(/{reactNativePath}/, path.relative(fullOutputPath, reactNativePath));
const finalPathH = path.join(outputDir, 'Package.swift');
fs.writeFileSync(finalPathH, templateH);
codegenLog(`Generated artifact: ${finalPathH}`);
}
module.exports = {
generatePackageSwift
};
@@ -22,6 +22,7 @@ const {
} = require('./generateAppDependencyProvider');
const {generateCustomURLHandlers} = require('./generateCustomURLHandlers');
const {generateNativeCode} = require('./generateNativeCode');
const {generatePackageSwift} = require('./generatePackageSwift');
const {generateRCTModuleProviders} = require('./generateRCTModuleProviders');
const {
generateRCTThirdPartyComponents,
@@ -37,6 +38,7 @@ const {
codegenLog,
findCodegenEnabledLibraries,
findDisabledLibrariesByPlatform,
findReactNativeRootPath,
pkgJsonIncludesGeneratedCode,
readPkgJsonInDirectory,
readReactNativeConfig,
@@ -121,10 +123,12 @@ function execute(
platform,
);
const reactCodegenOutputPath = platform === 'android' ? outputPath : path.join(outputPath, 'ReactCodegen');
if (runReactNativeCodegen) {
const schemaInfos = generateSchemaInfos(libraries);
generateNativeCode(
outputPath,
reactCodegenOutputPath,
schemaInfos.filter(schemaInfo =>
mustGenerateNativeCode(projectRoot, schemaInfo),
),
@@ -135,20 +139,21 @@ function execute(
if (source === 'app' && platform !== 'android') {
// These components are only required by apps, not by libraries and are Apple specific.
generateRCTThirdPartyComponents(libraries, outputPath);
generateRCTModuleProviders(projectRoot, pkgJson, libraries, outputPath);
generateCustomURLHandlers(libraries, outputPath);
generateRCTThirdPartyComponents(libraries, reactCodegenOutputPath);
generateRCTModuleProviders(projectRoot, pkgJson, libraries, reactCodegenOutputPath);
generateCustomURLHandlers(libraries, reactCodegenOutputPath);
generateUnstableModulesRequiringMainQueueSetupProvider(
libraries,
outputPath,
reactCodegenOutputPath,
);
generateAppDependencyProvider(outputPath);
generateAppDependencyProvider(path.join(outputPath, 'ReactAppDependencyProvider'));
generateReactCodegenPodspec(
projectRoot,
pkgJson,
outputPath,
reactCodegenOutputPath,
baseOutputPath,
);
generatePackageSwift(projectRoot, outputPath, findReactNativeRootPath(projectRoot));
}
cleanupEmptyFilesAndFolders(outputPath);
@@ -410,6 +410,14 @@ function findDisabledLibrariesByPlatform(
);
}
function findReactNativeRootPath(projectRoot /* : string */) /* : string */ {
const reactNativePackageJsonPath = require.resolve(path.join('react-native', 'package.json'), {
paths: [projectRoot],
});
return path.dirname(reactNativePackageJsonPath);
}
module.exports = {
buildCodegenIfNeeded,
pkgJsonIncludesGeneratedCode,
@@ -423,4 +431,5 @@ module.exports = {
parseiOSAnnotations,
readReactNativeConfig,
findDisabledLibrariesByPlatform,
findReactNativeRootPath,
};
@@ -0,0 +1,59 @@
// swift-tools-version: 6.1
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "React-GeneratedCode",
platforms: [.iOS(.v15), .macCatalyst(SupportedPlatform.MacCatalystVersion.v13)],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "ReactCodegen",
targets: ["ReactCodegen"]),
.library(
name: "ReactAppDependencyProvider",
targets: ["ReactAppDependencyProvider"]),
],
dependencies: [
.package(name: "React", path: "{reactNativePath}")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "ReactCodegen",
dependencies: ["React"],
path: "ReactCodegen",
exclude: ["ReactCodegen.podspec"],
publicHeadersPath: ".",
cSettings: [
.headerSearchPath("headers")
],
cxxSettings: [
.headerSearchPath("headers"),
.unsafeFlags(["-std=c++20"]),
],
linkerSettings: [
.linkedFramework("Foundation")
]
),
.target(
name: "ReactAppDependencyProvider",
dependencies: ["ReactCodegen"],
path: "ReactAppDependencyProvider",
exclude: ["ReactAppDependencyProvider.podspec"],
publicHeadersPath: ".",
cSettings: [
.headerSearchPath("headers"),
],
cxxSettings: [
.headerSearchPath("headers"),
.unsafeFlags(["-std=c++20"]),
],
linkerSettings: [
.linkedFramework("Foundation")
]
)
]
)
@@ -24,7 +24,8 @@ require_relative './cocoapods/rncore.rb'
# Importing to expose use_native_modules!
require_relative './cocoapods/autolinking.rb'
$CODEGEN_OUTPUT_DIR = 'build/generated/ios'
$CODEGEN_OUTPUT_DIR = 'build/generated/ios/ReactCodegen'
$APP_DEPENDENCY_PROVIDER_OUTPUT_DIR = 'build/generated/ios/ReactAppDependencyProvider'
$CODEGEN_COMPONENT_DIR = 'react/renderer/components'
$CODEGEN_MODULE_DIR = '.'
@@ -196,7 +197,7 @@ def use_react_native! (
end
pod 'ReactCodegen', :path => $CODEGEN_OUTPUT_DIR, :modular_headers => true
pod 'ReactAppDependencyProvider', :path => $CODEGEN_OUTPUT_DIR, :modular_headers => true
pod 'ReactAppDependencyProvider', :path => $APP_DEPENDENCY_PROVIDER_OUTPUT_DIR, :modular_headers => true
# Not needed, but run_codegen expects this to be set.
folly_config = get_folly_config()
run_codegen!(