lib/tap/env.rb in tap-0.9.0 vs lib/tap/env.rb in tap-0.9.1

- old
+ new

@@ -90,11 +90,10 @@ attr_accessor :logger def initialize @config = nil @logger = nil - # @on_handle_unknown_configs = nil reset end def debug_setup $DEBUG = true @@ -175,11 +174,11 @@ # will be loaded. The loading cycle recurses as specified by the configurations. # # Configuration paths are expanded relative to the parent directory # of the loaded file. Raises an error if non-env configuration are # found (as determined by Tap::Env::Configurtion::ENV_CONFIG_KEYS). - def load_config(path, root=Tap::Root.new, &block) + def load_config(path, root=Tap::Root.new, &block) # :yields: non_env_configs path = File.join(path, DEFAULT_CONFIG_FILE) if File.directory?(path) path = File.expand_path(path) # prevent infinite looping config_paths = config['config_paths'] @@ -190,21 +189,16 @@ config_paths << path config = read_config(path) config['root'] = File.dirname(path) unless config['root'] - configure(config, root) do |configured_root, other_configs| - if block_given? - yield(configured_root, path, other_configs) - else - log(:warn, "ignoring configs: #{path} (#{other_configs.keys.join(',')})", Logger::WARN) - end - end - + configure(config, root, &block) end - def configure(config, root=Tap::Root.new, &block) + #-- + # Note: always yields to the block, even if non_env_configs is empty + def configure(config, root=Tap::Root.new, &block) # :yields: non_env_configs root_configs, env_configs, other_configs = partition_configs(config, ['root', 'directories', 'absolute_paths'], DEFAULT_CONFIG.keys) env_configs = join_configs(DEFAULT_CONFIG, env_configs) # assign root configs root.send(:assign_paths, @@ -212,24 +206,23 @@ root_configs['directories'] || root.directories, root_configs['absolute_paths'] || root.absolute_paths) # handle unknown configs (handle before setting # env configs in case the configs modify root) - unless other_configs.empty? - if block_given? - yield(root, other_configs) - else - log(:warn, "ignoring configs: (#{other_configs.keys.join(',')})", Logger::WARN) - end + case + when block_given? + yield(other_configs) + when !other_configs.empty? + log(:warn, "ignoring non-env configs: #{other_configs.keys.join(',')}", Logger::DEBUG) end - + # load gems and configurations gem_paths = env_configs.delete('gems').collect do |gem_name| full_gem_path(gem_name) end config_paths = env_configs.delete('config_paths') + gem_paths - config_paths.each {|path| load_config(root[path], &block) } + config_paths.each {|path| load_config(root[path]) } # assign env configs env_configs.each_pair do |key, value| case key when 'load_paths' @@ -237,11 +230,11 @@ when 'load_once_paths' assign_paths(root, value, self.config[key], Dependencies.load_once_paths) when /_paths$/ assign_paths(root, value, self.config[key]) else - handle_unknown_config(key, value) + handle_unknown_env_config(root, key, value) end end true end @@ -264,14 +257,40 @@ end spec.full_gem_path end + # Loads the config for the specified gem. A gem version can be + # specified in the name, see full_gem_path. def load_gem(gem_name) load_config(full_gem_path(gem_name)) end + # Returns the path to all DEFAULT_CONFIG_FILEs for installed gems. + # If latest==true, then only the config files for the latest gem + # specs will be returned (ie for the most current version of a + # gem). + def gem_config_files(latest=true) + if latest + Gem.source_index.latest_specs.collect do |spec| + config_file = File.join(spec.full_gem_path, DEFAULT_CONFIG_FILE) + File.exists?(config_file) ? config_file : nil + end.compact + else + Gem.path.collect do |dir| + Dir.glob( File.join(dir, "gems/*", DEFAULT_CONFIG_FILE) ) + end.flatten.uniq + end + end + + # Loads the config files discovered by gem_config_files(true). + def discover_gems + gem_config_files.collect do |config_file| + load_config(config_file) + end + end + # Searches for and returns all .rb files under each of the command_paths # as well as the default tap commands. Commands with conflicting names # raise an error; however, user commands are allowed to override the # default tap commands and will NOT raise an error. def commands @@ -307,10 +326,10 @@ end array.uniq! end end - def handle_unknown_config(key, value) + def handle_unknown_env_config(key, value) raise "unknown env config: #{key}" end end end \ No newline at end of file