Module: Docscribe::CLI::Generate

Defined in:
lib/docscribe/cli/generate.rb

Overview

Generator for TagPlugin and CollectorPlugin boilerplate.

Usage:

docscribe generate tag MyPlugin
docscribe generate collector MyPlugin
docscribe generate tag MyPlugin --output lib/docscribe_plugins
docscribe generate tag MyPlugin --stdout

Constant Summary collapse

PLUGIN_TYPES =
%w[tag collector].freeze

Class Method Summary collapse

Class Method Details

.run(argv) ⇒ Integer

Run the ‘generate` subcommand.

Parameters:

  • argv (Array<String>)

Returns:

  • (Integer)

    exit code

Raises:

  • (OptionParser::InvalidOption)


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
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/docscribe/cli/generate.rb', line 23

def run(argv)
  opts = {
    output: nil,
    stdout: false
  }

  parser = OptionParser.new do |o|
    o.banner = <<~TEXT
      Usage: docscribe generate <type> <PluginName> [options]

      Types:
        tag         Generate a TagPlugin skeleton
        collector   Generate a CollectorPlugin skeleton

      Options:
    TEXT

    o.on('--output DIR', 'Directory to write the plugin file (default: .)') { |v| opts[:output] = v }
    o.on('--stdout', 'Print the generated plugin to STDOUT instead of writing a file') { opts[:stdout] = true }
    o.on('-h', '--help', 'Show this help') do
      puts o
      return 0
    end
  end

  begin
    parser.parse!(argv)
  rescue OptionParser::InvalidOption => e
    warn e.message
    warn parser
    return 1
  end

  plugin_type = argv.shift
  class_name  = argv.shift

  unless plugin_type && class_name
    warn 'Error: both <type> and <PluginName> are required.'
    warn parser
    return 1
  end

  unless PLUGIN_TYPES.include?(plugin_type)
    warn "Error: unknown type #{plugin_type.inspect}. Must be one of: #{PLUGIN_TYPES.join(', ')}."
    return 1
  end

  unless valid_constant?(class_name)
    warn "Error: #{class_name.inspect} is not a valid Ruby constant name."
    return 1
  end

  content = render(plugin_type, class_name)

  if opts[:stdout]
    puts content
    return 0
  end

  write_plugin(content, plugin_type: plugin_type, class_name: class_name, output_dir: opts[:output] || '.')
end