From 34ec2f0d4d423ecaf8cc99c44b7743cdb46a7b44 Mon Sep 17 00:00:00 2001 From: grabbou Date: Wed, 17 Jun 2020 19:09:32 +0200 Subject: [PATCH 1/5] fix: cp paths --- packages/platform-ios/native_modules.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/platform-ios/native_modules.rb b/packages/platform-ios/native_modules.rb index 05deb19d2..e68e8fdef 100644 --- a/packages/platform-ios/native_modules.rb +++ b/packages/platform-ios/native_modules.rb @@ -120,7 +120,9 @@ def use_native_modules!(config = nil) Pod::UI.puts "Auto-linking React Native #{"module".pluralize(found_pods.size)} for target `#{current_target_definition.name}`: #{pods}" end - config + absolute_react_native_path = Pathname.new(config["reactNativePath"]) + + { :reactNativePath => absolute_react_native_path.relative_path_from(project_root).to_s } end # You can run the tests for this file by running: From ded6426e6f51622f86e4073c008d89954f30aeb7 Mon Sep 17 00:00:00 2001 From: grabbou Date: Wed, 17 Jun 2020 19:34:28 +0200 Subject: [PATCH 2/5] chore: fix tests --- packages/cli-types/src/ios.ts | 1 + .../config/__tests__/native_modules.test.ts | 27 ++++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/cli-types/src/ios.ts b/packages/cli-types/src/ios.ts index f3c8c812d..7aa0f8478 100644 --- a/packages/cli-types/src/ios.ts +++ b/packages/cli-types/src/ios.ts @@ -65,6 +65,7 @@ export type IOSScriptPhase = ({script: string} | {path: string}) & { * requirements of `native_modules.rb` change. */ export interface IOSNativeModulesConfig { + reactNativePath: string; project: { ios?: { sourceDir: string; diff --git a/packages/platform-ios/src/config/__tests__/native_modules.test.ts b/packages/platform-ios/src/config/__tests__/native_modules.test.ts index 2c7942490..4a93c5dbc 100644 --- a/packages/platform-ios/src/config/__tests__/native_modules.test.ts +++ b/packages/platform-ios/src/config/__tests__/native_modules.test.ts @@ -86,7 +86,7 @@ function describeIfSupportedEnv() { } console.warn( '[!] The `native_modules.rb` tests are disabled – ensure you have `ruby` ' + - 'and the `cocoapods` gem installed in order to run them.', + 'and the `cocoapods` gem installed in order to run them.', ); return describe.skip; } @@ -97,6 +97,7 @@ describeIfSupportedEnv()('native_modules.rb', () => { beforeEach(() => { runConfig = { dependencyConfig: { + reactNativePath: '/root/app/node_modules/react_native', project: {ios: {sourceDir: FIXTURES_ROOT}}, dependencies: { 'android-dep': { @@ -247,12 +248,12 @@ describeIfSupportedEnv()('native_modules.rb', () => { runConfig.dependencyConfig.dependencies[ 'ios-dep' ].platforms.ios.scriptPhases = [ - { - script: '123', - name: 'My Name', - execution_position: 'before_compile', - }, - ]; + { + script: '123', + name: 'My Name', + execution_position: 'before_compile', + }, + ]; return run(runConfig).then((rootTargetDefinition: TargetDefinition) => { expect(rootTargetDefinition.children[0].script_phases) .toMatchInlineSnapshot(` @@ -271,12 +272,12 @@ describeIfSupportedEnv()('native_modules.rb', () => { runConfig.dependencyConfig.dependencies[ 'ios-dep' ].platforms.ios.scriptPhases = [ - { - path: './some_shell_script.sh', - name: 'My Name', - execution_position: 'before_compile', - }, - ]; + { + path: './some_shell_script.sh', + name: 'My Name', + execution_position: 'before_compile', + }, + ]; return run(runConfig).then((rootTargetDefinition: TargetDefinition) => { expect(rootTargetDefinition.children[0].script_phases) .toMatchInlineSnapshot(` From 66d7d3d75b1cc481a518d6832a333864031160bb Mon Sep 17 00:00:00 2001 From: Mike Grabowski Date: Wed, 17 Jun 2020 19:36:17 +0200 Subject: [PATCH 3/5] Update native_modules.test.ts --- .../config/__tests__/native_modules.test.ts | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/platform-ios/src/config/__tests__/native_modules.test.ts b/packages/platform-ios/src/config/__tests__/native_modules.test.ts index 4a93c5dbc..ebb35f9e1 100644 --- a/packages/platform-ios/src/config/__tests__/native_modules.test.ts +++ b/packages/platform-ios/src/config/__tests__/native_modules.test.ts @@ -86,7 +86,7 @@ function describeIfSupportedEnv() { } console.warn( '[!] The `native_modules.rb` tests are disabled – ensure you have `ruby` ' + - 'and the `cocoapods` gem installed in order to run them.', + 'and the `cocoapods` gem installed in order to run them.', ); return describe.skip; } @@ -248,12 +248,12 @@ describeIfSupportedEnv()('native_modules.rb', () => { runConfig.dependencyConfig.dependencies[ 'ios-dep' ].platforms.ios.scriptPhases = [ - { - script: '123', - name: 'My Name', - execution_position: 'before_compile', - }, - ]; + { + script: '123', + name: 'My Name', + execution_position: 'before_compile', + }, + ]; return run(runConfig).then((rootTargetDefinition: TargetDefinition) => { expect(rootTargetDefinition.children[0].script_phases) .toMatchInlineSnapshot(` @@ -272,12 +272,12 @@ describeIfSupportedEnv()('native_modules.rb', () => { runConfig.dependencyConfig.dependencies[ 'ios-dep' ].platforms.ios.scriptPhases = [ - { - path: './some_shell_script.sh', - name: 'My Name', - execution_position: 'before_compile', - }, - ]; + { + path: './some_shell_script.sh', + name: 'My Name', + execution_position: 'before_compile', + }, + ]; return run(runConfig).then((rootTargetDefinition: TargetDefinition) => { expect(rootTargetDefinition.children[0].script_phases) .toMatchInlineSnapshot(` From 1f408cfc467b623c9dc935aae26dde0abab333f8 Mon Sep 17 00:00:00 2001 From: grabbou Date: Fri, 19 Jun 2020 12:20:55 +0200 Subject: [PATCH 4/5] Add tests --- packages/platform-ios/native_modules.rb | 8 +- .../config/__tests__/native_modules.test.ts | 73 +++++++++++++------ 2 files changed, 55 insertions(+), 26 deletions(-) diff --git a/packages/platform-ios/native_modules.rb b/packages/platform-ios/native_modules.rb index e68e8fdef..225415840 100644 --- a/packages/platform-ios/native_modules.rb +++ b/packages/platform-ios/native_modules.rb @@ -135,6 +135,8 @@ def use_native_modules!(config = nil) Pod::Config.instance.silent = true end + return_values = [] + podfile = Pod::Podfile.new do if runInput["podsActivatedByUser"] runInput["podsActivatedByUser"].each do |name| @@ -143,15 +145,15 @@ def use_native_modules!(config = nil) end target 'iOS Target' do platform :ios - use_native_modules!(runInput["dependencyConfig"]) + return_values[0] = use_native_modules!(runInput["dependencyConfig"]) end target 'macOS Target' do platform :osx - use_native_modules!(runInput["dependencyConfig"]) + return_values[1] = use_native_modules!(runInput["dependencyConfig"]) end end unless runInput["captureStdout"] - puts podfile.to_hash.to_json + puts podfile.to_hash.merge({ "return_values": return_values }).to_json end end diff --git a/packages/platform-ios/src/config/__tests__/native_modules.test.ts b/packages/platform-ios/src/config/__tests__/native_modules.test.ts index 4a93c5dbc..41097dd59 100644 --- a/packages/platform-ios/src/config/__tests__/native_modules.test.ts +++ b/packages/platform-ios/src/config/__tests__/native_modules.test.ts @@ -5,11 +5,16 @@ import hasbin from 'hasbin'; const SCRIPT_PATH = path.resolve(__dirname, '../../../native_modules.rb'); const FIXTURES_ROOT = path.resolve(__dirname, '../__fixtures__/native_modules'); +const REACT_NATIVE_ROOT = '/root/app/node_modules/react_native'; interface Dependency { path: string; } +interface NativeModulesReturnValue { + reactNativePath: string; +} + interface TargetDefinition { name: string; abstract?: boolean; @@ -21,8 +26,9 @@ interface TargetDefinition { script_phases?: unknown[]; } -interface Podfile { +interface TestScriptOutput { target_definitions: TargetDefinition[]; + return_values: NativeModulesReturnValue[]; } interface RunConfig { @@ -31,8 +37,13 @@ interface RunConfig { dependencyConfig: IOSNativeModulesConfig; } +interface RunResult { + rootTargetDefinition: TargetDefinition; + returnValues: NativeModulesReturnValue[]; +} + function run(runConfig: RunConfig) { - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { const child = spawn('ruby', [SCRIPT_PATH]); child.stdin.write(JSON.stringify(runConfig)); child.stdin.end(); @@ -50,8 +61,14 @@ function run(runConfig: RunConfig) { if (runConfig.captureStdout) { resolve(data.trimRight()); } else { - const podfile: Podfile = JSON.parse(data); - resolve(podfile.target_definitions[0]); + const { + target_definitions, + return_values, + }: TestScriptOutput = JSON.parse(data); + resolve({ + rootTargetDefinition: target_definitions[0], + returnValues: return_values, + }); } } else { reject(Buffer.concat(stderrData).toString()); @@ -86,7 +103,7 @@ function describeIfSupportedEnv() { } console.warn( '[!] The `native_modules.rb` tests are disabled – ensure you have `ruby` ' + - 'and the `cocoapods` gem installed in order to run them.', + 'and the `cocoapods` gem installed in order to run them.', ); return describe.skip; } @@ -111,6 +128,16 @@ describeIfSupportedEnv()('native_modules.rb', () => { addDependency(runConfig, 'ios-dep'); }); + it('returns relative path to a React Native location from source dir', () => { + return run(runConfig).then(({returnValues}: RunResult) => { + returnValues.forEach(rv => { + expect(rv.reactNativePath).toBe( + path.relative(FIXTURES_ROOT, REACT_NATIVE_ROOT), + ); + }); + }); + }); + describe('concerning platform specificity', () => { beforeEach(() => { addDependency(runConfig, 'macos-dep'); @@ -118,7 +145,7 @@ describeIfSupportedEnv()('native_modules.rb', () => { }); it('only activates pods that support iOS in targets that target `ios`', () => { - return run(runConfig).then((rootTargetDefinition: TargetDefinition) => { + return run(runConfig).then(({rootTargetDefinition}: RunResult) => { expect( rootTargetDefinition.children.find( target => target.name === 'iOS Target', @@ -145,7 +172,7 @@ describeIfSupportedEnv()('native_modules.rb', () => { }); it('only activates pods that support macOS in targets that target `osx`', () => { - return run(runConfig).then((rootTargetDefinition: TargetDefinition) => { + return run(runConfig).then(({rootTargetDefinition}: RunResult) => { expect( rootTargetDefinition.children.find( target => target.name === 'macOS Target', @@ -174,7 +201,7 @@ describeIfSupportedEnv()('native_modules.rb', () => { it('does not activate pods that were already activated previously (by the user in their Podfile)', () => { runConfig.podsActivatedByUser = ['ios-dep']; - return run(runConfig).then(rootTargetDefinition => { + return run(runConfig).then(({rootTargetDefinition}: RunResult) => { expect(rootTargetDefinition).toMatchInlineSnapshot(` Object { "abstract": true, @@ -204,7 +231,7 @@ describeIfSupportedEnv()('native_modules.rb', () => { it('does not activate pods whose root spec were already activated previously (by the user in their Podfile)', () => { runConfig.podsActivatedByUser = ['ios-dep/foo/bar']; - return run(runConfig).then(rootTargetDefinition => { + return run(runConfig).then(({rootTargetDefinition}: RunResult) => { expect(rootTargetDefinition).toMatchInlineSnapshot(` Object { "abstract": true, @@ -248,13 +275,13 @@ describeIfSupportedEnv()('native_modules.rb', () => { runConfig.dependencyConfig.dependencies[ 'ios-dep' ].platforms.ios.scriptPhases = [ - { - script: '123', - name: 'My Name', - execution_position: 'before_compile', - }, - ]; - return run(runConfig).then((rootTargetDefinition: TargetDefinition) => { + { + script: '123', + name: 'My Name', + execution_position: 'before_compile', + }, + ]; + return run(runConfig).then(({rootTargetDefinition}: RunResult) => { expect(rootTargetDefinition.children[0].script_phases) .toMatchInlineSnapshot(` Array [ @@ -272,13 +299,13 @@ describeIfSupportedEnv()('native_modules.rb', () => { runConfig.dependencyConfig.dependencies[ 'ios-dep' ].platforms.ios.scriptPhases = [ - { - path: './some_shell_script.sh', - name: 'My Name', - execution_position: 'before_compile', - }, - ]; - return run(runConfig).then((rootTargetDefinition: TargetDefinition) => { + { + path: './some_shell_script.sh', + name: 'My Name', + execution_position: 'before_compile', + }, + ]; + return run(runConfig).then(({rootTargetDefinition}: RunResult) => { expect(rootTargetDefinition.children[0].script_phases) .toMatchInlineSnapshot(` Array [ From 30c77209810aebc723186006020950d5b76a161f Mon Sep 17 00:00:00 2001 From: grabbou Date: Fri, 19 Jun 2020 12:42:30 +0200 Subject: [PATCH 5/5] chore: reuse constant --- .../platform-ios/src/config/__tests__/native_modules.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/platform-ios/src/config/__tests__/native_modules.test.ts b/packages/platform-ios/src/config/__tests__/native_modules.test.ts index 41097dd59..e2b0c68c6 100644 --- a/packages/platform-ios/src/config/__tests__/native_modules.test.ts +++ b/packages/platform-ios/src/config/__tests__/native_modules.test.ts @@ -5,7 +5,7 @@ import hasbin from 'hasbin'; const SCRIPT_PATH = path.resolve(__dirname, '../../../native_modules.rb'); const FIXTURES_ROOT = path.resolve(__dirname, '../__fixtures__/native_modules'); -const REACT_NATIVE_ROOT = '/root/app/node_modules/react_native'; +const REACT_NATIVE_ROOT = '/root/app/node_modules/react-native'; interface Dependency { path: string; @@ -114,7 +114,7 @@ describeIfSupportedEnv()('native_modules.rb', () => { beforeEach(() => { runConfig = { dependencyConfig: { - reactNativePath: '/root/app/node_modules/react_native', + reactNativePath: REACT_NATIVE_ROOT, project: {ios: {sourceDir: FIXTURES_ROOT}}, dependencies: { 'android-dep': {