Commit 8928250b authored by Fabio Pelosin + Marin Usalj's avatar Fabio Pelosin + Marin Usalj Committed by Marin Usalj

started rewriting config class

parent 7b8c95b4
...@@ -4,6 +4,8 @@ module Pod ...@@ -4,6 +4,8 @@ module Pod
# #
class Config class Config
autoload :ConfigManager, 'cocoapods/config/config_manager'
# The default settings for the configuration. # The default settings for the configuration.
# #
# Users can specify custom settings in `~/.cocoapods/config.yaml`. # Users can specify custom settings in `~/.cocoapods/config.yaml`.
...@@ -106,19 +108,54 @@ module Pod ...@@ -106,19 +108,54 @@ module Pod
@aggressive_cache || (ENV['CP_AGGRESSIVE_CACHE'] == 'TRUE') @aggressive_cache || (ENV['CP_AGGRESSIVE_CACHE'] == 'TRUE')
end end
# def aggressive_cache?
# @aggressive_cache? || (parent.aggressive_cache? if parent)
# end
# pod config verbose true --global
# pod config verbose true
# pod config unset verbose
# pod config get verbose
#
# pod config # defaults to show
#
# ~/.cocoapods/config.yaml
# ~/code/OSS/AwesomeApp/Pods/config.yaml
#
# load the configuration file (path is returned by the manager)
# convert to hash
# user_config? BOOL
# keypath STRING
# value STRING
# manager = Config::ConfigManager.new
# manager.set_local(keypath, value)
# manager.unset_local(keypath)
# manager.set_global(keypath, value)
# manager.unset_global(keypath)
public public
#-------------------------------------------------------------------------# #-------------------------------------------------------------------------#
# @!group Initialization # @!group Initialization
def initialize(use_user_settings = true) # Sets the values of the attributes with the given hash.
if use_user_settings && user_settings_file.exist? #
require 'yaml' # @param [Hash{String,Symbol => Object}] values_by_key
user_settings = YAML.load_file(user_settings_file) # The values of the attributes grouped by key.
initialize_with(user_settings) #
else # @return [void]
initialize_with(DEFAULTS) #
def initialize(settings = {})
settings.each do |key, value|
self.instance_variable_set("@#{key}", value)
end end
end end
...@@ -132,13 +169,6 @@ module Pod ...@@ -132,13 +169,6 @@ module Pod
# @!group Paths # @!group Paths
# @return [Pathname] the directory where repos, templates and configuration
# files are stored.
#
def home_dir
@home_dir ||= Pathname.new(ENV['CP_HOME_DIR'] || "~/.cocoapods").expand_path
end
# @return [Pathname] the directory where the CocoaPods sources are stored. # @return [Pathname] the directory where the CocoaPods sources are stored.
# #
def repos_dir def repos_dir
...@@ -291,26 +321,6 @@ module Pod ...@@ -291,26 +321,6 @@ module Pod
# @!group Private helpers # @!group Private helpers
# @return [Pathname] The path of the file which contains the user settings.
#
def user_settings_file
home_dir + "config.yaml"
end
# Sets the values of the attributes with the given hash.
#
# @param [Hash{String,Symbol => Object}] values_by_key
# The values of the attributes grouped by key.
#
# @return [void]
#
def initialize_with(values_by_key)
return unless values_by_key
values_by_key.each do |key, value|
self.instance_variable_set("@#{key}", value)
end
end
# @return [Array<String>] The filenames that the Podfile can have ordered # @return [Array<String>] The filenames that the Podfile can have ordered
# by priority. # by priority.
# #
......
module Pod
class Config
require 'yaml'
# The config manager is responsible for reading and writing the config.yaml
# file.
#
class ConfigManager
# The default settings for the configuration.
#
# Users can specify custom settings in `~/.cocoapods/config.yaml`.
# An example of the contents of this file might look like:
#
# ---
# skip_repo_update: true
# new_version_message: false
#
DEFAULTS = {
:verbose => false,
:silent => false,
:skip_repo_update => false,
:clean => true,
:integrate_targets => true,
:new_version_message => true,
:cache_root => Pathname.new(File.join(ENV['HOME'], 'Library/Caches/CocoaPods')),
:max_cache_size => 500,
:aggressive_cache => false,
}
class NoKeyError < ArgumentError; end
def initialize()
end
def get_setting(keypath)
value = global_config[keypath] || DEFAULTS[keypath.to_sym]
if value.nil?
raise NoKeyError, "Unrecognized keypath for configuration `#{keypath}`. " \
"\nSupported ones are:\n - #{DEFAULTS.keys.join("\n - ")}"
end
value
end
def set_global(keypath, value)
hash = load_configuration
if value == 'true'
value = true
end
hash[keypath] = value
store_configuration(hash)
end
def unset_global(keypath)
end
# def set_local(keypath, value)
# end
# def unset_local(keypath)
# end
private
def global_config
@global_config ||= load_configuration
end
# @return [Hash]
#
def load_configuration()
if global_config_filepath.exist?
YAML.load_file(global_config_filepath)
else
Hash.new
end
end
def store_configuration(hash)
yaml = YAML.dump(hash)
global_config_filepath
File.open(global_config_filepath, 'w') { |f| f.write(yaml) }
end
def set_keypath(keypath, value, hash)
end
# @return [Pathname] The path of the file which contains the user settings.
#
def global_config_filepath
home_dir + "config.yaml"
end
def local_config_filepath
end
# @return [Pathname] the directory where repos, templates and configuration
# files are stored.
#
def home_dir
# TODO: test ENV
# @home_dir ||= Pathname.new(ENV['CP_HOME_DIR'] || "~/.cocoapods").expand_path
@home_dir ||= Pathname.new("~/.cocoapods").expand_path
end
end
end
end
...@@ -12,25 +12,36 @@ module Pod ...@@ -12,25 +12,36 @@ module Pod
project_name = 'SampleProject' project_name = 'SampleProject'
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.yaml" @config_file_path = temporary_directory + "mock_config.yaml"
Pod::Config.instance.stubs(:user_settings_file).returns(@config_file_path) Pod::Config.instance.stubs(:user_settings_file).returns(@config_file_path)
end end
it "writes local repos for each project" do
run_command('config', "--local", pod_name, pod_path)
yaml = YAML.load(File.open(@config_file_path))
yaml[LOCAL_OVERRIDES][project_name][pod_name].should.equal pod_path it "writes local repos for each project" do
end run_command('config', "--local", pod_name, pod_path)
yaml = YAML.load(File.open(@config_file_path))
yaml[LOCAL_OVERRIDES][project_name][pod_name].should.equal pod_path
end
it "writes global repos without specifying project" do it "writes global repos without specifying project" do
run_command('config', "--global", pod_name, pod_path)
yaml = YAML.load(File.open(@config_file_path))
yaml[GLOBAL_OVERRIDES][pod_name].should.equal pod_path
end
it "deletes global configuration" do
run_command('config', "--global", pod_name, pod_path) run_command('config', "--global", pod_name, pod_path)
run_command('config', "--global", "--delete", pod_name)
yaml = YAML.load(File.open(@config_file_path)) yaml = YAML.load(File.open(@config_file_path))
yaml[GLOBAL_OVERRIDES][pod_name].should.equal pod_path yaml.should.not.has_key? GLOBAL_OVERRIDES
end end
it "defaults to local scope" do it "defaults to local scope" do
run_command('config', pod_name, pod_path) run_command('config', pod_name, pod_path)
...@@ -48,7 +59,7 @@ module Pod ...@@ -48,7 +59,7 @@ module Pod
].each { |invalid| invalid.should.raise CLAide::Help } ].each { |invalid| invalid.should.raise CLAide::Help }
end end
it "deletes local configuration by default" do xit "deletes local configuration by default" do
run_command('config', "--global", pod_name, pod_path) run_command('config', "--global", pod_name, pod_path)
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)
...@@ -58,13 +69,6 @@ module Pod ...@@ -58,13 +69,6 @@ module Pod
yaml[GLOBAL_OVERRIDES][pod_name].should.equal pod_path yaml[GLOBAL_OVERRIDES][pod_name].should.equal pod_path
end end
it "deletes global configuration" do
run_command('config', "--global", pod_name, pod_path)
run_command('config', "--global", "--delete", pod_name)
yaml = YAML.load(File.open(@config_file_path))
yaml.should.not.has_key? GLOBAL_OVERRIDES
end
end end
end end
......
require File.expand_path('../../../spec_helper', __FILE__)
require 'yaml'
module Pod
describe Config::ConfigManager do
# manager = Config::ConfigManager.new
# manager.set_local(keypath, value)
# manager.unset_local(keypath)
# manager.set_global(keypath, value)
# manager.unset_global(keypath)
describe "global" do
@config_file_path = temporary_directory + 'config.yaml'
before do
@sut = Config::ConfigManager.new
@sut.stubs(:home_dir).returns(temporary_directory)
end
it "creates a global config file if one didn't exist" do
FileUtils.rm_rf(@config_file_path)
@sut.set_global('verbose', 'true')
@config_file_path.should.exist
end
it "stores a global setting" do
@sut.set_global('verbose', 'true')
yaml = YAML.load_file(@config_file_path)
yaml['verbose'].should == true
end
it "preserves the existing settings of the configuration file" do
@sut.set_global('user_name', 'Super Marin')
@sut.set_global('verbose', 'true')
yaml = YAML.load_file(@config_file_path)
yaml['user_name'].should == 'Super Marin'
end
xit "allows to store a development pod" do
@sut.set_global('development.ObjectiveSugar', '~/code/OS')
yaml = YAML.load_file(@config_file_path)
yaml['development.ObjectiveSugar'].should == '~/code/OS'
end
it "returns a globally decided setting" do
@sut.set_global('user_name', 'Super Marin')
@sut.get_setting('user_name').should == 'Super Marin'
end
it "verbose by default is false" do
@sut.get_setting('verbose').should == false
end
it "silent by default is false" do
@sut.get_setting('silent').should == false
end
it "skips repo update by default is false" do
@sut.get_setting('skip_repo_update').should == false
end
it "clean by default is true" do
@sut.get_setting('clean').should == true
end
it "integrate_targets by default is true" do
@sut.get_setting('integrate_targets').should == true
end
it "new_version_message by default is true" do
@sut.get_setting('new_version_message').should == true
end
it "cache_root returns the cache root by default" do
@sut.get_setting('cache_root').to_s.should.include('Library/Caches/CocoaPods')
end
it "max_cache_size is 500 MB by default" do
@sut.get_setting('max_cache_size').should == 500
end
it "aggressive_cache is false by default" do
@sut.get_setting('aggressive_cache').should == false
end
it "raises if there is an attempt to access an unrecognized attribute" do
should.raise Config::ConfigManager::NoKeyError do
@sut.get_setting('idafjaeilfjoasijfopasdj')
end
end
xit "it converts string boolean values"
xit "it support keypath"
# key.keypath = 'my value'
end
xit "writes local repos for each project" do
@sut.set_local('verbose', 'true')
yaml['verbose'].should == true
end
xit "works with ENV" do
end
xit "writes global repos without specifying project" do
run_command('config', "--global", pod_name, pod_path)
yaml = YAML.load(File.open(@config_file_path))
yaml[GLOBAL_OVERRIDES][pod_name].should.equal pod_path
end
xit "deletes global configuration" do
run_command('config', "--global", pod_name, pod_path)
run_command('config', "--global", "--delete", pod_name)
yaml = YAML.load(File.open(@config_file_path))
yaml.should.not.has_key? GLOBAL_OVERRIDES
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