Class: Lutaml::CommandLine

Inherits:
Object
  • Object
show all
Includes:
Uml::HasAttributes
Defined in:
lib/lutaml/command_line.rb

Defined Under Namespace

Classes: Error, FileError, NotSupportedInputFormat

Constant Summary collapse

SUPPORTED_FORMATS =
%w[yaml lutaml exp].freeze
DEFAULT_INPUT_FORMAT =
"lutaml".freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Uml::HasAttributes

#update_attributes

Constructor Details

#initialize(attributes = {}, out_object = $stdout) ⇒ CommandLine

Returns a new instance of CommandLine.



24
25
26
27
28
29
30
31
32
33
34
35
# 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

Class Method Details

.run(args, out_object, attributes = {}) ⇒ Object



20
21
22
# File 'lib/lutaml/command_line.rb', line 20

def self.run(args, out_object, attributes = {})
  new(attributes, out_object).run(args)
end

Instance Method Details

#determine_output_path_value(value) ⇒ Object



41
42
43
44
45
46
47
# 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) ⇒ Object

Raises:



53
54
55
56
57
58
59
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
# 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) ⇒ Object



37
38
39
# File 'lib/lutaml/command_line.rb', line 37

def output_path=(value)
  @output_path = determine_output_path_value(value)
end

#paths=(values) ⇒ Object



49
50
51
# File 'lib/lutaml/command_line.rb', line 49

def paths=(values)
  @paths = values.to_a.map { |path| Pathname.new(path) }
end

#run(original_args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lutaml/command_line.rb', line 71

def run(original_args)
  args = original_args.dup
  if args.empty? || args.first.strip == "help"
    print_help
    exit
  end
  begin
    @option_parser.parse!(args)
  rescue StandardError
    nil
  end
  @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