Class: Vop::Plugin

Inherits:
ThingWithParams show all
Defined in:
lib/vop/objects/plugin.rb

Instance Attribute Summary collapse

Attributes inherited from ThingWithParams

#params

Instance Method Summary collapse

Methods inherited from ThingWithParams

#param

Constructor Details

#initialize(op, plugin_name, plugin_path, options = {}) ⇒ Plugin

Returns a new instance of Plugin.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vop/objects/plugin.rb', line 26

def initialize(op, plugin_name, plugin_path, options = {})
  super()

  @op = op
  @name = plugin_name
  @path = plugin_path

  defaults = {
    auto_load: true
  }
  @options = defaults.merge(options)

  @description = nil

  @state = {}

  @config_file_name = File.join(op.plugin_config_path, plugin_name + ".json")
  @config = {}

  @dependencies = []
  @external_dependencies = Hash.new { |h, k| h[k] = [] }

  @hooks = {}
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



17
18
19
# File 'lib/vop/objects/plugin.rb', line 17

def commands
  @commands
end

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/vop/objects/plugin.rb', line 21

def config
  @config
end

#dependenciesObject

Returns the value of attribute dependencies.



23
24
25
# File 'lib/vop/objects/plugin.rb', line 23

def dependencies
  @dependencies
end

#descriptionObject

Returns the value of attribute description.



15
16
17
# File 'lib/vop/objects/plugin.rb', line 15

def description
  @description
end

#external_dependenciesObject

Returns the value of attribute external_dependencies.



24
25
26
# File 'lib/vop/objects/plugin.rb', line 24

def external_dependencies
  @external_dependencies
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/vop/objects/plugin.rb', line 14

def name
  @name
end

#opObject (readonly)

Returns the value of attribute op.



12
13
14
# File 'lib/vop/objects/plugin.rb', line 12

def op
  @op
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/vop/objects/plugin.rb', line 16

def options
  @options
end

#sourcesObject (readonly)

Returns the value of attribute sources.



19
20
21
# File 'lib/vop/objects/plugin.rb', line 19

def sources
  @sources
end

#stateObject (readonly)

Returns the value of attribute state.



20
21
22
# File 'lib/vop/objects/plugin.rb', line 20

def state
  @state
end

Instance Method Details

#auto_load?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/vop/objects/plugin.rb', line 51

def auto_load?
  @options[:auto_load]
end

#call_hook(name, *args) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/vop/objects/plugin.rb', line 225

def call_hook(name, *args)
  result = nil
  if @hooks.has_key? name
    begin
      result = @hooks[name].call(self, *args)
    rescue => e
      $logger.error "hit a problem while running hook #{name} on #{self.name}"
      raise e
    end
  end
  result
end

#has_hook?(name) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/vop/objects/plugin.rb', line 221

def has_hook?(name)
  ! @hooks[name.to_sym].nil?
end

#hook(name, &block) ⇒ Object



217
218
219
# File 'lib/vop/objects/plugin.rb', line 217

def hook(name, &block)
  @hooks[name.to_sym] = block
end

#initObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/vop/objects/plugin.rb', line 80

def init
  # TODO proceed only if auto_load
  call_hook :init

  load_entities
  load_commands
  load_filters

  #@op.call_global_hook :plugin_loaded, self
end

#inject_helpers(target, sub_type_name = nil) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/vop/objects/plugin.rb', line 186

def inject_helpers(target, sub_type_name = nil)
  type_name = 'helpers'

  plugins_to_load_helpers_from = [ self ]

  self.dependencies.each do |name|
    other = @op.plugin(name)
    raise "can not resolve plugin dependency #{name}" unless other
    plugins_to_load_helpers_from << other
  end

  plugins_to_load_helpers_from.each do |other_plugin|
    helper_sources = other_plugin.sources[type_name]

    next if helper_sources.size == 0

    helper_module = Module.new()

    helper_sources.each do |name, helper|
      begin
        helper_module.class_eval helper[:code]
      rescue Exception => e
        $stderr.puts("could not read helper #{name} : #{e.message}")
        raise e
      end
    end

    target.extend helper_module
  end
end

#inspectObject



59
60
61
62
63
64
# File 'lib/vop/objects/plugin.rb', line 59

def inspect
  {
    name: name,
    path: @path
  }.to_json()
end

#loadObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vop/objects/plugin.rb', line 66

