Class: MGit::PluginManager
- Inherits:
-
Object
- Object
- MGit::PluginManager
- Defined in:
- lib/m-git/plugin_manager.rb
Class Method Summary collapse
-
.load_gem_plugins(plugin_prefix) ⇒ Object
加载已安装的gem插件.
-
.load_local_plugin_dir(plugin_prefix, plugins_dir) ⇒ Object
加载插件集合目录,该目录下每个文件夹遍历加载一次.
-
.load_local_plugins(plugin_prefix, plugin_root, with_name = nil) ⇒ Object
加载单个本地插件.
-
.loaded_plugins ⇒ Object
加载本地的plugin优先,然后加载gem的plugin [Hash[String]].
- .safe_activate_gem(spec, paths) ⇒ Object
- .safe_activate_plugin_files(plugin_name, paths) ⇒ Object
-
.setup ⇒ Object
1.
Class Method Details
.load_gem_plugins(plugin_prefix) ⇒ Object
加载已安装的gem插件
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/m-git/plugin_manager.rb', line 70 def self.load_gem_plugins(plugin_prefix) glob = "#{plugin_prefix}_plugin#{Gem.suffix_pattern}" gem_plugins = Gem::Specification.latest_specs.map do |spec| spec = spec.to_spec unless spec.is_a?(Gem::Specification) matches = spec.matches_for_glob(glob) [spec, matches] unless matches.empty? end.compact gem_plugins.map do |spec, paths| next if loaded_plugins[spec.name] safe_activate_gem(spec, paths) loaded_plugins[spec.full_name] = paths end end |
.load_local_plugin_dir(plugin_prefix, plugins_dir) ⇒ Object
加载插件集合目录,该目录下每个文件夹遍历加载一次
47 48 49 50 51 52 53 54 |
# File 'lib/m-git/plugin_manager.rb', line 47 def self.load_local_plugin_dir(plugin_prefix, plugins_dir) Dir.foreach(plugins_dir) do |file| next if file == '.' || file == '..' || file == '.DS_Store' plugin_root = File.join(plugins_dir, file) next unless File.directory?(plugin_root) load_local_plugins(plugin_prefix, plugin_root, file) end if Dir.exist?(plugins_dir) end |
.load_local_plugins(plugin_prefix, plugin_root, with_name = nil) ⇒ Object
加载单个本地插件
58 59 60 61 62 63 64 65 66 |
# File 'lib/m-git/plugin_manager.rb', line 58 def self.load_local_plugins(plugin_prefix, plugin_root, with_name = nil) with_name ||= plugin_root glob = "#{plugin_prefix}_plugin#{Gem.suffix_pattern}" glob = File.join(plugin_root, '**', glob) plugin_files = Dir[glob] return if loaded_plugins[with_name] || plugin_files.nil? || plugin_files.empty? safe_activate_plugin_files(with_name, plugin_files) loaded_plugins[with_name] = plugin_files end |
.loaded_plugins ⇒ Object
加载本地的plugin优先,然后加载gem的plugin
- Hash[String]
41 42 43 |
# File 'lib/m-git/plugin_manager.rb', line 41 def self.loaded_plugins @loaded_plugins ||= {} end |
.safe_activate_gem(spec, paths) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/m-git/plugin_manager.rb', line 85 def self.safe_activate_gem(spec, paths) spec.activate paths.each { |path| require(path) } true rescue Exception => exception # rubocop:disable RescueException = "\n---------------------------------------------" << "\n加载插件失败 `#{spec.full_name}`.\n" << "\n#{exception.class} - #{exception.}" << "\n#{exception.backtrace.join("\n")}" << "\n---------------------------------------------\n" warn false end |
.safe_activate_plugin_files(plugin_name, paths) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/m-git/plugin_manager.rb', line 99 def self.safe_activate_plugin_files(plugin_name, paths) paths.each { |path| require(path) } true rescue Exception => exception = "\n---------------------------------------------" << "\n加载插件失败 `#{plugin_name}`.\n" << "\n#{exception.class} - #{exception.}" << "\n#{exception.backtrace.join("\n")}" << "\n---------------------------------------------\n" warn false end |
.setup ⇒ Object
-
先加载本地源码插件
-
搜索加载gem插件
-
处理加载注入的插件
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/m-git/plugin_manager.rb', line 10 def self.setup lib_dir = File.dirname(__FILE__) plugins_dir = File.join(File.dirname(File.dirname(lib_dir)), 'plugins') load_local_plugin_dir('mgit', plugins_dir) load_local_plugin_dir('m-git', plugins_dir) load_gem_plugins('mgit') load_gem_plugins('m-git') inject_flag = '--inject='.freeze inject_arg = ::ARGV.find { |arg| arg.start_with?(inject_flag) } if inject_arg ::ARGV.delete(inject_arg) inject_file = inject_arg[inject_flag.length..-1] if !inject_file.start_with?('~') && !inject_file.start_with?('/') inject_file = File.join(Dir.pwd, inject_file) end inject_file = File.(inject_file) if File.exist?(inject_file) if File.file?(inject_file) require inject_file elsif File.directory?(inject_file) load_local_plugins('mgit', inject_file) load_local_plugins('m-git', inject_file) end end end end |