Class: CodeFormatter::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/code_formatter/formatters/formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, path = Dir.pwd) ⇒ Formatter

Returns a new instance of Formatter.

Raises:

  • (TypeError)


10
11
12
13
14
15
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/code_formatter/formatters/formatter.rb', line 7

def config
  @config
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/code_formatter/formatters/formatter.rb', line 8

def path
  @path
end

Instance Method Details

#all_filesObject



60
61
62
# File 'lib/code_formatter/formatters/formatter.rb', line 60

def all_files
  `git ls-files -- #{files_filter}`
end

#commands_exist?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
# 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_filterObject

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/code_formatter/formatters/formatter.rb', line 37

def files_filter
  raise NotImplementedError
end

#files_to_format(force_all) ⇒ Object



55
56
57
58
# 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) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# 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_filesObject

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/code_formatter/formatters/formatter.rb', line 29

def format_files(*)
  raise NotImplementedError
end

#included_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
# 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_filesObject



64
65
66
67
68
# 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_commandObject

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/code_formatter/formatters/formatter.rb', line 33

def shell_command
  raise NotImplementedError
end