Commit fd1e891f authored by Fabio Pelosin's avatar Fabio Pelosin

[#112] Refactoring based on suggestions by @alloy

parent 89f81e10
...@@ -8,8 +8,6 @@ module Pod ...@@ -8,8 +8,6 @@ module Pod
autoload :Setup, 'cocoapods/command/setup' autoload :Setup, 'cocoapods/command/setup'
autoload :Spec, 'cocoapods/command/spec' autoload :Spec, 'cocoapods/command/spec'
attr_accessor :output
class Help < Informative class Help < Informative
def initialize(command_class, argv) def initialize(command_class, argv)
@command_class, @argv = command_class, argv @command_class, @argv = command_class, argv
......
...@@ -14,11 +14,7 @@ module Pod ...@@ -14,11 +14,7 @@ module Pod
$ pod repo update NAME $ pod repo update NAME
Updates the local clone of the spec-repo `NAME'. If `NAME' is omitted Updates the local clone of the spec-repo `NAME'. If `NAME' is omitted
this will update all spec-repos in `~/.cocoapods'. this will update all spec-repos in `~/.cocoapods'.}
$ pod repo set-url NAME URL
Updates the remote `URL' of the spec-repo `NAME'.}
end end
extend Executable extend Executable
...@@ -26,11 +22,11 @@ module Pod ...@@ -26,11 +22,11 @@ module Pod
def initialize(argv) def initialize(argv)
case @action = argv.arguments[0] case @action = argv.arguments[0]
when 'add', 'set-url' when 'add'
unless (@name = argv.arguments[1]) && (@url = argv.arguments[2]) unless (@name = argv.arguments[1]) && (@url = argv.arguments[2])
raise Informative, "#{@action == 'add' ? 'Adding' : 'Updating the remote of'} a repo needs a `name' and a `url'." raise Informative, "#{@action == 'add' ? 'Adding' : 'Updating the remote of'} a repo needs a `name' and a `url'."
end end
when 'update', 'read-url' when 'update'
@name = argv.arguments[1] @name = argv.arguments[1]
else else
super super
...@@ -58,19 +54,6 @@ module Pod ...@@ -58,19 +54,6 @@ module Pod
Dir.chdir(dir) { git("pull") } Dir.chdir(dir) { git("pull") }
end end
end end
def set_url
Dir.chdir(dir) do
git("remote set-url origin '#{@url}'")
end
end
def read_url
Dir.chdir(dir) do
@output = git('config --get remote.origin.url')
end
end
end end
end end
end end
......
...@@ -19,47 +19,53 @@ module Pod ...@@ -19,47 +19,53 @@ module Pod
super super
end end
extend Executable
executable :git
def initialize(argv) def initialize(argv)
@push_access = argv.option('--push') || already_push? @push_option = argv.option('--push')
puts "Setup with push access" if @push_access && !config.silent
super unless argv.empty? super unless argv.empty?
end end
def already_push? def dir
if master_repo_exists? config.repos_dir + 'master'
read_master_repo_remote_command.run
read_master_repo_remote_command.output.chomp == master_repo_url_with_push
else
false
end
end end
def master_repo_exists? def read_only_url
(config.repos_dir + 'master').exist?
end
def master_repo_url
'git://github.com/CocoaPods/Specs.git' 'git://github.com/CocoaPods/Specs.git'
end end
def master_repo_url_with_push def read_write_url
'git@github.com:CocoaPods/Specs.git' 'git@github.com:CocoaPods/Specs.git'
end end
def repo_url def url
@push_access ? master_repo_url_with_push : master_repo_url if push?
read_write_url
else
read_only_url
end
end end
def add_master_repo_command def origin_url_push?
@command ||= Repo.new(ARGV.new(['add', 'master', repo_url])) Dir.chdir(dir) do
origin_url = git('config --get remote.origin.url')
origin_url.chomp == read_write_url
end
end
def push?
@push_option || (dir.exist? && origin_url_push?)
end end
def read_master_repo_remote_command def set_master_repo_url
@read_command ||= Repo.new(ARGV.new(['read-url', 'master'])) Dir.chdir(dir) do
git("remote set-url origin '#{url}'")
end
end end
def update_master_repo_remote_command def add_master_repo_command
Repo.new(ARGV.new(['set-url', 'master', repo_url])) @command ||= Repo.new(ARGV.new(['add', 'master', url]))
end end
def update_master_repo_command def update_master_repo_command
...@@ -67,8 +73,9 @@ module Pod ...@@ -67,8 +73,9 @@ module Pod
end end
def run def run
if master_repo_exists? puts "Using push access" if push? && !config.silent
update_master_repo_remote_command.run if dir.exist?
set_master_repo_url
update_master_repo_command.run update_master_repo_command.run
else else
add_master_repo_command.run add_master_repo_command.run
......
...@@ -6,11 +6,21 @@ describe "Pod::Command" do ...@@ -6,11 +6,21 @@ describe "Pod::Command" do
it "creates the local spec-repos directory and creates a clone of the `master' repo" do it "creates the local spec-repos directory and creates a clone of the `master' repo" do
command = Pod::Command.parse('setup', '--silent') command = Pod::Command.parse('setup', '--silent')
def command.master_repo_url; SpecHelper.fixture('spec-repos/master'); end def command.url; SpecHelper.fixture('spec-repos/master'); end
command.run command.run
git_config('master', 'remote.origin.url').should == fixture('spec-repos/master').to_s git_config('master', 'remote.origin.url').should == fixture('spec-repos/master').to_s
end end
it "preserve push access for the `master' repo" do
command = Pod::Command.parse('setup', '--silent')
def command.url; SpecHelper.fixture('spec-repos/master'); end
command.run
command2 = Pod::Command.parse('setup', '--silent')
command2.url.should == 'git://github.com/CocoaPods/Specs.git'
git('master', 'remote set-url origin git@github.com:CocoaPods/Specs.git')
command2.url.should == 'git@github.com:CocoaPods/Specs.git'
end
it "adds a spec-repo" do it "adds a spec-repo" do
add_repo('private', fixture('spec-repos/master')) add_repo('private', fixture('spec-repos/master'))
git_config('private', 'remote.origin.url').should == fixture('spec-repos/master').to_s git_config('private', 'remote.origin.url').should == fixture('spec-repos/master').to_s
......
...@@ -2,6 +2,7 @@ require File.expand_path('../../spec_helper', __FILE__) ...@@ -2,6 +2,7 @@ require File.expand_path('../../spec_helper', __FILE__)
describe "Pod::Command" do describe "Pod::Command" do
it "returns the proper command class" do it "returns the proper command class" do
config.silent.should == true
Pod::Command.parse('setup').should.be.instance_of Pod::Command::Setup Pod::Command.parse('setup').should.be.instance_of Pod::Command::Setup
#Pod::Command.parse('spec').should.be.instance_of Pod::Command::Spec #Pod::Command.parse('spec').should.be.instance_of Pod::Command::Spec
Pod::Command.parse('repo', 'update').should.be.instance_of Pod::Command::Repo Pod::Command.parse('repo', 'update').should.be.instance_of Pod::Command::Repo
...@@ -13,9 +14,15 @@ describe "Pod::Command::Setup" do ...@@ -13,9 +14,15 @@ describe "Pod::Command::Setup" do
lambda { Pod::Command::Setup.new(argv('something')) }.should.raise Pod::Command::Help lambda { Pod::Command::Setup.new(argv('something')) }.should.raise Pod::Command::Help
end end
it "returns the URL of the `master' spec-repo" do it "returns the read only URL of the `master' spec-repo" do
command = Pod::Command::Setup.new(argv) command = Pod::Command::Setup.new(argv)
command.master_repo_url.should == 'git://github.com/CocoaPods/Specs.git' command.url.should == 'git://github.com/CocoaPods/Specs.git'
end
it "returns the push URL of the `master' spec-repo" do
config.silent = true
command = Pod::Command::Setup.new(argv('--push'))
command.url.should == 'git@github.com:CocoaPods/Specs.git'
end end
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