Skip to content

Commit 0480f56

Browse files
Riccardo Cipolleschifacebook-github-bot
Riccardo Cipolleschi
authored andcommitted
Introduce .xcode.env configuration file to source node (#33546)
Summary: Pull Request resolved: #33546 This Diff does 2 things: 1. Removes all the remnant of the `find-node.sh` script. This allows React Native to stay agnostic from any other node manager 2. Introduces a way for the developers to specify which `node` executable they want to use, through a simple `.env` file. ## Changelog [iOS][Changed] - This PR removes the `find-node.sh` scripts and replaces it with an `.xcode.env` file that is sourced by the script phases that needs it. The `.xcode.env` file is versioned: to customize a local environment, an unversioned `.xcode.local.env` can be used. Reviewed By: cortinico Differential Revision: D35317070 fbshipit-source-id: 4b400ba56aa2d574db563fa67b2008e1ddde1c59
1 parent 61b013e commit 0480f56

File tree

13 files changed

+86
-19
lines changed

13 files changed

+86
-19
lines changed

Diff for: .circleci/config.yml

-6
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,6 @@ jobs:
468468
- brew_install:
469469
package: applesimutils
470470

471-
- run:
472-
name: Configure Node
473-
# Sourcing find-node.sh will ensure nvm is set up.
474-
# It also helps future invocation of find-node.sh prevent permission issue with nvm.sh.
475-
command: source scripts/find-node-for-xcode.sh && nvm install 16 && nvm alias default 16
476-
477471
- run:
478472
name: Configure Watchman
479473
command: echo "{}" > .watchmanconfig

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ DerivedData
2020
*.ipa
2121
*.xcuserstate
2222
project.xcworkspace
23+
**/.xcode.env.local
2324

2425
# Gradle
2526
/build/

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"scripts/generate-provider-cli.js",
4444
"scripts/generate-specs-cli.js",
4545
"scripts/ios-configure-glog.sh",
46+
"scripts/xcode/with-environment.sh",
4647
"scripts/launchPackager.bat",
4748
"scripts/launchPackager.command",
4849
"scripts/node-binary.sh",

Diff for: packages/rn-tester/.xcode.env

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This `.xcode.env` file is versioned and is used to source the environment
2+
# used when running script phases inside Xcode.
3+
# To customize your local environment, you can create an `.xcode.env.local`
4+
# file that is not versioned.
5+
6+
# NODE_BINARY variable contains the PATH to the node executable.
7+
#
8+
# Customize the NODE_BINARY variable here.
9+
# For example, to use nvm with brew, add the following line
10+
# . "$(brew --prefix nvm)/nvm.sh" --no-use
11+
export NODE_BINARY=$(command -v node)

Diff for: packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -633,13 +633,15 @@
633633
files = (
634634
);
635635
inputPaths = (
636+
"$(SRCROOT)/.xcode.env.local",
637+
"$(SRCROOT)/.xcode.env",
636638
);
637639
name = "Build JS Bundle";
638640
outputPaths = (
639641
);
640642
runOnlyForDeploymentPostprocessing = 0;
641643
shellPath = /bin/sh;
642-
shellScript = "set -e\n\nexport NODE_BINARY=node\nexport PROJECT_ROOT=\"$SRCROOT/../../\"\nexport ENTRY_FILE=\"$SRCROOT/js/RNTesterApp.ios.js\"\nexport SOURCEMAP_FILE=../sourcemap.ios.map\n# export FORCE_BUNDLING=true\n\"$SRCROOT/../../scripts/react-native-xcode.sh\"\n";
644+
shellScript = "set -e\n\nexport PROJECT_ROOT=\"$SRCROOT/../../\"\nexport ENTRY_FILE=\"$SRCROOT/js/RNTesterApp.ios.js\"\nexport SOURCEMAP_FILE=../sourcemap.ios.map\n# export FORCE_BUNDLING=true \n\nWITH_ENVIRONMENT=\"../../scripts/xcode/with-environment.sh\"\nREACT_NATIVE_XCODE=\"../../scripts/react-native-xcode.sh\"\n\n/bin/sh -c \"$WITH_ENVIRONMENT $REACT_NATIVE_XCODE\"\n";
643645
};
644646
98E057AC8860597818FB485A /* [CP] Copy Pods Resources */ = {
645647
isa = PBXShellScriptBuildPhase;

Diff for: scripts/react-native-xcode.sh

-4
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ if [[ $DEV != true && ! -f "$ENTRY_FILE" ]]; then
7979
exit 2
8080
fi
8181

82-
# Find path to Node
83-
# shellcheck source=/dev/null
84-
source "$REACT_NATIVE_DIR/scripts/find-node-for-xcode.sh"
85-
8682
# check and assign NODE_BINARY env
8783
# shellcheck source=/dev/null
8884
source "$REACT_NATIVE_DIR/scripts/node-binary.sh"

Diff for: scripts/react_native_pods.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,11 @@ def use_react_native_codegen!(spec, options={})
630630
system(prepare_command) # Always run prepare_command when a podspec uses the codegen, as CocoaPods may skip invoking this command in certain scenarios. Replace with pre_integrate_hook after updating to CocoaPods 1.11
631631
spec.prepare_command = prepare_command
632632

633+
env_files = ["$PODS_ROOT/../.xcode.env.local", "$PODS_ROOT/../.xcode.env"]
634+
633635
spec.script_phase = {
634636
:name => 'Generate Specs',
635-
:input_files => input_files, # This also needs to be relative to Xcode
637+
:input_files => input_files + env_files, # This also needs to be relative to Xcode
636638
:output_files => ["${DERIVED_FILE_DIR}/codegen-#{library_name}.log"].concat(generated_files.map { |filename| "${PODS_TARGET_SRCROOT}/#{filename}"} ),
637639
# The final generated files will be created when this script is invoked at Xcode build time.
638640
:script => get_script_phases_no_codegen_discovery(

Diff for: scripts/react_native_pods_utils/script_phases.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ def get_script_template(react_native_path, export_vars={})
4545
<% end %>
4646
4747
SCRIPT_PHASES_SCRIPT="$RCT_SCRIPT_RN_DIR/scripts/react_native_pods_utils/script_phases.sh"
48-
/bin/sh -c "$SCRIPT_PHASES_SCRIPT"
48+
WITH_ENVIRONMENT="$RCT_SCRIPT_RN_DIR/scripts/xcode/with-environment.sh"
49+
/bin/sh -c "$WITH_ENVIRONMENT $SCRIPT_PHASES_SCRIPT"
4950
EOS
5051
result = ERB.new(template, 0, '->').result(binding)
51-
# puts result
5252
return result
5353
end

Diff for: scripts/react_native_pods_utils/script_phases.sh

+3-4
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ else
3333
fi
3434

3535
find_node () {
36-
# shellcheck disable=SC1091
37-
source "$RCT_SCRIPT_RN_DIR/scripts/find-node-for-xcode.sh"
38-
3936
NODE_BINARY="${NODE_BINARY:-$(command -v node || true)}"
4037
if [ -z "$NODE_BINARY" ]; then
41-
error "error: Could not find node. Make sure it is in bash PATH or set the NODE_BINARY environment variable."
38+
error "[Error] Could not find node. It looks like that the .xcode.env or .xcode.env.local " \
39+
"files are misconfigured. Please check that they are exporting a valid NODE_BINARY " \
40+
"variable, pointing to a node executable."
4241
fi
4342
}
4443

Diff for: scripts/xcode/with-environment.sh

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
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+
# This script is used to source in Xcode the environment settings required to run properly.
8+
# The script first sources the base `.xcode.env` file.
9+
# Then it sources the `.xcode.env.local` file if present, to override some local config
10+
# Finally, it will execute the command passed i input if any.
11+
#
12+
# USAGE:
13+
# ./with-environment.sh command
14+
15+
# Start with a default
16+
NODE_BINARY=$(command -v node)
17+
export NODE_BINARY
18+
19+
# Override the default with the global environment
20+
ENV_PATH="$PODS_ROOT/../.xcode.env"
21+
if [ -f "$ENV_PATH" ]; then
22+
source "$ENV_PATH"
23+
fi
24+
25+
# Override the global with the local environment
26+
LOCAL_ENV_PATH="${ENV_PATH}.local"
27+
if [ -f "$LOCAL_ENV_PATH" ]; then
28+
source "$LOCAL_ENV_PATH"
29+
fi
30+
31+
# Check whether NODE_BINARY has been properly set, otherwise help the users with a meaningful error.
32+
if [ -n "$NODE_BINARY" ]; then
33+
echo "Node found at: ${NODE_BINARY}"
34+
else
35+
echo "[Warning] You need to configure your node path in the `'.xcode.env' file` environment. " \
36+
"You can set it up quickly by running: " \
37+
"echo 'export NODE_BINARY=$(command -v node)' > .xcode.env " \
38+
"in the ios folder. This is needed by React Native to work correctly. " \
39+
"We fallback to the DEPRECATED behavior of finding `node`. This will be REMOVED in a future version. " \
40+
"You can read more about this here: <TODO-ADD LINK HERE>" >&2
41+
source "../find-node-for-xcode.sh"
42+
fi
43+
44+
# Execute argument, if present
45+
if [ -n "$1" ]; then
46+
$1
47+
fi

Diff for: template/_gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ DerivedData
2020
*.hmap
2121
*.ipa
2222
*.xcuserstate
23+
ios/.xcode.env.local
2324

2425
# Android/IntelliJ
2526
#

Diff for: template/ios/HelloWorld.xcodeproj/project.pbxproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,15 @@
256256
files = (
257257
);
258258
inputPaths = (
259+
"$(SRCROOT)/.xcode.env.local",
260+
"$(SRCROOT)/.xcode.env",
259261
);
260262
name = "Bundle React Native code and images";
261263
outputPaths = (
262264
);
263265
runOnlyForDeploymentPostprocessing = 0;
264266
shellPath = /bin/sh;
265-
shellScript = "set -e\n\nexport NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh\n";
267+
shellScript = "set -e\n\nWITH_ENVIRONMENT=\"../node_modules/react-native/scripts/xcode/with-environment.sh\"\nREACT_NATIVE_XCODE=\"../node_modules/react-native/scripts/react-native-xcode.sh\"\n\n/bin/sh -c \"$WITH_ENVIRONMENT $REACT_NATIVE_XCODE\"\n";
266268
};
267269
00EEFC60759A1932668264C0 /* [CP] Embed Pods Frameworks */ = {
268270
isa = PBXShellScriptBuildPhase;

Diff for: template/ios/_xcode.env

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This `.xcode.env` file is versioned and is used to source the environment
2+
# used when running script phases inside Xcode.
3+
# To customize your local environment, you can create an `.xcode.env.local`
4+
# file that is not versioned.
5+
6+
# NODE_BINARY variable contains the PATH to the node executable.
7+
#
8+
# Customize the NODE_BINARY variable here.
9+
# For example, to use nvm with brew, add the following line
10+
# . "$(brew --prefix nvm)/nvm.sh" --no-use
11+
export NODE_BINARY=$(command -v node)

0 commit comments

Comments
 (0)