Class: SCSSLint::AutoCorrect::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/scss_lint/auto_correct/cli.rb

Defined Under Namespace

Classes: DummyWriter, FilesystemWriter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



5
6
7
8
9
10
# File 'lib/scss_lint/auto_correct/cli.rb', line 5

def initialize
  @paths = []
  @options = {}
  @writer = FilesystemWriter.new
  @correctors = Correctors.all
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/scss_lint/auto_correct/cli.rb', line 3

def config
  @config
end

#correctorsObject

Returns the value of attribute correctors.



3
4
5
# File 'lib/scss_lint/auto_correct/cli.rb', line 3

def correctors
  @correctors
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/scss_lint/auto_correct/cli.rb', line 3

def options
  @options
end

#pathsObject

Returns the value of attribute paths.



3
4
5
# File 'lib/scss_lint/auto_correct/cli.rb', line 3

def paths
  @paths
end

#writerObject

Returns the value of attribute writer.



3
4
5
# File 'lib/scss_lint/auto_correct/cli.rb', line 3

def writer
  @writer
end

Instance Method Details

#invoke_correctors(contents) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/scss_lint/auto_correct/cli.rb', line 125

def invoke_correctors(contents)
  @correctors.each do |corrector|
    config = corrector.linter_name ? @config.options['linters'].fetch(corrector.linter_name) : {}
    instance = corrector.new(config)
    contents = instance.call(contents) if instance.enabled? || option?(:force)
  end
  contents
end

#load_config!Object



82
83
84
85
# File 'lib/scss_lint/auto_correct/cli.rb', line 82

def load_config!
  config_file = relevant_configuration_file
  @config = config_file ? SCSSLint::Config.load(config_file) : SCSSLint::Config.default
end

#option?(*names) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/scss_lint/auto_correct/cli.rb', line 30

def option?(*names)
  names.each do |name|
    return true if @options[name.to_sym]
  end
  false
end

#parse_args!(argv) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/scss_lint/auto_correct/cli.rb', line 12

def parse_args!(argv)
  @paths = []
  @options = {}
  argv.each do |arg|
    if arg[0..1] == "--"
      @options[arg[2..-1].to_sym] = true
    elsif arg[0] == "-"
      @options[arg[1..-1].to_sym] = true
    elsif File.directory?(arg)
      Dir["#{arg.chomp('/')}/**/*.scss"].each do |f|
        @paths << f
      end
    else
      @paths << arg
    end
  end
end


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/scss_lint/auto_correct/cli.rb', line 56

def print_help
  puts "Fix your messy code"
  puts "USAGE: scss-lint-auto-correct [OPTIONS] PATH [PATH ...]"
  puts "OPTIONS are:"
  puts "  --help, -h"
  puts "  --verbose, -v"
  puts "  --dry"
  puts "  --list"
  puts "  --force  Ignore config disabling of corrector"
  puts "Only apply specific correctors, using any combination of:"
  @correctors.each do |c|
    puts "  --#{c.short_name}"
  end
  puts "Disable specific correctors, using any combination of:"
  @correctors.each do |c|
    puts "  --no-#{c.short_name}"
  end
end


75
76
77
78
79
80
# File 'lib/scss_lint/auto_correct/cli.rb', line 75

def print_list
  puts "Configured correctors:"
  @correctors.each do |c|
    puts "  #{c.short_name}: #{c.description}"
  end
end

#process_all(paths) ⇒ Object



95
96
97
98
99
# File 'lib/scss_lint/auto_correct/cli.rb', line 95

def process_all(paths)
  paths.each do |path|
    process_file path
  end
end

#process_file(path) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/scss_lint/auto_correct/cli.rb', line 101

def process_file(path)
  puts "Processing file #{path}" if option? :verbose, :v
  before = File.read(path)
  after = invoke_correctors(before)

  write_file(path, after) if before != after
end

#relevant_configuration_fileObject



87
88
89
90
91
92
93
# File 'lib/scss_lint/auto_correct/cli.rb', line 87

def relevant_configuration_file
  if File.exist?(SCSSLint::Config::FILE_NAME)
    SCSSLint::Config::FILE_NAME
  elsif File.exist?(SCSSLint::Config.user_file)
    SCSSLint::Config.user_file
  end
end

#run(argv) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/scss_lint/auto_correct/cli.rb', line 37

def run(argv)
  parse_args! argv
  load_config!
  if option? :help, :h
    print_help
    return 0
  end
  if option? :list
    print_list
    return 0
  end
  if @paths.empty?
    puts "Nothing to process"
    return 1
  end
  process_all @paths
  0
end

#selected_correctorsObject



114
115
116
117
118
119
120
121
122
123
# File 'lib/scss_lint/auto_correct/cli.rb', line 114

def selected_correctors
  selected = []
  unselected = []
  @correctors.each do |c|
    selected << c if option? c.short_name
    unselected << c if option? "no-#{c.short_name}"
  end
  all = selected.any? ? selected : @correctors
  all - unselected
end

#write_file(path, contents) ⇒ Object



109
110
111
112
# File 'lib/scss_lint/auto_correct/cli.rb', line 109

def write_file(path, contents)
  puts "Writing file #{path}" if option? :verbose, :v
  writer.write_file(path, contents)
end