Commit 9577c6d3 authored by Fabio Pelosin's avatar Fabio Pelosin

[Config] Detect Podfile in parent dirs

Closes #940
parent 7885605e
...@@ -38,6 +38,10 @@ To install or update CocoaPods see this [guide](http://docs.cocoapods.org/guides ...@@ -38,6 +38,10 @@ To install or update CocoaPods see this [guide](http://docs.cocoapods.org/guides
* The naked `pod` command now defaults to `pod install`. * The naked `pod` command now defaults to `pod install`.
[#958](https://github.com/CocoaPods/CocoaPods/issues/958) [#958](https://github.com/CocoaPods/CocoaPods/issues/958)
* CocoaPods will look for the Podfile in the ancestors paths if one is
not available in the working directory.
[#940](https://github.com/CocoaPods/CocoaPods/issues/940)
* Documentation generation has been removed from CocoaPods as it graduated * Documentation generation has been removed from CocoaPods as it graduated
to CocoaDocs. This decision was taken because CocoaDocs is a much better to CocoaDocs. This decision was taken because CocoaDocs is a much better
solution which doesn't clutter Xcode's docsets while still allowing solution which doesn't clutter Xcode's docsets while still allowing
......
...@@ -24,7 +24,9 @@ module Pod ...@@ -24,7 +24,9 @@ module Pod
:new_version_message => true, :new_version_message => true,
} }
#--------------------------------------# public
#-------------------------------------------------------------------------#
# @!group UI # @!group UI
...@@ -45,7 +47,7 @@ module Pod ...@@ -45,7 +47,7 @@ module Pod
attr_accessor :new_version_message attr_accessor :new_version_message
alias_method :new_version_message?, :new_version_message alias_method :new_version_message?, :new_version_message
#--------------------------------------# #-------------------------------------------------------------------------#
# @!group Installation # @!group Installation
...@@ -81,7 +83,9 @@ module Pod ...@@ -81,7 +83,9 @@ module Pod
@aggressive_cache || (ENV['CP_AGGRESSIVE_CACHE'] != 'FALSE') @aggressive_cache || (ENV['CP_AGGRESSIVE_CACHE'] != 'FALSE')
end end
#--------------------------------------# public
#-------------------------------------------------------------------------#
# @!group Initialization # @!group Initialization
...@@ -99,7 +103,9 @@ module Pod ...@@ -99,7 +103,9 @@ module Pod
@verbose && !silent @verbose && !silent
end end
#--------------------------------------# public
#-------------------------------------------------------------------------#
# @!group Paths # @!group Paths
...@@ -115,7 +121,21 @@ module Pod ...@@ -115,7 +121,21 @@ module Pod
# Podfile is located. # Podfile is located.
# #
def installation_root def installation_root
@installation_root ||= Pathname.pwd current_path = Pathname.pwd
unless @installation_root
while(!current_path.root?)
if podfile_path_in_dir(current_path)
@installation_root = current_path
unless current_path == Pathname.pwd
UI.puts ("[in #{current_path}]")
end
break
else
current_path = current_path.parent
end
end
end
@installation_root
end end
attr_writer :installation_root attr_writer :installation_root
...@@ -137,23 +157,46 @@ module Pod ...@@ -137,23 +157,46 @@ module Pod
end end
# @return [Podfile] The Podfile to use for the current execution. # @return [Podfile] The Podfile to use for the current execution.
# @return [Nil] If no Podfile is available.
# #
def podfile def podfile
@podfile ||= Podfile.from_file(podfile_path) if podfile_path.exist? @podfile ||= Podfile.from_file(podfile_path) if podfile_path
end end
attr_writer :podfile attr_writer :podfile
# @return [Lockfile] The Lockfile to use for the current execution. # @return [Lockfile] The Lockfile to use for the current execution.
# @return [Nil] If no Lockfile is available.
# #
def lockfile def lockfile
@lockfile ||= Lockfile.from_file(lockfile_path) if lockfile_path.exist? @lockfile ||= Lockfile.from_file(lockfile_path) if lockfile_path
end
# Returns the path of the Podfile.
#
# @note The Podfile can be named either `CocoaPods.podfile.yaml`,
# `CocoaPods.podfile` or `Podfile`. The first two are preferred as
# they allow to specify an OS X UTI.
#
# @return [Pathname]
# @return [Nil]
#
def podfile_path
@podfile_path ||= podfile_path_in_dir(installation_root) if installation_root
end
# Returns the path of the Lockfile.
#
# @note The Lockfile is named `Podfile.lock`.
#
def lockfile_path
@lockfile_path ||= installation_root + 'Podfile.lock'
end end
#--------------------------------------# private
# @!group Helpers #-------------------------------------------------------------------------#
# private # @!group Private helpers
# @return [Pathname] The path of the file which contains the user settings. # @return [Pathname] The path of the file which contains the user settings.
# #
...@@ -175,30 +218,38 @@ module Pod ...@@ -175,30 +218,38 @@ module Pod
end end
end end
# Returns the path of the Podfile. # @return [Array<String>] The filenames that the Podfile can have ordered
# by priority.
# #
# @note The Podfile can be named either `CocoaPods.podfile` or `Podfile`. PODFILE_NAMES = [
# The first is preferred as it allows to specify an OS X UTI. 'CocoaPods.podfile.yaml',
# 'CocoaPods.podfile',
def podfile_path 'Podfile',
unless @podfile_path ]
path = installation_root + 'CocoaPods.podfile.yaml'
path = installation_root + 'CocoaPods.podfile' unless path.exist?
path = installation_root + 'Podfile' unless path.exist?
@podfile_path = path
end
@podfile_path
end
# Returns the path of the Lockfile. # Returns the path of the Podfile in the given dir if any exists.
# #
# @note The Lockfile is named `Podfile.lock`. # @param [Pathname] dir
# The directory where to look for the Podfile.
# #
def lockfile_path # @return [Pathname] The path of the Podfile.
@lockfile_path ||= installation_root + 'Podfile.lock' # @return [Nil] If not Podfile was found in the given dir
#
def podfile_path_in_dir(dir)
PODFILE_NAMES.each do |filename|
candidate = dir + filename
if candidate.exist?
return candidate
end
end
nil
end end
#--------------------------------------# public
#-------------------------------------------------------------------------#
# @!group Singleton
# @return [Config] the current config instance creating one if needed. # @return [Config] the current config instance creating one if needed.
# #
...@@ -217,8 +268,6 @@ module Pod ...@@ -217,8 +268,6 @@ module Pod
@instance = instance @instance = instance
end end
#-------------------------------------------------------------------------#
# Provides support for accessing the configuration instance in other # Provides support for accessing the configuration instance in other
# scopes. # scopes.
# #
......
...@@ -280,7 +280,7 @@ module Pod ...@@ -280,7 +280,7 @@ module Pod
def prepare_pods_project def prepare_pods_project
UI.message "- Creating Pods project" do UI.message "- Creating Pods project" do
@pods_project = Pod::Project.new(sandbox.project_path) @pods_project = Pod::Project.new(sandbox.project_path)
if config.podfile_path.exist? if config.podfile_path
@pods_project.add_podfile(config.podfile_path) @pods_project.add_podfile(config.podfile_path)
end end
sandbox.project = @pods_project sandbox.project = @pods_project
......
...@@ -148,7 +148,7 @@ module Pod ...@@ -148,7 +148,7 @@ module Pod
# #
def path(pathname) def path(pathname)
if pathname if pathname
path = pathname.relative_path_from(config.podfile_path.dirname || Pathname.pwd) path = pathname.relative_path_from((config.podfile_path.dirname if config.podfile_path) || Pathname.pwd)
"`#{path}`" "`#{path}`"
else else
'' ''
......
...@@ -7,6 +7,8 @@ module Pod ...@@ -7,6 +7,8 @@ module Pod
Config.instance = nil Config.instance = nil
end end
#-------------------------------------------------------------------------#
describe "In general" do describe "In general" do
it "returns the singleton config instance" do it "returns the singleton config instance" do
...@@ -35,7 +37,33 @@ module Pod ...@@ -35,7 +37,33 @@ module Pod
end end
end end
describe "Concerning a user's project, which is expected in the current working directory" do #-------------------------------------------------------------------------#
describe "Paths" do
it "returns the working directory as the installation root if a Podfile can be found" do
Dir.chdir(temporary_directory) do
File.open("Podfile", "w") {}
config.installation_root.should == temporary_directory
end
end
it "returns the parent directory which contains the Podfile if it can be found" do
Dir.chdir(temporary_directory) do
File.open("Podfile", "w") {}
sub_dir = temporary_directory + 'sub_dir'
sub_dir.mkpath
Dir.chdir(sub_dir) do
config.installation_root.should == temporary_directory
end
end
end
it "it returns a nil installation root if no Podfile can be found" do
Dir.chdir(temporary_directory) do
config.installation_root.should == nil
end
end
before do before do
config.installation_root = temporary_directory config.installation_root = temporary_directory
...@@ -63,9 +91,33 @@ module Pod ...@@ -63,9 +91,33 @@ module Pod
it "returns the path to the Pods directory that holds the dependencies" do it "returns the path to the Pods directory that holds the dependencies" do
config.sandbox_root.should == temporary_directory + 'Pods' config.sandbox_root.should == temporary_directory + 'Pods'
end end
it "returns the Podfile path" do
Dir.chdir(temporary_directory) do
File.open("Podfile", "w") {}
config.podfile_path.should == temporary_directory + "Podfile"
end
end end
describe "Concerning default settings" do it "returns nils if the Podfile if no paths exists" do
Dir.chdir(temporary_directory) do
config.podfile_path.should == nil
end
end
it "returns the Lockfile path" do
Dir.chdir(temporary_directory) do
File.open("Podfile", "w") {}
File.open("Podfile.lock", "w") {}
config.lockfile_path.should == temporary_directory + "Podfile.lock"
end
end
end
#-------------------------------------------------------------------------#
describe "Default settings" do
before do before do
Config.any_instance.stubs(:user_settings_file).returns(Pathname.new('not_found')) Config.any_instance.stubs(:user_settings_file).returns(Pathname.new('not_found'))
...@@ -87,13 +139,49 @@ module Pod ...@@ -87,13 +139,49 @@ module Pod
describe "Private helpers" do describe "Private helpers" do
it "returns the path of the user settings file" do it "returns the path of the user settings file" do
config.user_settings_file.should == Pathname.new("~/.cocoapods/config.yaml").expand_path config.send(:user_settings_file).should == Pathname.new("~/.cocoapods/config.yaml").expand_path
end end
it "returns the path of the user settings file" do it "can be configured with a hash" do
config.user_settings_file.should == Pathname.new("~/.cocoapods/config.yaml").expand_path hash = { :verbose => true }
config.send(:configure_with, hash)
config.should.be.verbose
end
#----------------------------------------#
describe "#podfile_path_in_dir" do
it "detects the CocoaPods.podfile.yaml file" do
expected = temporary_directory + "CocoaPods.podfile.yaml"
File.open(expected, "w") {}
path = config.send(:podfile_path_in_dir, temporary_directory)
path.should == expected
end end
it "detects the CocoaPods.podfile file" do
expected = temporary_directory + "CocoaPods.podfile"
File.open(expected, "w") {}
path = config.send(:podfile_path_in_dir, temporary_directory)
path.should == expected
end
it "detects the Podfile file" do
expected = temporary_directory + "Podfile"
File.open(expected, "w") {}
path = config.send(:podfile_path_in_dir, temporary_directory)
path.should == expected
end
it "returns nils if the Podfile is not available" do
path = config.send(:podfile_path_in_dir, temporary_directory)
path.should == nil
end
end
#-----------------------------------------------------------------------#
end end
end end
end end
...@@ -218,7 +218,7 @@ module Pod ...@@ -218,7 +218,7 @@ module Pod
it "adds the Podfile to the Pods project" do it "adds the Podfile to the Pods project" do
@installer.stubs(:libraries).returns([]) @installer.stubs(:libraries).returns([])
config.podfile_path.stubs(:exist?).returns(true) config.stubs(:podfile_path).returns(Pathname.new('/Podfile'))
@installer.send(:prepare_pods_project) @installer.send(:prepare_pods_project)
f = @installer.pods_project['Podfile'] f = @installer.pods_project['Podfile']
f.name.should == 'Podfile' f.name.should == 'Podfile'
......
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