Commit a0a26baf authored by Orta Therox's avatar Orta Therox

[Installer] Adds support for a pre-installer hook

parent aa11287e
......@@ -13,6 +13,13 @@ To install release candidates run `[sudo] gem install cocoapods --pre`
[Samuel Giddins](https://github.com/segiddins)
[cocoapods-try#31](https://github.com/CocoaPods/cocoapods-try/issues/31)
* Supports running pre-install hooks in plugins. This happens before the resolver
does its work, and offers easy access to the sandbox, podfile and lockfile via a
PreInstallHooksContext object. This also renames the post-install hooks from HooksContext
to PostInstallHooksContext.
[Orta Therox](https://github.com/orta)
[cocoapods#3540](https://github.com/CocoaPods/cocoapods/issues/3409)
## 0.37.1
......
......@@ -31,7 +31,8 @@ module Pod
autoload :AggregateTargetInstaller, 'cocoapods/installer/target_installer/aggregate_target_installer'
autoload :Analyzer, 'cocoapods/installer/analyzer'
autoload :FileReferencesInstaller, 'cocoapods/installer/file_references_installer'
autoload :HooksContext, 'cocoapods/installer/hooks_context'
autoload :PostInstallHooksContext, 'cocoapods/installer/post_install_hooks_context'
autoload :PreInstallHooksContext, 'cocoapods/installer/pre_install_hooks_context'
autoload :Migrator, 'cocoapods/installer/migrator'
autoload :PodSourceInstaller, 'cocoapods/installer/pod_source_installer'
autoload :PodSourcePreparer, 'cocoapods/installer/pod_source_preparer'
......@@ -104,6 +105,7 @@ module Pod
sandbox.prepare
ensure_plugins_are_installed!
Migrator.migrate(sandbox)
run_plugins_pre_install_hooks
end
end
......@@ -120,7 +122,7 @@ module Pod
UI.section 'Downloading dependencies' do
create_file_accessors
install_pod_sources
run_pre_install_hooks
run_podfile_pre_install_hooks
clean_pod_sources
lock_pod_sources
end
......@@ -397,7 +399,16 @@ module Pod
end
end
end
# Runs the registered callbacks for the plugins pre install hooks.
#
# @return [void]
#
def run_plugins_pre_install_hooks
context = PreInstallHooksContext.generate(sandbox, podfile, lockfile)
HooksManager.run(:pre_install, context, podfile.plugins)
end
# Performs any post-installation actions
#
# @return [void]
......@@ -410,7 +421,7 @@ module Pod
# Runs the registered callbacks for the plugins post install hooks.
#
def run_plugins_post_install_hooks
context = HooksContext.generate(sandbox, aggregate_targets)
context = PostInstallHooksContext.generate(sandbox, aggregate_targets)
HooksManager.run(:post_install, context, podfile.plugins)
end
......@@ -652,7 +663,7 @@ module Pod
#
# @return [void]
#
def run_pre_install_hooks
def run_podfile_pre_install_hooks
UI.message '- Running pre install hooks' do
executed = run_podfile_pre_install_hook
UI.message '- Podfile' if executed
......
......@@ -3,7 +3,7 @@ module Pod
# Context object designed to be used with the HooksManager which describes
# the context of the installer.
#
class HooksContext
class PostInstallHooksContext
# @return [String] The path to the sandbox root (`Pods` directory).
#
attr_accessor :sandbox_root
......@@ -13,7 +13,7 @@ module Pod
#
attr_accessor :umbrella_targets
# @return [HooksContext] Convenience class method to generate the
# @return [PostInstallHooksContext] Convenience class method to generate the
# static context.
#
def self.generate(sandbox, aggregate_targets)
......
module Pod
class Installer
# Context object designed to be used with the HooksManager which describes
# the context of the installer before analysis has been completed.
#
class PreInstallHooksContext
# @return [String] The path to the sandbox root (`Pods` directory).
#
attr_accessor :sandbox_root
# @return [Podfile] The Podfile for the project.
#
attr_accessor :podfile
# @return [Sandbox] The Sandbox for the project.
#
attr_accessor :sandbox
# @return [Lockfile] The Lockfile for the project.
#
attr_accessor :lockfile
# @return [PreInstallHooksContext] Convenience class method to generate the
# static context.
#
def self.generate(sandbox, podfile, lockfile)
result = new
result.podfile = podfile
result.sandbox = sandbox
result.lockfile = lockfile
result
end
end
end
end
require File.expand_path('../../../spec_helper', __FILE__)
module Pod
describe Installer::PreInstallHooksContext do
it 'offers a convenience method to be generated' do
sandbox, podfile, lockfile = stub, stub, stub
result = Installer::PreInstallHooksContext.generate(sandbox, podfile, lockfile)
result.class.should == Installer::PreInstallHooksContext
result.sandbox.should == sandbox
result.podfile.should == podfile
result.lockfile.should == lockfile
end
end
end
......@@ -2,7 +2,7 @@
require File.expand_path('../../../spec_helper', __FILE__)
module Pod
describe Installer::HooksContext do
describe Installer::PostInstallHooksContext do
it 'offers a convenience method to be generated' do
sandbox = stub(:root => '/path')
......@@ -15,8 +15,8 @@ module Pod
umbrella.stubs(:platform).returns(Platform.new(:ios, '8.0'))
umbrella.pod_targets = [pod_target]
result = Installer::HooksContext.generate(sandbox, [umbrella])
result.class.should == Installer::HooksContext
result = Installer::PostInstallHooksContext.generate(sandbox, [umbrella])
result.class.should == Installer::PostInstallHooksContext
result.sandbox_root.should == '/path'
result.umbrella_targets.count.should == 1
umbrella_target = result.umbrella_targets.first
......
......@@ -63,7 +63,7 @@ module Pod
@installer.unstub(:download_dependencies)
@installer.stubs(:create_file_accessors)
@installer.stubs(:install_pod_sources)
def @installer.run_pre_install_hooks
def @installer.run_podfile_pre_install_hooks
@hook_called = true
end
def @installer.clean_pod_sources
......@@ -74,7 +74,7 @@ module Pod
it 'in runs the post-install hooks before serializing the Pods project' do
@installer.stubs(:prepare_pods_project)
@installer.stubs(:run_pre_install_hooks)
@installer.stubs(:run_podfile_pre_install_hooks)
@installer.stubs(:install_file_references)
@installer.stubs(:install_libraries)
@installer.stubs(:set_target_dependencies)
......@@ -654,16 +654,23 @@ module Pod
@installer.stubs(:installed_specs).returns(@specs)
end
it 'runs plugins pre install hook' do
context = stub
Installer::PreInstallHooksContext.expects(:generate).returns(context)
HooksManager.expects(:run).with(:pre_install, context, {})
@installer.send(:run_plugins_pre_install_hooks)
end
it 'runs plugins post install hook' do
context = stub
Installer::HooksContext.expects(:generate).returns(context)
Installer::PostInstallHooksContext.expects(:generate).returns(context)
HooksManager.expects(:run).with(:post_install, context, {})
@installer.send(:run_plugins_post_install_hooks)
end
it 'only runs the podfile-specified post-install hooks' do
it 'only runs the podfile-specified hooks' do
context = stub
Installer::HooksContext.expects(:generate).returns(context)
Installer::PostInstallHooksContext.expects(:generate).returns(context)
plugins_hash = { 'cocoapods-keys' => { 'keyring' => 'Eidolon' } }
@installer.podfile.stubs(:plugins).returns(plugins_hash)
HooksManager.expects(:run).with(:post_install, context, plugins_hash)
......@@ -691,7 +698,7 @@ module Pod
#-------------------------------------------------------------------------#
describe 'Hooks' do
describe 'Podfile Hooks' do
before do
@installer.send(:analyze)
@specs = @installer.pod_targets.map(&:specs).flatten
......@@ -705,7 +712,7 @@ module Pod
@installer.expects(:installer_rep).returns(installer_rep)
@installer.podfile.expects(:pre_install!).with(installer_rep)
@installer.send(:run_pre_install_hooks)
@installer.send(:run_podfile_pre_install_hooks)
end
it 'run_podfile_post_install_hooks' do
......@@ -725,7 +732,7 @@ module Pod
@installer.stubs(:pod_targets).returns([pod_target_ios, pod_target_osx])
@installer.stubs(:installer_rep).returns(stub)
@installer.podfile.expects(:pre_install!)
@installer.send(:run_pre_install_hooks)
@installer.send(:run_podfile_pre_install_hooks)
@installer.send(:run_podfile_post_install_hooks)
end
......
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