class MGit::MGitConfig
Constants
- CONFIG_KEY
Public Class Methods
dump_config(root)
click to toggle source
列出所有配置
@param root [String] 工程根目录
# File lib/m-git/foundation/mgit_config.rb, line 120 def dump_config(root) query(root) { |config| CONFIG_KEY.each_with_index { |(key, value_dict), index| line = "#{Output.blue_message("[#{value_dict[:info]}]")}\n#{key.to_s}: " set_value = config[key.to_s] if !set_value.nil? line += "#{set_value}\n\n" else line += "#{value_dict[:default]}\n\n" end puts line } } end
query(root) { |config| ... }
click to toggle source
查询配置
@param root [String] 工程根目录
@raise [MGit::Error] 异常错误
# File lib/m-git/foundation/mgit_config.rb, line 49 def query(root) config, error = __load_file(root) if !error.nil? raise Error.new(error) return elsif block_given? # 如果文件存在但无内容,此时读取到的数据类型是FalseClass,此处统一规范化 config = {} if !config.is_a?(Hash) yield(config) end end
query_with_key(root, key_symbol)
click to toggle source
Description of query_with_key
@param root [String] 工程根目录
@param key_symbol [Symbol] 符号key值
@return [Object] 配置值
@raise [MGit::Error] 异常错误
# File lib/m-git/foundation/mgit_config.rb, line 70 def query_with_key(root, key_symbol) query(root) { |config| if !config[key_symbol.to_s].nil? return config[key_symbol.to_s] elsif !CONFIG_KEY[key_symbol].nil? return CONFIG_KEY[key_symbol][:default] else return nil end } end
to_suitable_value_for_key(root, key, value)
click to toggle source
验证一个value的值是否符合key的类型
@param root [String] 工程根目录
@param key [String] 字符串key值
@param value [String] 字符串格式的值
@return [Object] key值对应的合法类型value值
# File lib/m-git/foundation/mgit_config.rb, line 145 def to_suitable_value_for_key(root, key, value) return unless CONFIG_KEY.keys.include?(key.to_sym) return nil if !value.is_a?(String) key_dict = CONFIG_KEY[key.to_sym] set_value = nil # 是否是数字 if key_dict[:type] == 'Integer' && value.to_i.to_s == value set_value = value.to_i # 是否是true elsif key_dict[:type] == 'Boolean' && value.to_s.downcase == 'true' set_value = true # 是否是false elsif key_dict[:type] == 'Boolean' && value.to_s.downcase == 'false' set_value = false # 是否是其他字符串 elsif key_dict[:type] == 'String' && value.is_a?(String) set_value = value end set_value end
update(root) { |config| ... }
click to toggle source
更新配置
@param root [String] 工程根目录
@raise [MGit::Error] 异常错误
# File lib/m-git/foundation/mgit_config.rb, line 88 def update(root) # 加载配置 config, error = __load_file(root) if !error.nil? raise Error.new(error) return end # 如果文件存在但无内容,此时读取到的数据类型是FalseClass,此处统一规范化 config = {} if !config.is_a?(Hash) # 更新 yield(config) if block_given? # 更新完后校验格式 if !config.is_a?(Hash) raise Error.new("工具配置更新数据格式错误,更新失败!") return end # 写回配置 error = write_to_file(root, config) if !error.nil? raise Error.new(error) return end end
Private Class Methods
__load_file(root)
click to toggle source
加载mgit配置文件
@param root [String] 工程根目录
@return [(Hash, Boolean)] (配置内容,错误信息)
# File lib/m-git/foundation/mgit_config.rb, line 180 def __load_file(root) config_path = File.join(root, Constants::MGIT_CONFIG_PATH) if File.exists?(config_path) begin return YAML.load_file(config_path), nil rescue => e return nil, "工具配置文件\"#{File.basename(config_path)}\"读取失败,原因:\n#{e.message}" end end [nil, nil] end
write_to_file(root, content)
click to toggle source
将配置写回文件
@param root [String] 工程根目录
@param content [Hash] 新配置
@return [String] 错误信息
# File lib/m-git/foundation/mgit_config.rb, line 201 def write_to_file(root, content) config_path = File.join(root, Constants::MGIT_CONFIG_PATH) begin FileUtils.rm_f(config_path) if File.exist?(config_path) dir_name = File.dirname(config_path) FileUtils.mkdir_p(dir_name) if !Dir.exist?(dir_name) file = File.new(config_path, 'w') if !file.nil? file.write(content.to_yaml) file.close end return nil rescue => e return "工具配置文件\"#{File.basename(config_path)}\"写入失败,原因:\n#{e.message}" end end