Skip to content

Commit a7cd3cc

Browse files
cipolleschifacebook-github-bot
authored andcommitted
Make codegen generate a Package.swift file for Codegen'd target (facebook#53619)
Summary: Pull Request resolved: facebook#53619 With this change, we are making Codegen generate a Package.swift file so that we can integrate the `ReactCodegen` and the `ReactAppDependencyProvider` in apps only using SwiftPM ## Changelog [iOS][Added] - Make codegen generate PAckage.swift file for the codegen targets Reviewed By: cortinico Differential Revision: D81769543 fbshipit-source-id: 1f1a1b9f41126e142931d5eda6e75109a69f828c
1 parent 87f8e85 commit a7cd3cc

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
'use strict';
12+
const {TEMPLATES_FOLDER_PATH} = require('./constants');
13+
const {codegenLog} = require('./utils');
14+
const fs = require('fs');
15+
const path = require('path');
16+
17+
const PACKAGE_SWIFT_TEMPLATE_PATH = path.join(
18+
TEMPLATES_FOLDER_PATH,
19+
'Package.swift.template',
20+
);
21+
22+
function generatePackageSwift(
23+
projectRoot /*: string */,
24+
outputDir /*: string */,
25+
reactNativePath /*: string */,
26+
) {
27+
const fullOutputPath = path.join(projectRoot, outputDir);
28+
fs.mkdirSync(outputDir, {recursive: true});
29+
// Generate PAckage.swift File
30+
codegenLog('Generating Package.swift');
31+
const templateH = fs
32+
.readFileSync(PACKAGE_SWIFT_TEMPLATE_PATH, 'utf8')
33+
.replace(
34+
/{reactNativePath}/,
35+
path.relative(fullOutputPath, reactNativePath),
36+
);
37+
const finalPathH = path.join(outputDir, 'Package.swift');
38+
fs.writeFileSync(finalPathH, templateH);
39+
codegenLog(`Generated artifact: ${finalPathH}`);
40+
}
41+
42+
module.exports = {
43+
generatePackageSwift,
44+
};

packages/react-native/scripts/codegen/generate-artifacts-executor/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {
2222
} = require('./generateAppDependencyProvider');
2323
const {generateCustomURLHandlers} = require('./generateCustomURLHandlers');
2424
const {generateNativeCode} = require('./generateNativeCode');
25+
const {generatePackageSwift} = require('./generatePackageSwift');
2526
const {generateRCTModuleProviders} = require('./generateRCTModuleProviders');
2627
const {
2728
generateRCTThirdPartyComponents,
@@ -37,6 +38,7 @@ const {
3738
codegenLog,
3839
findCodegenEnabledLibraries,
3940
findDisabledLibrariesByPlatform,
41+
findReactNativeRootPath,
4042
pkgJsonIncludesGeneratedCode,
4143
readPkgJsonInDirectory,
4244
readReactNativeConfig,
@@ -160,6 +162,11 @@ function execute(
160162
reactCodegenOutputPath,
161163
baseOutputPath,
162164
);
165+
generatePackageSwift(
166+
projectRoot,
167+
outputPath,
168+
findReactNativeRootPath(projectRoot),
169+
);
163170
}
164171

165172
cleanupEmptyFilesAndFolders(outputPath);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// swift-tools-version: 6.1
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "React-GeneratedCode",
8+
platforms: [.iOS(.v15), .macCatalyst(SupportedPlatform.MacCatalystVersion.v13)],
9+
products: [
10+
// Products define the executables and libraries a package produces, making them visible to other packages.
11+
.library(
12+
name: "ReactCodegen",
13+
targets: ["ReactCodegen"]),
14+
.library(
15+
name: "ReactAppDependencyProvider",
16+
targets: ["ReactAppDependencyProvider"]),
17+
],
18+
dependencies: [
19+
.package(name: "React", path: "{reactNativePath}")
20+
],
21+
targets: [
22+
// Targets are the basic building blocks of a package, defining a module or a test suite.
23+
// Targets can depend on other targets in this package and products from dependencies.
24+
.target(
25+
name: "ReactCodegen",
26+
dependencies: ["React"],
27+
path: "ReactCodegen",
28+
exclude: ["ReactCodegen.podspec"],
29+
publicHeadersPath: ".",
30+
cSettings: [
31+
.headerSearchPath("headers")
32+
],
33+
cxxSettings: [
34+
.headerSearchPath("headers"),
35+
.unsafeFlags(["-std=c++20"]),
36+
],
37+
linkerSettings: [
38+
.linkedFramework("Foundation")
39+
]
40+
),
41+
.target(
42+
name: "ReactAppDependencyProvider",
43+
dependencies: ["ReactCodegen"],
44+
path: "ReactAppDependencyProvider",
45+
exclude: ["ReactAppDependencyProvider.podspec"],
46+
publicHeadersPath: ".",
47+
cSettings: [
48+
.headerSearchPath("headers"),
49+
],
50+
cxxSettings: [
51+
.headerSearchPath("headers"),
52+
.unsafeFlags(["-std=c++20"]),
53+
],
54+
linkerSettings: [
55+
.linkedFramework("Foundation")
56+
]
57+
)
58+
]
59+
)

0 commit comments

Comments
 (0)