lib/tailor/configuration.rb in tailor-1.0.0.alpha2 vs lib/tailor/configuration.rb in tailor-1.0.0
- old
+ new
@@ -1,8 +1,9 @@
require_relative '../tailor'
-require_relative 'runtime_error'
require_relative 'logger'
+require_relative 'runtime_error'
+require_relative 'configuration/style'
class Tailor
# Pulls in any configuration from the places configuration can be set:
# 1. ~/.tailorrc
@@ -15,31 +16,10 @@
include Tailor::Logger::Mixin
DEFAULT_GLOB = 'lib/**/*.rb'
DEFAULT_RC_FILE = Dir.home + '/.tailorrc'
DEFAULT_PROJECT_CONFIG = Dir.pwd + '/.tailor'
- DEFAULT_STYLE = {
- allow_camel_case_methods: false,
- allow_hard_tabs: false,
- allow_screaming_snake_case_classes: false,
- allow_trailing_line_spaces: false,
- indentation_spaces: 2,
- max_code_lines_in_class: 300,
- max_code_lines_in_method: 30,
- max_line_length: 80,
- spaces_after_comma: 1,
- spaces_before_comma: 0,
- spaces_before_lbrace: 1,
- spaces_after_lbrace: 1,
- spaces_before_rbrace: 1,
- spaces_in_empty_braces: 0,
- spaces_after_lbracket: 0,
- spaces_before_rbracket: 0,
- spaces_after_lparen: 0,
- spaces_before_rparen: 0,
- trailing_newlines: 1
- }
# @return [Hash]
def self.default
new
end
@@ -51,15 +31,16 @@
# @param [OpenStruct] options
# @option options [String] config_file
# @option options [Array] formatters
# @option options [Hash] style
def initialize(runtime_file_list=nil, options=nil)
+ @style = Style.new
@formatters = ['text']
@file_sets = {
default: {
file_list: file_list(DEFAULT_GLOB),
- style: DEFAULT_STYLE
+ style: @style.to_hash
}
}
@runtime_file_list = runtime_file_list
log "Got runtime file list: #{@runtime_file_list}"
@@ -73,52 +54,76 @@
@config_file = @options.config_file unless @options.config_file.empty?
load_from_config_file(config_file) if config_file
if @config_file
if @rc_file_config
- # Get formatters from config file
- unless @rc_file_config.formatters.empty?
- @formatters = @rc_file_config.formatters
- log "@formatters is now #{@formatters}"
- end
+ get_formatters_from_config_file
+ get_files_sets_from_config_file
+ end
+ end
- # Get file sets from config file
- unless @rc_file_config.file_sets.empty?
- @rc_file_config.file_sets.each do |label, file_set|
- unless @file_sets[label]
- @file_sets[label] = { style: DEFAULT_STYLE }
- end
+ get_formatters_from_cli_opts
+ get_files_sets_from_cli_opts
+ get_style_from_cli_opts
- @file_sets[label][:file_list].concat file_set[:file_list]
- @file_sets[label][:file_list].uniq!
- @file_sets[label][:style].merge! file_set[:style]
- end
+ if @file_sets[:default][:file_list].empty?
+ @file_sets[:default][:file_list] = file_list(DEFAULT_GLOB)
+ end
+ end
+
+ def get_files_sets_from_config_file
+ unless @rc_file_config.file_sets.empty?
+ @rc_file_config.file_sets.each do |label, file_set|
+ log "file set: #{file_set}"
+
+ if @file_sets[label]
+ @file_sets[label][:file_list].concat file_set[:file_list]
+ @file_sets[label][:file_list].uniq!
+ @file_sets[label][:style].merge! file_set[:style]
+ else
+ @file_sets[label] = {
+ file_list: file_set[:file_list],
+ style: @style.to_hash.merge(file_set[:style])
+ }
end
end
end
+ end
- # Get formatters from CLI options
- unless @options.formatters.empty? || @options.formatters.nil?
- @formatters = @options.formatters
+ def get_formatters_from_config_file
+ unless @rc_file_config.formatters.empty?
+ @formatters = @rc_file_config.formatters
log "@formatters is now #{@formatters}"
end
+ end
- # Get file sets from CLI options
+ def get_style_from_cli_opts
+ if @options.style
+ @options.style.each do |property, value|
+ if value == :off || value == "off"
+ @file_sets[:default][:style][property][1] = { level: :off }
+ else
+ @file_sets[:default][:style][property][0] = value
+ end
+ end
+ end
+ end
+
+ def get_files_sets_from_cli_opts
unless @runtime_file_list.nil? || @runtime_file_list.empty?
# Only use options set for the :default file set because the user gave
# a different set of files to measure.
@file_sets.delete_if { |k, v| k != :default }
@file_sets[:default][:file_list] = file_list(@runtime_file_list)
end
+ end
- # Get style overrides from CLI options
- @file_sets[:default][:style].merge!(@options.style)
-
- if @file_sets[:default][:file_list].empty?
- @file_sets[:default][:file_list] = file_list(DEFAULT_GLOB)
+ def get_formatters_from_cli_opts
+ unless @options.formatters.empty? || @options.formatters.nil?
+ @formatters = @options.formatters
+ log "@formatters is now #{@formatters}"
end
-
end
# @return [String] Name of the config file to use.
def config_file
return @config_file if @config_file
@@ -137,57 +142,52 @@
@formatters = new_formatters unless new_formatters.empty?
@formatters
end
+ # Adds a file set to the list of file sets in the Configuration object.
+ #
# @param [String] file_glob The String that represents the file set. This
# can be a file, directory, or a glob.
# @param [Symbol] label The label that represents the file set.
- def file_set(file_glob=DEFAULT_GLOB, label=:default, &block)
+ def file_set(file_glob=DEFAULT_GLOB, label=:default)
+ log "file sets before: #{@file_sets}"
log "file set label #{label}"
- @temp_style = {}
- instance_eval(&block) if block_given?
+ new_style = Style.new
+ if block_given?
+ yield new_style
+
+ if @file_sets[label]
+ @file_sets[label][:style].merge! new_style
+ end
+ end
+
if @file_sets[label]
@file_sets[label][:file_list].concat file_list(file_glob)
- log "file set: #{@file_sets}"
@file_sets[label][:file_list].uniq!
- @file_sets[label][:style].merge @temp_style
else
@file_sets[label] = {
file_list: file_list(file_glob),
- style: DEFAULT_STYLE.merge(@temp_style)
+ style: @style.to_hash.merge(new_style)
}
end
- @temp_style = {}
+ log "file sets after: #{@file_sets}"
end
- # Implemented for {file_set}, this converts the config file lines that look
- # like methods into a Hash.
- #
- # @return [Hash] The new style as defined by the config file.
- def method_missing(meth, *args, &blk)
- ok_methods = DEFAULT_STYLE.keys
-
- if ok_methods.include? meth
- @temp_style[meth] = args.first
- else
- super(meth, args, blk)
- end
- end
-
# Tries to open the file at the path given at +config_file+ and read in
# the configuration given there.
#
# @param [String] config_file Path to the config file to use.
def load_from_config_file(config_file)
user_config_file = File.expand_path(config_file)
if File.exists? user_config_file
log "Loading config from file: #{user_config_file}"
+
begin
config = instance_eval File.read(user_config_file)
rescue LoadError => ex
raise Tailor::RuntimeError,
"Couldn't load config file: #{user_config_file}"
@@ -246,16 +246,27 @@
log "All files: #{list_with_absolute_paths}"
list_with_absolute_paths.sort
end
+ # Displays the current configuration as a text table.
def show
table = Text::Table.new(horizontal_padding: 4)
table.head = [{ value: 'Configuration', colspan: 2, align: :center }]
table.rows << :separator
- table.rows << ['Style', @file_sets.inspect]
- table.rows << :separator
table.rows << ['Formatters', @formatters]
+
+ @file_sets.each do |label, file_set|
+ table.rows << :separator
+ table.rows << ['Label', label]
+ table.rows << ['Style', '']
+ file_set[:style].each do |k, v|
+ table.rows << ['', "#{k}: #{v}"]
+ end
+
+ table.rows << ['File List', '']
+ file_set[:file_list].each { |file| table.rows << ['', file] }
+ end
puts table
end
end
end