Skip to content

Commit 9827b4a

Browse files
sota000facebook-github-bot
authored andcommitted
Refactor script_phases script out of react_native_pods.rb (facebook#32764)
Summary: Pull Request resolved: facebook#32764 Changelog: [Internal] This diff refactors react_native_pods.rb so that it's a bit more readable/maintainable. With the intorduction of the codegen discovery script, we have two script phases that shares some code. I've factored it out of the main file and wrote a snapshot test so that it's easier to see the output script file. Reviewed By: cortinico Differential Revision: D33045541 fbshipit-source-id: 9c80b5d7e11862cc44275e36882487a7d63e8125
1 parent 3a01dc4 commit 9827b4a

File tree

4 files changed

+77
-212
lines changed

4 files changed

+77
-212
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
"scripts/node-binary.sh",
4949
"scripts/packager.sh",
5050
"scripts/packager-reporter.js",
51+
"scripts/react_native_pods_utils/script_phases.rb",
52+
"scripts/react_native_pods_utils/script_phases.sh",
5153
"scripts/react_native_pods.rb",
5254
"scripts/react-native-xcode.sh",
5355
"template.config.js",

packages/rn-tester/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ SPEC CHECKSUMS:
881881
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
882882
DoubleConversion: 831926d9b8bf8166fd87886c4abab286c2422662
883883
FBLazyVector: b81a2b70c72d8b0aefb652cea22c11e9ffd02949
884-
FBReactNativeSpec: 140e95aa3cc1f6084938f6b8123bb0c4398325d1
884+
FBReactNativeSpec: a9c75a44813c347e748342bc4ce0789e162d22e6
885885
Flipper: 30e8eeeed6abdc98edaf32af0cda2f198be4b733
886886
Flipper-Boost-iOSX: fd1e2b8cbef7e662a122412d7ac5f5bea715403c
887887
Flipper-DoubleConversion: 57ffbe81ef95306cc9e69c4aa3aeeeeb58a6a28c
@@ -923,10 +923,10 @@ SPEC CHECKSUMS:
923923
React-RCTTest: 12bbd7fc2e72bd9920dc7286c5b8ef96639582b6
924924
React-RCTText: e9146b2c0550a83d1335bfe2553760070a2d75c7
925925
React-RCTVibration: 50be9c390f2da76045ef0dfdefa18b9cf9f35cfa
926-
React-rncore: 90798d1c013f1c62d0066c5618ddf8681c7a7c22
926+
React-rncore: 419b6945917d7eb76b304b76acbb49f79fb7e7e0
927927
React-runtimeexecutor: 4b0c6eb341c7d3ceb5e2385cb0fdb9bf701024f3
928928
ReactCommon: 7a2714d1128f965392b6f99a8b390e3aa38c9569
929-
ScreenshotManager: ccf2b410b3c59385b26b8460a58cca8a8aca5e85
929+
ScreenshotManager: 025a85e18687aff80bca262c3a7065ff54e636ae
930930
Yoga: c0d06f5380d34e939f55420669a60fe08b79bd75
931931
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
932932

scripts/react_native_pods.rb

Lines changed: 19 additions & 209 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# LICENSE file in the root directory of this source tree.
55

66
require 'pathname'
7+
require_relative './react_native_pods_utils/script_phases.rb'
78

89
$CODEGEN_OUTPUT_DIR = 'build/generated/ios'
910
$CODEGEN_COMPONENT_DIR = 'react/renderer/components'
@@ -392,96 +393,12 @@ def get_react_codegen_script_phases(options={})
392393
'input_files' => input_files,
393394
'show_env_vars_in_log': true,
394395
'output_files': ["${DERIVED_FILE_DIR}/react-codegen.log"],
395-
'script': %{set -o pipefail
396-
set -e
397-
398-
pushd "${PODS_ROOT}/../" > /dev/null
399-
POD_INSTALLATION_ROOT=$(pwd)
400-
popd >/dev/null
401-
RN_DIR=$(cd "$POD_INSTALLATION_ROOT/#{react_native_path}" && pwd)
402-
403-
GENERATED_SRCS_DIR="$\{DERIVED_FILE_DIR\}/generated/source/codegen-discovery"
404-
TEMP_OUTPUT_DIR="$GENERATED_SRCS_DIR/out"
405-
406-
APP_PATH="$POD_INSTALLATION_ROOT/#{$relative_app_root}"
407-
CONFIG_FILE_DIR="#{relative_config_file_dir ? "$POD_INSTALLATION_ROOT/#{relative_config_file_dir}" : ''}"
408-
OUTPUT_DIR="$POD_INSTALLATION_ROOT"
409-
FABRIC_ENABLED="#{fabric_enabled}"
410-
411-
CODEGEN_REPO_PATH="$RN_DIR/packages/react-native-codegen"
412-
CODEGEN_NPM_PATH="$RN_DIR/../react-native-codegen"
413-
CODEGEN_CLI_PATH=""
414-
415-
# Determine path to react-native-codegen
416-
if [ -d "$CODEGEN_REPO_PATH" ]; then
417-
CODEGEN_CLI_PATH=$(cd "$CODEGEN_REPO_PATH" && pwd)
418-
elif [ -d "$CODEGEN_NPM_PATH" ]; then
419-
CODEGEN_CLI_PATH=$(cd "$CODEGEN_NPM_PATH" && pwd)
420-
else
421-
echo "error: Could not determine react-native-codegen location. Try running 'yarn install' or 'npm install' in your project root." >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
422-
exit 1
423-
fi
424-
425-
find_node () {
426-
source "$RN_DIR/scripts/find-node.sh"
427-
428-
NODE_BINARY="${NODE_BINARY:-$(command -v node || true)}"
429-
if [ -z "$NODE_BINARY" ]; then
430-
echo "error: Could not find node. Make sure it is in bash PATH or set the NODE_BINARY environment variable." >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
431-
exit 1
432-
fi
433-
}
434-
435-
setup_dirs () {
436-
set +e
437-
rm -rf "$GENERATED_SRCS_DIR"
438-
set -e
439-
440-
mkdir -p "$GENERATED_SRCS_DIR" "$TEMP_OUTPUT_DIR"
441-
442-
# Clear output files
443-
> "${SCRIPT_OUTPUT_FILE_0}"
444-
}
445-
446-
describe () {
447-
printf "\\n\\n>>>>> %s\\n\\n\\n" "$1" >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
448-
}
449-
450-
buildCodegenCLI () {
451-
if [ ! -d "$CODEGEN_CLI_PATH/lib" ]; then
452-
describe "Building react-native-codegen package"
453-
bash "$CODEGEN_CLI_PATH/scripts/oss/build.sh"
454-
fi
455-
}
456-
457-
generateArtifacts () {
458-
describe "Generating codegen artifacts"
459-
pushd "$RN_DIR" >/dev/null || exit 1
460-
CONFIG_FILE_DIR="$POD_INSTALLATION_ROOT/#{$relative_config_file_dir}"
461-
"$NODE_BINARY" "scripts/generate-artifacts.js" --path "$APP_PATH" --outputPath "$OUTPUT_DIR" --fabricEnabled "$FABRIC_ENABLED" --configFileDir "$CONFIG_FILE_DIR"
462-
popd >/dev/null || exit 1
463-
}
464-
465-
moveOutputs () {
466-
mkdir -p "$OUTPUT_DIR"
467-
468-
# Copy all output to output_dir
469-
cp -R "$TEMP_OUTPUT_DIR/" "$OUTPUT_DIR" || exit 1
470-
echo "Output has been written to $OUTPUT_DIR:" >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
471-
ls -1 "$OUTPUT_DIR" >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
472-
}
473-
474-
main () {
475-
setup_dirs
476-
find_node
477-
buildCodegenCLI
478-
generateArtifacts
479-
moveOutputs
480-
}
481-
482-
main "$@"
483-
echo 'Done.' >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
484-
},
396+
'script': get_script_phases_with_codegen_discovery(
397+
react_native_path: react_native_path,
398+
relative_app_root: relative_app_root,
399+
relative_config_file_dir: relative_config_file_dir,
400+
fabric_enabled: fabric_enabled
401+
),
485402
}
486403

