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.



19
20
21
22
23
24
25
26
# File 'lib/lutaml/cli/lml_commands.rb', line 19

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)


200
201
202
# File 'lib/lutaml/cli/lml_commands.rb', line 200

def self.exit_on_failure?
  true
end

Instance Method Details

#generate(*paths) ⇒ Object

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



58
59
60
61
62
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
# File 'lib/lutaml/cli/lml_commands.rb', line 58

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 = parse_document(input_path)
    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



110
111
112
113
114
115
116
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
# File 'lib/lutaml/cli/lml_commands.rb', line 110

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
      parse_document(input_path)
      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