Class: RUtilAnts::Plugins::PluginsManager
- Inherits:
-
Object
- Object
- RUtilAnts::Plugins::PluginsManager
- Defined in:
- lib/rUtilAnts/Plugins.rb
Overview
Main class storing info about plugins
Instance Method Summary collapse
-
#accessPlugin(iCategoryName, iPluginName, iParameters = {}) ⇒ Object
Give access to a plugin.
-
#clearPlugins ⇒ Object
Clear the registered plugins.
-
#getPluginDescription(iCategory, iPluginName) ⇒ Object
Get the named plugin description.
-
#getPluginInstance(iCategory, iPluginName, iParameters = {}) ⇒ Object
Get the named plugin instance.
-
#getPluginNames(iCategoryName, iParameters = {}) ⇒ Object
Get the list of plugin names of a given category.
-
#getPluginsDescriptions(iCategoryName, iParameters = {}) ⇒ Object
Get the map of plugins descriptions, indexed with plugin names.
-
#initialize ⇒ PluginsManager
constructor
Constructor.
-
#parsePluginsFromDir(iCategory, iDir, iBaseClassNames, &iInitCodeBlock) ⇒ Object
Parse plugins from a given directory.
-
#registerNewPlugin(iCategoryName, iPluginName, iFileName, iDesc, iClassName, iInitCodeBlock) ⇒ Object
Register a new plugin.
Constructor Details
#initialize ⇒ PluginsManager
Constructor
60 61 62 63 64 |
# File 'lib/rUtilAnts/Plugins.rb', line 60 def initialize # Map of plugins, per category # map< String, map< String, map< Symbol, Object > > > @Plugins = {} end |
Instance Method Details
#accessPlugin(iCategoryName, iPluginName, iParameters = {}) ⇒ Object
Give access to a plugin. An exception is thrown if the plugin does not exist.
Parameters:
-
iCategoryName (String): Category of the plugin to access
-
iPluginName (String): Name of the plugin to access
-
iParameters (map<Symbol,Object>): Additional parameters:
** OnlyIfExtDepsResolved (Boolean): Do we return the plugin only if there is no need to install external dependencies ? [optional = false] ** RDIInstaller (RDI::Installer): The RDI installer if available, or nil otherwise [optional = nil] ** RDIContextModifiers (map<String,list<[String,Object]>>): The map of context modifiers to be filled by the RDI installer if specified, or nil if ignored [optional = nil]
-
CodeBlock: The code called when the plugin is found:
** ioPlugin (Object): The corresponding plugin
302 303 304 305 306 307 308 309 |
# File 'lib/rUtilAnts/Plugins.rb', line 302 def accessPlugin(iCategoryName, iPluginName, iParameters = {}) lPlugin, lError = getPluginInstance(iCategoryName, iPluginName, iParameters) if (lPlugin == nil) raise lError else yield(lPlugin) end end |
#clearPlugins ⇒ Object
Clear the registered plugins
312 313 314 |
# File 'lib/rUtilAnts/Plugins.rb', line 312 def clearPlugins @Plugins = {} end |
#getPluginDescription(iCategory, iPluginName) ⇒ Object
Get the named plugin description
Parameters:
-
iCategory (Object): Category those plugins will belong to
-
iPluginName (String): Plugin name
Return:
-
map<Symbol,Object>: The corresponding description, or nil in case of failure
275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/rUtilAnts/Plugins.rb', line 275 def getPluginDescription(iCategory, iPluginName) rDesc = nil if (@Plugins[iCategory] == nil) logErr "Unknown plugins category #{iCategory}." else rDesc = @Plugins[iCategory][iPluginName] if (rDesc == nil) logErr "Unknown plugin #{iPluginName} in category #{iCategory}." end end return rDesc end |
#getPluginInstance(iCategory, iPluginName, iParameters = {}) ⇒ Object
Get the named plugin instance. Uses RDI if given in parameters or if Main RDI Installer defined to resolve Plugins’ dependencies.
Parameters:
-
iCategory (Object): Category those plugins will belong to
-
iPluginName (String): Plugin name
-
iParameters (map<Symbol,Object>): Additional parameters:
** OnlyIfExtDepsResolved (Boolean): Do we return the plugin only if there is no need to install external dependencies ? [optional = false] ** RDIInstaller (RDI::Installer): The RDI installer if available, or nil otherwise [optional = nil] ** RDIContextModifiers (map<String,list<[String,Object]>>): The map of context modifiers to be filled by the RDI installer if specified, or nil if ignored [optional = nil] Return:
-
Object: The corresponding plugin, or nil in case of failure
-
Exception: The error, or nil in case of success
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/rUtilAnts/Plugins.rb', line 162 def getPluginInstance(iCategory, iPluginName, iParameters = {}) rPlugin = nil rError = nil lOnlyIfExtDepsResolved = iParameters[:OnlyIfExtDepsResolved] if (lOnlyIfExtDepsResolved == nil) lOnlyIfExtDepsResolved = false end lRDIInstaller = iParameters[:RDIInstaller] lRDIContextModifiers = iParameters[:RDIContextModifiers] if (@Plugins[iCategory] == nil) rError = UnknownCategoryError.new("Unknown plugins category #{iCategory}.") else lDesc = @Plugins[iCategory][iPluginName] if (lDesc == nil) rError = UnknownPluginError.new("Unknown plugin #{iPluginName} in category #{iCategory}.") elsif (lDesc[:Enabled] == false) rError = DisabledPluginError.new("Plugin #{iPluginName} in category #{iCategory} is disabled.") else if (lDesc[:PluginInstance] == nil) lSuccess = true # If RDI is present, call it to get dependencies first if needed if (lDesc[:Dependencies] != nil) # If it is not given as parameter, try getting the singleton if ((lRDIInstaller == nil) and (defined?(RDI::Installer) != nil)) lRDIInstaller = RDI::Installer.getMainInstance end if (lRDIInstaller != nil) if (lOnlyIfExtDepsResolved) # Test that each dependency is accessible lSuccess = true lDesc[:Dependencies].each do |iDepDesc| lSuccess = lRDIInstaller.testDependency(iDepDesc) if (!lSuccess) # It is useless to continue break end end else # Load other dependencies lError, lContextModifiers, lIgnored, lUnresolved = lRDIInstaller.ensureDependencies(lDesc[:Dependencies]) if (lRDIContextModifiers != nil) lRDIContextModifiers.merge!(lContextModifiers) end lSuccess = ((lError == nil) and (lIgnored.empty?) and (lUnresolved.empty?)) if (!lSuccess) if (!lIgnored.empty?) rError = PluginDependenciesIgnoredError.new("Unable to load plugin #{iPluginName} without its dependencies (ignored #{lIgnored.size} dependencies).") elsif (!lUnresolved.empty?) rError = PluginDependenciesUnresolvedError.new("Unable to load plugin #{iPluginName} without its dependencies (couldn't load #{lUnresolved.size} dependencies):\n#{lError}") else rError = PluginDependenciesError.new("Could not load dependencies for plugin #{iPluginName}: #{lError}") end end end end end if (lSuccess) if (lDesc[:PluginsDependencies] != nil) # Load other plugins lDesc[:PluginsDependencies].each do |iPluginInfo| iPluginCategory, iPluginName = iPluginInfo lPlugin, lError = getPluginInstance(iPluginCategory, iPluginName, iParameters) lSuccess = (lError == nil) if (!lSuccess) # Don't try further rError = PluginDependenciesError.new("Could not load plugins dependencies for plugin #{iPluginName}: #{lError}.") break end end end if (lSuccess) # Load the plugin begin # If the file name is to be required, do it now if (lDesc[:PluginFileName] != nil) require lDesc[:PluginFileName] end lPlugin = eval("#{lDesc[:PluginClassName]}.new") # Add a reference to the description in the instantiated object lPlugin.instance_variable_set(:@rUtilAnts_Desc, lDesc) def lPlugin.pluginDescription return @rUtilAnts_Desc end # Register this instance lDesc[:PluginInstance] = lPlugin # If needed, execute the init code if (lDesc[:PluginInitCode] != nil) lDesc[:PluginInitCode].call(lPlugin) end rescue Exception rError = FailedPluginError.new("Error while loading file #{lDesc[:PluginFileName]} and instantiating #{lDesc[:PluginClassName]}: #{$!}. Ignoring this plugin.") end end end end rPlugin = lDesc[:PluginInstance] end end return rPlugin, rError end |
#getPluginNames(iCategoryName, iParameters = {}) ⇒ Object
Get the list of plugin names of a given category
Parameters:
-
iCategoryName (String): The category for which we want the plugin names list
-
iParameters (map<Symbol,Object>): Additional parameters:
** IncludeDisabled (Boolean): Do we include disabled plugins ? [optional = false] Return:
-
list<String>: The list of plugin names in this category
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/rUtilAnts/Plugins.rb', line 324 def getPluginNames(iCategoryName, iParameters = {}) rPlugins = [] lIncludeDisabled = iParameters[:IncludeDisabled] if (lIncludeDisabled == nil) lIncludeDisabled = false end if (@Plugins[iCategoryName] != nil) @Plugins[iCategoryName].each do |iPluginName, iPluginDesc| if ((lIncludeDisabled) or (iPluginDesc[:Enabled] != false)) rPlugins << iPluginName end end end return rPlugins end |
#getPluginsDescriptions(iCategoryName, iParameters = {}) ⇒ Object
Get the map of plugins descriptions, indexed with plugin names
Parameters:
-
iCategoryName (String): The category for which we want the plugin names list
-
iParameters (map<Symbol,Object>): Additional parameters:
** IncludeDisabled (Boolean): Do we include disabled plugins ? [optional = false] Return:
-
map<String,map<Symbol,Object>>: The map of plugin descriptions per plugin name
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/rUtilAnts/Plugins.rb', line 351 def getPluginsDescriptions(iCategoryName, iParameters = {}) rPlugins = {} lIncludeDisabled = iParameters[:IncludeDisabled] if (lIncludeDisabled == nil) lIncludeDisabled = false end if (@Plugins[iCategoryName] != nil) if (lIncludeDisabled) rPlugins = @Plugins[iCategoryName] else @Plugins[iCategoryName].each do |iPluginName, iPluginDesc| if (iPluginDesc[:Enabled] != false) rPlugins[iPluginName] = iPluginDesc end end end end return rPlugins end |
#parsePluginsFromDir(iCategory, iDir, iBaseClassNames, &iInitCodeBlock) ⇒ Object
Parse plugins from a given directory
Parameters:
-
iCategory (Object): Category those plugins will belong to
-
iDir (String): Directory to parse for plugins
-
iBaseClassNames (String): The base class name of plugins to be instantiated
-
iInitCodeBlock (CodeBlock): Code to be executed first time the plugin will be instantiated (can be ommitted):
** ioPlugin (Object): Plugin instance
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/rUtilAnts/Plugins.rb', line 104 def parsePluginsFromDir(iCategory, iDir, iBaseClassNames, &iInitCodeBlock) # Gather descriptions # map< String, map > lDescriptions = {} lDescFiles = Dir.glob("#{iDir}/*.desc.rb") lDescFiles.each do |iFileName| lPluginName = File.basename(iFileName)[0..-9] # Load the description file begin File.open(iFileName) do |iFile| lDesc = eval(iFile.read, nil, iFileName) if (lDesc.is_a?(Hash)) lDescriptions[lPluginName] = lDesc else logBug "Plugin description #{iFileName} is incorrect. The file should just describe a simple hash map." end end rescue Exception logExc $!, "Error while loading file #{iFileName}. Ignoring this description." end end # Now, parse the plugins themselves if (@Plugins[iCategory] == nil) @Plugins[iCategory] = {} end (Dir.glob("#{iDir}/*.rb") - lDescFiles).each do |iFileName| lPluginName = File.basename(iFileName)[0..-4] # Don't load it now, but store it along with its description if it exists if (@Plugins[iCategory][lPluginName] == nil) # Check if we have a description lDesc = lDescriptions[lPluginName] registerNewPlugin( iCategory, lPluginName, iFileName, lDescriptions[lPluginName], "#{iBaseClassNames}::#{lPluginName}", iInitCodeBlock ) else logErr "Plugin named #{lPluginName} in category #{iCategory} already exists. Please name it differently. Ignoring it from #{iFileName}." end end end |
#registerNewPlugin(iCategoryName, iPluginName, iFileName, iDesc, iClassName, iInitCodeBlock) ⇒ Object
Register a new plugin
Parameters:
-
iCategoryName (String): Category this plugin belongs to
-
iPluginName (String): Plugin name
-
iFileName (String): File name containing the plugin (can be nil)
-
iDesc (map<Symbol,Object>): Plugin’s description (can be nil)
-
iClassName (String): Name of the plugin class
-
iInitCodeBlock (Proc): Code block to call when initializing the real instance (can be nil)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rUtilAnts/Plugins.rb', line 75 def registerNewPlugin(iCategoryName, iPluginName, iFileName, iDesc, iClassName, iInitCodeBlock) # Complete the description with some metadata if (@Plugins[iCategoryName] == nil) @Plugins[iCategoryName] = {} end lDesc = nil if (iDesc == nil) lDesc = {} else lDesc = iDesc.clone end lDesc[:PluginFileName] = iFileName lDesc[:PluginInstance] = nil lDesc[:PluginClassName] = iClassName lDesc[:PluginInitCode] = iInitCodeBlock lDesc[:PluginIndex] = @Plugins[iCategoryName].size lDesc[:PluginName] = iPluginName lDesc[:PluginCategoryName] = iCategoryName @Plugins[iCategoryName][iPluginName] = lDesc end |