Class: Tailor::Configuration

Inherits:
Object
  • Object
show all
Includes:
Logger::Mixin
Defined in:
lib/tailor/configuration.rb,
lib/tailor/configuration/style.rb,
lib/tailor/configuration/file_set.rb

Overview

Pulls in any configuration from the places configuration can be set:

1. ~/.tailorrc
2. CLI options
3. Default options

It then basically represents a list of “file sets” and the rulers that should be applied against each file set.

If a file list is given from the CLI and a configuration file is given/found, tailor uses the style settings for the default file set and only checks the default file set.

Defined Under Namespace

Classes: FileSet, Style

Constant Summary collapse

DEFAULT_RC_FILE =
Dir.home + '/.tailorrc'
DEFAULT_PROJECT_CONFIG =
Dir.pwd + '/.tailor'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger::Mixin

included

Constructor Details

#initialize(runtime_file_list = nil, options = nil) ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

  • runtime_file_list (Array) (defaults to: nil)
  • options (OpenStruct) (defaults to: nil)

Options Hash (options):

  • config_file (String)
  • formatters (Array)
  • style (Hash)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tailor/configuration.rb', line 45

def initialize(runtime_file_list=nil, options=nil)
  @formatters = %w[text]
  @file_sets = {}
  @output_file = ''
  @runtime_file_list = runtime_file_list
  log "Got runtime file list: #{@runtime_file_list}"

  @options = options
  log "Got options: #{@options}"

  unless @options.nil?
    @config_file = @options.config_file unless @options.config_file.empty?
  end
end

Instance Attribute Details

#file_setsHash (readonly)

Returns:

  • (Hash)


32
33
34
# File 'lib/tailor/configuration.rb', line 32

def file_sets
  @file_sets
end

#formatters(*new_formatters) ⇒ Array (readonly)

Returns The list of formatters.

Returns:

  • (Array)

    The list of formatters.



35
36
37
# File 'lib/tailor/configuration.rb', line 35

def formatters
  @formatters
end

#output_fileString (readonly)

Returns:



38
39
40
# File 'lib/tailor/configuration.rb', line 38

def output_file
  @output_file
end

Class Method Details

.defaultHash

Returns:

  • (Hash)


27
28
29
# File 'lib/tailor/configuration.rb', line 27

def self.default
  new
end

Instance Method Details

#config_fileString

Returns Name of the config file to use.

Returns:

  • (String)

    Name of the config file to use.



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tailor/configuration.rb', line 105

def config_file
  return @config_file if @config_file

  if File.exists?(DEFAULT_PROJECT_CONFIG)
    return @config_file = DEFAULT_PROJECT_CONFIG
  end

  if File.exists?(DEFAULT_RC_FILE)
    return @config_file = DEFAULT_RC_FILE
  end
end

#file_set(file_expression = 'lib/**/*.rb', label = :default) {|new_style| ... } ⇒ Object

Adds a file set to the list of file sets in the Configuration object.

Parameters:

  • file_expression (String) (defaults to: 'lib/**/*.rb')

    The String that represents the file set. This can be a file, directory, or a (Ruby Dir) glob.

  • label (Symbol) (defaults to: :default)

    The label that represents the file set.

Yields:

  • (new_style)


200
201
202
203
204
205
206
207
208
209
# File 'lib/tailor/configuration.rb', line 200

def file_set(file_expression='lib/**/*.rb', label=:default)
  log "file sets before: #{@file_sets}"
  log "file set label #{label}"
  new_style = Style.new

  yield new_style if block_given?

  @file_sets[label] = FileSet.new(file_expression, new_style)
  log "file sets after: #{@file_sets}"
end

#get_file_sets_from_cli_optsObject

If any files are given from the CLI, this gets that list of files and replaces those in any :default file set.



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/tailor/configuration.rb', line 160

def get_file_sets_from_cli_opts
  return if @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, _| k != :default }

  if @file_sets.include? :default
    @file_sets[:default].file_list = @runtime_file_list
  else
    @file_sets = { default: FileSet.new(@runtime_file_list) }
  end
end

