class Webgen::BundleLoader
This class is used for loading extension bundles. It provides a DSL for the most commonly needed commands.
When an extension bundle is provided by a Rubygem and the Rubygem is not already activated, the Rubygem is automatically activated. This only works when one follows the standard naming conventions for webgen extension bundles, i.e. the Rubygem has to be named 'webgen-BUNDLE_NAME-bundle'.
Public Class Methods
Create a new BundleLoader
object belonging to the website object website
.
# File lib/webgen/bundle_loader.rb 82 def initialize(website, ext_dir) 83 @website = website 84 @website.ext.bundle_infos = BundleInformation.new 85 @ext_dir = ext_dir 86 @loaded = [] 87 @stack = [] 88 end
Public Instance Methods
Load the extension bundle in the context of this BundleLoader
object.
# File lib/webgen/bundle_loader.rb 91 def load(name) 92 file = resolve_init_file(name) 93 raise Webgen::BundleLoadError.new(name) if !file 94 file = File.expand_path(file) 95 return if @loaded.include?(file) 96 97 load!(file) 98 99 if file != File.expand_path(File.join(@ext_dir, 'init.rb')) 100 name = File.basename(File.dirname(file)) 101 info_file = File.join(File.dirname(file), 'info.yaml') 102 @website.ext.bundle_infos.add_bundle(name, File.file?(info_file) ? info_file : nil) 103 end 104 end
Force-loads the given file and does just that (i.e. no checking if file exists and no bundle registration).
Note: This method should normally not be called in an extension bundle, use the load
method instead.
# File lib/webgen/bundle_loader.rb 111 def load!(file) 112 @loaded.push(file) 113 @stack.push(file) 114 self.instance_eval(File.read(file), file) 115 @stack.pop 116 end
Loads all bundles that are marked for auto-loading.
# File lib/webgen/bundle_loader.rb 119 def load_autoload_bundles 120 bundles = Gem::Specification.map {|s| s.name }.uniq.map do |gem_name| 121 md = /^webgen-(.*)-bundle$/.match(gem_name) 122 next unless md 123 md[1] 124 end.compact 125 126 bundles += $LOAD_PATH.map do |path| 127 Dir[File.join(path, 'webgen/bundle', '*')].map {|d| File.basename(d)} 128 end.flatten.compact 129 130 bundles.each do |bundle_name| 131 file = resolve_init_file(bundle_name) 132 next unless file 133 134 info_file = File.join(File.dirname(file), 'info.yaml') 135 next unless File.file?(info_file) 136 next unless (begin YAML.load(File.read(info_file))['autoload']; rescue Exception; false end) 137 138 load(bundle_name) 139 end 140 end
Private Instance Methods
Create all possible initialization file names for the given directory name.
# File lib/webgen/bundle_loader.rb 161 def possible_init_file_names(dir_name) 162 [File.join(@ext_dir, dir_name, 'init.rb')] + 163 $LOAD_PATH.map {|path| File.join(path, 'webgen/bundle', dir_name, 'init.rb')} 164 end
Search in the website extension directory and then in the load path to find the initialization file of the bundle.
# File lib/webgen/bundle_loader.rb 144 def resolve_init_file(name) 145 name.sub!(/(\/|^)init\.rb$/, '') 146 147 if name =~ /\A[\w-]+\z/ 148 begin 149 Gem::Specification.find_by_name("webgen-#{name}-bundle").activate 150 rescue Gem::LoadError 151 end 152 end 153 154 possible_init_file_names(name).each {|path| return path if File.file?(path)} 155 156 nil 157 end
DSL methods
↑ topPublic Instance Methods
Return the absolute path of the given path which is assumed to be relative to the currently loaded file.
# File lib/webgen/bundle_loader.rb 199 def absolute_path(path) 200 File.expand_path(File.join(File.dirname(@stack.last), path)) 201 end
Mount the directory relative to the currently loaded file on the given mount point as passive source.
See Webgen::Source
for more information.
# File lib/webgen/bundle_loader.rb 193 def mount_passive(dir, mount_point = '/', glob = '{*,**/*}') 194 @website.ext.source.passive_sources.unshift([mount_point, :file_system, absolute_path(dir), glob]) 195 end
Define a configuration option.
See Webgen::Configuration#define_option
for more information.
# File lib/webgen/bundle_loader.rb 180 def option(name, default, &validator) 181 @website.config.define_option(name, default, &validator) 182 end
Require the file relative to the currently loaded file.
# File lib/webgen/bundle_loader.rb 173 def require_relative(file) 174 require(File.join(File.dirname(@stack.last), file)) 175 end
Return the website object.
# File lib/webgen/bundle_loader.rb 185 def website 186 @website 187 end