Skip to content
Merged
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
5 changes: 5 additions & 0 deletions docs/migrate-7-to-8.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ last sync at all, so run `spm update` in that case.
`CustomerIOLiveActivities`, `CustomerIOMessagingPushAPN`, `CustomerIOMessagingPushFCM`, `CioFirebaseWrapper`), turn
that module or push provider on with its subspec or Podfile global instead (A1), or it leaves the app. The error
lists the switch for each one.
- **`pod install` stops with "customerio-reactnative needs React Native 0.79 or later on iOS"**: below 0.79,
React Native doesn't register the SDK's TurboModules on iOS, so the app would build and the SDK would do nothing
there (7.x behaves the same). Update React Native to 0.79 or later (Expo SDK 53 or later for Expo apps), then run
`pod install` again. The version comes from the `package.json` in the React Native folder your Podfile's
`use_react_native!` points at.
- **`pod install` stops because an app target installs customerio-reactnative-richpush, or CocoaPods finds
two sources for it**: your app gets the SDK from a folder not named `customerio-reactnative` (a workspace
package or a `file:` link), and React Native's autolinking took the rich push podspec, as 7.x's did. Name
Expand Down
49 changes: 45 additions & 4 deletions ios/cocoapods_swift_packages.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

require 'json'
require 'pathname'

# The podspecs can reach this file through different paths (a pnpm or symlinked node_modules and its real path), and
# Ruby 2.6 loads it once per path. Keep the first copy: its registrations, constants and hooks.
return if defined?(CustomerIO::SwiftPackages)
Expand Down Expand Up @@ -55,6 +58,11 @@ module SwiftPackages
'CustomerIOMessagingPushFCM' => FCM_SWITCH,
'CioFirebaseWrapper' => FCM_SWITCH,
}.freeze
# Below 0.79, React Native doesn't register the SDK's TurboModules on iOS, so the app builds and the SDK does
# nothing there.
MINIMUM_REACT_NATIVE = [0, 79].freeze
# The private CocoaPods installer steps InstallerHook overrides. CocoaPods 1.13 to 1.17 have both.
INSTALLER_STEPS = %i[validate_targets run_podfile_post_install_hooks].freeze

@registrations = {}

