class Lutaml::CommandLine
Constants
- DEFAULT_INPUT_FORMAT
- SUPPORTED_FORMATS
Public Class Methods
new(attributes = {}, out_object = STDOUT)
click to toggle source
# File lib/lutaml/command_line.rb, line 24 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
run(args, out_object, attributes = {})
click to toggle source
# File lib/lutaml/command_line.rb, line 20 def self.run(args, out_object, attributes = {}) new(attributes, out_object).run(args) end
Public Instance Methods
determine_output_path_value(value)
click to toggle source
# File lib/lutaml/command_line.rb, line 41 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)
click to toggle source
# File lib/lutaml/command_line.rb, line 53 def formatter=(value) value = value.to_s.strip.downcase.to_sym value = Lutaml::Uml::Formatter.find_by(name: value) raise Error, "Formatter not found: #{value}" if value.nil? @formatter = value end
input_format=(value)
click to toggle source
# File lib/lutaml/command_line.rb, line 61 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)
click to toggle source
# File lib/lutaml/command_line.rb, line 37 def output_path=(value) @output_path = determine_output_path_value(value) end
paths=(values)
click to toggle source
# File lib/lutaml/command_line.rb, line 49 def paths=(values) @paths = values.to_a.map { |path| Pathname.new(path) } end
run(original_args)
click to toggle source
# File lib/lutaml/command_line.rb, line 71 def run(original_args) args = original_args.dup if args.length.zero? || args.first.strip == "help" print_help exit end @option_parser.parse!(args) rescue nil @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
Protected Instance Methods
print_help()
click to toggle source
# File lib/lutaml/command_line.rb, line 195 def print_help @out_object.puts <<~HELP #{text_bold('Usage:')} lutaml [options] PATHS #{text_bold('Overview:')} Generate output from Supplied language files if supported #{text_bold('Options:')} #{@option_parser} #{text_bold('Paths:')} LUTAML can accept multiple paths for parsing for easier batch processing. The location of the output by default is standard output. The output can be directed to a path with #{text_bold_italic('--output')}, which can be a file or a directory. If the output path is a directory, then the filename will be the same as the input filename, with it's file extension substituted with the #{text_bold_italic('--type')}. #{text_underline('Examples')} `lutaml project.lutaml` Produces DOT notation, sent to standard output `lutaml -o . project.lutaml` Produces DOT notation, written to #{text_italic('./project.dot')} `lutaml -o ./diagram.dot project.lutaml` Produces DOT notation, written to #{text_italic('./diagram.dot')} `lutaml -o ./diagram.png project.lutaml` Produces PNG image, written to #{text_italic('./diagram.png')} `lutaml -t png -o . project.lutaml` Produces PNG image, written to #{text_italic('./project.png')} `lutaml -t png -o . project.lutaml core_ext.lutaml` Produces PNG images, written to #{text_italic('./project.png')} and #{text_italic('./core_ext.png')} #{text_bold('Inputs:')} #{text_underline('Lutaml')} Lutaml dsl syntax files, supports diagram generation(image or dot files) with Graphviz #{text_bold('Formatters:')} #{text_underline('Graphviz')} Generates DOT notation and can use the DOT notation to generate any format Graphviz can produce. The output format is based on #{text_bold_italic('--type')}, which by default is "dot". If #{text_bold_italic('--type')} is not given and #{text_bold_italic('--output')} is, the file extension of the #{text_bold_italic('--output')} path will be used. Valid types/extensions are: #{Lutaml::Formatter::Graphviz::VALID_TYPES.join(', ')} #{text_bold('Options:')} -g, --graph VALUE The graph attributes -e, --edge VALUE The edge attributes -n, --node VALUE The node attributes -a, --all VALUE Set attributes for graph, edge, and node HELP end
setup_parser_options()
click to toggle source
# File lib/lutaml/command_line.rb, line 142 def setup_parser_options @option_parser.banner = "" format_desc = "The output formatter (Default: '#{@formatter.name}')" @option_parser .on("-f", "--formatter VALUE", format_desc) do |value| @formatter = value end @option_parser .on("-t", "--type VALUE", "The output format type") do |value| @type = value end @option_parser .on("-o", "--output VALUE", "The output path") do |value| @output_path = Pathname.new(value) end @option_parser .on("-i", "--input-format VALUE", "The input format") do |value| @input_format = value end @option_parser .on("-h", "--help", "Prints this help") do print_help exit end @option_parser.on("-g", "--graph VALUE") do |value| Parsers::Attribute.parse(value).each do |key, attr_value| @formatter.graph[key] = attr_value end end @option_parser.on("-e", "--edge VALUE") do |value| Parsers::Attribute.parse(value).each do |key, attr_value| @formatter.edge[key] = attr_value end end @option_parser.on("-n", "--node VALUE") do |value| Parsers::Attribute.parse(value).each do |key, attr_value| @formatter.node[key] = attr_value end end @option_parser.on("-a", "--all VALUE") do |value| Parsers::Attribute.parse(value).each do |key, attr_value| @formatter.graph[key] = attr_value @formatter.edge[key] = attr_value @formatter.node[key] = attr_value end end end
text_bold(body = nil)
click to toggle source
# File lib/lutaml/command_line.rb, line 115 def text_bold(body = nil) text_effect(1, body) end
text_bold_italic(body = nil)
click to toggle source
# File lib/lutaml/command_line.rb, line 123 def text_bold_italic(body = nil) text_bold(text_italic(body)) end
text_effect(num, body = nil)
click to toggle source
# File lib/lutaml/command_line.rb, line 131 def text_effect(num, body = nil) result = "\e[#{num}m" result << "#{body}#{text_reset}" unless body.nil? result end
text_italic(body = nil)
click to toggle source
# File lib/lutaml/command_line.rb, line 119 def text_italic(body = nil) text_effect(3, body) end
text_reset()
click to toggle source
# File lib/lutaml/command_line.rb, line 138 def text_reset "\e[0m" end
text_underline(body = nil)
click to toggle source
# File lib/lutaml/command_line.rb, line 127 def text_underline(body = nil) text_effect(4, body) end