Class: KPM::PluginsManager
- Inherits:
-
Object
- Object
- KPM::PluginsManager
- Defined in:
- lib/kpm/plugins_manager.rb
Instance Method Summary collapse
- #add_plugin_identifier_key(plugin_key, plugin_name, language, coordinate_map) ⇒ Object
- #create_symbolic_link(path, link) ⇒ Object
- #get_identifier_key_and_entry(plugin_name_or_key) ⇒ Object
- #get_plugin_key_and_name(plugin_name_or_key) ⇒ Object
- #guess_plugin_name(artifact_id) ⇒ Object
-
#initialize(plugins_dir, logger) ⇒ PluginsManager
constructor
A new instance of PluginsManager.
- #read_plugin_identifiers ⇒ Object
- #remove_plugin_identifier_key(plugin_key) ⇒ Object
- #restart(plugin_name_or_path, plugin_version = nil) ⇒ Object
- #set_active(plugin_name_or_path, plugin_version = nil) ⇒ Object
- #uninstall(plugin_name_or_path, plugin_version = nil) ⇒ Object
- #validate_plugin_identifier_key(plugin_key, coordinate_map) ⇒ Object
Constructor Details
#initialize(plugins_dir, logger) ⇒ PluginsManager
Returns a new instance of PluginsManager.
8 9 10 11 |
# File 'lib/kpm/plugins_manager.rb', line 8 def initialize(plugins_dir, logger) @plugins_dir = Pathname.new(plugins_dir) @logger = logger end |
Instance Method Details
#add_plugin_identifier_key(plugin_key, plugin_name, language, coordinate_map) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/kpm/plugins_manager.rb', line 76 def add_plugin_identifier_key(plugin_key, plugin_name, language, coordinate_map) identifiers = read_plugin_identifiers # If key does not already exists or if the version in the json is not the one we are currently installing we update the entry, if not nothing to do if !identifiers.key?(plugin_key) || (coordinate_map && identifiers[plugin_key]['version'] != coordinate_map[:version]) entry = { 'plugin_name' => plugin_name } entry['language'] = language if coordinate_map entry['group_id'] = coordinate_map[:group_id] entry['artifact_id'] = coordinate_map[:artifact_id] entry['packaging'] = coordinate_map[:packaging] entry['classifier'] = coordinate_map[:classifier] entry['version'] = coordinate_map[:version] end identifiers[plugin_key] = entry write_plugin_identifiers(identifiers) end identifiers end |
#create_symbolic_link(path, link) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/kpm/plugins_manager.rb', line 13 def create_symbolic_link(path, link) FileUtils.rm_f(link) FileUtils.ln_s(path, link, force: true) rescue Errno::EACCES @logger.warn('Unable to create symbolic link LATEST, will copy the directory') FileUtils.rm_rf(link) FileUtils.cp_r(path, link) end |
#get_identifier_key_and_entry(plugin_name_or_key) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/kpm/plugins_manager.rb', line 121 def get_identifier_key_and_entry(plugin_name_or_key) identifiers = read_plugin_identifiers identifiers.each_pair do |key, value| return [key, value] if key == plugin_name_or_key || value['plugin_name'] == plugin_name_or_key end nil end |
#get_plugin_key_and_name(plugin_name_or_key) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/kpm/plugins_manager.rb', line 109 def get_plugin_key_and_name(plugin_name_or_key) identifiers = read_plugin_identifiers if identifiers.key?(plugin_name_or_key) # It's a plugin key [plugin_name_or_key, identifiers[plugin_name_or_key]['plugin_name']] else # Check it's already a plugin name identifiers.each { |plugin_key, entry| return [plugin_key, plugin_name_or_key] if entry['plugin_name'] == plugin_name_or_key } nil end end |
#guess_plugin_name(artifact_id) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/kpm/plugins_manager.rb', line 129 def guess_plugin_name(artifact_id) return nil if artifact_id.nil? captures = artifact_id.scan(/(.*)-plugin/) short_name = if captures.empty? || captures.first.nil? || captures.first.first.nil? artifact_id else # 'analytics-plugin' or 'stripe-plugin' passed captures.first.first end Dir.glob(@plugins_dir.join('*').join('*')).each do |plugin_path| plugin_name = File.basename(plugin_path) if plugin_name == short_name || plugin_name == artifact_id || !plugin_name.scan(/-#{short_name}/).empty? || !plugin_name.scan(/#{short_name}-/).empty? return plugin_name end end nil end |
#read_plugin_identifiers ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/kpm/plugins_manager.rb', line 151 def read_plugin_identifiers path = Pathname.new(@plugins_dir).join('plugin_identifiers.json') identifiers = {} begin identifiers = File.open(path, 'r') do |f| JSON.parse(f.read) end rescue Errno::ENOENT # Ignore end identifiers end |
#remove_plugin_identifier_key(plugin_key) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/kpm/plugins_manager.rb', line 98 def remove_plugin_identifier_key(plugin_key) identifiers = read_plugin_identifiers # If key does not already exists we update it, if not nothing to do. if identifiers.key?(plugin_key) identifiers.delete(plugin_key) write_plugin_identifiers(identifiers) end identifiers end |
#restart(plugin_name_or_path, plugin_version = nil) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/kpm/plugins_manager.rb', line 57 def restart(plugin_name_or_path, plugin_version = nil) update_fs(plugin_name_or_path, plugin_version) do |tmp_dir| # Remove disabled.txt so that the plugin is started if it was stopped FileUtils.rm_f(tmp_dir.join('disabled.txt')) FileUtils.touch(tmp_dir.join('restart.txt')) end end |
#set_active(plugin_name_or_path, plugin_version = nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/kpm/plugins_manager.rb', line 22 def set_active(plugin_name_or_path, plugin_version = nil) if plugin_name_or_path.nil? @logger.warn('Unable to mark a plugin as active: no name or path specified') return end if plugin_version.nil? # Full path specified, with version link = Pathname.new(plugin_name_or_path).join('../SET_DEFAULT') create_symbolic_link(plugin_name_or_path, link) else # Plugin name (fs directory) specified plugin_dir_glob = @plugins_dir.join('*').join(plugin_name_or_path) # Only one should match (java or ruby plugin) Dir.glob(plugin_dir_glob).each do |plugin_dir_path| plugin_dir = Pathname.new(plugin_dir_path) link = plugin_dir.join('SET_DEFAULT') create_symbolic_link(plugin_dir.join(plugin_version), link) end end update_fs(plugin_name_or_path, plugin_version) do |tmp_dir| FileUtils.rm_f(tmp_dir.join('disabled.txt')) FileUtils.rm_f(tmp_dir.join('restart.txt')) end end |
#uninstall(plugin_name_or_path, plugin_version = nil) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/kpm/plugins_manager.rb', line 49 def uninstall(plugin_name_or_path, plugin_version = nil) update_fs(plugin_name_or_path, plugin_version) do |tmp_dir| FileUtils.rm_f(tmp_dir.join('restart.txt')) # Be safe, keep the code, just never start it FileUtils.touch(tmp_dir.join('disabled.txt')) end end |
#validate_plugin_identifier_key(plugin_key, coordinate_map) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/kpm/plugins_manager.rb', line 65 def validate_plugin_identifier_key(plugin_key, coordinate_map) identifiers = read_plugin_identifiers entry = identifiers[plugin_key] if entry coordinate_map.each_pair do |key, value| return false unless validate_plugin_identifier_key_value(plugin_key, key, entry[key.to_s], value) end end true end |