487404
end
@@ -566,7 +483,7 @@ def use_react_native_codegen!(spec, options={})
566483
# TODO: Once the new codegen approach is ready for use, we should output a warning here to let folks know to migrate.
567484

568485
# The prefix to react-native
569-
prefix = options[:react_native_path] ||= "../.."
486+
react_native_path = options[:react_native_path] ||= "../.."
570487

571488
# Library name (e.g. FBReactNativeSpec)
572489
library_name = options[:library_name] ||= "#{spec.name.gsub('_','-').split('-').collect(&:capitalize).join}Spec"
@@ -638,124 +555,17 @@ def use_react_native_codegen!(spec, options={})
638555
:input_files => input_files, # This also needs to be relative to Xcode
639556
:output_files => ["${DERIVED_FILE_DIR}/codegen-#{library_name}.log"].concat(generated_files.map { |filename| " ${PODS_TARGET_SRCROOT}/#{filename}"} ),
640557
# The final generated files will be created when this script is invoked at Xcode build time.
641-
:script => %{set -o pipefail
642-
set -e
643-
644-
pushd "${PODS_ROOT}/../" > /dev/null
645-
POD_INSTALLATION_ROOT=$(pwd)
646-
popd >/dev/null
647-
RN_DIR=$(cd "$\{PODS_TARGET_SRCROOT\}/#{prefix}" && pwd)
648-
649-
GENERATED_SRCS_DIR="$\{DERIVED_FILE_DIR\}/generated/source/codegen"
650-
GENERATED_SCHEMA_FILE="$GENERATED_SRCS_DIR/schema.json"
651-
TEMP_OUTPUT_DIR="$GENERATED_SRCS_DIR/out"
652-
653-
LIBRARY_NAME="#{library_name}"
654-
OUTPUT_DIR="$POD_INSTALLATION_ROOT/#{$CODEGEN_OUTPUT_DIR}"
655-
656-
CODEGEN_REPO_PATH="$RN_DIR/packages/react-native-codegen"
657-
CODEGEN_NPM_PATH="$RN_DIR/../react-native-codegen"
658-
CODEGEN_CLI_PATH=""
659-
660-
LIBRARY_TYPE="#{library_type ? library_type : 'all'}"
661-
662-
# Determine path to react-native-codegen
663-
if [ -d "$CODEGEN_REPO_PATH" ]; then
664-
CODEGEN_CLI_PATH=$(cd "$CODEGEN_REPO_PATH" && pwd)
665-
elif [ -d "$CODEGEN_NPM_PATH" ]; then
666-
CODEGEN_CLI_PATH=$(cd "$CODEGEN_NPM_PATH" && pwd)
667-
else
668-
echo "error: Could not determine react-native-codegen location. Try running 'yarn install' or 'npm install' in your project root." >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
669-
exit 1
670-
fi
671-
672-
find_node () {
673-
source "$RN_DIR/scripts/find-node.sh"
674-
675-
NODE_BINARY="${NODE_BINARY:-$(command -v node || true)}"
676-
if [ -z "$NODE_BINARY" ]; then
677-
echo "error: Could not find node. Make sure it is in bash PATH or set the NODE_BINARY environment variable." >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
678-
exit 1
679-
fi
680-
}
681-
682-
setup_dirs () {
683-
set +e
684-
rm -rf "$GENERATED_SRCS_DIR"
685-
set -e
686-
687-
mkdir -p "$GENERATED_SRCS_DIR" "$TEMP_OUTPUT_DIR"
688-
689-
# Clear output files
690-
> "${SCRIPT_OUTPUT_FILE_0}"
691-
}
692-
693-
describe () {
694-
printf "\\n\\n>>>>> %s\\n\\n\\n" "$1" >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
695-
}
696-
697-
buildCodegenCLI () {
698-
if [ ! -d "$CODEGEN_CLI_PATH/lib" ]; then
699-
describe "Building react-native-codegen package"
700-
bash "$CODEGEN_CLI_PATH/scripts/oss/build.sh"
701-
fi
702-
}
703-
704-
generateCodegenSchemaFromJavaScript () {
705-
describe "Generating codegen schema from JavaScript"
706-
707-
SRCS_PATTERN="#{js_srcs_pattern}"
708-
SRCS_DIR="#{js_srcs_dir}"
709-
if [ $SRCS_PATTERN ]; then
710-
JS_SRCS=$(find "$\{PODS_TARGET_SRCROOT\}"/$SRCS_DIR -type f -name "$SRCS_PATTERN" -print0 | xargs -0)
711-
echo "#{file_list}" >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
712-
else
713-
JS_SRCS="$\{PODS_TARGET_SRCROOT\}/$SRCS_DIR"
714-
echo "#{js_srcs_dir}" >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
715-
fi
716-
717-
"$NODE_BINARY" "$CODEGEN_CLI_PATH/lib/cli/combine/combine-js-to-schema-cli.js" "$GENERATED_SCHEMA_FILE" $JS_SRCS
718-
}
719-
720-
runSpecCodegen () {
721-
"$NODE_BINARY" "scripts/generate-specs-cli.js" --platform ios --schemaPath "$GENERATED_SCHEMA_FILE" --outputDir "$1" --libraryName "$LIBRARY_NAME" --libraryType "$2"
722-
}
723-
724-
generateCodegenArtifactsFromSchema () {
725-
describe "Generating codegen artifacts from schema"
726-
pushd "$RN_DIR" >/dev/null || exit 1
727-
if [ "$LIBRARY_TYPE" = "all" ]; then
728-
runSpecCodegen "$TEMP_OUTPUT_DIR/#{$CODEGEN_MODULE_DIR}/#{library_name}" "modules"
729-
runSpecCodegen "$TEMP_OUTPUT_DIR/#{$CODEGEN_COMPONENT_DIR}/#{library_name}" "components"
730-
elif [ "$LIBRARY_TYPE" = "components" ]; then
731-
runSpecCodegen "$TEMP_OUTPUT_DIR/#{$CODEGEN_COMPONENT_DIR}/#{library_name}" "$LIBRARY_TYPE"
732-
elif [ "$LIBRARY_TYPE" = "modules" ]; then
733-
runSpecCodegen "$TEMP_OUTPUT_DIR/#{$CODEGEN_MODULE_DIR}/#{library_name}" "$LIBRARY_TYPE"
734-
fi
735-
popd >/dev/null || exit 1
736-
}
737-
738-
moveOutputs () {
739-
mkdir -p "$OUTPUT_DIR"
740-
741-
# Copy all output to output_dir
742-
cp -R "$TEMP_OUTPUT_DIR/" "$OUTPUT_DIR" || exit 1
743-
echo "$LIBRARY_NAME output has been written to $OUTPUT_DIR:" >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
744-
ls -1 "$OUTPUT_DIR" >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
745-
}
746-
747-
main () {
748-
setup_dirs
749-
find_node
750-
buildCodegenCLI
751-
generateCodegenSchemaFromJavaScript
752-
generateCodegenArtifactsFromSchema
753-
moveOutputs
754-
}
755-
756-
main "$@"
757-
echo 'Done.' >> "${SCRIPT_OUTPUT_FILE_0}" 2>&1
758-
},
558+
:script => get_script_phases_no_codegen_discovery(
559+
react_native_path: react_native_path,
560+
codegen_output_dir: $CODEGEN_OUTPUT_DIR,
561+
codegen_module_dir: $CODEGEN_MODULE_DIR,
562+
codegen_component_dir: $CODEGEN_COMPONENT_DIR,
563+
library_name: library_name,
564+
library_type: library_type,
565+
js_srcs_pattern: js_srcs_pattern,
566+
js_srcs_dir: js_srcs_dir,
567+
file_list: file_list
568+
),
759569
:execution_position => :before_compile,
760570
:show_env_vars_in_log => true
761571
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
# Run test manually by running `ruby react-native/scripts/react_native_pods_utils/__test__/script_phases.test.rb`
7+
8+
require "erb"
9+
10+
def get_script_phases_with_codegen_discovery(options)
11+
export_vars = {
12+
'RCT_SCRIPT_RN_DIR' => "$RCT_SCRIPT_POD_INSTALLATION_ROOT/#{options[:react_native_path]}",
13+
'RCT_SCRIPT_APP_PATH' => "$RCT_SCRIPT_POD_INSTALLATION_ROOT/#{options[:relative_app_root]}",
14+
'RCT_SCRIPT_CONFIG_FILE_DIR' => "#{options[:relative_config_file_dir] ? "$RCT_SCRIPT_POD_INSTALLATION_ROOT/#{options[:relative_config_file_dir]}" : ''}",
15+
'RCT_SCRIPT_OUTPUT_DIR' => "$RCT_SCRIPT_POD_INSTALLATION_ROOT",
16+
'RCT_SCRIPT_FABRIC_ENABLED' => "#{options[:fabric_enabled]}",
17+
'RCT_SCRIPT_TYPE' => "withCodegenDiscovery",
18+
}
19+
return get_script_template(options[:react_native_path], export_vars)
20+
end
21+
22+
def get_script_phases_no_codegen_discovery(options)
23+
export_vars = {
24+
'RCT_SCRIPT_RN_DIR' => "${PODS_TARGET_SRCROOT}/#{options[:react_native_path]}",
25+
'RCT_SCRIPT_LIBRARY_NAME' => "#{options[:library_name]}",
26+
'RCT_SCRIPT_OUTPUT_DIR' => "$RCT_SCRIPT_POD_INSTALLATION_ROOT/#{options[:codegen_output_dir]}",
27+
'RCT_SCRIPT_LIBRARY_TYPE' => "#{options[:library_type] ? options[:library_type] : 'all'}",
28+
'RCT_SCRIPT_JS_SRCS_PATTERN' => "#{options[:js_srcs_pattern]}",
29+
'RCT_SCRIPT_JS_SRCS_DIR' => "#{options[:js_srcs_dir]}",
30+
'RCT_SCRIPT_CODEGEN_MODULE_DIR' => "#{options[:codegen_module_dir]}",
31+
'RCT_SCRIPT_CODEGEN_COMPONENT_DIR' => "#{options[:codegen_component_dir]}",
32+
'RCT_SCRIPT_FILE_LIST' => ("#{options[:file_list]}").dump,
33+
}
34+
return get_script_template(options[:react_native_path], export_vars)
35+
end
36+
37+
38+
def get_script_template(react_native_path, export_vars={})
39+
template =<<~EOS
40+
pushd "$PODS_ROOT/../" > /dev/null
41+
RCT_SCRIPT_POD_INSTALLATION_ROOT=$(pwd)
42+
popd >/dev/null
43+
<% export_vars.each do |(varname, value)| %>
44+
export <%= varname -%>=<%= value -%>
45+
<% end %>
46+
47+
SCRIPT_PHASES_SCRIPT="$RCT_SCRIPT_RN_DIR/scripts/react_native_pods_utils/script_phases.sh"
48+
/bin/sh -c "$SCRIPT_PHASES_SCRIPT"
49+
EOS
50+
result = ERB.new(template, 0, '->').result(binding)
51+
# puts result
52+
return result
53+
end

0 commit comments

Comments
 (0)