Skip to content
This repository has been archived by the owner on Apr 14, 2018. It is now read-only.

Commit

Permalink
Started implementing the plugin loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien Thebo committed Nov 1, 2011
1 parent 24bcba9 commit 114b300
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
14 changes: 3 additions & 11 deletions lib/wrack/plugin.rb
Expand Up @@ -8,6 +8,7 @@

require 'wrack/irc'
require 'wrack/receiver'
require 'wrack/pluginloader'

module Wrack

Expand Down Expand Up @@ -49,17 +50,8 @@ def registered
@klasses.dup
end

def paths
@paths ||= []
end

def load(path)
paths << path
Kernel.load path
end

def reload_all
@paths.each { |path| Kernel.load path }
def loader
@loader ||= Wrack::PluginLoader.new
end
end

Expand Down
8 changes: 2 additions & 6 deletions lib/wrack/plugin/plugin.rb
Expand Up @@ -11,12 +11,8 @@ class Plugin
receive do
restrict :message => /reload/
match do |msg|
paths = Wrack::Plugin.paths.dup

paths.each do |path|
privmsg msg.sender, "Reloading #{path}"
Wrack::Plugin.load path
end
puts "Reloading all plugins"
Wrack::Plugin.loader.reload_known_plugins!
bot.reload_plugins!
end
end
Expand Down
48 changes: 48 additions & 0 deletions lib/wrack/pluginloader.rb
@@ -0,0 +1,48 @@

require 'thread'

module Wrack
class PluginLoader

def initialize
@plugin_files = Hash.new(Array.new)
@sem = Mutex.new
end

def addplugin(plugin_file)
@plugin_files[plugin_file] = [] unless @plugin_files[plugin_file]
end

def load_file!(file)
@sem.synchronize do
begin
$stderr.puts "Loading file #{file}"
preload_plugins = Wrack::Plugin.registered
Kernel.load file
postload_plugins = Wrack::Plugin.registered

# Associate which plugins were loaded from the files
@plugin_files[file] = (postload_plugins - preload_plugins)
puts @plugin_files
rescue => e
$stderr.puts "Error while loading plugin file #{plugin_file}: #{e}"
end
end
end

def unload_file!(file)
constants = @plugin_files[file]

constants.each do |const|
Object.send(:remove_const, const.name.intern)
end
end

def reload_known_plugins!
@plugin_files.keys.each do |plugin|
unload_file! plugin
load_file! plugin
end
end
end
end

0 comments on commit 114b300

Please sign in to comment.