Commit 7b8c95b4 authored by Marin Usalj's avatar Marin Usalj

pulled the file stuff to commanc/config

parent 3965697e
...@@ -16,9 +16,6 @@ module Pod ...@@ -16,9 +16,6 @@ module Pod
# pod config --delete Kiwi # pod config --delete Kiwi
# #
class Config < Command class Config < Command
CONFIG_FILE_PATH = File.expand_path('~/.config/cocoapods')
LOCAL_OVERRIDES = 'PER_PROJECT_REPO_OVERRIDES'
GLOBAL_OVERRIDES = 'GLOBAL_REPO_OVERRIDES'
self.summary = 'Something like `bundle config` ... but better.' self.summary = 'Something like `bundle config` ... but better.'
self.description = <<-DESC self.description = <<-DESC
...@@ -59,57 +56,17 @@ module Pod ...@@ -59,57 +56,17 @@ module Pod
def update_config def update_config
if @should_delete if @should_delete
@local ? delete_local_config : delete_global_config @local ? config.delete_local(@pod_name) : config.delete_global(@pod_name)
else else
@local ? store_local_config : store_global_config @local ? config.store_local(@pod_name, @pod_path) : config.store_global(@pod_name, @pod_path)
end end
write_config_to_file config.write_config_to_file
end end
def store_global_config def config
config_hash[GLOBAL_OVERRIDES][@pod_name] = @pod_path Pod::Config.instance
end end
def store_local_config
config_hash[LOCAL_OVERRIDES][project_name] ||= {}
config_hash[LOCAL_OVERRIDES][project_name][@pod_name] = @pod_path
end
def delete_local_config
config_hash[LOCAL_OVERRIDES][project_name].delete(@pod_name)
end
def delete_global_config
config_hash[GLOBAL_OVERRIDES].delete(@pod_name)
end
def config_hash
@config_hash ||= load_config
end
def load_config
FileUtils.touch(CONFIG_FILE_PATH) unless File.exists? CONFIG_FILE_PATH
config = YAML.load(File.open(CONFIG_FILE_PATH)) || {}
config[LOCAL_OVERRIDES] ||= {}
config[GLOBAL_OVERRIDES] ||= {}
config
end
def project_name
`basename #{Dir.pwd}`.chomp
end
def write_config_to_file
File.open(CONFIG_FILE_PATH, 'w') { |f| f.write(config_hash.delete_blank.to_yaml) }
end
end end
end end
end end
class Hash
def delete_blank
delete_if{|k, v| v.empty? or v.instance_of?(Hash) && v.delete_blank.empty?}
end
end
...@@ -113,12 +113,12 @@ module Pod ...@@ -113,12 +113,12 @@ module Pod
# @!group Initialization # @!group Initialization
def initialize(use_user_settings = true) def initialize(use_user_settings = true)
configure_with(DEFAULTS)
if use_user_settings && user_settings_file.exist? if use_user_settings && user_settings_file.exist?
require 'yaml' require 'yaml'
user_settings = YAML.load_file(user_settings_file) user_settings = YAML.load_file(user_settings_file)
configure_with(user_settings) initialize_with(user_settings)
else
initialize_with(DEFAULTS)
end end
end end
...@@ -304,7 +304,7 @@ module Pod ...@@ -304,7 +304,7 @@ module Pod
# #
# @return [void] # @return [void]
# #
def configure_with(values_by_key) def initialize_with(values_by_key)
return unless values_by_key return unless values_by_key
values_by_key.each do |key, value| values_by_key.each do |key, value|
self.instance_variable_set("@#{key}", value) self.instance_variable_set("@#{key}", value)
...@@ -338,6 +338,56 @@ module Pod ...@@ -338,6 +338,56 @@ module Pod
nil nil
end end
public
#-------------------------------------------------------------------------#
# @!group Local repos
#
#
LOCAL_OVERRIDES = 'PER_PROJECT_REPO_OVERRIDES'
GLOBAL_OVERRIDES = 'GLOBAL_REPO_OVERRIDES'
def store_global(pod_name, pod_path)
config_hash[GLOBAL_OVERRIDES] ||= {}
config_hash[GLOBAL_OVERRIDES][pod_name] = pod_path
end
def store_local(pod_name, pod_path)
config_hash[LOCAL_OVERRIDES] ||= {}
config_hash[LOCAL_OVERRIDES][project_name] ||= {}
config_hash[LOCAL_OVERRIDES][project_name][pod_name] = pod_path
end
def delete_local(pod_name)
config_hash[LOCAL_OVERRIDES] ||= {}
config_hash[LOCAL_OVERRIDES][project_name].delete(pod_name)
end
def delete_global(pod_name)
config_hash[GLOBAL_OVERRIDES] ||= {}
config_hash[GLOBAL_OVERRIDES].delete(pod_name)
end
def config_hash
@config_hash ||= load_config
end
def load_config
FileUtils.touch(user_settings_file) unless File.exists? user_settings_file
YAML.load(File.open(user_settings_file)) || {}
end
def project_name
`basename #{Dir.pwd}`.chomp
end
def write_config_to_file
File.open(user_settings_file, 'w') { |f| f.write(config_hash.delete_blank.to_yaml) }
end
public public
#-------------------------------------------------------------------------# #-------------------------------------------------------------------------#
...@@ -370,4 +420,11 @@ module Pod ...@@ -370,4 +420,11 @@ module Pod
end end
end end
end end
end
class Hash
def delete_blank
delete_if { |k, v| v.empty? or v.instance_of?(Hash) && v.delete_blank.empty? }
end
end end
...@@ -14,9 +14,8 @@ module Pod ...@@ -14,9 +14,8 @@ module Pod
before do before do
Dir.stubs(:pwd).returns('~/code/OSS/SampleProject') Dir.stubs(:pwd).returns('~/code/OSS/SampleProject')
@config_file_path = temporary_directory + "mock_config" @config_file_path = temporary_directory + "mock_config.yaml"
Command::Config.send(:remove_const, 'CONFIG_FILE_PATH') Pod::Config.instance.stubs(:user_settings_file).returns(@config_file_path)
Command::Config.const_set("CONFIG_FILE_PATH", @config_file_path)
end end
it "writes local repos for each project" do it "writes local repos for each project" do
...@@ -54,7 +53,7 @@ module Pod ...@@ -54,7 +53,7 @@ module Pod
run_command('config', "--local", pod_name, pod_path) run_command('config', "--local", pod_name, pod_path)
run_command('config', "--delete", pod_name) run_command('config', "--delete", pod_name)
yaml = YAML.load(File.open(@config_file_path)) yaml = YAML.load(File.open(@config_file_path))
puts yaml
yaml.should.not.has_key? LOCAL_OVERRIDES yaml.should.not.has_key? LOCAL_OVERRIDES
yaml[GLOBAL_OVERRIDES][pod_name].should.equal pod_path yaml[GLOBAL_OVERRIDES][pod_name].should.equal pod_path
end end
......
...@@ -196,9 +196,9 @@ module Pod ...@@ -196,9 +196,9 @@ module Pod
@sut.send(:user_settings_file).should == Pathname.new("~/.cocoapods/config.yaml").expand_path @sut.send(:user_settings_file).should == Pathname.new("~/.cocoapods/config.yaml").expand_path
end end
it "can be configured with a hash" do it "can be initialized with a hash" do
hash = { :verbose => true } hash = { :verbose => true }
@sut.send(:configure_with, hash) @sut.send(:initialize_with, hash)
@sut.should.be.verbose @sut.should.be.verbose
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