-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start adding dynamic plugin support (#111)
* Start adding dynamic plugin support * Correct extra space * Update Gemfile Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Add and remove commands dynamically * Plugin working * Plugin command running * Fix tests * Fix plugins and update read_me * Update version --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
0f21c24
commit f122881
Showing
9 changed files
with
258 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'thor' | ||
require_relative '../plugin/plugin' | ||
|
||
module RubyRaider | ||
class LoadedCommands < Thor | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'yaml' | ||
require_relative 'plugin_exposer' | ||
|
||
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') | ||
PluginExposer.expose_commands(plugin_name) | ||
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) | ||
PluginExposer.remove_command(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 comment_present? | ||
file.puts "gem '#{plugin_name}'" unless plugin_present?(plugin_name) | ||
end | ||
end | ||
|
||
def remove_plugin_from_gemfile(plugin_name) | ||
output_lines = remove_plugins_and_comments(plugin_name) | ||
update_gemfile(output_lines) | ||
end | ||
|
||
def last_plugin? | ||
installed_plugins.count == 1 | ||
end | ||
|
||
def plugins | ||
@plugins ||= YAML.load_file(File.expand_path('plugins.yml')) | ||
end | ||
|
||
def read_gemfile | ||
File.readlines('Gemfile') | ||
end | ||
|
||
def comment_present? | ||
read_gemfile.grep(/Ruby Raider Plugins/).any? | ||
end | ||
|
||
def plugin_present?(plugin_name) | ||
read_gemfile.grep(/#{plugin_name}/).any? | ||
end | ||
|
||
def remove_plugins_and_comments(plugin_name) | ||
read_gemfile.reject do |line| | ||
line.include?(plugin_name) || line.include?('Ruby Raider Plugins') && last_plugin? | ||
end | ||
end | ||
|
||
# :reek:NestedIterators { enabled: false } | ||
def update_gemfile(output_lines) | ||
File.open('Gemfile', 'w') do |file| | ||
output_lines.each { |line| file.puts line } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../plugin/plugin' | ||
|
||
module RubyRaider | ||
module PluginExposer | ||
class << self | ||
FILE_PATH = File.expand_path('../commands/loaded_commands.rb', __dir__) | ||
# :reek:NestedIterators { enabled: false } | ||
def expose_commands(plugin_name) | ||
return pp 'The plugin is already installed' if plugin_present?(plugin_name) | ||
|
||
commands = read_loaded_commands | ||
|
||
File.open(FILE_PATH, 'w') do |file| | ||
commands.each do |line| | ||
file.puts line | ||
file.puts require_plugin(plugin_name, line) | ||
file.puts select_command_formatting(plugin_name, line) | ||
end | ||
end | ||
end | ||
|
||
def remove_command(plugin_name) | ||
return pp 'The plugin is not installed' unless plugin_present?(plugin_name) | ||
|
||
delete_plugin_command(plugin_name) | ||
end | ||
|
||
private | ||
|
||
def any_commands? | ||
read_loaded_commands.any? { |line| line.include?('subcommand') } | ||
end | ||
|
||
def read_loaded_commands | ||
File.readlines(FILE_PATH) | ||
end | ||
|
||
def formatted_command_without_space(plugin_name) | ||
"desc '#{plugin_name}', 'Provides access to all the commands for #{plugin_name}'\n" \ | ||
"subcommand '#{plugin_name}', #{Plugin.camelize(plugin_name)}::PluginCommands" | ||
end | ||
|
||
def formatted_command_with_space(plugin_name) | ||
"\n#{formatted_command_without_space(plugin_name)}" | ||
end | ||
|
||
def plugin_present?(plugin_name) | ||
read_loaded_commands.grep(/#{plugin_name}/).any? | ||
end | ||
|
||
def select_command_formatting(plugin_name, line) | ||
if line.strip == 'class LoadedCommands < Thor' && !any_commands? | ||
formatted_command_without_space(plugin_name) | ||
elsif any_commands? | ||
formatted_command_with_space(plugin_name) | ||
end | ||
end | ||
|
||
def require_plugin(plugin_name, line) | ||
"require '#{plugin_name}'" if line.include?("require 'thor'") && !plugin_present?(plugin_name) | ||
end | ||
|
||
# :reek:NestedIterators { enabled: false } | ||
def delete_plugin_command(plugin_name) | ||
output_lines = read_loaded_commands.reject do |line| | ||
line.include?(plugin_name) if plugin_present?(plugin_name) | ||
end | ||
|
||
File.open(FILE_PATH, 'w') do |file| | ||
output_lines.each { |line| file.puts line } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.9.5 | ||
0.9.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
plugins: | ||
- great_axe |