Skip to content

Commit

Permalink
Add and remove commands dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
aguspe committed Jul 28, 2024
1 parent 8d64d24 commit 2965b1e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/plugin/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'yaml'
require_relative 'plugin_exposer'

module RubyRaider
module Plugin
Expand All @@ -12,6 +13,7 @@ def add_plugin(plugin_name)
pp "Adding #{plugin_name}..."
add_plugin_to_gemfile(plugin_name)
system('bundle install')
PluginExposer.expose_commands(plugin_name)
pp "The plugin #{plugin_name} is added"
end

Expand All @@ -20,6 +22,7 @@ def delete_plugin(plugin_name)

pp "Deleting #{plugin_name}..."
remove_plugin_from_gemfile(plugin_name)
PluginExposer.remove_command(plugin_name)
system('bundle install')
pp "The plugin #{plugin_name} is deleted"
end
Expand Down
40 changes: 40 additions & 0 deletions lib/plugin/plugin_exposer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require 'thor'

module RubyRaider
module PluginExposer
def self.expose_commands(plugin_name)
File.new('lib/commands/loaded_commands.rb', 'w') unless File.exist?('lib/commands/loaded_commands.rb')
File.open('lib/commands/loaded_commands.rb', 'w') do |file|
file.puts '# frozen_string_literal: true'
file.puts
file.puts 'require \'thor\''
file.puts "require \'#{plugin_name}\'"
file.puts 'require_relative \'../plugin/plugin\''
file.puts
file.puts 'module RubyRaider'
file.puts ' class LoadedCommands < Thor'
file.puts " desc \'#{plugin_name} [COMMAND] [NAME]\', \'Plugin commands\'"
file.puts
file.puts ' subcommand \'great_axe\', GreatAxe::PluginCommands'
file.puts ' end'
file.puts 'end'
end
end

def self.remove_command(plugin_name)
File.open('lib/commands/loaded_commands.rb', 'w') do |file|
file.puts '# frozen_string_literal: true'
file.puts
file.puts 'require \'thor\''
file.puts 'require_relative \'../plugin/plugin\''
file.puts
file.puts 'module RubyRaider'
file.puts ' class LoadedCommands < Thor'
file.puts ' end'
file.puts 'end'
end
end
end
end
8 changes: 7 additions & 1 deletion lib/ruby_raider.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require 'great_axe'
require_relative '../lib/plugin/plugin'
require_relative '../lib/commands/plugin_commands'
require_relative '../lib/commands/loaded_commands'
require_relative '../lib/commands/scaffolding_commands'
require_relative '../lib/commands/utility_commands'

Expand Down Expand Up @@ -47,6 +47,12 @@ def version
subcommand 'plugin', PluginCommands
map 'pl' => 'plugin'

if File.readlines(File.expand_path('/Users/apeque01/Desktop/main_folder/Projects/open_source/ruby_raider/lib/commands/loaded_commands.rb')).any? do |line|
line.include?('subcommand')
end
RubyRaider::LoadedCommands.start
end

no_commands do
def current_version = File.read(File.expand_path('version', __dir__)).strip
end
Expand Down

0 comments on commit 2965b1e

Please sign in to comment.