Class: KPM::PluginsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/kpm/plugins_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(plugins_dir, logger) ⇒ PluginsManager

Returns a new instance of PluginsManager.



7
8
9
10
# File 'lib/kpm/plugins_manager.rb', line 7

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kpm/plugins_manager.rb', line 68

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.has_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

#get_identifier_key_and_entry(plugin_name_or_key) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/kpm/plugins_manager.rb', line 114

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



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kpm/plugins_manager.rb', line 102

def get_plugin_key_and_name(plugin_name_or_key)
  identifiers = read_plugin_identifiers
  if identifiers.has_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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/kpm/plugins_manager.rb', line 122

def guess_plugin_name(artifact_id)
  return nil if artifact_id.nil?
  captures = artifact_id.scan(/(.*)-plugin/)
  if captures.empty? || captures.first.nil? || captures.first.first.nil?
    short_name = artifact_id
  else
    # 'analytics-plugin' or 'stripe-plugin' passed
    short_name = 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_identifiersObject



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/kpm/plugins_manager.rb', line 143

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
  end
  identifiers
end

#remove_plugin_identifier_key(plugin_key) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/kpm/plugins_manager.rb', line 91

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.has_key?(plugin_key)
    identifiers.delete(plugin_key)
    write_plugin_identifiers(identifiers)
  end

  identifiers
end

#restart(plugin_name_or_path, plugin_version = nil) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/kpm/plugins_manager.rb', line 49

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



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
37
38
39
# File 'lib/kpm/plugins_manager.rb', line 12

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')
    FileUtils.rm_f(link)
    FileUtils.ln_s(plugin_name_or_path, link, :force => true)
  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')
      FileUtils.rm_f(link)
      FileUtils.ln_s(plugin_dir.join(plugin_version), link, :force => true)
    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



41
42
43
44
45
46
47
# File 'lib/kpm/plugins_manager.rb', line 41

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



57
58
59
60
61
62
63
64
65
66
# File 'lib/kpm/plugins_manager.rb', line 57

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 if !validate_plugin_identifier_key_value(plugin_key, key, entry[key.to_s], value)
    end
  end
  true
end