Class: Lutaml::Cli::LmlCommands

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

Overview

LmlCommands provides CLI commands for LutaML DSL diagram generation

This subcommand handles operations related to the LutaML textual DSL notation (.lutaml files), including generating diagrams and validating DSL syntax.

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(*args) ⇒ LmlCommands

Returns a new instance of LmlCommands.



24
25
26
27
28
29
30
31
# File 'lib/lutaml/cli/lml_commands.rb', line 24

def initialize(*args)
  super
  # Only initialize Graphviz formatter if available
  if defined?(::Lutaml::Formatter::Graphviz)
    @formatter = ::Lutaml::Formatter::Graphviz.new
  end
  @out_object = $stdout
end

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/lutaml/cli/lml_commands.rb', line 196

def self.exit_on_failure?
  true
end

Instance Method Details

#generate(*paths) ⇒ Object

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



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
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/lutaml/cli/lml_commands.rb', line 63

def generate(*paths) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  if paths.empty?
    raise Thor::Error,
          "No input files provided. Please specify at least " \
          "one .lutaml file."
  end

  setup_options
  @paths = paths.map { |path| Pathname.new(path) }

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

  @paths.each do |input_path|
    unless input_path.exist?
      raise Thor::Error, "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) }
      say "Generated: #{output_path}", :green
    else
      @out_object.puts(result)
    end
  end
end

#validate(*paths) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/lutaml/cli/lml_commands.rb', line 117

def validate(*paths) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  if paths.empty?
    raise Thor::Error,
          "No input files provided. Please specify at least " \
          "one .lutaml file."
  end

  errors = []
  paths.each do |path_string|
    input_path = Pathname.new(path_string)

    unless input_path.exist?
      errors << "File does not exist: #{input_path}"
      next
    end

    begin
      Lutaml::Parser.parse_into_document(
        File.new(input_path),
        DEFAULT_INPUT_FORMAT,
      )
      say "#{input_path}", :green
    rescue StandardError => e
      errors << "#{input_path}: #{e.message}"
      say "#{input_path}: #{e.message}", :red
    end
  end

  if errors.any?
    say "\nValidation failed with #{errors.size} error(s)", :red
    exit 1
  else
    say "\nAll files valid!", :green
  end
end