Skip to content

Commit

Permalink
[ios] Ensure that the right cwd is set up for auto-linking dependenci…
Browse files Browse the repository at this point in the history
…es (react-native-community#354)

* [ios] Ensure that the right cwd is set up for auto-linking dependencies

* [autolink] Switch to DIing in the root path as a param in to use_native_modules

* [autolink] Docs for autolink root changes
  • Loading branch information
orta authored and thymikee committed Apr 25, 2019
1 parent 9315f05 commit c1a8ec8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
2 changes: 2 additions & 0 deletions docs/autolinking.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ The implementation ensures that a library is imported only once, so if you need

See implementation of [native_modules.rb](https://github.com/react-native-community/cli/blob/master/packages/platform-ios/native_modules.rb).

_Notes_: Auto-linking assumes your Podfile is in a sub-folder from your `package.json` - if this is not the case, use the first parameter to tell the linker where to find the `package.json` e.g. `use_native_modules!("../../")`.

## Platform Android

1. At build time, before the build script is run, a first gradle plugin (`settings.gradle`) is ran that takes the package metadata from `react-native config` to dynamically include Android library projects into the build.
Expand Down
37 changes: 24 additions & 13 deletions packages/platform-ios/native_modules.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
def use_native_modules!(packages = nil)
# This is a function which is used inside your Podfile.
# It uses `react-native config` to grab a list of dependencies, and pulls out.all of the ones
# which declare themselves to be iOS dependencies (via having a Podspec) and automatically
# imports those into your current target.
#
def use_native_modules!(root = "..", packages = nil)
if (!packages)
# Resolve the CLI's main index file
cli_bin = Pod::Executable.execute_command("node", ["-e", "console.log(require.resolve('@react-native-community/cli/build/index.js'))"], true).strip
output = Pod::Executable.execute_command("node", [cli_bin, "config"], true)
output = ""
# Make sure `react-native config` is ran from your project root
Dir.chdir(root) do
output = Pod::Executable.execute_command("node", [cli_bin, "config"], true)
end

json = []
output.each_line do |line|
case line
Expand Down Expand Up @@ -71,7 +82,7 @@ def use_native_modules!(packages = nil)
end

# You can run the tests for this file by running:
# $ ruby use_native_modules.rb
# $ ruby packages/platform-ios/native_modules.rb
if $0 == __FILE__
require "minitest/spec"
require "minitest/autorun"
Expand Down Expand Up @@ -136,8 +147,8 @@ def pluralize(count)

spec.singleton_class.send(:define_method, :name) { "ios-dep" }

podfile.singleton_class.send(:define_method, :use_native_modules) do |config|
use_native_modules!(config)
podfile.singleton_class.send(:define_method, :use_native_modules) do |path, config|
use_native_modules!('..', config)
end

Pod::Specification.singleton_class.send(:define_method, :from_file) do |podspec_path|
Expand Down Expand Up @@ -167,7 +178,7 @@ def pluralize(count)
end

it "activates iOS pods" do
@podfile.use_native_modules(@config)
@podfile.use_native_modules('..', @config)
@activated_pods.must_equal [{
name: "ios-dep",
options: { path: @ios_package["root"] }
Expand All @@ -178,22 +189,22 @@ def pluralize(count)
activated_pod = Object.new
activated_pod.singleton_class.send(:define_method, :name) { "ios-dep" }
@current_target_definition_dependencies << activated_pod
@podfile.use_native_modules(@config)
@podfile.use_native_modules('..', @config)
@activated_pods.must_equal []
end

it "does not activate pods whose root spec were already activated previously (by the user in their Podfile)" do
activated_pod = Object.new
activated_pod.singleton_class.send(:define_method, :name) { "ios-dep/foo/bar" }
@current_target_definition_dependencies << activated_pod
@podfile.use_native_modules(@config)
@podfile.use_native_modules('..', @config)
@activated_pods.must_equal []
end

it "prints out the native module pods that were found" do
@podfile.use_native_modules({})
@podfile.use_native_modules({ "pkg-1" => @ios_package })
@podfile.use_native_modules({ "pkg-1" => @ios_package, "pkg-2" => @ios_package })
@podfile.use_native_modules('..', {})
@podfile.use_native_modules('..', { "pkg-1" => @ios_package })
@podfile.use_native_modules('..', { "pkg-1" => @ios_package, "pkg-2" => @ios_package })
@printed_messages.must_equal [
"Detected React Native module pod for ios-dep",
"Detected React Native module pods for ios-dep, and ios-dep"
Expand All @@ -203,7 +214,7 @@ def pluralize(count)
describe "concerning script_phases" do
it "uses the options directly" do
@config["ios-dep"]["platforms"]["ios"]["scriptPhases"] = [@script_phase]
@podfile.use_native_modules(@config)
@podfile.use_native_modules('..', @config)
@added_scripts.must_equal [{
"script" => "123",
"name" => "My Name",
Expand All @@ -221,7 +232,7 @@ def pluralize(count)
file_read_mock.expect(:call, "contents from file", [File.join(@ios_package["root"], "some_shell_script.sh")])

File.stub(:read, file_read_mock) do
@podfile.use_native_modules(@config)
@podfile.use_native_modules('..', @config)
end

@added_scripts.must_equal [{
Expand Down

0 comments on commit c1a8ec8

Please sign in to comment.