Repository navigation
feat(ios): add opt-in Swift Package Manager mode for the mParticle SDKs #423
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
Merged
thomson-t
merged 1 commit into
workstation/spm-migration
from
thomson-t/spm-03-spm-core-mode
Oct 6, 2026
+280
−21
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # Opt-in Swift Package Manager mode for the mParticle SDKs (see README › Swift Package Manager). | ||
| # | ||
| # Podfile usage (bare React Native): | ||
| # | ||
| # $RNMParticleUseSPM = true # must be set before `target` / use_native_modules! | ||
| # require_relative '../node_modules/react-native-mparticle/ios/mparticle_spm' | ||
| # ... | ||
| # post_install do |installer| | ||
| # react_native_post_install(installer, ...) | ||
| # mparticle_spm_post_install(installer, kits: [ | ||
| # { url: 'https://github.com/mparticle-integrations/mp-apple-integration-rokt', product: 'mParticle-Rokt', version: '9.6.1' }, | ||
| # ]) | ||
| # end | ||
| # | ||
| # What it does: | ||
| # 1. Refuses to install when a pod would add a second copy of the mParticle / Rokt SDKs (mixing pods | ||
| # and Swift packages builds and archives silently, then crashes at runtime). | ||
| # 2. Adds the mParticle core (and any kits) as Swift packages to every iOS application target that | ||
| # uses this pod, idempotently, pinned to an exact version. The app target owns the only copy of each | ||
| # SDK. | ||
| require 'xcodeproj' | ||
|
|
||
| module MParticleSPM | ||
| # Keep these URLs byte-identical to the ones the mParticle kits use, so SwiftPM unifies them into one | ||
| # package identity (mparticle-apple-sdk). No `.git` suffix for the core. | ||
| CORE_URL = 'https://github.com/mParticle/mparticle-apple-sdk'.freeze | ||
| CORE_PRODUCT = 'mParticle-Apple-SDK'.freeze | ||
| # Default core version when the Podfile does not pass one. Bump with each tested native release. | ||
| DEFAULT_CORE_VERSION = '9.6.1'.freeze | ||
| CONFLICTING_PODS = /\A(mParticle-.*|Rokt-Widget|RoktContracts|RoktUXHelper|DcuiSchema)\z/.freeze | ||
|
|
||
| def self.guard!(installer) | ||
| conflicts = installer.pod_targets.map(&:pod_name).uniq.grep(CONFLICTING_PODS) | ||
| return if conflicts.empty? | ||
|
|
||
| raise Pod::Informative, | ||
| "[mParticle] $RNMParticleUseSPM is set, but these pods would add a second copy of the " \ | ||
| "mParticle/Rokt SDKs next to the Swift packages: #{conflicts.sort.join(', ')}. Remove them " \ | ||
| "(including any mParticle pre_install dynamic-framework hook entries) and add kits via " \ | ||
| "mparticle_spm_post_install(installer, kits: [...]) instead." | ||
| end | ||
|
|
||
| # Only iOS apps that use this pod: other apps in the same Podfile must not get the SDKs. | ||
| def self.application_targets(installer) | ||
| aggregates = installer.aggregate_targets.select do |aggregate| | ||
| aggregate.platform.name == :ios && aggregate.pod_targets.any? { |pod| pod.pod_name == 'react-native-mparticle' } | ||
| end | ||
| aggregates.flat_map do |aggregate| | ||
| aggregate.user_targets | ||
| .select { |t| t.product_type == 'com.apple.product-type.application' } | ||
| .map { |t| [aggregate.user_project, t] } | ||
| end.uniq { |project, target| [project.path.to_s, target.uuid] } | ||
| end | ||
|
|
||
| def self.add_package(project, target, url:, product:, version:) | ||
| ref = project.root_object.package_references.find do |r| | ||
| r.is_a?(Xcodeproj::Project::Object::XCRemoteSwiftPackageReference) && r.repositoryURL == url | ||
| end | ||
| unless ref | ||
| ref = project.new(Xcodeproj::Project::Object::XCRemoteSwiftPackageReference) | ||
| ref.repositoryURL = url | ||
| project.root_object.package_references << ref | ||
| end | ||
| requirement = { 'kind' => 'exactVersion', 'version' => version } | ||
| changed = ref.requirement != requirement | ||
| ref.requirement = requirement | ||
|
|
||
| return changed if target.package_product_dependencies.any? { |d| d.product_name == product } | ||
|
|
||
| dep = project.new(Xcodeproj::Project::Object::XCSwiftPackageProductDependency) | ||
| dep.package = ref | ||
| dep.product_name = product | ||
| target.package_product_dependencies << dep | ||
| build_file = project.new(Xcodeproj::Project::Object::PBXBuildFile) | ||
| build_file.product_ref = dep | ||
| target.frameworks_build_phase.files << build_file | ||
| true | ||
| end | ||
| end | ||
|
|
||
| def mparticle_spm_post_install(installer, core_version: MParticleSPM::DEFAULT_CORE_VERSION, kits: []) | ||
| MParticleSPM.guard!(installer) | ||
| packages = [{ url: MParticleSPM::CORE_URL, product: MParticleSPM::CORE_PRODUCT, version: core_version }] + kits | ||
| touched = {} | ||
| MParticleSPM.application_targets(installer).each do |project, target| | ||
| packages.each do |pkg| | ||
| added = MParticleSPM.add_package(project, target, **pkg.slice(:url, :product, :version)) | ||
| Pod::UI.puts "[mParticle] #{target.name} <- #{pkg[:product]} #{pkg[:version]}".green if added | ||
| end | ||
| touched[project.path.to_s] = project | ||
| end | ||
| touched.each_value(&:save) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.