Skip to content

Commit

Permalink
Merge pull request #10 from intercom/james/better_context
Browse files Browse the repository at this point in the history
Encapsulate PostInstallHooksContext within CocoapodsMangle::Context
  • Loading branch information
jtreanor authored Dec 11, 2017
2 parents 0333078 + a1ea2bd commit 26357fe
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 127 deletions.
18 changes: 9 additions & 9 deletions lib/cocoapods_mangle/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ module CocoapodsMangle
class Builder
BUILT_PRODUCTS_DIR = 'build/Release-iphonesimulator'

# @param [Pod::Project] pods_project
# the pods project to build.
# @param [String] pods_project_path
# path to the pods project to build.
#
# @param [Array<Pod::Installer::PostInstallHooksContext::UmbrellaTargetDescription>] umbrella_pod_targets
# the umbrella pod targets to build.
def initialize(pods_project, umbrella_pod_targets)
@pods_project = pods_project
@umbrella_pod_targets = umbrella_pod_targets
# @param [Array<String>] pod_target_labels
# the pod targets to build.
def initialize(pods_project_path, pod_target_labels)
@pods_project_path = pods_project_path
@pod_target_labels = pod_target_labels
end

# Build the pods project
def build!
FileUtils.remove_dir(BUILT_PRODUCTS_DIR, true)
@umbrella_pod_targets.each { |target| build_target(target.cocoapods_target_label) }
@pod_target_labels.each { |target| build_target(target) }
end

# Gives the built binaries to be mangled
Expand All @@ -33,7 +33,7 @@ def binaries_to_mangle

def build_target(target)
Pod::UI.message "- Building '#{target}'"
output = `xcodebuild -project "#{@pods_project.path}" -target "#{target}" -configuration Release -sdk iphonesimulator build 2>&1`
output = `xcodebuild -project "#{@pods_project_path}" -target "#{target}" -configuration Release -sdk iphonesimulator build 2>&1`
unless $?.success?
raise "error: Building the Pods target '#{target}' failed.\ This is the build log:\n#{output}"
end
Expand Down
14 changes: 3 additions & 11 deletions lib/cocoapods_mangle/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(context)
# Update the mangling xcconfig file with new mangling defines
def update_mangling!
Pod::UI.message '- Updating mangling xcconfig' do
builder = Builder.new(@context.pods_project, @context.umbrella_pod_targets)
builder = Builder.new(@context.pods_project_path, @context.pod_target_labels)
builder.build!

defines = Defines.mangling_defines(@context.mangle_prefix, builder.binaries_to_mangle)
Expand Down Expand Up @@ -49,16 +49,8 @@ def needs_update?

# Update all pod xcconfigs to use the mangling defines
def update_pod_xcconfigs_for_mangling!
pod_xcconfigs = Set.new

@context.pods_project.targets.each do |target|
target.build_configurations.each do |config|
pod_xcconfigs.add(config.base_configuration_reference.real_path)
end
end

Pod::UI.message "- Updating Pod xcconfig files" do
pod_xcconfigs.each do |pod_xcconfig_path|
Pod::UI.message '- Updating Pod xcconfig files' do
@context.pod_xcconfig_paths.each do |pod_xcconfig_path|
Pod::UI.message "- Updating '#{File.basename(pod_xcconfig_path)}'"
update_pod_xcconfig_for_mangling!(pod_xcconfig_path)
end
Expand Down
91 changes: 51 additions & 40 deletions lib/cocoapods_mangle/context.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
module CocoapodsMangle
# Context for mangling
class Context
# @!attribute xcconfig_path
# @return [String] The path to the mangle xcconfig
attr_accessor :xcconfig_path
# @!attribute mangle_prefix
# @return [String] The mangle prefix to be used
attr_accessor :mangle_prefix
# @!attribute pods_project
# @return [Pod::Project] The Pods Xcode project
attr_accessor :pods_project
# @!attribute umbrella_pod_targets
# @return [Array<Pod::Installer::PostInstallHooksContext::UmbrellaTargetDescription>]
# The umbrella targets to be mangled
attr_accessor :umbrella_pod_targets

# Initializes the context for mangling
# @param [Pod::Installer::PostInstallHooksContext] installer_context
# The post install context
Expand All @@ -26,48 +12,73 @@ class Context
# @option options [Array<String>] :targets
# The user targets whose dependencies should be mangled
def initialize(installer_context, options)
@xcconfig_path = build_xcconfig_path(installer_context, options[:xcconfig_path])
@pods_project = installer_context.pods_project
@umbrella_pod_targets = build_umbrella_pod_targets(installer_context, options[:targets])
@mangle_prefix = build_mangle_prefix(@umbrella_pod_targets, options[:mangle_prefix])
@installer_context = installer_context
@options = options
end

# @return [String] The path to the mangle xcconfig
def xcconfig_path
return default_xcconfig_path unless @options[:xcconfig_path]
File.join(@installer_context.sandbox.root.parent, @options[:xcconfig_path])
end

# @return [String] The mangle prefix to be used
def mangle_prefix
return default_mangle_prefix unless @options[:mangle_prefix]
@options[:mangle_prefix]
end

# @return [String] The path to pods project
def pods_project_path
@installer_context.pods_project.path
end

