class CodeFormatter::Formatter

Attributes

config[R]
path[R]

Public Class Methods

new(config, path = Dir.pwd) click to toggle source
# File lib/code_formatter/formatters/formatter.rb, line 10
def initialize(config, path = Dir.pwd)
  raise TypeError unless config.is_a? Configuration
  raise TypeError unless Dir.exist?(path)
  @config = config
  @path = path
end

Public Instance Methods

all_files() click to toggle source
# File lib/code_formatter/formatters/formatter.rb, line 60
def all_files
  `git ls-files -- #{files_filter}`
end
commands_exist?() click to toggle source
# File lib/code_formatter/formatters/formatter.rb, line 41
def commands_exist?
  unless Environment.exist? 'git'
    warn 'warning: failed to find git command'
    return false
  end

  unless Environment.exist? shell_command
    warn "warning: [#{shell_command}] not found (to install it run 'brew install #{shell_command}')"
    return false
  end

  true
end
excluded_file?(file) click to toggle source
# File lib/code_formatter/formatters/formatter.rb, line 81
def excluded_file?(file)
  excluded_files = config.excluded_files.map { |f| File.expand_path(f, path) }
  excluded_files.any? { |f| file.start_with?(f) }
end
files_filter() click to toggle source
# File lib/code_formatter/formatters/formatter.rb, line 37
def files_filter
  raise NotImplementedError
end
files_to_format(force_all) click to toggle source
# File lib/code_formatter/formatters/formatter.rb, line 55
def files_to_format(force_all)
  files = force_all ? all_files : modified_and_staged_files
  files.lines.uniq.map { |f| File.expand_path f.strip }.select { |f| format_file? f }
end
format(force_all = false) click to toggle source
# File lib/code_formatter/formatters/formatter.rb, line 17
def format(force_all = false)
  return unless commands_exist?

  files = files_to_format(force_all)
  if files.empty?
    puts "[#{shell_command}]: everything is up-to-date"
  else
    puts "[#{shell_command}]: going to format files:\n#{files.join('\n')}"
    format_files(files)
  end
end
format_file?(file) click to toggle source
# File lib/code_formatter/formatters/formatter.rb, line 70
def format_file?(file)
  return false unless File.file?(file) && File.exist?(file)
  return false if excluded_file?(file)
  included_file?(file)
end
format_files(*) click to toggle source
# File lib/code_formatter/formatters/formatter.rb, line 29
def format_files(*)
  raise NotImplementedError
end
included_file?(file) click to toggle source
# File lib/code_formatter/formatters/formatter.rb, line 76
def included_file?(file)
  included_files = config.included_files.map { |f| File.expand_path(f, path) }
  included_files.any? { |f| file.start_with?(f) }
end
modified_and_staged_files() click to toggle source
# File lib/code_formatter/formatters/formatter.rb, line 64
def modified_and_staged_files
  modified_and_untracked = `git ls-files --others --exclude-standard -m -- #{files_filter}`
  staged = `git diff --cached --name-only -- #{files_filter}`
  modified_and_untracked + staged
end
shell_command() click to toggle source
# File lib/code_formatter/formatters/formatter.rb, line 33
def shell_command
  raise NotImplementedError
end