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
# pod config --delete Kiwi
#
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.description = <<-DESC
......@@ -59,57 +56,17 @@ module Pod
def update_config
if @should_delete
@local ? delete_local_config : delete_global_config
@local ? config.delete_local(@pod_name) : config.delete_global(@pod_name)
else
@local ? store_local_config : store_global_config
@local ? config.store_local(@pod_name, @pod_path) : config.store_global(@pod_name, @pod_path)
end
write_config_to_file
config.write_config_to_file
end
def store_global_config
config_hash[GLOBAL_OVERRIDES][@pod_name] = @pod_path
def config
Pod::Config.instance
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
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
# @!group Initialization
def initialize(use_user_settings = true)
configure_with(DEFAULTS)
if use_user_settings && user_settings_file.exist?
require 'yaml'
user_settings = YAML.load_file(user_settings_file)
configure_with(user_settings)
initialize_with(user_settings)
else
initialize_with(DEFAULTS)
end
end
......@@ -304,7 +304,7 @@ module Pod
#
# @return [void]
#
def configure_with(values_by_key)
def initialize_with(values_by_key)
return unless values_by_key
values_by_key.each do |key, value|
self.instance_variable_set("@#{key}", value)
......@@ -338,6 +338,56 @@ module Pod
nil
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
#-------------------------------------------------------------------------#
......@@ -370,4 +420,11 @@ module Pod
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
......@@ -14,9 +14,8 @@ module Pod
before do
Dir.stubs(:pwd).returns('~/code/OSS/SampleProject')
@config_file_path = temporary_directory + "mock_config"
Command::Config.send(:remove_const, 'CONFIG_FILE_PATH')
Command::Config.const_set("CONFIG_FILE_PATH", @config_file_path)
@config_file_path = temporary_directory + "mock_config.yaml"
Pod::Config.instance.stubs(:user_settings_file).returns(@config_file_path)
end
it "writes local repos for each project" do
......@@ -54,7 +53,7 @@ module Pod
run_command('config', "--local", pod_name, pod_path)
run_command('config', "--delete", pod_name)
yaml = YAML.load(File.open(@config_file_path))
puts yaml
yaml.should.not.has_key? LOCAL_OVERRIDES
yaml[GLOBAL_OVERRIDES][pod_name].should.equal pod_path
end
......
......@@ -196,9 +196,9 @@ module Pod
@sut.send(:user_settings_file).should == Pathname.new("~/.cocoapods/config.yaml").expand_path
end
it "can be configured with a hash" do
it "can be initialized with a hash" do
hash = { :verbose => true }
@sut.send(:configure_with, hash)
@sut.send(:initialize_with, hash)
@sut.should.be.verbose
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