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, coordinates) ⇒ Object



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 69

def add_plugin_identifier_key(plugin_key, plugin_name, language, coordinates)

  identifiers = read_plugin_identifiers
  # If key does not already exists we update it, if not nothing to do
  if !identifiers.has_key?(plugin_key)

    entry = {'plugin_name' => plugin_name}
    entry['language'] = language
    if coordinates
      entry['group_id'] = coordinates[0]
      entry['artifact_id'] = coordinates[1]
      entry['packaging'] = coordinates[2]
      entry['classifier'] = coordinates[3]
      entry['version'] = coordinates[4]
    end
    identifiers[plugin_key] = entry
    write_plugin_identifiers(identifiers)
  end

  identifiers
end

#get_plugin_name_from_key(plugin_key) ⇒ Object



104
105
106
107
108
# File 'lib/kpm/plugins_manager.rb', line 104

def get_plugin_name_from_key(plugin_key)
  identifiers = read_plugin_identifiers
  return nil if identifiers.nil? || !identifiers.has_key?(plugin_key)
  identifiers[plugin_key]['plugin_name']
end

#guess_plugin_name(artifact_id) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/kpm/plugins_manager.rb', line 111

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

#remove_plugin_identifier_key(plugin_key) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# 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, coordinates) ⇒ Object



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

def validate_plugin_identifier_key(plugin_key, coordinates)

  identifiers = read_plugin_identifiers
  entry = identifiers[plugin_key]
  if entry
    [:group_id, :artifact_id, :packaging, :classifier, :version].each_with_index do |value_type, idx|
      return false if !validate_plugin_identifier_key_value(plugin_key, value_type, entry[value_type.to_s], coordinates[idx])
    end
  end
  true
end