module Ratch module ConfigUtils DEFAULT_CONFIG_FILE = 'config' # def configuration(file=nil) @configuration ||= {} if file = config_file(file) @configuration[file] ||= config_read(file) else @configuration[file] ||= Hash.new{ |h,k| h[k] = {} } end end # def config_file(path=nil) path ||= DEFAULT_CONFIG_FILE find = "{#{utility_directory}/,}#{path}{.yaml,.yml,}" file = Dir.glob(find)[0] return file end # Load configuration data from a file. THe file will # be looked for in the current script directory then # from the project root. # # Since they are YAML files, they can optionally # end with '.yaml' or '.yml'. def config_read(path) find = "{#{utility_directory}/,}#{path}{.yaml,.yml,}" if file = Dir.glob(find)[0] YAML::load(File.open(file)) else raise LoadError, "Missing file -- #{path}" end end # TODO Better name? Better definition? (Won't handle task subdirs well). def utility_directory File.dirname($0) end # Create an argument vector from a set of config options. # TODO Deprecate in favor of Hash extension. def config_vector(config, args_field=nil) config.command_vector(args_field) end # # FIXME ProjectInfo, if available. # # def projectinfo # require 'box/project' # @projectinfo # # #begin # # @projectinfo = ProjectInfo.open # #rescue LoadError # # @projectinfo = nil # #end # end # def config_file # @config_file ||= ( # dir = utility_directory # name = Dir.basename(dir) # Dir.glob("{#{dir},meta,info,}/{#{name},config}{.yaml,.yml}") # ) # end # # Load task configuration if any. # # def config_load(*names) #, defaults=nil) # # names.inject({}) do |memo, name| # name = name.to_s # #defaults = defaults || {} # # if file = config_file(name) # config = YAML.load(File.open(file)) # elsif file = Dir.glob("#{utility_directory}/config{,.yaml,.yml}")[0] # config = YAML.load(File.open(file))[name.to_s] # else # config = {} # end # # config.update(memo) # end # #return defaults.update(config || {}) # end end end