Expand Down Expand Up @@ -101,17 +109,48 @@ def register(spec_or_name, version:, products:, subspecs: {}, push_provider: nil
# React Native's list, then wires them in), and CocoaPods' installer (refuses trunk pods before CocoaPods'
# own target checks, and wires the pods in after the Podfile's post_install if React Native didn't).
def install_hook
if defined?(::Pod::Installer) && !::Pod::Installer.ancestors.include?(InstallerHook) &&
::Pod::Installer.private_method_defined?(:validate_targets) &&
::Pod::Installer.private_method_defined?(:run_podfile_post_install_hooks)
::Pod::Installer.prepend(InstallerHook)
if defined?(::Pod::Installer)
missing_steps = INSTALLER_STEPS.reject { |step| ::Pod::Installer.private_method_defined?(step) }
if missing_steps.empty?
::Pod::Installer.prepend(InstallerHook) unless ::Pod::Installer.ancestors.include?(InstallerHook)
elsif !defined?(::SPMManager)
# Raised while CocoaPods evaluates the podspec: no later step of this install is known to run our code.
version = defined?(::Pod::VERSION) ? ::Pod::VERSION : 'this version'
raise Pod::Informative, "customerio-reactnative can't add customerio-ios to its pods with CocoaPods " \
"#{version}: its installer has no #{missing_steps.map { |s| "`#{s}`" }.join(' or ')} " \
"step, and React Native's SPMManager (behind `spm_dependency`) isn't loaded. " \
'The build would then fail with `No such module` for customerio-ios modules. Use ' \
'CocoaPods 1.13 to 1.17 (React Native excludes 1.15.0 and 1.15.1), or report ' \
'this with your CocoaPods version: https://github.com/customerio/customerio-reactnative/issues'
end
end
return unless defined?(::SPMManager)
return if ::SPMManager.ancestors.include?(SPMManagerHook)

::SPMManager.prepend(SPMManagerHook)
end

# `use_react_native!` exports REACT_NATIVE_PATH while CocoaPods reads the Podfile (relative to the Podfile's
# folder or absolute, depending on the React Native version), and React Native's pod scripts read its version
# from the package.json there. That's why this runs from the install hooks: React Native's autolinking
# evaluates the podspec before `use_react_native!`. Major and minor only, so release candidates and nightlies of
# a supported version pass. An install without the SDK's own pod (an extension's rich push pod) isn't checked.
def refuse_unsupported_react_native!(installer, react_native_path = ENV['REACT_NATIVE_PATH'], podfile_folder = nil)
return if react_native_path.to_s.empty?
return unless installer.pod_targets.any? { |t| t.pod_name == 'customerio-reactnative' }

path = Pathname.new(react_native_path)
path = Pathname.new(podfile_folder || ::Pod::Config.instance.installation_root) + path if path.relative?
version = JSON.parse(File.read(path + 'package.json'))['version'].to_s
return if (version.split('.').first(2).map(&:to_i) <=> MINIMUM_REACT_NATIVE) >= 0

minimum = MINIMUM_REACT_NATIVE.join('.')
raise Pod::Informative, "customerio-reactnative needs React Native #{minimum} or later on iOS, and this app has " \
"React Native #{version}. Before #{minimum}, React Native doesn't register the SDK's " \
'native modules, so the app would build and the SDK would do nothing on iOS. Update ' \
'React Native (Expo SDK 53 or later for Expo apps), then run `pod install` again.'
end

# With `incremental_installation`, CocoaPods keeps a target whose inputs didn't change, but whether apply!
# flattens a static target depends on how many static variants of its pod the install has. When a target in the
# last generated project was flattened the other way (one static variant became two, or two became one), its kept
Expand Down Expand Up @@ -142,6 +181,7 @@ def apply!(installer)
return if @applied_to.equal?(installer)

@applied_to = installer
refuse_unsupported_react_native!(installer)
refuse_trunk_pods!(installer)
refuse_conflicting_push!(installer)
# With `skip_pods_project_generation`, CocoaPods generates no projects, so whatever builds the pods adds the
Expand Down Expand Up @@ -360,6 +400,7 @@ module InstallerHook
# CocoaPods' own checks would otherwise reject some trunk pods first (CioFirebaseWrapper defines no module
# map), with a message that says nothing about what replaces them.
def validate_targets
CustomerIO::SwiftPackages.refuse_unsupported_react_native!(self)
CustomerIO::SwiftPackages.refuse_trunk_pods!(self)
CustomerIO::SwiftPackages.refuse_rich_push_in_app!(self)
CustomerIO::SwiftPackages.refuse_conflicting_push!(self)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"./package.json": "./package.json"
},
"cioNativeiOSSdkVersion": "= 5.4.0",
"cioNativeiOSSdkVersion": "= 5.4.1",
"files": [
"src",
"lib",
Expand Down
114 changes: 114 additions & 0 deletions scripts/test_cocoapods_swift_packages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def xcconfig_path(name)

def setup
@dir = Dir.mktmpdir
# React Native's use_react_native! exports it; a value from the shell mustn't reach these tests.
@react_native_path = ENV.delete('REACT_NATIVE_PATH')
registrations.clear
%i[$customerio_location_enabled $customerio_geofence_enabled $customerio_live_activities_enabled
$customerio_push_provider].each do |name|
Expand All @@ -85,6 +87,7 @@ def setup

def teardown
FileUtils.rm_rf(@dir)
ENV['REACT_NATIVE_PATH'] = @react_native_path
end

def registrations
Expand Down Expand Up @@ -202,6 +205,57 @@ def install_modules_dependencies(_spec); end
JSON.parse(output.lines.last)
end

# React Native version

# Writes <app>/node_modules/react-native/package.json with this version and returns that folder.
def react_native(version)
folder = File.join(@dir, 'node_modules', 'react-native')
FileUtils.mkdir_p(folder)
File.write(File.join(folder, 'package.json'), JSON.generate(name: 'react-native', version: version))
folder
end

def sdk_install
installer([pod_target('customerio-reactnative')], nil)
end

# Older React Native exports the path as the Podfile gave it, relative to the Podfile's folder.
def test_refuses_react_native_before_0_79
react_native('0.78.3')
error = assert_raises(Pod::Informative) do
CustomerIO::SwiftPackages.refuse_unsupported_react_native!(sdk_install, '../node_modules/react-native',
File.join(@dir, 'ios'))
end
assert_match(/needs React Native 0\.79 or later on iOS, and this app has React Native 0\.78\.3\./, error.message)
end

# Release candidates and nightlies of 0.79 and later pass, as npm's peer ranges wouldn't let them.
def test_accepts_react_native_0_79_and_later_including_prereleases
%w[0.79.0-rc.3 0.79.7 0.88.0 0.90.0-nightly-20261120-0123abcdef 1000.0.0].each do |version|
CustomerIO::SwiftPackages.refuse_unsupported_react_native!(sdk_install, react_native(version))
end
end

# A Notification Service Extension's rich push pod doesn't need React Native, and a Podfile that doesn't call
# use_react_native! exports no path.
def test_an_install_without_the_sdk_pod_or_react_native_isnt_checked
folder = react_native('0.78.3')
extension = installer([pod_target('customerio-reactnative-richpush', pod_name: 'customerio-reactnative-richpush')], nil)
CustomerIO::SwiftPackages.refuse_unsupported_react_native!(extension, folder)
CustomerIO::SwiftPackages.refuse_unsupported_react_native!(sdk_install, nil)
end

# React Native's autolinking evaluates the podspec before use_react_native! exports the path, so the install hook
# checks, before CocoaPods' own target checks.
def test_the_installer_hook_refuses_react_native_before_0_79
ENV['REACT_NATIVE_PATH'] = react_native('0.77.3')
install = FakeInstaller.new([pod_target('customerio-reactnative')])
install.singleton_class.prepend(CustomerIO::SwiftPackages::InstallerHook)
error = assert_raises(Pod::Informative) { install.send(:validate_targets) }
assert_match(/this app has React Native 0\.77\.3/, error.message)
assert_nil install.validated
end

# The two podspecs can load the module through different paths (pnpm: the real path and node_modules' symlink), which
# Ruby 2.6 loads twice. The second load must keep the first pod's registration.
def test_loading_through_a_second_path_keeps_registrations
Expand Down Expand Up @@ -636,6 +690,66 @@ def test_cocoapods_post_install_applies_when_react_native_did_not
assert_equal 2, target.package_product_dependencies.size
end

# Registers a pod in a child process whose CocoaPods installer has only the given private steps, with or without
# React Native's SPMManager, and returns the hooked classes or the install's error.
def register_with(installer_steps:, spm_manager:)
script = <<~RUBY
module Pod
VERSION = '1.99.0'
class Informative < StandardError; end
class Installer
private

#{installer_steps.map { |step| "def #{step}; end" }.join("\n")}
end
end
#{'class SPMManager; end' if spm_manager}
load ARGV[0]
begin
CustomerIO::SwiftPackages.register('customerio-reactnative-richpush', version: '5.4.0', products: [])
hooked = [Pod::Installer, (SPMManager if defined?(SPMManager))].compact.select do |klass|
klass.ancestors.any? { |mod| mod.name.to_s.start_with?('CustomerIO::') }
end
puts "hooked: \#{hooked.join(',')}"
rescue Pod::Informative => e
puts e.message
end
RUBY
output, status = Open3.capture2e(RbConfig.ruby, '-e', script, File.join(REPO_ROOT, 'ios', 'cocoapods_swift_packages.rb'))
assert status.success?, output
output.strip
end

def test_cocoapods_with_its_installer_steps_gets_the_hook
assert_equal 'hooked: Pod::Installer',
register_with(installer_steps: %i[validate_targets run_podfile_post_install_hooks], spm_manager: false)
end

def test_without_the_installer_steps_react_natives_spm_manager_keeps_the_install_going
assert_equal 'hooked: SPMManager', register_with(installer_steps: [], spm_manager: true)
end

# Neither hook would add customerio-ios, so the app would only fail to build later.
def test_pod_install_stops_when_neither_hook_can_run
steps = { validate_targets: :run_podfile_post_install_hooks, run_podfile_post_install_hooks: :validate_targets }
steps.each do |present, missing|
output = register_with(installer_steps: [present], spm_manager: false)
assert_match(/with CocoaPods 1\.99\.0: its installer has no `#{missing}` step/, output)
assert_match(/Use CocoaPods 1\.13 to 1\.17/, output)
end
end

# Without CocoaPods' installer steps, React Native's post_install is the only hook, and it checks too.
def test_react_natives_hook_refuses_react_native_before_0_79
ENV['REACT_NATIVE_PATH'] = react_native('0.78.3')
manager = FakeSPMManager.new
manager.singleton_class.prepend(CustomerIO::SwiftPackages::SPMManagerHook)
error = assert_raises(Pod::Informative) do
manager.apply_on_post_install(installer([pod_target('customerio-reactnative')], project))
end
assert_match(/this app has React Native 0\.78\.3/, error.message)
end

def test_the_hook_takes_our_pods_out_of_react_natives_list
registrations['customerio-reactnative'] = MAIN
manager = FakeSPMManager.new
Expand Down
Loading