Class: Belt::CLI::PluginCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/belt/cli/plugin_command.rb

Overview

Scaffold a new Belt plugin gem (similar to rails plugin new).

Usage:

belt plugin new messaging
belt plugin new belt-pay --path ~/code

Creates a gem that registers with Belt's GeneratorRegistry via:

lib/belt/generators/<name>_generator.rb

Constant Summary collapse

TEMPLATE_DIR =
File.expand_path('../../templates/plugin', __dir__)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ PluginCommand

Returns a new instance of PluginCommand.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/belt/cli/plugin_command.rb', line 72

def initialize(args)
  @raw_name = nil
  @path = Dir.pwd
  @summary = nil
  @force = false

  i = 0
  while i < args.length
    arg = args[i]
    case arg
    when '--path'
      i += 1
      @path = args[i] || Dir.pwd
    when /^--path=/
      @path = arg.split('=', 2).last
    when '--summary'
      i += 1
      @summary = args[i]
    when /^--summary=/
      @summary = arg.split('=', 2).last
    when '--force'
      @force = true
    when '--help', '-h'
      self.class.print_help
      exit 0
    else
      if arg.start_with?('-')
        puts "Unknown option: #{arg}"
        exit 1
      end
      @raw_name ||= arg
    end
    i += 1
  end
end

Class Method Details



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
# File 'lib/belt/cli/plugin_command.rb', line 35

def self.print_help
  puts <<~HELP
    Manage Belt plugin gems.

    Usage:
      belt plugin new <name> [options]

    Arguments:
      name                    Plugin name (e.g. messaging, pay, or belt-messaging)

    Options:
      --path DIR              Parent directory for the gem (default: current directory)
      --summary TEXT          Short gem summary for the gemspec
      --force                 Overwrite files if the directory already exists
      -h, --help              Show this help

    Examples:
      belt plugin new messaging
      belt plugin new pay --path ~/Code
      belt plugin new belt-notifications --summary "Push notifications for Belt apps"

    What you get:
      A standalone gem (belt-<name>) with:
        - Module under Belt::<Name>
        - Generator at lib/belt/generators/<name>_generator.rb
        - Configuration stub, version, RSpec, README, AGENTS.md
        - Hooked into `belt generate <name>` / `belt destroy <name>` once
          the gem is in an app's Gemfile

    Reference plugins:
      belt-messaging  — SMS via AWS End User Messaging
      belt-pay        — Stripe payments and subscriptions

    See the Belt README (Plugins / Contributing) and AGENTS.md for the full guide.
  HELP
end

.run(args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/belt/cli/plugin_command.rb', line 19

def self.run(args)
  subcommand = args.shift

  case subcommand
  when 'new'
    new(args).generate
  when nil, '--help', '-h', 'help'
    print_help
  else
    puts "Unknown plugin subcommand: #{subcommand}"
    puts ''
    print_help
    exit 1
  end
end

Instance Method Details

#generateObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/belt/cli/plugin_command.rb', line 108

def generate
  if @raw_name.nil? || @raw_name.strip.empty?
    puts 'Usage: belt plugin new <name> [options]'
    puts "Run 'belt plugin --help' for more information."
    exit 1
  end

  normalize_names!

  if Dir.exist?(@gem_dir) && !@force
    puts "✗ Directory already exists: #{@gem_dir}"
    puts '  Use --force to overwrite, or choose a different name/path.'
    exit 1
  end

  FileUtils.mkdir_p(@gem_dir)
  write_files
  print_success
end