1require 'open3'
2require 'pathname'
3
4def generate_or_remove_xcode_env_updates_file!()
5  project_directory = Pod::Config.instance.project_root
6  xcode_env_file = File.join(project_directory, '.xcode.env.updates')
7
8  ex_updates_native_debug = ENV['EX_UPDATES_NATIVE_DEBUG'] == '1' ||
9                              ENV['EX_UPDATES_NATIVE_DEBUG'] == 'true'
10  if ex_updates_native_debug
11    Pod::UI.info "EX_UPDATES_NATIVE_DEBUG is set; auto-generating `.xcode.env.updates` to disable packager and generate debug bundle"
12    if File.exist?(xcode_env_file)
13      File.delete(xcode_env_file)
14    end
15    File.write(xcode_env_file, <<~EOS
16      export FORCE_BUNDLING=1
17      unset SKIP_BUNDLING
18      export RCT_NO_LAUNCH_PACKAGER=1
19EOS
20    )
21  else
22    if File.exist?(xcode_env_file)
23      Pod::UI.info "EX_UPDATES_NATIVE_DEBUG has been unset; removing `.xcode.env.updates`"
24      File.delete(xcode_env_file)
25    end
26  end
27end
28
29def maybe_generate_xcode_env_file!()
30  project_directory = Pod::Config.instance.project_root
31  xcode_env_file = File.join(project_directory, '.xcode.env.local')
32  if File.exist?(xcode_env_file)
33    return
34  end
35
36  # Adding the meta character `;` at the end of command for Ruby `Kernel.exec` to execute the command in shell.
37  stdout, stderr, status = Open3.capture3('node --print "process.argv[0]";')
38  node_path = stdout.strip
39  if !stderr.empty? || status.exitstatus != 0 || node_path.empty?
40    Pod::UI.warn "Unable to generate `.xcode.env.local` for Node.js binary path: #{stderr}"
41  else
42    Pod::UI.info "Auto-generating `.xcode.env.local` with $NODE_BINARY=#{node_path}"
43    File.write(xcode_env_file, "export NODE_BINARY=\"#{node_path}\"\n")
44  end
45end
46