Class: Lutaml::Cli::LmlCommands

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

Overview

Thor CLI commands for LutaML DSL generation and validation.

Constant Summary collapse

SUPPORTED_FORMATS =
%w[yaml lutaml exp].freeze
DEFAULT_INPUT_FORMAT =
'lutaml'
DEFAULT_OUTPUT_FORMAT =
'svg'
EXTENSION_FORMAT_MAP =
{
  '.svg' => 'svg', '.svgz' => 'svgz',
  '.png' => 'png', '.gif' => 'gif', '.jpg' => 'jpg', '.jpeg' => 'jpeg',
  '.pdf' => 'pdf', '.ps' => 'ps', '.eps' => 'eps',
  '.dot' => 'dot', '.xdot' => 'xdot',
  '.fig' => 'fig', '.json' => 'json',
  '.imap' => 'imap', '.cmapx' => 'cmapx'
}.freeze
PARSE_HANDLERS =
{
  'lutaml' => ->(path) { Lutaml::Lml::Parser.parse(File.new(path)) },
  'yaml' => ->(path) { Lutaml::Lml::YamlParser.parse(path.to_s) },
  'yml' => ->(path) { Lutaml::Lml::YamlParser.parse(path.to_s) },
  'exp' => lambda { |path|
    require 'lutaml/express'
    Lutaml::Express::Parsers::Exp.parse(File.new(path))
  }
}.freeze
FORMATTER_ATTR_TARGETS =
%i[graph edge node].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lml::HasAttributes

#update_attributes

Constructor Details

#initialize(*args) ⇒ LmlCommands

Returns a new instance of LmlCommands.



38
39
40
41
42
# File 'lib/lutaml/lml/cli.rb', line 38

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

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


257
258
259
# File 'lib/lutaml/lml/cli.rb', line 257

def self.exit_on_failure?
  true
end

Instance Method Details

#compile(path) ⇒ Object

Raises:

  • (Thor::Error)


119
120
121
122
123
124
125
126
127
128
129
# File 'lib/lutaml/lml/cli.rb', line 119

def compile(path)
  raise Thor::Error, "File does not exist: #{path}" unless File.exist?(path)

  ns = options[:namespace] ? resolve_namespace(options[:namespace]) : nil
  result = Lutaml::Lml::ModelCompiler.new(namespace: ns).compile(File.new(path))

  result.each_key do |name|
    say "  compiled: #{name}", :green
  end
  say "\n#{result.size} class(es) compiled", :green
end

#generate(*paths) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/lutaml/lml/cli.rb', line 74

def generate(*paths)
  assert_input_paths(paths)

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

  assert_output_path

  @paths.each { |input_path| process_single_file(input_path) }
end

#validate(*paths) ⇒ Object



97
98
99
100
101
# File 'lib/lutaml/lml/cli.rb', line 97

def validate(*paths)
  assert_input_paths(paths)
  errors = paths.map { |p| validate_single_file(p) }.compact
  report_validation(errors)
end