Skip to content

Instantly share code, notes, and snippets.

@milligramme
Last active December 15, 2015 11:29
Show Gist options
  • Save milligramme/5253047 to your computer and use it in GitHub Desktop.
Save milligramme/5253047 to your computer and use it in GitHub Desktop.
earthquake.gem pluginを管理する
# -*- coding: utf-8 -*-
# manage plugins for earthquake.gem
#
# usage
# :manage_plugins show installed plugin
# :manage_plugins on <plugin_file_name> enable plugin
# :manage_plugins off <plugin_file_name> disenable plugin
require "fileutils"
def conf
@plugin_dir = config[:plugin_dir]
@disenable = "xxx"
end
Earthquake.init do
command :manage_plugins do
conf
puts @plugin_dir.c(90)
puts Dir::glob("#{@plugin_dir}/*.rb").map { |e| " + #{File.basename(e)}".c(96) }
puts Dir::glob("#{@plugin_dir}/#{@disenable}/*.rb").map { |e| "- #{@disenable}/#{File.basename(e)}".indent(2).c(91) }
end
# disenable plugin
command %r|manage_plugins\s+off\s+(.+)$|, :as => :manage_plugins do |m|
conf
target_plugins = Dir::glob("#{@plugin_dir}/*.rb").select do |v|
File.basename(v) == m[1]
end
puts "No plugin named: '#{m[1]}'".c(93) if target_plugins.empty?
target_plugins.each do |path|
begin
dir, file = File.split(path)
disenabled_plugin_path = File.join(dir, @disenable, file)
unless File.exist?(disenabled_plugin_path)
puts "Disenabled plugin: moved #{file} => #{disenabled_plugin_path}".c(91)
FileUtils.move path, disenabled_plugin_path
else
raise "Duplicated plugins found: #{path} <=> #{disenabled_plugin_path}".c(93)
end
rescue Exception => e
puts e
end
end
# # TODO
# :restart without error when disenabling
Earthquake.reload
end
# enable plugin
command %r|manage_plugins\s+on\s+(.+)$|, :as => :manage_plugins do |m|
conf
#{@plugin_dir}/#{@disenable}
target_plugins = Dir::glob("#{@plugin_dir}/#{@disenable}/*.rb").select do |v|
File.basename(v) == m[1]
end
puts "No plugin named: '#{m[1]}'".c(93) if target_plugins.empty?
target_plugins.each do |path|
begin
dir, file = File.split(path)
enabled_plugin_path = File.join(@plugin_dir, file)
unless File.exist?(enabled_plugin_path)
puts "Enabled plugin: #{file} <= #{path}".c(96)
FileUtils.move path, enabled_plugin_path
else
raise "Duplicated plugins found: #{path} <=> #{enabled_plugin_path}".c(93)
end
rescue Exception => e
puts e
end
end
end
end
# https://gist.github.com/milligramme/5253047
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment