Class: Aspera::Transfer::SpecDoc

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/transfer/spec_doc.rb

Overview

Generate documentation from Schema, for Transfer Spec, or async Conf spec

Class Method Summary collapse

Class Method Details

.man_table(formatter, include_option: false, agent_columns: true, schema: Spec::SCHEMA) ⇒ Array

Returns a table suitable to display in manual.

Parameters:

  • formatter (Cli::Formatter)

    Formatter to use, methods: markdown_text, tick, check_row

  • include_option (Boolean) (defaults to: false)

    true : include CLI options

  • agent_columns (Boolean) (defaults to: true)

    true : include agents columns

  • schema (Hash) (defaults to: Spec::SCHEMA)

    The JSON spec

Returns:

  • (Array)

    a table suitable to display in manual



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/aspera/transfer/spec_doc.rb', line 16

def man_table(formatter, include_option: false, agent_columns: true, schema: Spec::SCHEMA)
  cols = i[name type description]
  cols.insert(-2, *Agent::Factory::ALL.values.map{ |i| i[:short]}.sort) if agent_columns
  rows = []
  schema['properties'].each do |name, info|
    rows.concat(man_table(formatter, include_option: include_option, agent_columns: agent_columns, schema: info).last.map{ |h| h.merge(name: "#{name}.#{h[:name]}")}) if info['type'].eql?('object') && info['properties']
    rows.concat(man_table(formatter, include_option: include_option, agent_columns: agent_columns, schema: info['items']).last.map{ |h| h.merge(name: "#{name}[].#{h[:name]}")}) if info['type'].eql?('array') && info['items'] && info['items']['properties']
    # Manual table
    columns = {
      name:        name,
      type:        info['type'],
      description: []
    }
    # Render Markdown formatting and split lines
    columns[:description] =
      info['description']
        .gsub(Markdown::FORMATS){formatter.markdown_text(Regexp.last_match)}
        .split("\n") if info.key?('description')
    columns[:description].unshift("DEPRECATED: #{info['x-deprecation']}") if info.key?('x-deprecation')
    # Add flags for supported agents in doc
    agents = []
    Agent::Factory::ALL.each_key do |sym|
      agents.push(sym) if info['x-agents'].nil? || info['x-agents'].include?(sym.to_s)
    end
    Aspera.assert(agents.include?(:direct)){"#{name}: x-cli-option requires agent direct (or nil)"} if info['x-cli-option']
    if agent_columns
      Agent::Factory::ALL.each do |sym, names|
        columns[names[:short]] = formatter.tick(agents.include?(sym))
      end
    else
      columns[:description].push("(#{agents.map{ |i| Agent::Factory::ALL[i][:short].to_s.upcase}.sort.join(', ')})") unless agents.length.eql?(Agent::Factory::ALL.length)
    end
    # Only keep lines that are usable in supported agents
    next false if agents.empty?
    columns[:description].push("Allowed values: #{info['enum'].map{ |v| formatter.markdown_text("`#{v}`")}.join(', ')}") if info.key?('enum')
    if include_option
      envvar_prefix = ''
      cli_option =
        if info.key?('x-cli-envvar')
          envvar_prefix = 'env:'
          info['x-cli-envvar']
        elsif info['x-cli-switch']
          info['x-cli-option']
        elsif info['x-cli-option']
          arg_type = info.key?('enum') ? '{enum}' : "{#{[info['type']].flatten.join('|')}}"
          # conversion_tag = info['x-cli-convert']
          conversion_tag = info.key?('x-cli-convert') ? 'conversion' : nil
          sep = info['x-cli-option'].start_with?('--') ? '=' : ' '
          "#{info['x-cli-option']}#{sep}#{"(#{conversion_tag})" if conversion_tag}#{arg_type}"
        end
      short = info.key?('x-cli-short') ? "(#{info['x-cli-short']})" : nil
      columns[:description].push("(#{'special:' if info['x-cli-special']}#{envvar_prefix}#{formatter.markdown_text("`#{cli_option}`")})#{short}") if cli_option
    end
    rows.push(formatter.check_row(columns))
  end
  [cols, rows.sort_by{ |i| i[:name]}]
end