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: RFC-0759 break apart iOS auto-linking #2335

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions packages/cli-platform-ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# 15.0.0-alpha.1 (2024-03-15)

feat: added autolinking.rb, which breaks up use_native_modules! into list_native_modules!, and link_native_modules! (which lives in react-native).
Comment on lines +6 to +8
Copy link
Member

Choose a reason for hiding this comment

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

I'm unusre we're actually using this CHANGELOG file 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

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

True, we're not adding CHANGELOG inside this repo.


# 8.0.0-alpha.3 (2022-04-22)

**Note:** Version bump only for package @react-native-community/cli-platform-ios
Expand Down
98 changes: 98 additions & 0 deletions packages/cli-platform-ios/autolinking.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# This is a function which is used inside your Podfile.
#
# This is a function which is used inside your Podfile. It is deeply coupled to the Community Config.
# init_native_modules! in @react-native/core-cli-utils/autolinking/ios/native_modules.rb#link_native_modules

require 'pathname'
require 'cocoapods'

# Including this as it bring in the *legacy* use_native_modules!, but we should remove this
# require once the deprecation is complete.
require_relative './native_modules.rb'

require Pod::Executable.execute_command('node', ['-p',
'require.resolve(
"react-native/scripts/react_native_pods.rb",
{paths: [process.argv[1]]},
)', __dir__]).strip


def list_native_modules!()
cli_bin = Pod::Executable.execute_command("node", ["-p", "require('@react-native-community/cli').bin"], true).strip

json = []

IO.popen(["node", cli_bin, "config"]) do |data|
while line = data.gets
json << line
end
end

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

project_root = Pathname.new(config["project"]["ios"]["sourceDir"])

packages = config["dependencies"]
found_pods = []

packages.each do |package_name, package|
next unless package_config = package["platforms"]["ios"]

podspec_path = package_config["podspecPath"]
configurations = package_config["configurations"]

# Add a warning to the queue and continue to the next dependency if the podspec_path is nil/empty
if podspec_path.nil? || podspec_path.empty?
Pod::UI.warn("list_native_modules! skipped the react-native dependency '#{package["name"]}'. No podspec file was found.",
[
"Check to see if there is an updated version that contains the necessary podspec file",
"Contact the library maintainers or send them a PR to add a podspec. The react-native-webview podspec is a good example of a package.json driven podspec. See https://github.com/react-native-community/react-native-webview/blob/master/react-native-webview.podspec",
"If necessary, you can disable autolinking for the dependency and link it manually. See https://github.com/react-native-community/cli/blob/main/docs/autolinking.md#how-can-i-disable-autolinking-for-unsupported-library"
])
end
next if podspec_path.nil? || podspec_path.empty?

spec = Pod::Specification.from_file(podspec_path)

# Skip pods that do not support the platform of the current target.
if platform = current_target_definition.platform
next unless spec.supported_on_platform?(platform.name)
else
# TODO: In a future RN version we should update the Podfile template and
# enable this assertion.
#
# raise Pod::Informative, "Cannot invoke `!` before defining the supported `platform`"
end

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

relative_path = podspec_dir_path.relative_path_from project_root

# pod spec.name, :path => relative_path.to_path, :configurations => configurations

found_pods.push({
"name": name,
"path": relative_path.to_path,
"configurations": configurations
})
end

if found_pods.size > 0
pods = found_pods.map { |p| p.name }.sort.to_sentence
Pod::UI.puts "Found #{found_pods.size} #{"module".pluralize(found_pods.size)} for target `#{current_target_definition.name}`"
end

absolute_react_native_path = Pathname.new(config["reactNativePath"])

{
"ios_packages": found_pods,
"project_root_path": project_root.to_s,
"react_native_path": absolute_react_native_path.relative_path_from(project_root).to_s
}
end

# You should be using this instead of use_native_modules, which is deprecated.
def autolink_native_modules!()
link_native_modules! list_native_modules!()
end
Comment on lines +94 to +97
Copy link
Member

Choose a reason for hiding this comment

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

Can we achieve the same without having to go through a deprecation cycle but by just changing the import path for the use_native_modules function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I spoke with @cipolleschi about the design, and I've leaning towards what you've described: keep the use_native_modules API to avoid a deprecation cycle / effort from our users.


19 changes: 19 additions & 0 deletions packages/cli-platform-ios/native_modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
require 'pathname'
require 'cocoapods'

VERSIONS = {
:warn => Pod::Version.create("0.75"),
:deprecated => Pod::Version.create("0.76"),
}

def use_native_modules!(config = nil)
if (config.is_a? String)
Pod::UI.warn("Passing custom root to use_native_modules! is deprecated.",
Expand All @@ -37,6 +42,20 @@ def use_native_modules!(config = nil)
config = JSON.parse(json.join("\n"))
end

version = Pod::Version.new(config["reactNativeVersion"])
if version >= VERSIONS.deprecated
Pod::UI.warn("use_native_modules! is deprecated since #{VERSIONS.deprecated}", [
"Please consult the React Native Upgrade Helper to upgrade your iOS Podfile:",
"https://react-native-community.github.io/upgrade-helper/?from=#{version}#RnDiffApp-ios-Podfile"
])
abort "use_native_modules! is depreacted"
elsif version >= VERSIONS.warn
Pod::UI.warn("use_native_modules! is going to be deprecated in #{VERSIONS.deprecated}", [
"Please consult the React Native Upgrade Helper to upgrade your iOS Podfile:",
"https://react-native-community.github.io/upgrade-helper/?from=#{version}#RnDiffApp-ios-Podfile"
])
end

project_root = Pathname.new(config["project"]["ios"]["sourceDir"])

packages = config["dependencies"]
Expand Down
7 changes: 6 additions & 1 deletion packages/cli-platform-ios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
"access": "public"
},
"dependencies": {
"@react-native-community/cli-platform-apple": "14.0.0-alpha.0"
"@react-native-community/cli-platform-apple": "14.0.0-alpha.0",
"@react-native/core-cli-utils": "nightly"
},
"peerDependencies": {
"react-native": ">= 0.75"
},
"files": [
"build",
"!*.d.ts",
"!*.map",
"autolinking.rb",
"native_modules.rb"
],
"homepage": "https://github.com/react-native-community/cli/tree/main/packages/cli-platform-ios",
Expand Down
10 changes: 10 additions & 0 deletions packages/cli-types/src/autolinking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface CocoaPodDescription {
name: string;
relativePath: string;
configurations: string[];
}

export interface ListModules {
reactNativePath: string;
pods: CocoaPodDescription[];
}
1 change: 1 addition & 0 deletions packages/cli-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
AndroidDependencyConfig,
AndroidDependencyParams,
} from './android';
export type {CocoaPodDescription, ListModules} from './autolinking';

export type Prompt = any;

Expand Down
Loading