Commit 396effc4 authored by Fabio Pelosin's avatar Fabio Pelosin

[Installer] Added pre install hooks.

They are called after the pods have been downloaded but before the targets are
integrated in the Pods.xcodeproj.

closes #486
parent 838376a2
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
###### Enhancements ###### Enhancements
- Added a pre install hook to the Podfile and to root specifications.
[#486](https://github.com/CocoaPods/CocoaPods/issues/486)
- Support for `header_mappings_dir` attribute in subspecs. - Support for `header_mappings_dir` attribute in subspecs.
- Refactored UI. - Refactored UI.
......
...@@ -121,6 +121,9 @@ module Pod ...@@ -121,6 +121,9 @@ module Pod
end end
ui_title("Generating support files", '', 2) do ui_title("Generating support files", '', 2) do
ui_message "- Running pre install hooks" do
run_pre_install_hooks
end
ui_message("- Installing targets", '', 2) do ui_message("- Installing targets", '', 2) do
generate_target_support_files generate_target_support_files
...@@ -144,6 +147,15 @@ module Pod ...@@ -144,6 +147,15 @@ module Pod
end end
end end
def run_pre_install_hooks
pods_by_target.each do |target_definition, pods|
pods.each do |pod|
pod.top_specification.pre_install(pod, target_definition)
end
end
@podfile.pre_install!(self)
end
def run_post_install_hooks def run_post_install_hooks
# we loop over target installers instead of pods, because we yield the target installer # we loop over target installers instead of pods, because we yield the target installer
# to the spec post install hook. # to the spec post install hook.
......
...@@ -472,6 +472,17 @@ module Pod ...@@ -472,6 +472,17 @@ module Pod
@target_definition = parent @target_definition = parent
end end
# This hook allows you to make any changes to the downloaded Pods and to
# their targets before they are installed.
#
# pre_install do |installer|
# # Do something fancy!
# end
#
def pre_install(&block)
@pre_install_callback = block
end
# This hook allows you to make any last changes to the generated Xcode project # This hook allows you to make any last changes to the generated Xcode project
# before it is written to disk, or any other tasks you might want to perform. # before it is written to disk, or any other tasks you might want to perform.
# #
...@@ -532,6 +543,10 @@ module Pod ...@@ -532,6 +543,10 @@ module Pod
configs_array.inject({}) { |hash, config| hash.merge(config) } configs_array.inject({}) { |hash, config| hash.merge(config) }
end end
def pre_install!(installer)
@pre_install_callback.call(installer) if @pre_install_callback
end
def post_install!(installer) def post_install!(installer)
@post_install_callback.call(installer) if @post_install_callback @post_install_callback.call(installer) if @post_install_callback
end end
......
...@@ -429,20 +429,36 @@ module Pod ...@@ -429,20 +429,36 @@ module Pod
header_mappings_dir ? from.relative_path_from(header_mappings_dir) : from.basename header_mappings_dir ? from.relative_path_from(header_mappings_dir) : from.basename
end end
# This is a convenience method which gets called after all pods have been
# downloaded but before they have been installed, and the Xcode project and
# related files have been generated. (It receives the Pod::LocalPod
# instance generated form the specification and the #
# Pod::Podfile::TargetDefinition instance for the current target.) Override
# this to, for instance, to run any build script:
#
# Pod::Spec.new do |s|
# def pre_install(pod, target_definition)
# Dir.chdir(pod.root){ `sh make.sh` }
# end
# end
def pre_install(pod, target_definition)
end
# This is a convenience method which gets called after all pods have been # This is a convenience method which gets called after all pods have been
# downloaded, installed, and the Xcode project and related files have been # downloaded, installed, and the Xcode project and related files have been
# generated. (It receives the Pod::Installer::Target instance for the current # generated. (It receives the Pod::Installer::TargetInstaller instance for
# target.) Override this to, for instance, add to the prefix header: # the current target.) Override this to, for instance, add to the prefix
# header:
# #
# Pod::Spec.new do |s| # Pod::Spec.new do |s|
# def s.post_install(target) # def s.post_install(target_installer)
# prefix_header = config.project_pods_root + target.prefix_header_filename # prefix_header = config.project_pods_root + target_installer.prefix_header_filename
# prefix_header.open('a') do |file| # prefix_header.open('a') do |file|
# file.puts(%{#ifdef __OBJC__\n#import "SSToolkitDefines.h"\n#endif}) # file.puts(%{#ifdef __OBJC__\n#import "SSToolkitDefines.h"\n#endif})
# end # end
# end # end
# end # end
def post_install(target) def post_install(target_installer)
end end
def podfile? def podfile?
......
...@@ -249,6 +249,22 @@ else ...@@ -249,6 +249,22 @@ else
FileUtils.cp_r(fixture('integration/.'), config.project_pods_root) FileUtils.cp_r(fixture('integration/.'), config.project_pods_root)
end end
it "runs the optional pre_install callback defined in the Podfile _before_ the targets are integrated but _after_ the pods have been downloaded" do
podfile = Pod::Podfile.new do
self.platform platform
xcodeproj 'dummy'
pod 'SSZipArchive', '0.1.0'
pre_install do |installer|
memo = "PODS:#{installer.pods * ', '} TARGETS:#{installer.project.targets.to_a * ', '}"
File.open(config.project_pods_root + 'memo.txt', 'w') {|f| f.puts memo}
end
end
resolver = Pod::Resolver.new(podfile, nil, Pod::Sandbox.new(config.project_pods_root))
SpecHelper::Installer.new(resolver).install!
File.open(config.project_pods_root + 'memo.txt','rb').read.should == "PODS:SSZipArchive (0.1.0) TARGETS:\n"
end
it "runs the optional post_install callback defined in the Podfile _before_ the project is saved to disk" do it "runs the optional post_install callback defined in the Podfile _before_ the project is saved to disk" do
podfile = Pod::Podfile.new do podfile = Pod::Podfile.new do
self.platform platform self.platform platform
......
...@@ -79,6 +79,16 @@ describe "Pod::Podfile" do ...@@ -79,6 +79,16 @@ describe "Pod::Podfile" do
Pod::Podfile.new { set_arc_compatibility_flag! }.should.set_arc_compatibility_flag Pod::Podfile.new { set_arc_compatibility_flag! }.should.set_arc_compatibility_flag
end end
it "stores a block that will be called with the Installer before the target integration" do
yielded = nil
Pod::Podfile.new do
pre_install do |installer|
yielded = installer
end
end.pre_install!(:an_installer)
yielded.should == :an_installer
end
it "stores a block that will be called with the Installer instance once installation is finished (but the project is not written to disk yet)" do it "stores a block that will be called with the Installer instance once installation is finished (but the project is not written to disk yet)" do
yielded = nil yielded = nil
Pod::Podfile.new do Pod::Podfile.new do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment