From 1b137139095043a34ff9b101bfbb044f6f7c3f3f Mon Sep 17 00:00:00 2001 From: aguspe Date: Sat, 27 Jul 2024 22:02:52 +0200 Subject: [PATCH] Start adding dynamic plugin support --- Gemfile | 13 ++++++ lib/commands/plugin_commands.rb | 34 +++++++++++++++ lib/plugin/plugin.rb | 76 +++++++++++++++++++++++++++++++++ lib/ruby_raider.rb | 15 ++++++- plugins.yml | 2 + 5 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 lib/commands/plugin_commands.rb create mode 100644 lib/plugin/plugin.rb create mode 100644 plugins.yml diff --git a/Gemfile b/Gemfile index 7f4f5e9..e9935a9 100644 --- a/Gemfile +++ b/Gemfile @@ -3,3 +3,16 @@ source 'https://rubygems.org' gemspec + + + + + + + + + + + + + diff --git a/lib/commands/plugin_commands.rb b/lib/commands/plugin_commands.rb new file mode 100644 index 0000000..f0c1726 --- /dev/null +++ b/lib/commands/plugin_commands.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'thor' +require_relative '../plugin/plugin' + +module RubyRaider + # :reek:FeatureEnvy { enabled: false } + # :reek:UtilityFunction { enabled: false } + class PluginCommands < Thor + desc 'add [NAME]', 'Adds a plugin to your project' + + def add(plugin_name) + Plugin.add_plugin(plugin_name) + end + + desc 'delete [NAME]', 'Deletes a plugin from your project' + + def delete(plugin_name) + Plugin.delete_plugin(plugin_name) + end + + desc 'local', 'Lists all the plugin in your project' + + def local + pp Plugin.installed_plugins + end + + desc 'list', 'Lists all the available plugin' + + def list + pp Plugin.available_plugins + end + end +end diff --git a/lib/plugin/plugin.rb b/lib/plugin/plugin.rb new file mode 100644 index 0000000..8a01e31 --- /dev/null +++ b/lib/plugin/plugin.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +require 'yaml' + +module RubyRaider + module Plugin + class << self + def add_plugin(plugin_name) + return pp 'The plugin was not found' unless available?(plugin_name) + return pp 'The plugin is already installed' if installed?(plugin_name) + + pp "Adding #{plugin_name}..." + add_plugin_to_gemfile(plugin_name) + system('bundle install') + pp "The plugin #{plugin_name} is added" + end + + def delete_plugin(plugin_name) + return 'The plugin is not installed' unless installed_plugins.include?(plugin_name) + + pp "Deleting #{plugin_name}..." + remove_plugin_from_gemfile(plugin_name) + system('bundle install') + pp "The plugin #{plugin_name} is deleted" + end + + def installed_plugins + parsed_gemfile = File.readlines('Gemfile').map { |line| line.sub('gem ', '').strip.delete("'") } + parsed_gemfile.select { |line| available_plugins.include?(line) } + end + + def available_plugins + plugins['plugins'] + end + + def installed?(plugin_name) + installed_plugins.include?(plugin_name) + end + + def available?(plugin_name) + available_plugins.include?(plugin_name) + end + + def camelize(str) + str.split('_').collect(&:capitalize).join + end + + private + + def add_plugin_to_gemfile(plugin_name) + File.open('Gemfile', 'a') do |file| + file.puts "\n# Ruby Raider Plugins\n" unless File.readlines('Gemfile').grep(/Ruby Raider Plugins/).any? + file.puts "gem '#{plugin_name}'" unless File.readlines('Gemfile').grep(/#{plugin_name}/).any? + end + end + + def remove_plugin_from_gemfile(plugin_name) + input_lines = File.readlines('Gemfile') + output_lines = input_lines.reject do |line| + line.include?(plugin_name) || line.include?('Ruby Raider Plugins') && last_plugin? + end + File.open('Gemfile', 'w') do |file| + output_lines.each { |line| file.puts line } + end + end + + def last_plugin? + installed_plugins.count == 1 + end + + def plugins + @plugins ||= YAML.load_file('plugins.yml') + end + end + end +end diff --git a/lib/ruby_raider.rb b/lib/ruby_raider.rb index 1e45ccd..4f2640e 100644 --- a/lib/ruby_raider.rb +++ b/lib/ruby_raider.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +require 'great_axe' +require_relative '../lib/plugin/plugin' +require_relative '../lib/commands/plugin_commands' require_relative '../lib/commands/scaffolding_commands' require_relative '../lib/commands/utility_commands' @@ -9,7 +12,7 @@ module RubyRaider class Raider < Thor desc 'new [PROJECT_NAME]', 'Creates a new framework based on settings picked' option :parameters, - type: :hash, required: false, desc: 'Parameters to avoid using the menu', aliases: '-p' + type: :hash, required: false, desc: 'Parameters to avoid using the menu', aliases: 'p' def new(project_name) params = options[:parameters] @@ -22,7 +25,7 @@ def new(project_name) MenuGenerator.new(project_name).generate_choice_menu end - map '-n' => 'new' + map 'n' => 'new' desc 'version', 'It shows the version of Ruby Raider you are currently using' @@ -40,8 +43,16 @@ def version subcommand 'utility', UtilityCommands map 'u' => 'utility' + desc 'plugin', 'Provides access to all the plugin commands' + subcommand 'plugin', PluginCommands + map 'pl' => 'plugin' + no_commands do def current_version = File.read(File.expand_path('version', __dir__)).strip + + def load_installed_plugins + + end end end end diff --git a/plugins.yml b/plugins.yml new file mode 100644 index 0000000..95bf38b --- /dev/null +++ b/plugins.yml @@ -0,0 +1,2 @@ +plugins: + - great_axe \ No newline at end of file