Commit 4359e82c authored by Luke Redpath's avatar Luke Redpath

Introduced two new classes: LocalPod and Sandbox.

The Sandbox represents the entire contents of the POD_ROOT (normally SOURCE_ROOT/Pods). A LocalPod represents a pod that has been installed within the Sandbox.

These two classes can be used as better homes for various pieces of logic currently spread throughout the installation process and provide a better API for working with the contents of this directory.

So far I've just refactored the downloaders to use them.
parent 4b42a185
...@@ -11,6 +11,7 @@ module Pod ...@@ -11,6 +11,7 @@ module Pod
autoload :Downloader, 'cocoapods/downloader' autoload :Downloader, 'cocoapods/downloader'
autoload :Executable, 'cocoapods/executable' autoload :Executable, 'cocoapods/executable'
autoload :Installer, 'cocoapods/installer' autoload :Installer, 'cocoapods/installer'
autoload :LocalPod, 'cocoapods/local_pod'
autoload :Platform, 'cocoapods/platform' autoload :Platform, 'cocoapods/platform'
autoload :Podfile, 'cocoapods/podfile' autoload :Podfile, 'cocoapods/podfile'
autoload :Resolver, 'cocoapods/resolver' autoload :Resolver, 'cocoapods/resolver'
......
...@@ -6,29 +6,31 @@ module Pod ...@@ -6,29 +6,31 @@ module Pod
extend Executable extend Executable
def self.for_source(pod_root, source) def self.for_pod(pod)
options = source.dup options = pod.specification.source.dup
if url = options.delete(:git) if url = options.delete(:git)
Git.new(pod_root, url, options) Git.new(pod, url, options)
elsif url = options.delete(:hg) elsif url = options.delete(:hg)
Mercurial.new(pod_root, url, options) Mercurial.new(pod, url, options)
elsif url = options.delete(:svn) elsif url = options.delete(:svn)
Subversion.new(pod_root, url, options) Subversion.new(pod, url, options)
else else
raise "Unsupported download strategy `#{source.inspect}'." raise "Unsupported download strategy `#{options.inspect}'."
end end
end end
attr_reader :pod_root, :url, :options attr_reader :pod, :url, :options
def initialize(pod_root, url, options) def initialize(pod, url, options)
@pod_root, @url, @options = pod_root, url, options @pod, @url, @options = pod, url, options
end end
def clean(clean_paths = []) def clean(clean_paths = [])
return unless clean_paths
clean_paths.each do |path| clean_paths.each do |path|
path.rmtree path.rmtree
end if clean_paths end
end end
end end
end end
...@@ -4,10 +4,9 @@ module Pod ...@@ -4,10 +4,9 @@ module Pod
executable :git executable :git
def download def download
@pod_root.dirname.mkpath if options[:tag]
if @options[:tag]
download_tag download_tag
elsif @options[:commit] elsif options[:commit]
download_commit download_commit
else else
download_head download_head
...@@ -15,32 +14,31 @@ module Pod ...@@ -15,32 +14,31 @@ module Pod
end end
def download_head def download_head
git "clone '#{@url}' '#{@pod_root}'" git "clone '#{url}' '#{pod.root}'"
end end
def download_tag def download_tag
@pod_root.mkpath pod.chdir do
Dir.chdir(@pod_root) do
git "init" git "init"
git "remote add origin '#{@url}'" git "remote add origin '#{url}'"
git "fetch origin tags/#{@options[:tag]}" git "fetch origin tags/#{options[:tag]}"
git "reset --hard FETCH_HEAD" git "reset --hard FETCH_HEAD"
git "checkout -b activated-pod-commit" git "checkout -b activated-pod-commit"
end end
end end
def download_commit def download_commit
git "clone '#{@url}' '#{@pod_root}'" git "clone '#{url}' '#{pod.root}'"
Dir.chdir(@pod_root) do
git "checkout -b activated-pod-commit #{@options[:commit]}" pod.chdir do
git "checkout -b activated-pod-commit #{options[:commit]}"
end end
end end
def clean(clean_paths = []) def clean(clean_paths = [])
super super
(@pod_root + '.git').rmtree (pod.root + '.git').rmtree
end end
end end
end end
end end
...@@ -4,8 +4,7 @@ module Pod ...@@ -4,8 +4,7 @@ module Pod
executable :hg executable :hg
def download def download
@pod_root.dirname.mkpath if options[:revision]
if @options[:revision]
download_revision download_revision
else else
download_head download_head
...@@ -13,16 +12,16 @@ module Pod ...@@ -13,16 +12,16 @@ module Pod
end end
def download_head def download_head
hg "clone '#{@url}' '#{@pod_root}'" hg "clone '#{url}' '#{pod.root}'"
end end
def download_revision def download_revision
hg "clone '#{@url}' --rev '#{@options[:revision]}' '#{@pod_root}'" hg "clone '#{url}' --rev '#{options[:revision]}' '#{pod.root}'"
end end
def clean(clean_paths = []) def clean(clean_paths = [])
super super
(@pod_root + '.hg').rmtree (pod.root + '.hg').rmtree
end end
end end
end end
......
...@@ -4,8 +4,7 @@ module Pod ...@@ -4,8 +4,7 @@ module Pod
executable :svn executable :svn
def download def download
@pod_root.dirname.mkpath if options[:revision]
if @options[:revision]
download_revision download_revision
else else
download_head download_head
...@@ -13,16 +12,16 @@ module Pod ...@@ -13,16 +12,16 @@ module Pod
end end
def download_head def download_head
svn "checkout '#{@url}' '#{@pod_root}'" svn "checkout '#{url}' '#{pod_root}'"
end end
def download_revision def download_revision
svn "checkout '#{@url}' -r '#{@options[:revision]}' '#{@pod_root}'" svn "checkout '#{url}' -r '#{options[:revision]}' '#{pod.root}'"
end end
def clean(clean_paths = []) def clean(clean_paths = [])
super super
@pod_root.glob('**/.svn').each(&:rmtree) pod.root.glob('**/.svn').each(&:rmtree)
end end
end end
end end
......
...@@ -20,6 +20,8 @@ module Pod ...@@ -20,6 +20,8 @@ module Pod
include Config::Mixin include Config::Mixin
include Shared include Shared
attr_reader :sandbox
def initialize(podfile) def initialize(podfile)
@podfile = podfile @podfile = podfile
...@@ -63,7 +65,8 @@ module Pod ...@@ -63,7 +65,8 @@ module Pod
else else
puts "Installing #{spec}" unless config.silent? puts "Installing #{spec}" unless config.silent?
spec = spec.part_of_specification if spec.part_of_other_pod? spec = spec.part_of_specification if spec.part_of_other_pod?
downloader = Downloader.for_source(spec.pod_destroot, spec.source) pod = Pod.new(sandbox, spec)
downloader = Downloader.for_pod(pod)
downloader.download downloader.download
# TODO move cleaning into the installer as well # TODO move cleaning into the installer as well
downloader.clean(spec.expanded_clean_paths) if config.clean downloader.clean(spec.expanded_clean_paths) if config.clean
......
module Pod
class LocalPod
attr_reader :specification
def initialize(specification, sandbox)
@specification, @sandbox = specification, sandbox
end
def root
@sandbox.root + specification.name
end
def create
root.mkpath unless root.exist?
end
def chdir(&block)
create
Dir.chdir(root, &block)
end
def implode
root.rmtree if root.exist?
end
end
end
module Pod
class Sandbox
attr_reader :root
def initialize(path)
@root = path
FileUtils.mkdir_p(@root)
end
def implode
@root.rmtree
end
end
end
...@@ -13,7 +13,7 @@ module Pod ...@@ -13,7 +13,7 @@ module Pod
unless path.exist? unless path.exist?
raise Informative, "No podspec exists at path `#{path}'." raise Informative, "No podspec exists at path `#{path}'."
end end
spec = Pod._eval_podspec(path) spec = ::Pod._eval_podspec(path)
spec.defined_in_file = path spec.defined_in_file = path
spec spec
end end
......
...@@ -40,3 +40,14 @@ class Pod::Spec::Set ...@@ -40,3 +40,14 @@ class Pod::Spec::Set
@sets = nil @sets = nil
end end
end end
require 'tmpdir'
def temporary_sandbox
Pod::Sandbox.new(Pathname.new(Dir.mktmpdir))
end
def fixture_spec(name)
file = SpecHelper::Fixture.fixture(name)
Pod::Specification.from_file(file)
end
require File.expand_path('../../spec_helper', __FILE__) require File.expand_path('../../spec_helper', __FILE__)
describe "Pod::Downloader" do describe "Pod::Downloader" do
it "returns a git downloader" do it "returns a git downloader with parsed options" do
downloader = Pod::Downloader.for_source( pod = Pod::LocalPod.new(fixture_spec('banana-lib/BananaLib.podspec'), temporary_sandbox)
'/path/to/pod_root', downloader = Pod::Downloader.for_pod(pod)
:git => 'http://example.local/banana.git', :tag => 'v1.0'
)
downloader.should.be.instance_of Pod::Downloader::Git downloader.should.be.instance_of Pod::Downloader::Git
downloader.pod_root.should == '/path/to/pod_root' downloader.url.should == 'http://banana-corp.local/banana-lib.git'
downloader.url.should == 'http://example.local/banana.git'
downloader.options.should == { :tag => 'v1.0' } downloader.options.should == { :tag => 'v1.0' }
end end
end end
require File.expand_path('../../spec_helper', __FILE__)
describe Pod::LocalPod do
# a LocalPod represents a local copy of the dependency, inside the pod root, built from a spec
before do
@spec = Pod::Specification.from_file(fixture('banana-lib/BananaLib.podspec'))
@sandbox = temporary_sandbox
@pod = Pod::LocalPod.new(@spec, @sandbox)
end
it 'returns the Pod root directory path' do
@pod.root.should == @sandbox.root + 'BananaLib'
end
it "creates it's own root directory if it doesn't exist" do
@pod.create
File.directory?(@pod.root).should.be.true
end
it "can execute a block within the context of it's root" do
@pod.chdir { FileUtils.touch("foo") }
Pathname(@pod.root + "foo").should.exist
end
it 'can delete itself' do
@pod.create
@pod.implode
@pod.root.should.not.exist
end
end
require File.expand_path('../../spec_helper', __FILE__)
require 'tmpdir'
TMP_POD_ROOT = ROOT + "tmp" + "podroot"
describe Pod::Sandbox do
before do
@sandbox = Pod::Sandbox.new(TMP_POD_ROOT)
end
after do
@sandbox.implode
end
it "automatically creates the TMP_POD_ROOT if it doesn't exist" do
File.directory?(TMP_POD_ROOT).should.be.true
end
it "deletes the entire root directory on implode" do
@sandbox.implode
File.directory?(TMP_POD_ROOT).should.be.false
FileUtils.mkdir(TMP_POD_ROOT) # put it back again
end
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