Skip to content

Commit

Permalink
FIX: Set platform of macro pods (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
trinhngocthuyen authored Dec 28, 2024
1 parent 6c56345 commit 3498493
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 10 deletions.
4 changes: 2 additions & 2 deletions examples/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ config_cocoapods_spm(

def shared_pods
pod "Logger", :path => "LocalPods/Logger"
pod "Wizard", :macro => { :path => "LocalPackages/ex-macros" }
end

target "EX" do # rubocop:disable Metrics/BlockLength
Expand All @@ -42,7 +43,6 @@ target "EX" do # rubocop:disable Metrics/BlockLength
:git => "https://github.com/mikhailmaslo/macro-codable-kit",
:tag => "0.3.0",
}
pod "Wizard", :macro => {:path => "LocalPackages/ex-macros"}

spm_pkg "SnapKit",
:url => "https://github.com/SnapKit/SnapKit.git",
Expand Down Expand Up @@ -77,7 +77,7 @@ end
post_install do |installer|
installer.native_targets.each do |target|
target.build_configurations.each do |config|
config.build_settings["IPHONEOS_DEPLOYMENT_TARGET"] = ios_deployment_target
# Custom build settings goes here
end
end
end
12 changes: 10 additions & 2 deletions lib/cocoapods-spm/def/installer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
module Pod
class Installer
def native_targets
pods_project.targets + pod_target_subprojects.flat_map(&:targets)
module InstallerMixin
def native_targets
projects_to_integrate.flat_map(&:targets)
end

def projects_to_integrate
[pods_project] + pod_target_subprojects
end
end

include InstallerMixin
end
end
19 changes: 15 additions & 4 deletions lib/cocoapods-spm/hooks/base.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require "cocoapods-spm/config"
require "cocoapods-spm/def/podfile"
require "cocoapods-spm/def/spec"
require "cocoapods-spm/macro/metadata"

module Pod
module SPM
class Hook
include Config::Mixin
include Installer::InstallerMixin

def initialize(context, options = {})
@context = context
Expand Down Expand Up @@ -34,10 +36,6 @@ def pod_target_subprojects
@context.pod_target_subprojects
end

def projects_to_integrate
[pods_project] + pod_target_subprojects
end

def user_build_configurations
@user_build_configurations ||= (pod_targets + aggregate_targets)[0].user_build_configurations
end
Expand Down Expand Up @@ -94,6 +92,19 @@ def perform_settings_update(
end
end
end

def macro_metadata_for_pod(name)
return nil unless spm_config.all_macros.include?(name)

@macro_metadata_cache ||= {}
@macro_metadata_cache[name] = MacroMetadata.for_pod(name) unless @macro_metadata_cache.key?(name)
@macro_metadata_cache[name]
end

def pod_name_of_target(name)
target = @analysis_result.pod_targets.find { |x| x.name == name }
target.nil? ? name : target.pod_name
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def macro_plugin_flag_by_config
@macro_plugin_flag_by_config ||= begin
hash = user_build_configurations.keys.to_h do |config|
flags = macro_pods.keys.map do |name|
metadata = MacroMetadata.for_pod(name)
metadata = macro_metadata_for_pod(name)
impl_module_name = metadata.macro_impl_name
plugin_executable_path =
"#{path_prefix}/#{name}/" \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "cocoapods-spm/hooks/base"
require "cocoapods-spm/macro/metadata"

module Pod
module SPM
class Hook
class UpdateMacroPlatforms < Hook
def run
to_save = Set.new
native_targets.each do |target|
name = pod_name_of_target(target.name)
metadata = macro_metadata_for_pod(name)
next if metadata.nil?

settings = metadata.platform_build_settings
target.build_configurations.each do |config|
settings.each { |k, v| config.build_settings[k] = v }
end
to_save << target.project
end
to_save.each(&:save)
end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/cocoapods-spm/macro/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ def self.for_pod(name)
end
from_file(path)
end

def platforms
raw["platforms"].to_h { |ds| [ds["platformName"], ds["version"]] }
end

def platform_build_settings
ds = {
"ios" => "IPHONEOS_DEPLOYMENT_TARGET",
"macos" => "MACOSX_DEPLOYMENT_TARGET",
"tvos" => "TVOS_DEPLOYMENT_TARGET",
"watchos" => "WATCHOS_DEPLOYMENT_TARGET",
"visionos" => "XROS_DEPLOYMENT_TARGET",
"driverkit" => "DRIVERKIT_DEPLOYMENT_TARGET",
}
platforms.transform_keys { |k| ds[k] }.reject { |k, _| k.nil? }
end
end
end
end
2 changes: 1 addition & 1 deletion lib/cocoapods-spm/resolver/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def spec_name_of(target)
return target.name if target.is_a?(Pod::AggregateTarget)

cmps = target.name.split("-")
return cmps[...-1].join("-") if ["iOS", "macOS", "watchOS", "tvOS"].include?(cmps[-1])
return cmps[...-1].join("-") if ["iOS", "macOS", "watchOS", "tvOS", "visionOS"].include?(cmps[-1])

target.name
end
Expand Down

0 comments on commit 3498493

Please sign in to comment.