Class: Erbf::CLI

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

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



13
14
15
16
17
18
# File 'lib/erbf/cli.rb', line 13

def initialize
  @command = :format
  @files = []
  @input = nil
  @config_path = nil
end

Class Method Details

.callObject



9
10
11
# File 'lib/erbf/cli.rb', line 9

def self.call(...)
  new.call(...)
end

Instance Method Details

#call(argv, stdin, stdout = $stdout, stderr = $stderr) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/erbf/cli.rb', line 92

def call(argv, stdin, stdout = $stdout, stderr = $stderr)
  parse!(argv, stdin)
  execute(stdout, stderr) ? 0 : 1
rescue Error => e
  stderr.puts "#{e.message}\n\n"
  stderr.puts options
  1
end

#execute(stdout = $stdout, stderr = $stderr) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/erbf/cli.rb', line 50

def execute(stdout = $stdout, stderr = $stderr)
  erbf = Erbf.new(config_file: @config_path)
  case @command
  when :help
    stdout.puts options
    true
  when :version
    stdout.puts Erbf::VERSION
    true
  when :format
    @files.each do |path|
      content = File.read(path)
      stdout.puts erbf.format_code(content)
    end
    stdout.puts erbf.format_code(@input) if @input
    true
  when :check
    success = true
    @files.each do |path|
      original = File.read(path)
      formatted = erbf.format_code(original)
      formatted = "#{formatted}\n"
      next if original == formatted

      stderr.puts "#{path} needs to be formatted"
      success = false
    end
    success
  when :write
    @files.each do |path|
      original = File.read(path)
      formatted = erbf.format_code(original)
      formatted = "#{formatted}\n"
      next if original == formatted

      File.write(path, formatted)
      stdout.puts "Formatted: #{path}"
    end
    true
  end
end

#optionsObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/erbf/cli.rb', line 101

def options
  @options ||=
    OptionParser.new do |opts|
      opts.banner = <<~USAGE
        Usage: erbf [options] [files/directories/glob]

        By default the output is written to stdout

        Output options:

      USAGE
      opts.on("-c", "--check", "Check if the files are formatted") do
        raise Error, "incompatible options: --#{@command} and --check" if @command != :format
        @command = :check
      end
      opts.on("-w", "--write", "Format the files in-place") do
        raise Error, "incompatible options: --#{@command} and --write" if @command != :format
        @command = :write
      end
      opts.separator <<~USAGE

        Other options:

      USAGE
      opts.on("--config PATH", String, "Use a config file at a different location") do |path|
        @config_path = path
      end
      opts.on("-h", "--help", "Show this help") do
        raise Error, "incompatible options: --#{@command} and --help" if @command != :format
        @command = :help
      end
      opts.on("-v", "--version", "Show erbf version") do
        raise Error, "incompatible options: --#{@command} and --version" if @command != :format
        @command = :version
      end
      opts.separator ""
    end
end

#parse!(argv, stdin) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/erbf/cli.rb', line 20

def parse!(argv, stdin)
  argv_left = argv.dup

  begin
    options.parse!(argv_left)
  rescue OptionParser::MissingArgument, OptionParser::InvalidOption => e
    raise Error, e.message
  end

  if @command == :format
    @input = stdin.read if (argv_left.empty? && !stdin.tty?) || argv_left.include?("-")
    argv_left -= ["-"]
  end

  @files =
    argv_left
      .flat_map do |path|
        paths = Dir[path]
        raise Error, "invalid file/directory/glob: #{path}" if paths.empty?

        dirs, files = paths.partition { |p| File.directory?(p) }
        files + dirs.flat_map { |d| Dir["#{d}/**/*.html.erb"] }
      end
      .uniq

  if @input.nil? && @files.empty? && %i[format check write].include?(@command)
    raise Error, "no file/directory/glob specified"
  end
end