Skip to content

Commit e5888de

Browse files
hramosgrabbou
authored andcommittedFeb 1, 2021
Add use_react_native_codegen!
Summary: Consolidate CocoaPods codegen scripts under a single `use_react_native_codegen!` method in `react_native_pods.rb`. This is the first step towards making the codegen scripts library-agnostic. There are still a handful of hardcoded assumptions in place (e.g. the output directory structure, the use of a separate directory for components), but with some work one would be able to add codegen support to arbitrary CocoaPods podspecs. The codegen script no longer takes a CODEGEN_PATH argument, and will instead attempt to use the local react-native-codegen package if available, and fallback to using the node_modules/react-native-codegen package if not. ## Usage The `use_react_native_codegen!` method has two arguments: - `spec`, a pod [Specification](https://www.rubydoc.info/github/CocoaPods/Core/Pod/Specification) object. - `options`, an optional object. Supported keys: - `:srcs_dir`, the path to your JavaScript sources. Your native module or component specs should be located somewhere in this directory. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D25728053 fbshipit-source-id: feec587b656d5b220598ce6196ea6bb34a9580a9
1 parent 0636c45 commit e5888de

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-44
lines changed
 

‎Libraries/FBReactNativeSpec/FBReactNativeSpec.podspec

+2-34
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,19 @@
44
# LICENSE file in the root directory of this source tree.
55

66
require "json"
7+
require_relative "../../scripts/react_native_pods.rb"
78

89
package = JSON.parse(File.read(File.join(__dir__, "..", "..", "package.json")))
910
version = package['version']
1011

1112
source = { :git => 'https://github.com/facebook/react-native.git' }
12-
codegen_path_prefix = ".."
1313
if version == '1000.0.0'
1414
# This is an unpublished version, use the latest commit hash of the react-native repo, which we’re presumably in.
1515
source[:commit] = `git rev-parse HEAD`.strip
16-
codegen_path_prefix = "packages"
1716
else
1817
source[:tag] = "v#{version}"
1918
end
2019

21-
react_native_path = File.join(__dir__, "..", "..")
22-
srcs_dir = File.join(__dir__, "..")
23-
codegen_script_path = File.join(react_native_path, "scripts", "generate-specs.sh")
24-
codegen_path = File.join(react_native_path, codegen_path_prefix, "react-native-codegen")
25-
codegen_command = "CODEGEN_PATH=#{codegen_path} sh '#{codegen_script_path}' | tee \"${SCRIPT_OUTPUT_FILE_0}\""
26-
modules_output_dir = File.join(__dir__, "FBReactNativeSpec")
27-
components_output_dir = File.join(react_native_path, "ReactCommon", "react", "renderer", "components", "rncore")
28-
generated_filenames = [ "FBReactNativeSpec.h", "FBReactNativeSpec-generated.mm" ]
29-
generated_files = generated_filenames.map { |filename| File.join(modules_output_dir, filename) }
30-
31-
if ENV['USE_FABRIC'] == '1'
32-
components_generated_filenames = [
33-
"ComponentDescriptors.h",
34-
"EventEmitters.cpp",
35-
"EventEmitters.h",
36-
"Props.cpp",
37-
"Props.h",
38-
"RCTComponentViewHelpers.h",
39-
"ShadowNodes.cpp",
40-
"ShadowNodes.h"
41-
]
42-
generated_files = generated_files.concat(components_generated_filenames.map { |filename| File.join(components_output_dir, filename) })
43-
end
44-
4520
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
4621
folly_version = '2020.01.13.00'
4722

@@ -72,12 +47,5 @@ Pod::Spec.new do |s|
7247
s.dependency "React-jsi", version
7348
s.dependency "ReactCommon/turbomodule/core", version
7449

75-
s.prepare_command = "mkdir -p #{modules_output_dir} #{components_output_dir} && touch #{generated_files.reduce() { |str, file| str + " " + file }}"
76-
s.script_phase = {
77-
:name => 'Generate Specs',
78-
:input_files => [srcs_dir],
79-
:output_files => ["$(DERIVED_FILE_DIR)/codegen.log"],
80-
:script => codegen_command,
81-
:execution_position => :before_compile
82-
}
50+
use_react_native_codegen! (s)
8351
end

‎packages/rn-tester/Podfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ SPEC CHECKSUMS:
798798
CocoaLibEvent: 2fab71b8bd46dd33ddb959f7928ec5909f838e3f
799799
DoubleConversion: cde416483dac037923206447da6e1454df403714
800800
FBLazyVector: fe973c09b2299b5e8154186ecf1f6554b4f70987
801-
FBReactNativeSpec: 259a715466e53b411664fdfbe166dbde93ece5b6
801+
FBReactNativeSpec: cc83b1f9c1a764577c37a7009d002b8afb36f192
802802
Flipper: be611d4b742d8c87fbae2ca5f44603a02539e365
803803
Flipper-DoubleConversion: 38631e41ef4f9b12861c67d17cb5518d06badc41
804804
Flipper-Folly: e4493b013c02d9347d5e0cb4d128680239f6c78a

‎scripts/generate-specs.sh

+13-8
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@
77
# This script collects the JavaScript spec definitions for core
88
# native modules and components, then uses react-native-codegen
99
# to generate native code.
10-
# The script will use the local react-native-codegen package by
11-
# default. Optionally, set the CODEGEN_PATH to point to the
12-
# desired codegen library (e.g. when using react-native-codegen
13-
# from npm).
1410
#
1511
# Usage:
1612
# ./scripts/generate-specs.sh
1713
#
18-
# Examples:
19-
# CODEGEN_PATH=.. ./scripts/generate-specs.sh
2014

