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

feat: support autolinking in monorepos #768

Merged
merged 44 commits into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
b3b09ef
Improve handling of files in monorepo
grabbou Oct 3, 2019
440f166
Fix tests with my poor Ruby haha
grabbou Oct 3, 2019
1ffc0c2
Remove note about monorepo from the docs
grabbou Oct 3, 2019
391505d
wat
grabbou Oct 3, 2019
e0722c3
Okay, this is how you do tests in Ruby
grabbou Oct 3, 2019
f85186f
Fix Ruby tests
grabbou Oct 3, 2019
435d314
Always resolve correct root
grabbou Oct 4, 2019
78065e0
Move files around
grabbou Oct 4, 2019
80be9d2
Add tests - one is failing, need to check why
grabbou Oct 4, 2019
3495fe5
Fix tests
grabbou Oct 4, 2019
7e46d77
Assert it works from the same level
grabbou Oct 4, 2019
68639b7
Warn if the root is passed
grabbou Oct 7, 2019
f8de995
Implement monorepo support for Android
grabbou Oct 8, 2019
5041be8
Note custom paths
grabbou Oct 8, 2019
53f59d1
Add quiet flag
grabbou Oct 8, 2019
9dd0ce1
Improve handling of files in monorepo
grabbou Oct 3, 2019
3be7ea4
Fix tests with my poor Ruby haha
grabbou Oct 3, 2019
4091c4c
Remove note about monorepo from the docs
grabbou Oct 3, 2019
27fac31
wat
grabbou Oct 3, 2019
0cbe32d
Okay, this is how you do tests in Ruby
grabbou Oct 3, 2019
e362dc6
Fix Ruby tests
grabbou Oct 3, 2019
19fd10e
Always resolve correct root
grabbou Oct 4, 2019
366b7c4
Move files around
grabbou Oct 4, 2019
553df50
Add tests - one is failing, need to check why
grabbou Oct 4, 2019
858ac79
Fix tests
grabbou Oct 4, 2019
883068f
Assert it works from the same level
grabbou Oct 4, 2019
7733cb2
Warn if the root is passed
grabbou Oct 7, 2019
c4c1745
Implement monorepo support for Android
grabbou Oct 8, 2019
a13e47f
Note custom paths
grabbou Oct 8, 2019
bdcc9fa
Add quiet flag
grabbou Oct 8, 2019
07bd62e
update locfile
thymikee Oct 10, 2019
858ea5e
Fix init - make detachedCommands and remove ugly setProjectDir hack
grabbou Oct 10, 2019
94d8bba
Resolve conflicts
grabbou Oct 10, 2019
81e7b10
Merge branch 'fix/paths' of github.com:react-native-community/react-n…
grabbou Oct 10, 2019
beca41b
Fix remainig uses or root and get rid of cwd
grabbou Oct 10, 2019
cc8c80c
Update paths
grabbou Oct 10, 2019
6661421
Remove unused func
grabbou Oct 10, 2019
781c0e6
Update templates.ts
grabbou Oct 10, 2019
46b0cd3
Fix unit tests
grabbou Oct 10, 2019
5033e50
Better logger
grabbou Oct 10, 2019
cc4b5bf
Debug
grabbou Oct 10, 2019
7402931
Add deprecations
grabbou Oct 10, 2019
90bacbc
Fix a typo
grabbou Oct 10, 2019
d0bd034
Better deprecation message
grabbou Oct 10, 2019
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
22 changes: 0 additions & 22 deletions docs/autolinking.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,6 @@ The implementation ensures that a library is imported only once. If you need to

See example usage in React Native template's [Podfile](https://github.com/facebook/react-native/blob/0.60-stable/template/ios/Podfile).

### Custom root (monorepos)

The project root is where `node_modules` with `react-native` is. Autolinking script assume your project root to be `".."`, relative to `ios` directory. If you're in a project with custom structure, like this:

```
root/
node_modules
example/
ios/
```

you'll need to set a custom root. Pass it as an argument to `use_native_modules!` function inside the targets and adjust the relatively required `native_modules` path accordingly:

```rb
# example/ios/Podfile
require_relative '../../node_modules/@react-native-community/cli-platform-ios/native_modules'
target 'RNapp' do
# React pods and custom pods here...
use_native_modules!("../..")
end
```

## Platform Android

