Module: RubyRaider::PluginExposer

Defined in:
lib/plugin/plugin_exposer.rb

Constant Summary collapse

FILE_PATH =
File.expand_path('../commands/loaded_commands.rb', __dir__)

Class Method Summary collapse

Class Method Details

.expose_commands(plugin_name) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/plugin/plugin_exposer.rb', line 9

def expose_commands(plugin_name)
  commands = read_loaded_commands
  return pp 'The plugin is already installed' if commands.any? { |l| l.include?(plugin_name) }

  has_subcommands = commands.any? { |l| l.include?('subcommand') }

  File.open(FILE_PATH, 'w') do |file|
    commands.each do |line|
      file.puts line
      file.puts "require '#{plugin_name}'" if line.include?("require 'thor'")
      if line.strip == 'class LoadedCommands < Thor' && !has_subcommands
        file.puts formatted_command_without_space(plugin_name)
      elsif line.strip == 'class LoadedCommands < Thor' && has_subcommands
        file.puts formatted_command_with_space(plugin_name)
      end
    end
  end
end

.remove_command(plugin_name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/plugin/plugin_exposer.rb', line 28

def remove_command(plugin_name)
  commands = read_loaded_commands
  return pp 'The plugin is not installed' unless commands.any? { |l| l.include?(plugin_name) }

  output_lines = commands.reject { |line| line.include?(plugin_name) }

  File.open(FILE_PATH, 'w') do |file|
    output_lines.each { |line| file.puts line }
  end
end