class YardJunk::Janitor

Constants

BASE_OPTS

Attributes

files[R]
mode[R]
yardopts[R]

Public Class Methods

new(mode: :full, pathes: nil) click to toggle source
# File lib/yard-junk/janitor.rb, line 9
def initialize(mode: :full, pathes: nil)
  @mode = mode
  @files = expand_pathes(pathes)
end

Public Instance Methods

exit_code() click to toggle source
# File lib/yard-junk/janitor.rb, line 49
def exit_code
  case
  when !errors.empty? then 2
  when !problems.empty? then 1
  else 0
  end
end
report(*args, **opts) click to toggle source
# File lib/yard-junk/janitor.rb, line 37
def report(*args, **opts)
  guess_reporters(*args, **opts).each do |reporter|
    reporter.section('Errors', 'severe code or formatting problems', errors)
    reporter.section('Problems', 'mistyped tags or other typos in documentation', problems)

    reporter.stats(**stats)
    reporter.finalize
  end

  exit_code
end
run(*opts) click to toggle source
# File lib/yard-junk/janitor.rb, line 14
def run(*opts)
  YARD::Registry.clear # Somehow loads all Ruby stdlib classes before Rake task started...
  Logger.instance.format = nil # Nothing shouuld be printed

  puts "Running YardJunk janitor (version #{YardJunk::VERSION})...\n\n"

  @duration = Benchmark.realtime do
    command = YARD::CLI::Yardoc.new
    command.run(*prepare_options(opts))
    Resolver.resolve_all(command.options) unless mode == :sanity
  end

  self
end
stats() click to toggle source
# File lib/yard-junk/janitor.rb, line 29
def stats
  {
    errors: errors.count,
    problems: problems.count,
    duration: @duration || 0
  }
end

Private Instance Methods

errors() click to toggle source
# File lib/yard-junk/janitor.rb, line 89
def errors
  messages.select(&:error?)
end
expand_pathes(pathes) click to toggle source
# File lib/yard-junk/janitor.rb, line 97
def expand_pathes(pathes)
  return unless pathes

  Array(pathes)
    .map { |path| File.directory?(path) ? File.join(path, '**', '*.*') : path }
    .flat_map(&Dir.method(:[]))
    .map(&File.method(:expand_path))
end
guess_reporters(*symbols, **symbols_with_args) click to toggle source

TODO: specs for the logic

# File lib/yard-junk/janitor.rb, line 107
def guess_reporters(*symbols, **symbols_with_args)
  symbols
    .to_h { |sym| [sym, nil] }.merge(symbols_with_args)
    .map { |sym, args| ["#{sym.to_s.capitalize}Reporter", args] }
    .each { |name,|
      Janitor.const_defined?(name) or fail(ArgumentError, "Reporter #{name} not found")
    }
    .map { |name, args| Janitor.const_get(name).new(*args) }
end
messages() click to toggle source
# File lib/yard-junk/janitor.rb, line 80
def messages
  # FIXME: dropping Undocumentable here is not DRY
  @messages ||= YardJunk::Logger
                .instance
                .messages
                .grep_v(Logger::Undocumentable)
                .select { |m| !files || !m.file || files.include?(File.expand_path(m.file)) }
end
prepare_options(opts) click to toggle source
# File lib/yard-junk/janitor.rb, line 63
def prepare_options(opts)
  case
  when mode == :full || mode == :sanity && files.nil?
    [*BASE_OPTS, *opts]
  when mode == :sanity
    # TODO: specs
    [*BASE_OPTS, '--no-yardopts', *yardopts_with_files(files)]
  else
    fail ArgumentError, "Undefined mode: #{mode.inspect}"
  end
end
problems() click to toggle source
# File lib/yard-junk/janitor.rb, line 93
def problems
  messages.select(&:warn?)
end
yardopts_with_files(files) click to toggle source
# File lib/yard-junk/janitor.rb, line 75
def yardopts_with_files(files)
  # Use all options from .yardopts file, but replace file lists
  YardOptions.new.remove_option('--files').set_files(*files)
end