Module: Jax::Generators::PluginBase

Included in:
PluginGenerator, PluginManager
Defined in:
lib/generators/jax/base/plugin_base.rb

Defined Under Namespace

Classes: ResponseError

Instance Method Summary collapse

Instance Method Details

#each_plugin(name = nil, &block) ⇒ Object



81
82
83
# File 'lib/generators/jax/base/plugin_base.rb', line 81

def each_plugin(name = nil, &block)
  matching_plugins(name).each &block
end

#extract_hash_from_response(response) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/generators/jax/base/plugin_base.rb', line 101

def extract_hash_from_response(response)
  begin
    hash = Hash.from_xml(response)
  rescue
    raise ResponseError.new("Fatal: response couldn't be parsed. (Maybe it wasn't valid XML?)")
  end
end

#find_plugin_list(hash_containing_plugin_list) ⇒ Object



66
67
68
69
70
# File 'lib/generators/jax/base/plugin_base.rb', line 66

def find_plugin_list(hash_containing_plugin_list)
  hash_containing_plugin_list['jax_plugins'] ||
  hash_containing_plugin_list['nil_classes'] ||
  raise(ResponseError.new("Fatal: couldn't find plugin list."))
end

#get_remote_plugins_matching(name = nil) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/generators/jax/base/plugin_base.rb', line 72

def get_remote_plugins_matching(name = nil)
  plugins = rest_resource("plugins")
  if name
    extract_hash_from_response plugins['search'][name].get
  else
    extract_hash_from_response plugins.get
  end
end

#installed_plugin_manifests(filter_name = nil) ⇒ Object



49
50
51
52
53
54
# File 'lib/generators/jax/base/plugin_base.rb', line 49

def installed_plugin_manifests(filter_name = nil)
  { 'jax_plugins' => search(installed_plugins, filter_name).collect do |name, path|
      load_or_infer_manifest(name, path)
    end
  }
end

#installed_pluginsObject



31
32
33
34
35
36
37
38
39
# File 'lib/generators/jax/base/plugin_base.rb', line 31

def installed_plugins
  plugins = []
  Dir.glob(::Rails.application.root.join("vendor/plugins/*").to_s).each do |path|
    if File.directory? path
      plugins.push [File.basename(path), Pathname.new(path)]
    end
  end
  plugins.sort { |a, b| a[0] <=> b[0] }
end

#load_or_infer_manifest(name, plugin_dir) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/generators/jax/base/plugin_base.rb', line 41

def load_or_infer_manifest(name, plugin_dir)
  if File.file?(manifest_path = File.join(plugin_dir, "manifest.yml"))
    YAML::load(File.read(manifest_path)) || { 'name' => name, 'description' => '(Description unavailable)' }
  else
    { 'name' => name, 'description' => '(Manifest file not found!)' }
  end
end

#matching_plugins(name = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/generators/jax/base/plugin_base.rb', line 56

def matching_plugins(name = nil)
  if options[:local]
    hash = installed_plugin_manifests(name)
  else
    hash = get_remote_plugins_matching name
  end
  
  find_plugin_list(hash)
end

#plugin_version(details) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/generators/jax/base/plugin_base.rb', line 85

def plugin_version(details)
  if options['version']
    for release in details['releases']
      if release['version'] && release['version'] == options['version']
        return options['version']
      end
    end
    raise "Release information for version #{options['version']} not found for plugin '#{details['name']}'!"
  else
    if release = details['releases'].last and release['version']
      return release['version']
    end
    raise "Release information not found for plugin '#{details['name']}'!"
  end
end

#rest_resource(name, accept = :xml) ⇒ Object



11
12
13
14
15
16
# File 'lib/generators/jax/base/plugin_base.rb', line 11

def rest_resource(name, accept = :xml)
  url = Jax.config.plugin_repository_url
  url.concat "/" unless url =~ /\/$/
  url.concat name
  RestClient::Resource.new(url, :accept => accept)
end

#search(plugin_list, query) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/generators/jax/base/plugin_base.rb', line 22

def search(plugin_list, query)
  plugin_list.select { |plugin, path_to_plugin|
    !query || plugin =~ search_query_rx(query)
  }.inject({}) do |hash, (plugin, path_to_plugin)|
    hash[plugin] = path_to_plugin
    hash
  end
end

#search_query_rx(query) ⇒ Object



18
19
20
# File 'lib/generators/jax/base/plugin_base.rb', line 18

def search_query_rx(query)
  /^#{Regexp::escape query}/i
end