module Jammit module Config CONFIG_TEMPLATE = Jammit::ROOT + '/assets.example.yml' class << self def dispatch(command, *params) case command when :create create when :set set(params.shift, params.shift) end end ## # load configuration file # @param {String} path # @param {Boolean} force reloading of config file # def load(force=false) raise Jammit::ConfigurationNotFound, "could not find the \"#{Jammit.config_path}\" configuration file" unless File.exists?(Jammit.config_path) && File.readable?(Jammit.config_path) if !@config || force == true @config = YAML.load(ERB.new(File.read(Jammit.config_path)).result) end @config end private ## # Write config to file # def write(config) File.open(Jammit.config_path, "w") {|f| f << config.to_yaml } end ## # If a config path is given, use that as the root, otherwise use DEFAULT_CONFIG_PATH # def create if File.exists?(Jammit.config_path) raise Jammit::AlreadyConfigured.new(" Config file #{Jammit.config_path} already exists.") end FileUtils.mkdir_p(File.dirname(Jammit.config_path)) FileUtils.cp(CONFIG_TEMPLATE, Jammit.config_path) parts = Jammit.config_path.split('/') name = parts.pop dir = parts.pop Jammit.ui.confirm(" create #{dir}/#{name}") end ## # Set a config property in file # @param {String} name # @param {String} value # def set(name, value) value = (value == "true") ? true : (value == "false") ? false : value config = load(true) option = config[name] # careful not to clobber raexisting values if updating a hash if value.kind_of?(Hash) && option.kind_of?(Hash) value.keys.each do |k| option[k] = value[k] end config.update(name => option) else config.update(name => value) end write(config) Jammit.ui.confirm(" set #{name}") end end end end