Class: ThorishCommand

Inherits:
Samovar::Command
  • Object
show all
Defined in:
bridgetown-core/lib/bridgetown-core/commands/thor_shim.rb

Constant Summary collapse

Actions =
Freyia::Setup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



90
91
92
# File 'bridgetown-core/lib/bridgetown-core/commands/thor_shim.rb', line 90

def options
  @options
end

Class Method Details

.desc(name, description) ⇒ Object



30
31
32
33
# File 'bridgetown-core/lib/bridgetown-core/commands/thor_shim.rb', line 30

def self.desc(name, description)
  @next_cmd_name = name
  @next_cmd_description = description
end

.inherited(klass) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'bridgetown-core/lib/bridgetown-core/commands/thor_shim.rb', line 10

def self.inherited(klass)
  super

  klass.nested :command, {}

  klass.define_method :call do
    Bridgetown::Deprecator.deprecation_message(
      "The #{self.class} command is using the Thor shim. Please migrate to using the " \
      "Samovar-based command API in Bridgetown 2.1+. The Thor shim will be removed " \
      "in a later Bridgetown release."
    )

    if @command
      @command.()
    else
      print_usage
    end
  end
end

.method_added(meth) ⇒ Object

rubocop:disable Metrics



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
84
85
86
87
88
# File 'bridgetown-core/lib/bridgetown-core/commands/thor_shim.rb', line 40

def self.method_added(meth) # rubocop:disable Metrics
  super

  return unless @next_cmd_name

  next_cmd_name = @next_cmd_name
  new_cmd = Class.new(Samovar::Command)
  new_cmd.define_method(:call) do
    parent.instance_variable_set(:@options, @options)
    parent.send(next_cmd_name)
  end
  new_cmd.description = @next_cmd_description

  if @next_cmd_options&.empty?&.!
    next_cmd_options = @next_cmd_options
    new_cmd.options do
      next_cmd_options.each do |opt|
        name = opt[:args].shift
        name = "--#{name}"
        extra_kwargs = {}
        extra_block = nil
        if !opt[:kwargs][:type] || opt[:kwargs][:type] != :boolean
          name = case opt[:kwargs][:type] # rubocop:disable Metrics/BlockNesting
                 when :numeric
                   extra_kwargs[:type] = Integer
                   "#{name} <NUM>"
                 when :array
                   extra_block = proc do |value|
                     value.split(%r{\s*,\s*})
                   end
                   "#{name} <ARR1,ARR2>"
                 else
                   "#{name} <STR>"
                 end
        end

        name = "#{opt[:kwargs][:aliases]}/#{name}" if opt[:kwargs][:aliases]

        option(name, opt[:args][1], required: opt[:kwargs][:required], **extra_kwargs,
               &extra_block)
      end
    end
  end

  table[:command].commands[next_cmd_name] = new_cmd

  @next_cmd_name = nil
  @next_cmd_description = nil
end

.option(name, desc: "...", **kwargs, &block) ⇒ Object



35
36
37
38
# File 'bridgetown-core/lib/bridgetown-core/commands/thor_shim.rb', line 35

def self.option(name, desc: "...", **kwargs, &block)
  @next_cmd_options ||= []
  @next_cmd_options << { args: [name, desc], kwargs:, block: }
end