Skip to content

Commit

Permalink
Start adding dynamic plugin support
Browse files Browse the repository at this point in the history
  • Loading branch information
aguspe committed Jul 27, 2024
1 parent 5cd5516 commit 1b13713
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,16 @@
source 'https://rubygems.org'

gemspec













34 changes: 34 additions & 0 deletions lib/commands/plugin_commands.rb
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
76 changes: 76 additions & 0 deletions lib/plugin/plugin.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 13 additions & 2 deletions lib/ruby_raider.rb
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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]
Expand All @@ -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'

Expand All @@ -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
2 changes: 2 additions & 0 deletions plugins.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
plugins:
- great_axe

0 comments on commit 1b13713

Please sign in to comment.