diff --git a/.vscode/settings.json b/.vscode/settings.json index 149aceb95..230a6715c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,7 @@ { - "editor.rulers": [80], + "editor.rulers": [ + 80 + ], "files.exclude": { "**/.git": true, "**/node_modules": true, 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/native_modules.rb b/packages/platform-ios/native_modules.rb index 05deb19d2..225415840 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: @@ -133,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| @@ -141,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 2c7942490..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,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()); @@ -97,6 +114,7 @@ describeIfSupportedEnv()('native_modules.rb', () => { beforeEach(() => { runConfig = { dependencyConfig: { + reactNativePath: REACT_NATIVE_ROOT, project: {ios: {sourceDir: FIXTURES_ROOT}}, dependencies: { 'android-dep': { @@ -110,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'); @@ -117,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', @@ -144,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', @@ -173,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, @@ -203,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, @@ -253,7 +281,7 @@ describeIfSupportedEnv()('native_modules.rb', () => { execution_position: 'before_compile', }, ]; - return run(runConfig).then((rootTargetDefinition: TargetDefinition) => { + return run(runConfig).then(({rootTargetDefinition}: RunResult) => { expect(rootTargetDefinition.children[0].script_phases) .toMatchInlineSnapshot(` Array [ @@ -277,7 +305,7 @@ describeIfSupportedEnv()('native_modules.rb', () => { execution_position: 'before_compile', }, ]; - return run(runConfig).then((rootTargetDefinition: TargetDefinition) => { + return run(runConfig).then(({rootTargetDefinition}: RunResult) => { expect(rootTargetDefinition.children[0].script_phases) .toMatchInlineSnapshot(` Array [