Class: Vop::PluginLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/vop/plugin_loader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op, dir) ⇒ PluginLoader

Returns a new instance of PluginLoader.



11
12
13
14
# File 'lib/vop/plugin_loader.rb', line 11

def initialize(op, dir)
  @op = op
  @dir = dir
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



9
10
11
# File 'lib/vop/plugin_loader.rb', line 9

def dir
  @dir
end

#opObject (readonly)

Returns the value of attribute op.



8
9
10
# File 'lib/vop/plugin_loader.rb', line 8

def op
  @op
end

Class Method Details

.read(op, dir) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vop/plugin_loader.rb', line 59

def self.read(op, dir)
  $logger.debug("scanning dir #{dir} for plugins...")

  loader = new(op, dir)
  loader.read_plugin_template

  Dir.new(dir).each do |plugin_name|
    next if /^\./.match(plugin_name)

    plugin_path = File.join(dir, plugin_name)
    file_name = File.join(plugin_path, "#{plugin_name}.plugin")
    next unless File.exists?(file_name)
    $logger.debug("reading plugin '#{plugin_name}' from '#{file_name}'")

    plugin = loader.new_plugin(plugin_name, plugin_path)

    code = File.read(file_name)
    begin
      loader.instance_eval(code, file_name)
    rescue => detail
      raise "problem loading plugin #{plugin_name} : #{detail.message}\n#{detail.backtrace.join("\n")}"
    end

    $logger.debug "loaded plugin #{plugin_name}"
  end
end

Instance Method Details

#dependency(sym) ⇒ Object Also known as: depends



37
38
39
# File 'lib/vop/plugin_loader.rb', line 37

def dependency(sym)
  @plugin.dependencies << sym.to_s
end

#new_plugin(plugin_name, plugin_path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vop/plugin_loader.rb', line 16

def new_plugin(plugin_name, plugin_path)
  @plugin = Plugin.new(@op, plugin_name, plugin_path)

  # TODO activate plugin_loader helpers?
  # (this will allow plugins to define helpers that can be used in plugins)
  #@plugin.inject_helpers(self)
  #@plugin.inject_helpers(self, 'plugin_loader')

  @op.plugins[plugin_name] = @plugin
  @plugin
end

#on(hook_sym, &block) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/vop/plugin_loader.rb', line 28

def on(hook_sym, &block)
  @plugin.hook(hook_sym, &block)

  core_hooks = %i|before_execute after_execute|
  if core_hooks.include? hook_sym
    @op.hook(hook_sym, @plugin.name)
  end
end

#read_plugin_templateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vop/plugin_loader.rb', line 42

def read_plugin_template
  template_path = File.join(@dir, 'plugin.vop')
  plugin_template = nil
  if File.exists?(template_path)
    plugin_template = IO.read(template_path)
  end
  if plugin_template
    begin
      self.instance_eval plugin_template
    rescue => e
      $stderr.puts "could not load plugin #{template_path} : " + e.message
      $stderr.puts e.backtrace.join("\n")
      raise e
    end
  end
end