Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
Moved plugin loading logic into BuilderPlugin; environment now respon…
Browse files Browse the repository at this point in the history
…sible for loading all plugins.
  • Loading branch information
bguthrie committed Jul 10, 2009
1 parent de819e6 commit 11929ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 47 deletions.
48 changes: 1 addition & 47 deletions app/models/project.rb
Expand Up @@ -471,50 +471,4 @@ def get_build_index(all_builds, build_label)
all_builds.each_with_index {|build, index| result = index if build.label == build_label}
result
end
end

# TODO make me pretty, move me to another file, invoke me from environment.rb
# TODO check what happens if loading a plugin raises an error (e.g, SyntaxError in plugin/init.rb)
plugin_loader = Object.new

def plugin_loader.load_plugin(plugin_path)
plugin_file = File.basename(plugin_path).sub(/\.rb$/, '')
plugin_is_directory = (plugin_file == 'init')
plugin_name = plugin_is_directory ? File.basename(File.dirname(plugin_path)) : plugin_file

CruiseControl::Log.debug("Loading plugin #{plugin_name}")
if RAILS_ENV == 'development'
load plugin_path
else
if plugin_is_directory then require "#{plugin_name}/init" else require plugin_name end
end
end

def plugin_loader.load_all
plugins = Dir[RAILS_ROOT + "/lib/builder_plugins/*"] + Dir[CRUISE_DATA_ROOT + "/builder_plugins/*"]

plugins.each do |plugin|
# ignore hidden files and directories (they should be considered hidden by Dir[], but just in case)
next if File.basename(plugin)[0, 1] == '.'
if File.file?(plugin)
if plugin[-3..-1] == '.rb'
load_plugin(File.basename(plugin))
else
# a file without .rb extension, ignore
end
elsif File.directory?(plugin)
init_path = File.join(plugin, 'init.rb')
if File.file?(init_path)
load_plugin(init_path)
else
log.error("No init.rb found in plugin directory #{plugin}")
end
else
# a path is neither file nor directory. whatever else it may be, let's ignore it.
# TODO: find out what happens with symlinks on a Linux here? how about broken symlinks?
end
end

end

plugin_loader.load_all
end
2 changes: 2 additions & 0 deletions config/environment.rb
Expand Up @@ -65,3 +65,5 @@ def find_home
# get rid of cached pages between runs
FileUtils.rm_rf RAILS_ROOT + "/public/builds"
FileUtils.rm_rf RAILS_ROOT + "/public/documentation"

BuilderPlugin.load_all
41 changes: 41 additions & 0 deletions lib/builder_plugins/builder_plugin.rb
Expand Up @@ -27,6 +27,47 @@ class << self
def known_event?(event_name)
self.instance_methods(false).include? event_name.to_s
end

def load_all
plugins_to_load.each do |plugin|
if can_load_immediately?(plugin)
load_plugin(File.basename(plugin))
elsif File.directory?(plugin)
init_path = File.join(plugin, 'init.rb')
if File.file?(init_path)
load_plugin(init_path)
else
log.error("No init.rb found in plugin directory #{plugin}")
end
end
end
end

private

def plugins_to_load
(Dir[RAILS_ROOT + "/lib/builder_plugins/*"] + Dir[CRUISE_DATA_ROOT + "/builder_plugins/*"]).reject do |plugin_path|
# ignore hidden files and directories (they should be considered hidden by Dir[], but just in case)
File.basename(plugin_path)[0, 1] == '.'
end
end

def can_load_immediately?(plugin)
File.file?(plugin) && plugin[-3..-1] == '.rb'
end

def load_plugin(plugin_path)
plugin_file = File.basename(plugin_path).sub(/\.rb$/, '')
plugin_is_directory = (plugin_file == 'init')
plugin_name = plugin_is_directory ? File.basename(File.dirname(plugin_path)) : plugin_file

CruiseControl::Log.debug("Loading plugin #{plugin_name}")
if RAILS_ENV == 'development'
load plugin_path
else
if plugin_is_directory then require "#{plugin_name}/init" else require plugin_name end
end
end
end

# Called by ChangeInSourceControlTrigger to indicate that it is about to poll source control.
Expand Down

0 comments on commit 11929ce

Please sign in to comment.