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"

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.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lutaml/command_line.rb', line 26

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



22
23
24
# File 'lib/lutaml/command_line.rb', line 22

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

Instance Method Details

#determine_output_path_value(value) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/lutaml/command_line.rb', line 43

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:



55
56
57
58
59
60
61
# File 'lib/lutaml/command_line.rb', line 55

def formatter=(value)
  value = value.to_s.strip.downcase.to_sym
  value = Lutaml::Formatter.find_by(name: value)
  raise Error, "Formatter not found: #{value}" if value.nil?

  @formatter = value
end

#input_format=(value) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/lutaml/command_line.rb', line 63

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



39
40
41
# File 'lib/lutaml/command_line.rb', line 39

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

#paths=(values) ⇒ Object



51
52
53
# File 'lib/lutaml/command_line.rb', line 51

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

#run(original_args) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



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
116
117
# File 'lib/lutaml/command_line.rb', line 73

def run(original_args) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  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