The [native_modules.gradle](https://github.com/react-native-community/cli/blob/master/packages/platform-android/native_modules.gradle) script is included in your project's `settings.gradle` and `app/build.gradle` files and:
Expand Down
49 changes: 26 additions & 23 deletions packages/platform-ios/native_modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@
#
require 'pathname'

def use_native_modules!(root = "..", config = nil)
grabbou marked this conversation as resolved.
Show resolved Hide resolved
grabbou marked this conversation as resolved.
Show resolved Hide resolved
def use_native_modules!(config = nil)
if (!config)
json = []

# Make sure `react-native config` is ran from your project root
Dir.chdir(root) do
IO.popen("./node_modules/.bin/react-native config") do |data|
while line = data.gets
json << line
end
# @todo: Do not use Yarn here, but find path dynamically
IO.popen("yarn run --silent react-native config") do |data|
grabbou marked this conversation as resolved.
Show resolved Hide resolved
while line = data.gets
json << line
end
end

config = JSON.parse(json.join("\n"))
end

project_root = Pathname.new(config["project"]["ios"]["sourceDir"])
grabbou marked this conversation as resolved.
Show resolved Hide resolved

packages = config["dependencies"]
config_root = config["root"]
grabbou marked this conversation as resolved.
Show resolved Hide resolved
found_pods = []

packages.each do |package_name, package|
Expand Down Expand Up @@ -57,10 +56,10 @@ def use_native_modules!(root = "..", config = nil)
end

podspec_dir_path = Pathname.new(File.dirname(podspec_path))
project_root = Pathname.new(config_root)

relative_path = podspec_dir_path.relative_path_from project_root
Copy link
Member

@thymikee thymikee Oct 10, 2019

Choose a reason for hiding this comment

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

Now that we don't have custom root, I think instead of using project_root we need to use cwd, because that's we look for – a relative path from cwd (ios dir) to our native module (in node_modules or custom path). This fixes the Slider repo example for me:

Suggested change
relative_path = podspec_dir_path.relative_path_from project_root
relative_path = podspec_dir_path.relative_path_from Dir.pwd

Copy link
Member Author

Choose a reason for hiding this comment

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

projet_root is project_root = Pathname.new(config["project"]["ios"]["sourceDir"]) few lines earlier - so that's exactly the location of ios folder.

PWD might be not a good idea, see: #657 when running from root folder with --ios-directory flag


pod spec.name, :path => File.join(root, relative_path)
pod spec.name, :path => relative_path

if package_config["scriptPhases"]
# Can be either an object, or an array of objects
Expand Down Expand Up @@ -129,7 +128,6 @@ def pluralize(count)
"execution_position" => "before_compile",
"input" => "string"
}

@ios_package = ios_package = {
"root" => "/root/app/node_modules/react",
"platforms" => {
Expand All @@ -148,8 +146,13 @@ def pluralize(count)
},
}
}
@project = {
"ios" => {
"sourceDir": "/root/app"
}
}
@config = {
"root" => "/root/app",
"project" => @project,
"dependencies" => {
"ios-dep" => @ios_package,
"android-dep" => @android_package
Expand All @@ -166,8 +169,8 @@ def pluralize(count)

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

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

Pod::Specification.singleton_class.send(:define_method, :from_file) do |podspec_path|
Expand Down Expand Up @@ -197,7 +200,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: "../node_modules/react" }
Expand All @@ -208,23 +211,23 @@ 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('..', { "root" => "/root/app", "dependencies" => {} })
@podfile.use_native_modules('..', { "root" => "/root/app", "dependencies" => { "pkg-1" => @ios_package }})
@podfile.use_native_modules('..', {
"root" => "/root/app", "dependencies" => { "pkg-1" => @ios_package, "pkg-2" => @ios_package }
@podfile.use_native_modules({ "project" => @project, "dependencies" => {} })
@podfile.use_native_modules({ "project" => @project, "dependencies" => { "pkg-1" => @ios_package }})
@podfile.use_native_modules({
"project" => @project, "dependencies" => { "pkg-1" => @ios_package, "pkg-2" => @ios_package }
})
@printed_messages.must_equal [
"Detected React Native module pod for ios-dep",
Expand All @@ -235,7 +238,7 @@ def pluralize(count)
describe "concerning script_phases" do
it "uses the options directly" do
@config["dependencies"]["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 @@ -253,7 +256,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