Class: Xcop::CLI

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

Overview

Command line interface.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2017-2026 Yegor Bugayenko

License

MIT

Constant Summary collapse

EXTENSIONS =

Extensions recognized when a directory is passed as input.

%w[xml xsd xhtml xsl html].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, nocolor: false) ⇒ CLI

Returns a new instance of CLI.



18
19
20
21
# File 'lib/xcop/cli.rb', line 18

def initialize(files, nocolor: false)
  @files = files.flat_map { |f| File.directory?(f) ? Xcop::CLI.expand(f) : [f] }
  @nocolor = nocolor
end

Class Method Details

.expand(dir) ⇒ Object

Recursively collect XML-like files inside a directory.



24
25
26
# File 'lib/xcop/cli.rb', line 24

def self.expand(dir)
  EXTENSIONS.flat_map { |ext| Dir.glob(File.join(dir, '**', "*.#{ext}")) }.sort
end

Instance Method Details

#fixObject

Fix them all.



41
42
43
44
45
46
# File 'lib/xcop/cli.rb', line 41

def fix
  @files.each do |f|
    Xcop::Document.new(f).fix
    yield(f) if block_given?
  end
end

#runObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/xcop/cli.rb', line 28

def run
  @files.each do |f|
    doc = Xcop::Document.new(f)
    diff = doc.diff(nocolor: @nocolor)
    unless diff.empty?
      puts diff
      raise "Invalid XML formatting in #{f}"
    end
    yield(f) if block_given?
  end
end