# @return [Array<String>] The targets in the pods project to be mangled
def pod_target_labels
umbrella_pod_targets.map(&:cocoapods_target_label)
end

# @return [Array<String>] Paths to all pod xcconfig files which should be updated
def pod_xcconfig_paths
pod_xcconfigs = []
@installer_context.pods_project.targets.each do |target|
target.build_configurations.each do |config|
pod_xcconfigs << config.base_configuration_reference.real_path
end
end
pod_xcconfigs.uniq
end

# @return [String] A checksum representing the current state of the target dependencies
def specs_checksum
gem_summary = "#{CocoapodsMangle::NAME}=#{CocoapodsMangle::VERSION}"
specs = @umbrella_pod_targets.map(&:specs).flatten.uniq
specs = umbrella_pod_targets.map(&:specs).flatten.uniq
specs_summary = specs.map(&:checksum).join(',')
Digest::SHA1.hexdigest("#{gem_summary},#{specs_summary}")
end

private

def build_xcconfig_path(installer_context, user_xcconfig_path)
unless user_xcconfig_path
xcconfig_dir = installer_context.sandbox.target_support_files_root
xcconfig_filename = "#{CocoapodsMangle::NAME}.xcconfig"
return File.join(xcconfig_dir, xcconfig_filename)
def umbrella_pod_targets
if @options[:targets].nil? || @options[:targets].empty?
return @installer_context.umbrella_targets
end
File.join(installer_context.sandbox.root.parent, user_xcconfig_path)
end

def build_umbrella_pod_targets(installer_context, user_targets)
if user_targets.nil? || user_targets.empty?
return installer_context.umbrella_targets
end
installer_context.umbrella_targets.reject do |target|
@installer_context.umbrella_targets.reject do |target|
target_names = target.user_targets.map(&:name)
(user_targets & target_names).empty?
(@options[:targets] & target_names).empty?
end
end

def build_mangle_prefix(umbrella_pod_targets, user_mangle_prefix)
unless user_mangle_prefix
project_path = umbrella_pod_targets.first.user_project.path
project_name = File.basename(project_path, '.xcodeproj')
return project_name.tr(' ', '_') + '_'
end
user_mangle_prefix
def default_xcconfig_path
xcconfig_dir = @installer_context.sandbox.target_support_files_root
xcconfig_filename = "#{CocoapodsMangle::NAME}.xcconfig"
File.join(xcconfig_dir, xcconfig_filename)
end

def default_mangle_prefix
project_path = umbrella_pod_targets.first.user_project.path
project_name = File.basename(project_path, '.xcodeproj')
project_name.tr(' ', '_') + '_'
end
end
end
1 change: 0 additions & 1 deletion lib/cocoapods_mangle/post_install.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'digest'
require 'cocoapods'
require 'cocoapods_mangle/config'

Expand Down
28 changes: 9 additions & 19 deletions spec/unit/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
require 'cocoapods_mangle/config'

describe CocoapodsMangle::Config do
let(:context) do
let(:context) do
instance_double('Mangle context',
xcconfig_path: 'path/to/mangle.xcconfig',
umbrella_pod_targets: [instance_double('target', cocoapods_target_label: 'Pods-A')],
pods_project: double('pods project'),
pod_target_labels: ['Pods-A'],
pods_project_path: 'path/to/Pods.xcodeproj',
mangle_prefix: 'prefix_',
specs_checksum: 'checksum')
specs_checksum: 'checksum',
pod_xcconfig_paths: ['path/to/A.xcconfig', 'path/to/B.xcconfig'])
end
let(:subject) do
CocoapodsMangle::Config.new(context)
Expand All @@ -21,7 +22,7 @@
let(:builder) { double('builder') }

before do
allow(CocoapodsMangle::Builder).to receive(:new).with(context.pods_project, context.umbrella_pod_targets).and_return(builder)
allow(CocoapodsMangle::Builder).to receive(:new).with(context.pods_project_path, context.pod_target_labels).and_return(builder)
allow(builder).to receive(:build!)
allow(builder).to receive(:binaries_to_mangle).and_return(binaries_to_mangle)
allow(CocoapodsMangle::Defines).to receive(:mangling_defines).with(context.mangle_prefix, binaries_to_mangle).and_return(mangling_defines)
Expand Down Expand Up @@ -88,21 +89,10 @@
end

context '.update_pod_xcconfigs_for_mangling!' do
let(:pod_target) { double('target') }
let(:debug_build_configuration) { double('debug') }
let(:release_build_configuration) { double('release') }

before do
allow(context.pods_project).to receive(:targets).and_return([pod_target])
build_configurations = [debug_build_configuration, release_build_configuration]
allow(pod_target).to receive(:build_configurations).and_return(build_configurations)
build_configurations.each do |config|
allow(config).to receive_message_chain(:base_configuration_reference, :real_path).and_return('pod.xcconfig')
end
end

it 'updates each unique config' do
expect(subject).to receive(:update_pod_xcconfig_for_mangling!).with('pod.xcconfig').once
context.pod_xcconfig_paths.each do |pod_xcconfig|
expect(subject).to receive(:update_pod_xcconfig_for_mangling!).with(pod_xcconfig)
end
subject.update_pod_xcconfigs_for_mangling!
end
end
Expand Down
Loading

0 comments on commit 26357fe

Please sign in to comment.