class Bashly::Script::Formatter

Attributes

mode[R]
script[R]

Public Class Methods

new(script, mode: nil) click to toggle source
# File lib/bashly/script/formatter.rb, line 9
def initialize(script, mode: nil)
  @script = script
  @mode = mode&.to_s || 'internal'
end

Public Instance Methods

formatted_script() click to toggle source
# File lib/bashly/script/formatter.rb, line 14
def formatted_script
  case mode
  when 'internal' then script.gsub(/\s+\n/m, "\n\n")
  when 'external' then shfmt_result
  when 'none' then script
  else custom_formatter_result mode
  end
end

Private Instance Methods

custom_formatter_result(command) click to toggle source
# File lib/bashly/script/formatter.rb, line 29
def custom_formatter_result(command)
  command = Shellwords.split command if command.is_a? String

  begin
    output, error, status = Open3.capture3(*command, stdin_data: script)
  rescue Errno::ENOENT
    raise Error, "Command not found: g`#{command.first}`"
  end

  raise Error, "Failed running g`#{Shellwords.join command}`:\n\n#{error}" unless status.success?

  output
end
shfmt_result() click to toggle source
# File lib/bashly/script/formatter.rb, line 25
def shfmt_result
  custom_formatter_result %w[shfmt --case-indent --indent 2]
end