Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"envinfo": "^7.1.0",
"errorhandler": "^1.5.0",
"execa": "^1.0.0",
"find-up": "^4.1.0",
"fs-extra": "^7.0.1",
"glob": "^7.1.1",
"graceful-fs": "^4.1.3",
Expand Down Expand Up @@ -69,8 +70,8 @@
},
"devDependencies": {
"@types/command-exists": "^1.2.0",
"@types/graceful-fs": "^4.1.3",
"@types/cosmiconfig": "^5.0.3",
"@types/graceful-fs": "^4.1.3",
"@types/hapi__joi": "^15.0.4",
"@types/minimist": "^1.2.0",
"@types/mkdirp": "^0.5.2",
Expand Down
36 changes: 36 additions & 0 deletions packages/cli/src/tools/config/__tests__/findProjectRoot-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import path from 'path';
import findProjectRoot from '../findProjectRoot';
import {
cleanup,
writeFiles,
getTempDirectory,
} from '../../../../../../jest/helpers';

beforeEach(() => {
cleanup(DIR);
jest.resetModules();
});

afterEach(() => cleanup(DIR));

const DIR = getTempDirectory('find_project_root_test');

test('resolves to correct project root', () => {
writeFiles(DIR, {
'package.json': '{}',
'ios/Podfile': '',
});
const cwd = path.join(DIR, 'ios');
expect(findProjectRoot(cwd)).toBe(DIR);
expect(findProjectRoot(DIR)).toBe(DIR);
});

test('resolves to correct project root in a monorepo', () => {
writeFiles(DIR, {
'package.json': '{}',
'packages/mobile/package.json': '{}',
'packages/mobile/ios/Podfile': '',
});
const cwd = path.join(DIR, 'packages/mobile/ios');
expect(findProjectRoot(cwd)).toBe(path.join(DIR, 'packages/mobile'));
});
26 changes: 26 additions & 0 deletions packages/cli/src/tools/config/findProjectRoot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import findUp from 'find-up';
import path from 'path';
import {CLIError} from '@react-native-community/cli-tools';

/**
* Finds project root by looking for a closest `package.json`.
*/
export default function findProjectRoot(cwd = process.cwd()): string {
const packageLocation = findUp.sync('package.json', {cwd});

/**
* It is possible that `package.json` doesn't exist
* in the tree. In that case, we want to throw an error.
*
* When executing via `npx`, this will never happen as `npm`
* requires that file to be present in order to run.
*/
if (!packageLocation) {
throw new CLIError(`
Comment thread
grabbou marked this conversation as resolved.
We couldn't find a package.json in your project.
Are you sure you are running it inside a React Native project?
`);
}

return path.dirname(packageLocation);
}
3 changes: 2 additions & 1 deletion packages/cli/src/tools/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {logger, inlineString} from '@react-native-community/cli-tools';
import * as ios from '@react-native-community/cli-platform-ios';
import * as android from '@react-native-community/cli-platform-android';
import findDependencies from './findDependencies';
import findProjectRoot from './findProjectRoot';
import resolveReactNativePath from './resolveReactNativePath';
import findAssets from './findAssets';
import {
Expand Down Expand Up @@ -61,7 +62,7 @@ function getDependencyConfig(
/**
* Loads CLI configuration
*/
function loadConfig(projectRoot: string = process.cwd()): Config {
function loadConfig(projectRoot: string = findProjectRoot()): Config {
let lazyProject: ProjectConfig;
const userConfig = readConfigFromDisk(projectRoot);

Expand Down
48 changes: 25 additions & 23 deletions packages/platform-ios/native_modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@
#
require 'pathname'

def use_native_modules!(root = "..", config = nil)
Comment thread
grabbou marked this conversation as resolved.
Comment thread
grabbou marked this conversation as 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
IO.popen("npx react-native config") do |data|
while line = data.gets
json << line
end
end

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

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

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

packages.each do |package_name, package|
Expand Down Expand Up @@ -57,10 +55,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
Copy Markdown
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
Copy Markdown
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.to_path

if package_config["scriptPhases"]
# Can be either an object, or an array of objects
Expand Down Expand Up @@ -129,7 +127,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 +145,13 @@ def pluralize(count)
},
}
}
@project = {
"ios" => {
"sourceDir" => "/root/app/ios"
}
}
@config = {
"root" => "/root/app",
"project" => @project,
"dependencies" => {
"ios-dep" => @ios_package,
"android-dep" => @android_package
Expand All @@ -166,8 +168,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 +199,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 +210,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 +237,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 +255,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