Skip to content

Instantly share code, notes, and snippets.

@melborne
Forked from milligramme/manage_plugins.rb
Last active August 29, 2015 14:02
Show Gist options
  • Save melborne/ef108e0270b871cfaeef to your computer and use it in GitHub Desktop.
Save melborne/ef108e0270b871cfaeef to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# manage plugins for earthquake.gem
#
# forked from:
# https://gist.github.com/milligramme/5253047
#
# usage
# :manage_plugins show installed plugin
# :manage_plugins on <plugin_name> enable plugin
# :manage_plugins off <plugin_name> disable plugin
# :manage_plugins rm <plugin_name> delete plugin
require "fileutils"
module ManagePlugins
def mp_plugin_paths
Dir["#{config[:plugin_dir]}/*.rb"]
end
def mp_disabled_dir(sub_name="disabled")
File.join(config[:plugin_dir], sub_name)
end
def mp_disabled_plugin_paths
Dir["#{mp_disabled_dir}/*.rb"]
end
def mp_plugins
mp_plugin_paths.map { |pl| File.basename pl, '.rb' }
end
def mp_disabled_plugins
mp_disabled_plugin_paths.map { |pl| File.basename pl, '.rb' }
end
def mp_subcommand(name, target, &blk)
if target
begin
blk.call(name, target)
rescue Exception => e
puts e
end
else
puts "`#{name}` not exist.".c(93)
end
Earthquake.reload
end
end
include ManagePlugins
Earthquake.init do
command :manage_plugins do
puts config[:plugin_dir].c(90)
puts mp_plugins.map { |pl| " " + pl.c(96) }
puts mp_disabled_plugins.map { |pl| "- " + pl.c(91) }
end
command /^:manage_plugins\s+(on|off|rm)\s+(.+)$/, :as => :manage_plugins do |m|
sub_command, name = m.captures
case sub_command
when 'off'
target = mp_plugin_paths.detect { |path| path.match /#{name}(\.rb)?$/ }
mp_subcommand(name, target) do |_name, _target|
Dir.mkdir(mp_disabled_dir) unless Dir.exist?(mp_disabled_dir)
FileUtils.mv(_target, mp_disabled_dir)
ActiveSupport::Dependencies.loaded.delete(_target.sub /\.rb$/, '')
puts "`#{_name}` plugin disabled.".c(91)
end
when 'on'
target = mp_disabled_plugin_paths.detect { |path| path.match /#{name}(\.rb)?$/ }
mp_subcommand(name, target) do |_name, _target|
dest = File.join(config[:plugin_dir], File.basename(_target))
FileUtils.mv(_target, dest)
ActiveSupport::Dependencies.loaded.add(dest.sub /\.rb$/, '')
puts "`#{_name}` plugin enabled.".c(96)
end
when 'rm'
unless target = mp_plugin_paths.detect { |path| path.match /#{name}(\.rb)?$/ }
target = mp_disabled_plugin_paths.detect { |path| path.match /#{name}(\.rb)?$/ }
end
mp_subcommand(name, target) do |_name, _target|
if confirm("You gonna delete `#{_name}`. Are you sure?")
FileUtils.rm(_target)
ActiveSupport::Dependencies.loaded.delete(_target.sub /\.rb$/, '')
puts "`#{_name}` plugin deleted.".c(91)
else
puts "plugin delete process canceled."
end
end
end
end
end
# https://gist.github.com/melborne/ef108e0270b871cfaeef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment