lutaml-diagram 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/workflows/release.yml +39 -0
- data/.gitignore +5 -0
- data/Gemfile +13 -0
- data/README.adoc +205 -0
- data/exe/lutaml-lml +6 -0
- data/lib/lutaml/diagram/version.rb +7 -0
- data/lib/lutaml/diagram.rb +39 -0
- data/lib/lutaml/lml/cli.rb +489 -0
- data/lib/lutaml/lml/formatter/elk.rb +45 -0
- data/lib/lutaml/lml/formatter/graphviz/document_formatter.rb +89 -0
- data/lib/lutaml/lml/formatter/graphviz/html_builder.rb +72 -0
- data/lib/lutaml/lml/formatter/graphviz/node_formatter.rb +89 -0
- data/lib/lutaml/lml/formatter/graphviz/relationship_formatter.rb +130 -0
- data/lib/lutaml/lml/formatter/graphviz.rb +93 -0
- data/lib/lutaml/lml/formatter/svg_pdf/path_parser.rb +134 -0
- data/lib/lutaml/lml/formatter/svg_pdf.rb +285 -0
- data/lib/lutaml/lml/layout/elk_engine.rb +15 -0
- data/lib/lutaml/lml/layout/elk_graph_builder.rb +129 -0
- data/lib/lutaml/lml/layout/elk_svg_markers.rb +45 -0
- data/lib/lutaml/lml/layout/elk_svg_renderer.rb +119 -0
- data/lutaml-diagram.gemspec +40 -0
- metadata +108 -0
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'thor'
|
|
4
|
+
require 'pathname'
|
|
5
|
+
require 'json'
|
|
6
|
+
require 'lutaml/lml'
|
|
7
|
+
|
|
8
|
+
module Lutaml
|
|
9
|
+
module Cli
|
|
10
|
+
# Thor CLI commands for LutaML DSL generation and validation.
|
|
11
|
+
class LmlCommands < Thor
|
|
12
|
+
include ::Lutaml::Lml::HasAttributes
|
|
13
|
+
|
|
14
|
+
SUPPORTED_FORMATS = %w[yaml lutaml exp].freeze
|
|
15
|
+
DEFAULT_INPUT_FORMAT = 'lutaml'
|
|
16
|
+
DEFAULT_OUTPUT_FORMAT = 'svg'
|
|
17
|
+
|
|
18
|
+
EXTENSION_FORMAT_MAP = {
|
|
19
|
+
'.svg' => 'svg', '.svgz' => 'svgz',
|
|
20
|
+
'.png' => 'png', '.gif' => 'gif', '.jpg' => 'jpg', '.jpeg' => 'jpeg',
|
|
21
|
+
'.pdf' => 'pdf', '.ps' => 'ps', '.eps' => 'eps',
|
|
22
|
+
'.dot' => 'dot', '.xdot' => 'xdot',
|
|
23
|
+
'.fig' => 'fig', '.json' => 'json',
|
|
24
|
+
'.imap' => 'imap', '.cmapx' => 'cmapx'
|
|
25
|
+
}.freeze
|
|
26
|
+
|
|
27
|
+
PARSE_HANDLERS = {
|
|
28
|
+
'lutaml' => ->(path) { File.open(path) { |f| Lutaml::Lml.parse(f) } },
|
|
29
|
+
'yaml' => ->(path) { Lutaml::Lml::YamlParser.parse(path.to_s) },
|
|
30
|
+
'yml' => ->(path) { Lutaml::Lml::YamlParser.parse(path.to_s) },
|
|
31
|
+
'exp' => lambda { |path|
|
|
32
|
+
require 'lutaml/express'
|
|
33
|
+
Lutaml::Express::Parsers::Exp.parse(File.new(path))
|
|
34
|
+
},
|
|
35
|
+
}.freeze
|
|
36
|
+
|
|
37
|
+
FORMATTER_ATTR_TARGETS = %i[graph edge node].freeze
|
|
38
|
+
|
|
39
|
+
def initialize(*args)
|
|
40
|
+
super
|
|
41
|
+
@formatter = case options[:layout]
|
|
42
|
+
when 'elk'
|
|
43
|
+
::Lutaml::Formatter::Elk.new
|
|
44
|
+
else
|
|
45
|
+
::Lutaml::Formatter::Graphviz.new
|
|
46
|
+
end
|
|
47
|
+
@out_object = $stdout
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
desc 'generate [PATHS]', 'Generate diagram output from LutaML DSL files'
|
|
51
|
+
long_desc <<-DESC
|
|
52
|
+
Generate diagram output from one or more LutaML DSL files.
|
|
53
|
+
|
|
54
|
+
Supports multiple input formats (lutaml, yaml, exp) and can output
|
|
55
|
+
to various formats (svg, png, dot, pdf, etc.).
|
|
56
|
+
|
|
57
|
+
Examples:
|
|
58
|
+
lutaml lml generate model.lutaml -o diagram.svg
|
|
59
|
+
|
|
60
|
+
lutaml lml generate model.lutaml -o diagram.dot -t dot
|
|
61
|
+
|
|
62
|
+
lutaml lml generate model.lutaml project.lutaml -o output/
|
|
63
|
+
DESC
|
|
64
|
+
method_option :output, type: :string, aliases: '-o',
|
|
65
|
+
desc: 'Output path (file or directory)'
|
|
66
|
+
method_option :formatter, type: :string, aliases: '-f',
|
|
67
|
+
desc: 'Output formatter (default: graphviz)'
|
|
68
|
+
method_option :type, type: :string, aliases: '-t',
|
|
69
|
+
desc: 'Output format type (svg, png, dot, etc.)'
|
|
70
|
+
method_option :input_format, type: :string, aliases: '-i',
|
|
71
|
+
desc: 'Input format (lutaml, yaml, exp)'
|
|
72
|
+
method_option :graph, type: :string, aliases: '-g',
|
|
73
|
+
desc: 'Graph attributes (key=value,key2=value2)'
|
|
74
|
+
method_option :edge, type: :string, aliases: '-e',
|
|
75
|
+
desc: 'Edge attributes (key=value,key2=value2)'
|
|
76
|
+
method_option :node, type: :string, aliases: '-n',
|
|
77
|
+
desc: 'Node attributes (key=value,key2=value2)'
|
|
78
|
+
method_option :all, type: :string, aliases: '-a',
|
|
79
|
+
desc: 'Set attributes for graph, edge, and node'
|
|
80
|
+
method_option :layout, type: :string, enum: %w[graphviz elk], default: 'graphviz',
|
|
81
|
+
desc: 'Layout engine (elk requires the elkrb gem)'
|
|
82
|
+
def generate(*paths)
|
|
83
|
+
assert_input_paths(paths)
|
|
84
|
+
|
|
85
|
+
setup_options
|
|
86
|
+
@paths = paths.map { |path| Pathname.new(path) }
|
|
87
|
+
|
|
88
|
+
assert_output_path
|
|
89
|
+
|
|
90
|
+
@paths.each { |input_path| process_single_file(input_path) }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
desc 'validate [PATHS]', 'Validate LutaML DSL syntax'
|
|
94
|
+
long_desc <<-DESC
|
|
95
|
+
Validate the syntax of one or more LutaML DSL files.
|
|
96
|
+
|
|
97
|
+
Checks for syntax errors and structural issues in the DSL files
|
|
98
|
+
without generating output.
|
|
99
|
+
|
|
100
|
+
Examples:
|
|
101
|
+
lutaml lml validate model.lutaml
|
|
102
|
+
|
|
103
|
+
lutaml lml validate model.lutaml project.lutaml
|
|
104
|
+
DESC
|
|
105
|
+
method_option :input_format, type: :string, aliases: '-i',
|
|
106
|
+
desc: 'Input format (lutaml, yaml, exp)'
|
|
107
|
+
method_option :format, type: :string, enum: %w[text json], default: 'text',
|
|
108
|
+
desc: 'Report format: text (default) or json'
|
|
109
|
+
def validate(*paths)
|
|
110
|
+
assert_input_paths(paths)
|
|
111
|
+
@input_format = options[:input_format] || DEFAULT_INPUT_FORMAT
|
|
112
|
+
diagnostics = paths.flat_map { |path| validate_single_file(path) }
|
|
113
|
+
report_validation(diagnostics)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
desc 'format [PATHS]', 'Render LML files in canonical form'
|
|
117
|
+
long_desc <<-DESC
|
|
118
|
+
Rewrite LML files into the RS 3001 canonical form (deterministic
|
|
119
|
+
member order, two-space layout, canonical attribute forms and
|
|
120
|
+
quoting).
|
|
121
|
+
|
|
122
|
+
With --check, files are not modified: each non-canonical file is
|
|
123
|
+
reported with a diff and the command exits 1.
|
|
124
|
+
|
|
125
|
+
Examples:
|
|
126
|
+
lutaml lml format model.lml
|
|
127
|
+
|
|
128
|
+
lutaml lml format model.lml --check
|
|
129
|
+
DESC
|
|
130
|
+
method_option :check, type: :boolean, default: false,
|
|
131
|
+
desc: 'Report non-canonical files instead of rewriting them'
|
|
132
|
+
def format(*paths)
|
|
133
|
+
assert_input_paths(paths)
|
|
134
|
+
|
|
135
|
+
noncanonical = paths.reject { |path| file_canonical?(path) }
|
|
136
|
+
return if noncanonical.empty?
|
|
137
|
+
|
|
138
|
+
exit 1
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
desc 'compile PATH', 'Compile LML model definitions into Ruby classes'
|
|
142
|
+
long_desc <<-DESC
|
|
143
|
+
Parse an LML file containing model definitions and compile them
|
|
144
|
+
into Ruby classes that can be used to instantiate and validate data.
|
|
145
|
+
|
|
146
|
+
With --namespace, classes are registered as constants in the given
|
|
147
|
+
module (created if absent).
|
|
148
|
+
|
|
149
|
+
With --schema, emits a JSON Schema or YAML Schema document for every
|
|
150
|
+
compiled model instead of the compile listing. Schema output requires
|
|
151
|
+
named classes, so --namespace defaults to Lutaml::Lml::Generated when omitted.
|
|
152
|
+
|
|
153
|
+
Examples:
|
|
154
|
+
lutaml lml compile models.lml
|
|
155
|
+
|
|
156
|
+
lutaml lml compile models.lml --namespace MyModels
|
|
157
|
+
|
|
158
|
+
lutaml lml compile models.lml --schema json -o schemas/
|
|
159
|
+
|
|
160
|
+
lutaml lml compile models.lml --schema yaml -o widget.schema.yaml
|
|
161
|
+
DESC
|
|
162
|
+
method_option :namespace, type: :string, aliases: '-n',
|
|
163
|
+
desc: 'Register compiled classes in a module (created if absent)'
|
|
164
|
+
method_option :schema, type: :string, enum: %w[json yaml],
|
|
165
|
+
desc: 'Emit JSON Schema or YAML Schema for the compiled models'
|
|
166
|
+
method_option :output, type: :string, aliases: '-o',
|
|
167
|
+
desc: 'Schema output path (file for one model, directory for many)'
|
|
168
|
+
def compile(path)
|
|
169
|
+
raise Thor::Error, "File does not exist: #{path}" unless File.exist?(path)
|
|
170
|
+
raise Thor::Error, "Expected a file, got directory: #{path}" if File.directory?(path)
|
|
171
|
+
|
|
172
|
+
ns = options[:namespace] || (::Lutaml::Lml::ModelCompiler::DEFAULT_NAMESPACE if options[:schema])
|
|
173
|
+
result = File.open(path) { |file| Lutaml::Lml::ModelCompiler.new(namespace: ns).compile(file) }
|
|
174
|
+
return emit_schemas(result) if options[:schema]
|
|
175
|
+
|
|
176
|
+
report_compilation(result)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
no_commands do
|
|
180
|
+
def report_compilation(result)
|
|
181
|
+
result.each_key do |name|
|
|
182
|
+
say " compiled: #{name}", :green
|
|
183
|
+
end
|
|
184
|
+
say "\n#{result.size} class(es) compiled", :green
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def emit_schemas(result)
|
|
188
|
+
require 'lutaml/model/schema'
|
|
189
|
+
if options[:output] && Pathname.new(options[:output]).directory?
|
|
190
|
+
generator = "to_#{options[:schema]}"
|
|
191
|
+
schemas = result.transform_values do |klass|
|
|
192
|
+
Lutaml::Model::Schema.public_send(generator, klass, pretty: true)
|
|
193
|
+
end
|
|
194
|
+
write_schemas(schemas)
|
|
195
|
+
else
|
|
196
|
+
# One multi-root document covers every compiled model; each
|
|
197
|
+
# root stays addressable under $defs by class name.
|
|
198
|
+
schema = generate_multi_root_schema(result)
|
|
199
|
+
return puts schema unless options[:output]
|
|
200
|
+
|
|
201
|
+
File.write(options[:output], schema)
|
|
202
|
+
say "Generated: #{options[:output]}", :green
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def generate_multi_root_schema(result)
|
|
207
|
+
klass = options[:schema] == 'yaml' ? Lutaml::Model::Schema::YamlSchema : Lutaml::Model::Schema::JsonSchema
|
|
208
|
+
klass.generate_many(result.values, pretty: true)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def write_schemas(schemas)
|
|
212
|
+
target = Pathname.new(options[:output])
|
|
213
|
+
if schemas.size > 1
|
|
214
|
+
unless target.directory?
|
|
215
|
+
raise Thor::Error,
|
|
216
|
+
'Output path must be a directory if multiple schemas ' \
|
|
217
|
+
'are generated'
|
|
218
|
+
end
|
|
219
|
+
write_schema_files(target, schemas)
|
|
220
|
+
else
|
|
221
|
+
target.write(schemas.values.first)
|
|
222
|
+
say "Generated: #{target}", :green
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def write_schema_files(dir, schemas)
|
|
227
|
+
schemas.each do |name, schema|
|
|
228
|
+
file = dir.join("#{schema_file_stem(name)}.#{options[:schema]}")
|
|
229
|
+
file.write(schema)
|
|
230
|
+
say "Generated: #{file}", :green
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def schema_file_stem(name)
|
|
235
|
+
name.split('::').last.gsub(/[^a-zA-Z0-9]+/, '_').downcase
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def file_canonical?(path)
|
|
239
|
+
input = Pathname.new(path)
|
|
240
|
+
raise Thor::Error, "File does not exist: #{path}" unless input.exist?
|
|
241
|
+
|
|
242
|
+
source = input.read
|
|
243
|
+
canonical = canonical_source(source)
|
|
244
|
+
return true if source == canonical
|
|
245
|
+
|
|
246
|
+
if options[:check]
|
|
247
|
+
say diff_source(source, canonical, path)
|
|
248
|
+
say "#{path} is not canonical", :red
|
|
249
|
+
false
|
|
250
|
+
else
|
|
251
|
+
input.write(canonical)
|
|
252
|
+
say "Formatted: #{path}", :green
|
|
253
|
+
true
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def canonical_source(source)
|
|
258
|
+
document = Lutaml::Lml.parse_document(source)
|
|
259
|
+
::Lutaml::Formatter::Canonical.new(document).render
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def diff_source(source, canonical, path)
|
|
263
|
+
old_lines = source.lines.map(&:chomp)
|
|
264
|
+
new_lines = canonical.lines.map(&:chomp)
|
|
265
|
+
out = ["--- #{path}", "+++ #{path} (canonical)"]
|
|
266
|
+
out.concat(diff_lines(old_lines, new_lines))
|
|
267
|
+
out.join("\n")
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def diff_lines(old_lines, new_lines)
|
|
271
|
+
return ['(diff omitted: file too large)'] if old_lines.length * new_lines.length > 4_000_000
|
|
272
|
+
|
|
273
|
+
table = Array.new(old_lines.length + 1) do
|
|
274
|
+
Array.new(new_lines.length + 1, 0)
|
|
275
|
+
end
|
|
276
|
+
(old_lines.length - 1).downto(0) do |i|
|
|
277
|
+
(new_lines.length - 1).downto(0) do |j|
|
|
278
|
+
table[i][j] = if old_lines[i] == new_lines[j]
|
|
279
|
+
table[i + 1][j + 1] + 1
|
|
280
|
+
else
|
|
281
|
+
[table[i + 1][j], table[i][j + 1]].max
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
walk_diff(old_lines, new_lines, table)
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def walk_diff(old_lines, new_lines, table)
|
|
289
|
+
result = []
|
|
290
|
+
i = 0
|
|
291
|
+
j = 0
|
|
292
|
+
while i < old_lines.length && j < new_lines.length
|
|
293
|
+
if old_lines[i] == new_lines[j]
|
|
294
|
+
result << " #{old_lines[i]}"
|
|
295
|
+
i += 1
|
|
296
|
+
j += 1
|
|
297
|
+
elsif table[i + 1][j] >= table[i][j + 1]
|
|
298
|
+
result << "- #{old_lines[i]}"
|
|
299
|
+
i += 1
|
|
300
|
+
else
|
|
301
|
+
result << "+ #{new_lines[j]}"
|
|
302
|
+
j += 1
|
|
303
|
+
end
|
|
304
|
+
end
|
|
305
|
+
while i < old_lines.length
|
|
306
|
+
result << "- #{old_lines[i]}"
|
|
307
|
+
i += 1
|
|
308
|
+
end
|
|
309
|
+
while j < new_lines.length
|
|
310
|
+
result << "+ #{new_lines[j]}"
|
|
311
|
+
j += 1
|
|
312
|
+
end
|
|
313
|
+
result
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
def assert_input_paths(paths)
|
|
317
|
+
return unless paths.empty?
|
|
318
|
+
|
|
319
|
+
raise Thor::Error,
|
|
320
|
+
'No input files provided. Please specify at least ' \
|
|
321
|
+
'one .lutaml file.'
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def assert_output_path
|
|
325
|
+
return unless @output_path&.file? && @paths.length > 1
|
|
326
|
+
|
|
327
|
+
raise Thor::Error,
|
|
328
|
+
'Output path must be a directory if multiple input files ' \
|
|
329
|
+
'are given'
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
def process_single_file(input_path)
|
|
333
|
+
raise Thor::Error, "File does not exist: #{input_path}" unless input_path.exist?
|
|
334
|
+
|
|
335
|
+
document = parse_document(input_path)
|
|
336
|
+
result = @formatter.format(document)
|
|
337
|
+
write_output(input_path, result)
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
def resolve_output_path(input_path)
|
|
341
|
+
return @out_object unless @output_path
|
|
342
|
+
|
|
343
|
+
path = @output_path
|
|
344
|
+
path = path.join("#{input_path.basename('.*')}.#{@formatter.type}") if path.directory?
|
|
345
|
+
path
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
def write_output(input_path, result)
|
|
349
|
+
target = resolve_output_path(input_path)
|
|
350
|
+
if target.is_a?(Pathname)
|
|
351
|
+
target.open('wb+') { |f| f.write(result) }
|
|
352
|
+
say "Generated: #{target}", :green
|
|
353
|
+
else
|
|
354
|
+
target.puts(result)
|
|
355
|
+
end
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
def validate_single_file(path_string)
|
|
359
|
+
input_path = Pathname.new(path_string)
|
|
360
|
+
return [missing_file_diagnostic(input_path)] unless input_path.exist?
|
|
361
|
+
|
|
362
|
+
document = parse_document(input_path)
|
|
363
|
+
diagnostics = Lutaml::Lml::Validator.diagnostics(document)
|
|
364
|
+
if diagnostics.any?
|
|
365
|
+
if text_output?
|
|
366
|
+
diagnostics.each do |diagnostic|
|
|
367
|
+
say diagnostic_line(diagnostic),
|
|
368
|
+
(diagnostic.severity == 'warning' ? :yellow : :red)
|
|
369
|
+
end
|
|
370
|
+
end
|
|
371
|
+
elsif text_output?
|
|
372
|
+
say "✓ #{input_path}", :green
|
|
373
|
+
end
|
|
374
|
+
diagnostics
|
|
375
|
+
rescue Thor::Error => e
|
|
376
|
+
[contract_diagnostic('contract/LML-C02', e.message, input_path)]
|
|
377
|
+
rescue Lutaml::Lml::Error => e
|
|
378
|
+
[error_diagnostic(e, input_path)]
|
|
379
|
+
rescue StandardError => e
|
|
380
|
+
[contract_diagnostic('contract/LML-C03', e.message, input_path)]
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
def missing_file_diagnostic(input_path)
|
|
384
|
+
contract_diagnostic('contract/LML-C01', 'File does not exist', input_path)
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
def contract_diagnostic(code, message, input_path)
|
|
388
|
+
diagnostic = Diagnostic.build(code, message: message, file: input_path.to_s)
|
|
389
|
+
say "✗ #{diagnostic}", :red if text_output?
|
|
390
|
+
diagnostic
|
|
391
|
+
end
|
|
392
|
+
|
|
393
|
+
def error_diagnostic(error, input_path)
|
|
394
|
+
diagnostic = error.diagnostic
|
|
395
|
+
return diagnostic if diagnostic&.file
|
|
396
|
+
|
|
397
|
+
Diagnostic.build(
|
|
398
|
+
diagnostic&.code || 'parse/LML-001',
|
|
399
|
+
message: diagnostic&.message || error.message.lines.first.to_s.strip,
|
|
400
|
+
file: input_path.to_s,
|
|
401
|
+
line: diagnostic&.line,
|
|
402
|
+
column: diagnostic&.column,
|
|
403
|
+
length: diagnostic&.length,
|
|
404
|
+
)
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
def text_output?
|
|
408
|
+
options[:format] != 'json'
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
def diagnostic_line(diagnostic)
|
|
412
|
+
marker = diagnostic.severity == 'warning' ? '⚠' : '✗'
|
|
413
|
+
"#{marker} #{diagnostic}"
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
def report_validation(diagnostics)
|
|
417
|
+
errors = diagnostics.count { |diagnostic| diagnostic.severity != 'warning' }
|
|
418
|
+
if options[:format] == 'json'
|
|
419
|
+
puts JSON.pretty_generate(diagnostics.map(&:to_hash))
|
|
420
|
+
exit 1 if errors.positive?
|
|
421
|
+
return
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
if errors.positive?
|
|
425
|
+
say "\nValidation failed with #{errors} error(s)", :red
|
|
426
|
+
exit 1
|
|
427
|
+
elsif diagnostics.any?
|
|
428
|
+
say "\nAll files valid (#{diagnostics.size} warning(s))", :yellow
|
|
429
|
+
else
|
|
430
|
+
say "\nAll files valid!", :green
|
|
431
|
+
end
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
def parse_document(input_path)
|
|
435
|
+
handler = PARSE_HANDLERS[@input_format]
|
|
436
|
+
return handler.call(input_path) if handler
|
|
437
|
+
|
|
438
|
+
raise Thor::Error, "Unsupported input format: #{@input_format}"
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
def setup_options
|
|
442
|
+
@formatter = options[:formatter] if options[:formatter]
|
|
443
|
+
@output_path = Pathname.new(options[:output]) if options[:output]
|
|
444
|
+
@input_format = options[:input_format] || DEFAULT_INPUT_FORMAT
|
|
445
|
+
@type = resolve_output_type
|
|
446
|
+
|
|
447
|
+
setup_formatter_options
|
|
448
|
+
end
|
|
449
|
+
|
|
450
|
+
def resolve_output_type
|
|
451
|
+
return options[:type] if options[:type]
|
|
452
|
+
return DEFAULT_OUTPUT_FORMAT unless @output_path
|
|
453
|
+
|
|
454
|
+
ext = @output_path.extname.downcase
|
|
455
|
+
EXTENSION_FORMAT_MAP.fetch(ext, DEFAULT_OUTPUT_FORMAT)
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
def parse_kv_string(str)
|
|
459
|
+
str.split(',').to_h do |pair|
|
|
460
|
+
key, value = pair.split('=', 2)
|
|
461
|
+
[key.strip, value&.strip]
|
|
462
|
+
end
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
def apply_kv_to_formatter(option_key, str)
|
|
466
|
+
targets = option_key == :all ? FORMATTER_ATTR_TARGETS : [option_key]
|
|
467
|
+
parse_kv_string(str).each do |key, value|
|
|
468
|
+
targets.each { |t| @formatter.public_send(t)[key] = value }
|
|
469
|
+
end
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
def setup_formatter_options
|
|
473
|
+
return unless @formatter
|
|
474
|
+
|
|
475
|
+
@formatter.type = @type if @type
|
|
476
|
+
|
|
477
|
+
FORMATTER_ATTR_TARGETS.each do |target|
|
|
478
|
+
apply_kv_to_formatter(target, options[target]) if options[target]
|
|
479
|
+
end
|
|
480
|
+
apply_kv_to_formatter(:all, options[:all]) if options[:all]
|
|
481
|
+
end
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
def self.exit_on_failure?
|
|
485
|
+
true
|
|
486
|
+
end
|
|
487
|
+
end
|
|
488
|
+
end
|
|
489
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Formatter
|
|
5
|
+
# ELK-layout diagram formatter: builds an Elkrb::Graph from the
|
|
6
|
+
# document, lays it out in-process (no external tools) and emits SVG,
|
|
7
|
+
# PS/EPS/EMF through vectory, or PDF through the pdfrb-backed
|
|
8
|
+
# SvgPdf renderer.
|
|
9
|
+
class Elk
|
|
10
|
+
VALID_TYPES = %i[svg ps eps emf pdf].freeze
|
|
11
|
+
|
|
12
|
+
attr_reader :type
|
|
13
|
+
|
|
14
|
+
def initialize(_attributes = {})
|
|
15
|
+
@type = :svg
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def type=(value)
|
|
19
|
+
@type = value.to_sym
|
|
20
|
+
return if VALID_TYPES.include?(@type)
|
|
21
|
+
|
|
22
|
+
raise ArgumentError,
|
|
23
|
+
"unsupported output type #{@type.inspect} (valid: #{VALID_TYPES.join(', ')})"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def format(document)
|
|
27
|
+
graph = Lutaml::Layout::ElkGraphBuilder.new.build(document)
|
|
28
|
+
svg = Lutaml::Layout::ElkEngine.new(input: graph).render(:svg)
|
|
29
|
+
@type == :svg ? svg : convert(svg)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def convert(svg)
|
|
35
|
+
return SvgPdf.new(svg).render if @type == :pdf
|
|
36
|
+
|
|
37
|
+
require 'vectory'
|
|
38
|
+
Vectory::Svg.new(svg).public_send(:"to_#{@type}").content
|
|
39
|
+
rescue LoadError
|
|
40
|
+
raise Error, 'The vectory gem is required for PS/EPS/EMF output: ' \
|
|
41
|
+
'add `gem "vectory"` to your Gemfile'
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Formatter
|
|
5
|
+
class Graphviz < Base
|
|
6
|
+
module DocumentFormatter
|
|
7
|
+
def format_document(node)
|
|
8
|
+
@fontname = node.fontname || DEFAULT_CLASS_FONT
|
|
9
|
+
@node['fontname'] = "#{@fontname}-bold"
|
|
10
|
+
|
|
11
|
+
hide_members, hide_other_classes = extract_fidelity_options(node)
|
|
12
|
+
classes = format_all_classes(node, hide_members)
|
|
13
|
+
associations = build_associations(node, hide_other_classes)
|
|
14
|
+
|
|
15
|
+
build_digraph(classes, associations)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def extract_fidelity_options(node)
|
|
19
|
+
if node.fidelity
|
|
20
|
+
[node.fidelity.hideMembers, node.fidelity.hideOtherClasses]
|
|
21
|
+
else
|
|
22
|
+
[nil, nil]
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def format_all_classes(node, hide_members)
|
|
27
|
+
node.all_classes.map do |class_node|
|
|
28
|
+
graph_node_name = generate_graph_name(class_node.name)
|
|
29
|
+
<<~HEREDOC
|
|
30
|
+
#{graph_node_name} [
|
|
31
|
+
shape="plain"
|
|
32
|
+
fontname="#{@fontname || DEFAULT_CLASS_FONT}"
|
|
33
|
+
label=<#{format_class(class_node, hide_members)}>]
|
|
34
|
+
HEREDOC
|
|
35
|
+
end.join("\n")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def build_associations(node, hide_other_classes)
|
|
39
|
+
associations = collect_all_associations(node)
|
|
40
|
+
associations = sort_by_document_grouping(node.groups, associations) if node.groups
|
|
41
|
+
classes_names = node.classes.map(&:name)
|
|
42
|
+
format_filtered_associations(associations, classes_names, hide_other_classes)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def collect_all_associations(node)
|
|
46
|
+
class_level = node.classifiable_classes
|
|
47
|
+
seen = Set.new
|
|
48
|
+
all = class_level.filter_map(&:associations).flatten + node.associations
|
|
49
|
+
all.uniq { |a| association_key(a) }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def format_filtered_associations(associations, classes_names, hide_other_classes)
|
|
53
|
+
associations.filter_map do |assoc_node|
|
|
54
|
+
next if hide_other_classes && !classes_names.include?(assoc_node.member_end)
|
|
55
|
+
|
|
56
|
+
format_relationship(assoc_node)
|
|
57
|
+
end.join("\n")
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def build_digraph(classes, associations)
|
|
61
|
+
indented_classes = indent_lines(classes)
|
|
62
|
+
indented_assocs = indent_lines(associations)
|
|
63
|
+
|
|
64
|
+
<<~HEREDOC
|
|
65
|
+
digraph G {
|
|
66
|
+
graph [#{@graph}]
|
|
67
|
+
edge [#{@edge}]
|
|
68
|
+
node [#{@node}]
|
|
69
|
+
|
|
70
|
+
#{indented_classes}
|
|
71
|
+
|
|
72
|
+
#{indented_assocs}
|
|
73
|
+
}
|
|
74
|
+
HEREDOC
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def indent_lines(text)
|
|
78
|
+
text.lines.map { |line| " #{line}" }.join.chomp
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
def association_key(assoc)
|
|
84
|
+
[assoc.owner_end.to_s, assoc.member_end.to_s, assoc.name.to_s]
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Formatter
|
|
5
|
+
class Graphviz < Base
|
|
6
|
+
module HtmlBuilder
|
|
7
|
+
EMPTY_MEMBER_TABLE = <<~HEREDOC.chomp
|
|
8
|
+
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
|
|
9
|
+
<TR><TD ALIGN="LEFT"></TD></TR>
|
|
10
|
+
</TABLE>
|
|
11
|
+
HEREDOC
|
|
12
|
+
|
|
13
|
+
def escape_html_chars(text)
|
|
14
|
+
text
|
|
15
|
+
.gsub('&', '&')
|
|
16
|
+
.gsub('<', '<')
|
|
17
|
+
.gsub('>', '>')
|
|
18
|
+
.gsub('[', '[')
|
|
19
|
+
.gsub(']', ']')
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def format_member_rows(members, hide_members, &formatter)
|
|
23
|
+
return EMPTY_MEMBER_TABLE if hide_members || !members&.any?
|
|
24
|
+
|
|
25
|
+
formatter ||= method(:dispatch_format)
|
|
26
|
+
field_rows = members.map do |member|
|
|
27
|
+
%(<TR><TD ALIGN="LEFT">#{formatter.call(member)}</TD></TR>)
|
|
28
|
+
end
|
|
29
|
+
build_member_table(field_rows)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def format_enum_member(member)
|
|
33
|
+
enum_literal?(member) ? format_enum_literal(member) : dispatch_format(member)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def enum_literal?(node)
|
|
37
|
+
node.is_a?(Lml::TopElementAttribute) &&
|
|
38
|
+
node.type.nil? && node.cardinality.nil?
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def build_member_table(field_rows)
|
|
42
|
+
indented = field_rows.map { |row| (' ' * 10) + row }.join("\n")
|
|
43
|
+
<<~HEREDOC.chomp
|
|
44
|
+
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
|
|
45
|
+
#{indented}
|
|
46
|
+
</TABLE>
|
|
47
|
+
HEREDOC
|
|
48
|
+
.concat("\n")
|
|
49
|
+
.concat(' ' * 6)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def build_name_table(name_parts)
|
|
53
|
+
<<~HEREDOC
|
|
54
|
+
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
|
|
55
|
+
#{name_parts.map { |n| %(<TR><TD ALIGN="CENTER">#{n}</TD></TR>) }.join("\n")}
|
|
56
|
+
</TABLE>
|
|
57
|
+
HEREDOC
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def build_table_body(name_html, field_table, method_table)
|
|
61
|
+
[name_html, field_table, method_table].compact.filter_map do |type|
|
|
62
|
+
<<~TEXT
|
|
63
|
+
<TR>
|
|
64
|
+
<TD>#{type}</TD>
|
|
65
|
+
</TR>
|
|
66
|
+
TEXT
|
|
67
|
+
end.join("\n")
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|