class Lutaml::CommandLine

Constants

DEFAULT_INPUT_FORMAT
SUPPORTED_FORMATS

Public Class Methods

new(attributes = {}, out_object = STDOUT) click to toggle source
# File lib/lutaml/command_line.rb, line 24
def initialize(attributes = {}, out_object = STDOUT)
  @formatter = ::Lutaml::Formatter::Graphviz.new
  @verbose = false
  @option_parser = OptionParser.new
  @out_object = out_object

  setup_parser_options

  # rubocop:disable Rails/ActiveRecordAliases
  update_attributes(attributes)
  # rubocop:enable Rails/ActiveRecordAliases
end
run(args, out_object, attributes = {}) click to toggle source
# File lib/lutaml/command_line.rb, line 20
def self.run(args, out_object, attributes = {})
  new(attributes, out_object).run(args)
end

Public Instance Methods

determine_output_path_value(value) click to toggle source
# File lib/lutaml/command_line.rb, line 41
def determine_output_path_value(value)
  unless value.nil? || @output_path = value.is_a?(Pathname)
    return Pathname.new(value.to_s)
  end

  value
end
formatter=(value) click to toggle source
# File lib/lutaml/command_line.rb, line 53
def formatter=(value)
  value = value.to_s.strip.downcase.to_sym
  value = Lutaml::Uml::Formatter.find_by(name: value)
  raise Error, "Formatter not found: #{value}" if value.nil?

  @formatter = value
end
input_format=(value) click to toggle source
# File lib/lutaml/command_line.rb, line 61
def input_format=(value)
  if value.nil?
    @input_format = DEFAULT_INPUT_FORMAT
    return
  end

  @input_format = SUPPORTED_FORMATS.detect { |n| n == value }
  raise(NotSupportedInputFormat, value) if @input_format.nil?
end
output_path=(value) click to toggle source
# File lib/lutaml/command_line.rb, line 37
def output_path=(value)
  @output_path = determine_output_path_value(value)
end
paths=(values) click to toggle source
# File lib/lutaml/command_line.rb, line 49
def paths=(values)
  @paths = values.to_a.map { |path| Pathname.new(path) }
end
run(original_args) click to toggle source
# File lib/lutaml/command_line.rb, line 71
def run(original_args)
  args = original_args.dup
  if args.length.zero? || args.first.strip == "help"
    print_help
    exit
  end
  @option_parser.parse!(args) rescue nil
  @paths = args
  @formatter.type = @type

  if @output_path&.file? && @paths.length > 1
    raise Error,
          'Output path must be a directory \
          if multiple input files are given'
  end

  @paths.each do |input_path_string|
    input_path = Pathname.new(input_path_string)
    unless input_path.exist?
      raise FileError, "File does not exist: #{input_path}"
    end

    document = Lutaml::Parser
      .parse_into_document(File.new(input_path), @input_format)
      .first
    result = @formatter.format(document)

    if @output_path
      output_path = @output_path
      if output_path.directory?
        output_path = output_path.join(input_path
                                        .basename(".*").to_s +
                                      ".#{@formatter.type}")
      end

      output_path.open("w+") { |file| file.write(result) }
    else
      @out_object.puts(result)
    end
  end
end

Protected Instance Methods

print_help() click to toggle source
setup_parser_options() click to toggle source
# File lib/lutaml/command_line.rb, line 142
def setup_parser_options
  @option_parser.banner = ""
  format_desc = "The output formatter (Default: '#{@formatter.name}')"
  @option_parser
    .on("-f",
        "--formatter VALUE",
        format_desc) do |value|
    @formatter = value
  end
  @option_parser
    .on("-t", "--type VALUE", "The output format type") do |value|
      @type = value
    end
  @option_parser
    .on("-o", "--output VALUE", "The output path") do |value|
      @output_path = Pathname.new(value)
    end
  @option_parser
    .on("-i", "--input-format VALUE", "The input format") do |value|
      @input_format = value
    end
  @option_parser
    .on("-h", "--help", "Prints this help") do
    print_help
    exit
  end
  @option_parser.on("-g", "--graph VALUE") do |value|
    Parsers::Attribute.parse(value).each do |key, attr_value|
      @formatter.graph[key] = attr_value
    end
  end

  @option_parser.on("-e", "--edge VALUE") do |value|
    Parsers::Attribute.parse(value).each do |key, attr_value|
      @formatter.edge[key] = attr_value
    end
  end

  @option_parser.on("-n", "--node VALUE") do |value|
    Parsers::Attribute.parse(value).each do |key, attr_value|
      @formatter.node[key] = attr_value
    end
  end

  @option_parser.on("-a", "--all VALUE") do |value|
    Parsers::Attribute.parse(value).each do |key, attr_value|
      @formatter.graph[key] = attr_value
      @formatter.edge[key] = attr_value
      @formatter.node[key] = attr_value
    end
  end
end
text_bold(body = nil) click to toggle source
# File lib/lutaml/command_line.rb, line 115
def text_bold(body = nil)
  text_effect(1, body)
end
text_bold_italic(body = nil) click to toggle source
# File lib/lutaml/command_line.rb, line 123
def text_bold_italic(body = nil)
  text_bold(text_italic(body))
end
text_effect(num, body = nil) click to toggle source
# File lib/lutaml/command_line.rb, line 131
def text_effect(num, body = nil)
  result = "\e[#{num}m"
  result << "#{body}#{text_reset}" unless body.nil?

  result
end
text_italic(body = nil) click to toggle source
# File lib/lutaml/command_line.rb, line 119
def text_italic(body = nil)
  text_effect(3, body)
end
text_reset() click to toggle source
# File lib/lutaml/command_line.rb, line 138
def text_reset
  "\e[0m"
end
text_underline(body = nil) click to toggle source
# File lib/lutaml/command_line.rb, line 127
def text_underline(body = nil)
  text_effect(4, body)
end