2115
# shellcheck disable=SC2038
2216

@@ -25,7 +19,6 @@ set -e
2519
THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)
2620
TEMP_DIR=$(mktemp -d /tmp/react-native-codegen-XXXXXXXX)
2721
RN_DIR=$(cd "$THIS_DIR/.." && pwd)
28-
CODEGEN_PATH="${CODEGEN_PATH:-$(cd "$RN_DIR/packages/react-native-codegen" && pwd)}"
2922
YARN_BINARY="${YARN_BINARY:-$(command -v yarn)}"
3023
USE_FABRIC="${USE_FABRIC:-0}"
3124

@@ -48,6 +41,18 @@ main() {
4841

4942
SCHEMA_FILE="$TEMP_DIR/schema.json"
5043

44+
CODEGEN_REPO_PATH="$RN_DIR/packages/react-native-codegen"
45+
CODEGEN_NPM_PATH="$RN_DIR/../react-native-codegen"
46+
47+
if [ -d "$CODEGEN_REPO_PATH" ]; then
48+
CODEGEN_PATH=$(cd "$CODEGEN_REPO_PATH" && pwd)
49+
elif [ -d "$CODEGEN_NPM_PATH" ]; then
50+
CODEGEN_PATH=$(cd "$CODEGEN_NPM_PATH" && pwd)
51+
else
52+
echo "Error: Could not determine react-native-codegen location. Try running 'yarn install' or 'npm install' in your project root." 1>&2
53+
exit 1
54+
fi
55+
5156
if [ ! -d "$CODEGEN_PATH/lib" ]; then
5257
describe "Building react-native-codegen package"
5358
pushd "$CODEGEN_PATH" >/dev/null || exit
@@ -61,7 +66,7 @@ main() {
6166

6267
describe "Generating native code from schema (iOS)"
6368
pushd "$RN_DIR" >/dev/null || exit
64-
USE_FABRIC="$USE_FABRIC" "$YARN_BINARY" --silent node scripts/generate-specs-cli.js ios "$SCHEMA_FILE" "$OUTPUT_DIR"
69+
"$YARN_BINARY" --silent node scripts/generate-specs-cli.js ios "$SCHEMA_FILE" "$OUTPUT_DIR"
6570
popd >/dev/null || exit
6671

6772
mkdir -p "$COMPONENTS_DIR" "$MODULES_DIR"

‎scripts/react_native_pods.rb

+49-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# LICENSE file in the root directory of this source tree.
55

66
def use_react_native! (options={})
7-
# The prefix to the react-native
7+
# The prefix to react-native
88
prefix = options[:path] ||= "../node_modules/react-native"
99

1010
# Include Fabric dependencies
@@ -144,3 +144,51 @@ def react_native_post_install(installer)
144144

145145
exclude_architectures(installer)
146146
end
147+
148+
def use_react_native_codegen!(spec, options={})
149+
# The path to react-native (e.g. react_native_path)
150+
prefix = options[:path] ||= File.join(__dir__, "..")
151+
152+
# The path to JavaScript files
153+
srcs_dir = options[:srcs_dir] ||= File.join(prefix, "Libraries")
154+
155+
# Library name (e.g. FBReactNativeSpec)
156+
library_name = spec.name
157+
modules_output_dir = File.join(prefix, "Libraries/#{library_name}/#{library_name}")
158+
159+
# Run the codegen as part of the Xcode build pipeline.
160+
spec.script_phase = {
161+
:name => 'Generate Specs',
162+
:input_files => [srcs_dir],
163+
:output_files => ["$(DERIVED_FILE_DIR)/codegen.log"],
164+
:script => "sh '#{File.join(__dir__, "generate-specs.sh")}' | tee \"${SCRIPT_OUTPUT_FILE_0}\"",
165+
:execution_position => :before_compile
166+
}
167+
168+
# Since the generated files are not guaranteed to exist when CocoaPods is run, we need to create
169+
# empty files to ensure the references are included in the resulting Pods Xcode project.
170+
mkdir_command = "mkdir -p #{modules_output_dir}"
171+
generated_filenames = [ "#{library_name}.h", "#{library_name}-generated.mm" ]
172+
generated_files = generated_filenames.map { |filename| File.join(modules_output_dir, filename) }
173+
174+
if ENV['USE_FABRIC'] == '1'
175+
# We use a different library name for components, as well as an additional set of files.
176+
# Eventually, we want these to be part of the same library as #{library_name} above.
177+
components_library_name = "rncore"
178+
components_output_dir = File.join(prefix, "ReactCommon/react/renderer/components/#{components_library_name}")
179+
mkdir_command += " #{components_output_dir}"
180+
components_generated_filenames = [
181+
"ComponentDescriptors.h",
182+
"EventEmitters.cpp",
183+
"EventEmitters.h",
184+
"Props.cpp",
185+
"Props.h",
186+
"RCTComponentViewHelpers.h",
187+
"ShadowNodes.cpp",
188+
"ShadowNodes.h"
189+
]
190+
generated_files = generated_files.concat(components_generated_filenames.map { |filename| File.join(components_output_dir, filename) })
191+
end
192+
193+
spec.prepare_command = "#{mkdir_command} && touch #{generated_files.reduce() { |str, file| str + " " + file }}"
194+
end

0 commit comments

Comments
 (0)
Please sign in to comment.