Skip to content

Commit d03c0f9

Browse files
hramosfacebook-github-bot
authored andcommitted
Generate FBReactNativeSpec ObjC++ source files using CocoaPods
Summary: Move FBReactNativeSpec codegen invocation to shared `react_native_pods.rb` script and trigger codegen to run as part of `pod install` in both RNTester as well as React Native for iOS projects. These files need to be generated before CocoaPods generates the Pods project, so the codegen is invoked as part of a `pre_install` hook during `pod install`. The codegen hook can now take optional paths to allow it to run within different contexts (as part of a `react-native` repo checkout with access to the codegen source, or as part of a React Native iOS project that depends on the `react-native-codegen` npm package). ## Motivation The FBReactNativeSpec ObjC++ source files (FBReactNativeSpec.h and FBReactNativeSpec-generated.mm) can be generated on demand from the native module specs in `Libraries/` using `react-native-codegen`. They can therefore be removed from the repository, but before we do so, we must ensure they get generated when a React Native iOS workspace is created or updated. Invoking the codegen as part of the `pod install` step that creates a React Native iOS Xcode workspace ensures the specs are available in the following scenarios: * Whenever a new React Native iOS project is created. The `react-native init` command invokes `pod install`. * Whenever a React Native iOS project is upgraded. The `react-native upgrade` command invokes `pod install` as well. * For contributors to the open source project running the RNTester application, the codegen will be invoked when the RNTester workspace is generated by CocoaPods using `pod install` in `packages/rn-tester`. In any other case, the codegen can still be invoked directly via `scripts/generate-native-modules-specs.sh`. > **Note:** > The codegen will only process native modules in React Native's own Libraries directory. Changelog: [iOS][Changed] - Generate FBReactNativeSpec ObjC++ source files using CocoaPods. Reviewed By: fkgozali Differential Revision: D24348111 fbshipit-source-id: d62ae5c6f8ce6358bf96a2801c3cdb3d94dd868d
1 parent 403d6f4 commit d03c0f9

File tree

7 files changed

+37
-8
lines changed

7 files changed

+37
-8
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"README.md",
3838
"rn-get-polyfills.js",
3939
"scripts/compose-source-maps.js",
40+
"scripts/generate-native-modules-specs-cli.js",
4041
"scripts/ios-configure-glog.sh",
4142
"scripts/launchPackager.bat",
4243
"scripts/launchPackager.command",

packages/rn-tester/Podfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ def frameworks_pre_install(installer)
5656
end
5757
end
5858

59-
def codegen_pre_install(installer)
60-
system("../../scripts/generate-native-modules-specs.sh")
61-
end
62-
6359
pre_install do |installer|
6460
frameworks_pre_install(installer) if ENV['USE_FRAMEWORKS'] == '1'
65-
codegen_pre_install(installer) if ENV['USE_CODEGEN'] == '1'
61+
if ENV['USE_CODEGEN'] != '0'
62+
prefix_path = "../.."
63+
codegen_path = "../../packages/react-native-codegen"
64+
codegen_pre_install(installer, {path:prefix_path, codegen_path:codegen_path})
65+
end
6666
end
6767

6868
post_install do |installer|

packages/rn-tester/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,6 @@ SPEC CHECKSUMS:
528528
Yoga: 69ef0b2bba5387523f793957a9f80dbd61e89631
529529
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
530530

531-
PODFILE CHECKSUM: c38c19657f5aaa2d604f5f4c607f030b60452997
531+
PODFILE CHECKSUM: e8fcdfe0cd22ad16c23d87ada51a7afb492b2ff1
532532

533533
COCOAPODS: 1.9.3

scripts/generate-native-modules-specs-cli.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@
99

1010
'use strict';
1111

12-
const RNCodegen = require('../packages/react-native-codegen/lib/generators/RNCodegen.js');
12+
let RNCodegen;
13+
try {
14+
RNCodegen = require('react-native-codegen/lib/generators/RNCodegen.js');
15+
} catch (e) {
16+
RNCodegen = require('../packages/react-native-codegen/lib/generators/RNCodegen.js');
17+
if (!RNCodegen) {
18+
throw 'RNCodegen not found.';
19+
}
20+
}
21+
1322
const fs = require('fs');
1423
const mkdirp = require('mkdirp');
1524
const os = require('os');

scripts/react_native_pods.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,17 @@ def flipper_post_install(installer)
109109
end
110110
end
111111
end
112+
113+
# Pre Install processing for Native Modules
114+
def codegen_pre_install(installer, options={})
115+
prefix = options[:path] ||= "../node_modules/react-native"
116+
codegen_path = options[:codegen_path] ||= "../node_modules/react-native-codegen"
117+
118+
Dir.mktmpdir do |dir|
119+
native_module_spec_name = "FBReactNativeSpec"
120+
schema_file = dir + "/schema-#{native_module_spec_name}.json"
121+
srcs_dir = "#{prefix}/Libraries"
122+
schema_generated = system("node #{codegen_path}/lib/cli/combine/combine-js-to-schema-cli.js #{schema_file} #{srcs_dir}")
123+
specs_generated = system("node #{prefix}/scripts/generate-native-modules-specs-cli.js ios #{schema_file} #{srcs_dir}/#{native_module_spec_name}/#{native_module_spec_name}")
124+
end
125+
end

template/ios/Podfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ target 'HelloWorld' do
1313
# Pods for testing
1414
end
1515

16+
pre_install do |installer|
17+
codegen_pre_install(installer)
18+
end
19+
1620
# Enables Flipper.
1721
#
1822
# Note that if you have use_frameworks! enabled, Flipper will not work and

template/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"eslint": "^6.5.1",
2222
"jest": "^25.1.0",
2323
"metro-react-native-babel-preset": "^0.63.0",
24-
"react-test-renderer": "16.13.1"
24+
"react-test-renderer": "16.13.1",
25+
"react-native-codegen": "0.0.4"
2526
},
2627
"jest": {
2728
"preset": "react-native"

0 commit comments

Comments
 (0)