def load
  $logger.debug "plugin init : #{@name}"

  @sources = Hash.new { |h, k| h[k] = {} }

  @config = {}

  load_helpers
  load_default_config
  load_config

  load_gem_dependencies unless ENV["VOP_IGNORE_PLUGINS"]
end

#load_code_from_dir(type_name) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/vop/objects/plugin.rb', line 140

def load_code_from_dir(type_name)
  dir = plugin_dir(type_name)

  if File.exists?(dir)
    Dir.glob(File.join(dir, "*.rb")).each do |file_name|
      name_from_file = /#{dir}\/(.+).rb$/.match(file_name).captures.first
      full_name = [@name, name_from_file].join(".")
      $logger.debug("  #{type_name} << #{full_name}")

      @sources[type_name][full_name] = {
        :file_name => file_name,
        :code => File.read(file_name)
      }
    end
  end
end

#load_commandsObject



165
166
167
168
169
170
171
# File 'lib/vop/objects/plugin.rb', line 165

def load_commands
  loader = CommandLoader.new(self)

  load_code_from_dir :commands
  @commands = loader.read_sources @sources[:commands]
  @op << @commands unless @commands.empty?
end

#load_configObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vop/objects/plugin.rb', line 103

def load_config
  $logger.debug "looking for config at #{@config_file_name}"
  if File.exists? @config_file_name
    raw = nil
    begin
      raw = IO.read(@config_file_name)
      config_from_file = JSON.parse(raw)
      @config.merge! config_from_file
      $logger.debug "plugin config loaded from #{@config_file_name}"
    rescue => e
      $logger.error "could not read JSON config from #{@config_file_name} (#{e.message}), ignoring:\n#{raw}"
    end
  end
end

#load_default_configObject



95
96
97
98
99
100
101
# File 'lib/vop/objects/plugin.rb', line 95

def load_default_config
  params.each do |param|
    if param.options.has_key?(:default)
      @config[param.name] = param.options[:default]
    end
  end
end

#load_entitiesObject



157
158
159
160
161
162
163
# File 'lib/vop/objects/plugin.rb', line 157

def load_entities
  loader = EntityLoader.new(self)

  load_code_from_dir :entities
  @entities = loader.read_sources @sources[:entities]
  @op << @entities unless @entities.empty?
end

#load_filtersObject



173
174
175
176
177
178
179
# File 'lib/vop/objects/plugin.rb', line 173

def load_filters
  loader = FilterLoader.new(self)

  load_code_from_dir :filters
  @filters = loader.read_sources @sources[:filters]
  @op << @filters unless @filters.empty?
end

#load_gem_dependenciesObject



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/vop/objects/plugin.rb', line 128

def load_gem_dependencies
  gems = @external_dependencies[:gem]
  return unless gems.any?
  $logger.debug "loading gem dependencies : #{gems}"

  gems.each do |g|
    gem, options = g
    gem_name = options[:require] || gem
    require gem_name
  end
end

#load_helpersObject



181
182
183
184
# File 'lib/vop/objects/plugin.rb', line 181

def load_helpers
  #load_code_from_dir :helpers
  load_code_from_dir("helpers") # TODO unify (symbol vs. string) with load_commands above
end

#plugin_dir(name) ⇒ Object



91
92
93
# File 'lib/vop/objects/plugin.rb', line 91

def plugin_dir(name)
  File.join(@path, name.to_s)
end

#template(name) ⇒ Object



244
245
246
# File 'lib/vop/objects/plugin.rb', line 244

def template(name)
  @op.read_template(template_path(name))
end

#template_path(name_or_sym) ⇒ Object



238
239
240
241
242
# File 'lib/vop/objects/plugin.rb', line 238

def template_path(name_or_sym)
  name = name_or_sym.to_s
  name += ".erb" unless name.end_with? ".erb"
  File.join(plugin_dir(:templates), name)
end

#to_sObject



55
56
57
# File 'lib/vop/objects/plugin.rb', line 55

def to_s
  "Vop::Plugin #{name}"
end

#write_configObject



118
119
120
121
122
123
124
125
126
# File 'lib/vop/objects/plugin.rb', line 118

def write_config
  $logger.info "writing config into #{@op.plugin_config_path}"
  unless Dir.exists?(@op.plugin_config_path)
    FileUtils.mkdir_p @op.plugin_config_path
  end
  File.open(@config_file_name, "w") do |file|
    file.write @config.to_json()
  end
end