Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iOS): #591 track env file changes on each build #801

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions react-native-config.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ Pod::Spec.new do |s|
s.script_phase = {
name: 'Config codegen',
script: %(
set -ex
HOST_PATH="$SRCROOT/../.."
"${PODS_TARGET_SRCROOT}/ios/ReactNativeConfig/BuildDotenvConfig.rb" "$HOST_PATH" "${PODS_TARGET_SRCROOT}/ios/ReactNativeConfig"
),
set -ex
HOST_PATH="$SRCROOT/../.."
"${PODS_TARGET_SRCROOT}/ios/ReactNativeConfig/BuildDotenvConfig.rb" "$HOST_PATH" "${PODS_TARGET_SRCROOT}/ios/ReactNativeConfig"
),
execution_position: :before_compile,
input_files: ['$PODS_TARGET_SRCROOT/ios/ReactNativeConfig/BuildDotenvConfig.rb'],
input_files: ['$PODS_TARGET_SRCROOT/ios/ReactNativeConfig/BuildDotenvConfig.rb', '$SRCROOT/../../.env', '$ENVFILE'],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe need to add '$SRCROOT/../../$ENVFILE' for relative file name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit tricky 😁
$ENVFILE is used for the case when the env file is set via build_settings, like so (in your Podfile):

ENVFILES = {
  'Debug' => '$(PODS_ROOT)/../../.env.debug',
  'Release' => '$(PODS_ROOT)/../../.env.production',
}
post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if target.name == 'react-native-config'
        config.build_settings['ENVFILE'] = ENVFILES[config.name]
      end
    end
  end
end

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I don't use the "alternative" path as described here: https://github.com/lugg/react-native-config?tab=readme-ov-file#ios-1

But instead define ENVFILE as environment variable before running the react native scripts from the command line:

In the scripts section of package.json:

"sim.prod": "cross-env ENVFILE=.env.production  react-native run-ios --no-packager --simulator 'iPhone 15 Pro (17.2)'",

Since the Ruby files in this package are reading the environment variables.

I guess I could switch that to use a scheme arg on the CLI instead, but others may also make the above assumption that setting the ENVFILE env var works.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I think the following is better:

"sim.prod": "cross-env ENVFILE=$(pwd)/.env.production react-native run-ios --no-packager --simulator 'iPhone 15 Pro (17.2)'"

Why:
To avoid mixing concerns. The Xcode project should not define where the ENVFILE is located (except for the default location like '$SRCROOT/../../.env').

output_files: ['$BUILD_DIR/GeneratedInfoPlistDotEnv.h', '$PODS_TARGET_SRCROOT/ios/ReactNativeConfig/GeneratedDotEnv.m']
}

Expand All @@ -48,7 +48,7 @@ HOST_PATH="$SRCROOT/../.."
"${PODS_TARGET_SRCROOT}/ios/ReactNativeConfig/BuildDotenvConfig.rb" "$HOST_PATH" "${PODS_TARGET_SRCROOT}/ios/ReactNativeConfig"
),
execution_position: :before_compile,
input_files: ['$PODS_TARGET_SRCROOT/ios/ReactNativeConfig/BuildDotenvConfig.rb'],
input_files: ['$PODS_TARGET_SRCROOT/ios/ReactNativeConfig/BuildDotenvConfig.rb', '$SRCROOT/../../.env', '$ENVFILE'],
output_files: ['$PODS_TARGET_SRCROOT/ios/ReactNativeConfig/GeneratedDotEnv.m']
}
ext.source_files = ['ios/**/RNCConfig.{h,m}', 'ios/**/GeneratedDotEnv.m']
Expand Down