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
* The naked `pod` command now defaults to `pod install`.
[#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
to CocoaDocs. This decision was taken because CocoaDocs is a much better
solution which doesn't clutter Xcode's docsets while still allowing
......
......@@ -24,7 +24,9 @@ module Pod
:new_version_message => true,
}
#--------------------------------------#
public
#-------------------------------------------------------------------------#
# @!group UI
......@@ -39,13 +41,13 @@ module Pod
attr_accessor :silent
alias_method :silent?, :silent
# @return [Bool] Whether a message should be printed when a new version of
# @return [Bool] Whether a message should be printed when a new version of
# CocoaPods is available.
#
attr_accessor :new_version_message
alias_method :new_version_message?, :new_version_message
#--------------------------------------#
#-------------------------------------------------------------------------#
# @!group Installation
......@@ -81,7 +83,9 @@ module Pod
@aggressive_cache || (ENV['CP_AGGRESSIVE_CACHE'] != 'FALSE')
end
#--------------------------------------#
public
#-------------------------------------------------------------------------#
# @!group Initialization
......@@ -99,7 +103,9 @@ module Pod
@verbose && !silent
end
#--------------------------------------#
public
#-------------------------------------------------------------------------#
# @!group Paths
......@@ -115,7 +121,21 @@ module Pod
# Podfile is located.
#
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
attr_writer :installation_root
......@@ -137,23 +157,46 @@ module Pod
end
# @return [Podfile] The Podfile to use for the current execution.
# @return [Nil] If no Podfile is available.
#
def podfile
@podfile ||= Podfile.from_file(podfile_path) if podfile_path.exist?
@podfile ||= Podfile.from_file(podfile_path) if podfile_path
end
attr_writer :podfile
# @return [Lockfile] The Lockfile to use for the current execution.
# @return [Nil] If no Lockfile is available.
#
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
#--------------------------------------#
private
# @!group Helpers
#-------------------------------------------------------------------------#
# private
# @!group Private helpers
# @return [Pathname] The path of the file which contains the user settings.
#
......@@ -175,30 +218,38 @@ module Pod
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`.
# The first is preferred as it allows to specify an OS X UTI.
#
def podfile_path
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
PODFILE_NAMES = [
'CocoaPods.podfile.yaml',
'CocoaPods.podfile',
'Podfile',
]
# 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
@lockfile_path ||= installation_root + 'Podfile.lock'
# @return [Pathname] The path of the Podfile.
# @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
#--------------------------------------#
public
#-------------------------------------------------------------------------#
# @!group Singleton
# @return [Config] the current config instance creating one if needed.
#
......@@ -217,8 +268,6 @@ module Pod
@instance = instance
end
#-------------------------------------------------------------------------#
# Provides support for accessing the configuration instance in other
# scopes.
#
......
......@@ -280,7 +280,7 @@ module Pod
def prepare_pods_project
UI.message "- Creating Pods project" do
@pods_project = Pod::Project.new(sandbox.project_path)
if config.podfile_path.exist?
if config.podfile_path
@pods_project.add_podfile(config.podfile_path)
end
sandbox.project = @pods_project
......
......@@ -148,7 +148,7 @@ module Pod
#
def path(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}`"
else
''
......
......@@ -7,6 +7,8 @@ module Pod
Config.instance = nil
end
#-------------------------------------------------------------------------#
describe "In general" do
it "returns the singleton config instance" do
......@@ -35,7 +37,33 @@ module Pod
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
config.installation_root = temporary_directory
......@@ -63,9 +91,33 @@ module Pod
it "returns the path to the Pods directory that holds the dependencies" do
config.sandbox_root.should == temporary_directory + 'Pods'
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
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 "Concerning default settings" do
#-------------------------------------------------------------------------#
describe "Default settings" do
before do
Config.any_instance.stubs(:user_settings_file).returns(Pathname.new('not_found'))
......@@ -87,13 +139,49 @@ module Pod
describe "Private helpers" 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
it "returns the path of the user settings file" do
config.user_settings_file.should == Pathname.new("~/.cocoapods/config.yaml").expand_path
it "can be configured with a hash" do
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
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
......@@ -27,7 +27,7 @@ module Pod
describe Installer do
before do
podfile = generate_podfile
podfile = generate_podfile
lockfile = generate_lockfile
config.integrate_targets = false
@installer = Installer.new(config.sandbox, podfile, lockfile)
......@@ -218,7 +218,7 @@ module Pod
it "adds the Podfile to the Pods project" do
@installer.stubs(:libraries).returns([])
config.podfile_path.stubs(:exist?).returns(true)
config.stubs(:podfile_path).returns(Pathname.new('/Podfile'))
@installer.send(:prepare_pods_project)
f = @installer.pods_project['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