#get_file_sets_from_config_fileObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/tailor/configuration.rb', line 117

def get_file_sets_from_config_file
  return if @config_from_file.file_sets.empty?

  @config_from_file.file_sets.each do |label, file_set|
    log "label: #{label}"
    log "file set file list: #{file_set[:file_list]}"
    log "file set style: #{file_set[:style]}"

    if @file_sets[label]
      log 'label already exists.  Updating...'
      @file_sets[label].update_file_list(file_set[:file_list])
      @file_sets[label].update_style(file_set[:style])
    else
      log "Creating new label: #{label}"
      @file_sets[label] =
        FileSet.new(file_set[:file_list], file_set[:style])
    end
  end
end

#get_formatters_from_cli_optsObject



181
182
183
184
185
186
# File 'lib/tailor/configuration.rb', line 181

def get_formatters_from_cli_opts
  unless @options.nil? || @options.formatters.empty? || @options.formatters.nil?
    @formatters = @options.formatters
    log "@formatters is now: '#{@formatters}'"
  end
end

#get_formatters_from_config_fileObject



137
138
139
140
141
142
# File 'lib/tailor/configuration.rb', line 137

def get_formatters_from_config_file
  return if @config_from_file.formatters.empty?

  @formatters = @config_from_file.formatters
  log "@formatters is now #{@formatters}"
end

#get_output_file_from_cli_optsObject



174
175
176
177
178
179
# File 'lib/tailor/configuration.rb', line 174

def get_output_file_from_cli_opts
  unless @options.nil? || @options.output_file.empty? || @options.output_file.nil?
    @output_file = @options.output_file
    log "@output_file is now: '#{@output_file}'"
  end
end

#get_style_from_cli_optsObject



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/tailor/configuration.rb', line 144

def get_style_from_cli_opts
  return unless @options && @options.style

  @options.style.each do |property, value|
    @file_sets.keys.each do |label|
      if value == :off || value == 'off'
        @file_sets[label].style[property][1] = { level: :off }
      else
        @file_sets[label].style[property][0] = value
      end
    end
  end
end

#load!Object

Call this to load settings from the config file and from CLI options.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tailor/configuration.rb', line 61

def load!
  if config_file
    load_from_config_file(config_file)

    if @config_from_file
      get_formatters_from_config_file
      #get_file_sets_from_config_file unless @runtime_file_list
      get_file_sets_from_config_file
    end
  else
    log 'Creating default file set...'
    @file_sets = { default: FileSet.new(@runtime_file_list) }
  end

  get_output_file_from_cli_opts
  get_formatters_from_cli_opts
  get_file_sets_from_cli_opts
  get_style_from_cli_opts
end

#load_from_config_file(config_file) ⇒ Object

Tries to open the file at the path given at config_file and read in the configuration given there.

Parameters:

  • config_file (String)

    Path to the config file to use.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tailor/configuration.rb', line 85

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_from_file =
        instance_eval(File.read(user_config_file), user_config_file)
      log "Got new config from file: #{user_config_file}"
    rescue LoadError
      raise Tailor::RuntimeError,
        "Couldn't load config file: #{user_config_file}"
    end
  else
    abort "No config file found at #{user_config_file}."
  end
end

#recursive_file_set(file_expression, label = :default) ⇒ Object

A helper to #file_set that allows you to specify ‘*.rb’ to get all files ending with .rb in your current path and deeper.

Parameters:

  • file_expression (String)

    The expression to match recursively.

  • label (Symbol) (defaults to: :default)

    The file set label to use.



216
217
218
219
220
# File 'lib/tailor/configuration.rb', line 216

def recursive_file_set(file_expression, label=:default)
  file_set("*/**/#{file_expression}", label) do |style|
    yield style if block_given?
  end
end

#showObject

Displays the current configuration as a text table.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/tailor/configuration.rb', line 223

def show
  table = Text::Table.new(horizontal_padding: 4)
  table.head = [{ value: 'Configuration', colspan: 2, align: :center }]
  table.rows << :separator
  table.rows << ['Formatters', @formatters]
  table.rows << ['Output File', @output_file]

  @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