moduleTrustyCmsclassExtensionPath# This class holds information about extensions that may be loaded. It has two roles: to remember the# location of the extension so that we don't have to search for it again, and to look within that path# for significant application subdirectories.## We can't just retrieve this information from the Extension class because the initializer sets up# most of the application load_paths before plugins (including extensions) are loaded. You can think# of this as a sort of pre-extension class preparing the way for extension loading.## You can use instances of this class to retrieve information about a particular extension:## ExtensionPath.new(:name, :path)# ExtensionPath.find(:name) #=> ExtensionPath instance# ExtensionPath.find(:name).plugin_paths #=> "path/vendor/plugins" if it exists and is a directory# ExtensionPath.for(:name) #=> "path"## The initializer calls class methods to get overall lists (in configured order) of enabled load paths:## ExtensionPath.enabled #=> ["path", "path", "path", "path"]# ExtensionPath.plugin_paths #=> ["path/vendor/plugins", "path/vendor/plugins"]attr_accessor:name,:path@@known_paths={}definitialize(options={})#:nodoc@name,@path=options[:name].underscore,options[:path]@@known_paths[@name.to_sym]=selfenddefrequiredFile.join(path,"#{name}_extension")enddefto_spathend# Builds a new ExtensionPath object from the supplied path, working out the name of the extension by# stripping the extra bits from radiant-something-extension-1.0.0 to leave just 'something'. The object# is returned, and also remembered here for later use by the initializer (to find load paths) and the# ExtensionLoader, to load and activate the extension.## If two arguments are given, the second is taken to be the full extension name.#defself.from_path(path,name=nil)name=pathifname.blank?name=File.basename(name).gsub(/^trusty-|-extension(-[\d\.a-z]+|-[a-z\d]+)*$/,'')new(:name=>name,:path=>path)end# Forgets all recorded extension paths.# Currently only used in testing.#defself.clear_paths!@@known_paths={}end# Returns a list of all the likely load paths found within this extension root. It includes all of these# that exist and are directories:## * path# * path/lib# * path/app/models# * path/app/controllers# * path/app/metal# * path/app/helpers# * path/test/helpers## You can call the class method ExtensionPath.load_paths to get a flattened list of all the load paths in all the enabled extensions.#defload_paths%w(lib app/models app/controllers app/metal app/helpers test/helpers).collect{|d|check_subdirectory(d)}.push(path).flatten.compactend# Returns a list of all the +vendor/plugin+ paths found within this extension root.# Call the class method ExtensionPath.plugin_paths to get a list of the plugin paths found in all enabled extensions.#defplugin_pathscheck_subdirectory("vendor/plugins")end# Returns a list of names of all the locale files found within this extension root.# Call the class method ExtensionPath.locale_paths to get a list of the locale files found in all enabled extensions# in reverse order so that locale definitions override one another correctly.#deflocale_pathsifcheck_subdirectory("config/locales")Dir[File.join("#{path}","config/locales","*.{rb,yml}")]endend# Returns the app/helpers path if it is found within this extension root.# Call the class method ExtensionPath.helper_paths to get a list of the helper paths found in all enabled extensions.#defhelper_pathscheck_subdirectory("app/helpers")end# Returns the app/models path if it is found within this extension root.# Call the class method ExtensionPath.model_paths to get a list of the model paths found in all enabled extensions.#defmodel_pathscheck_subdirectory("app/models")end# Returns the app/controllers path if it is found within this extension root.# Call the class method ExtensionPath.controller_paths to get a list of the controller paths found in all enabled extensions.#defcontroller_pathscheck_subdirectory("app/controllers")end# Returns the app/views path if it is found within this extension root.# Call the class method ExtensionPath.view_paths to get a list of the view paths found in all enabled extensions# in reverse order so that views override one another correctly.#defview_pathscheck_subdirectory("app/views")end# Returns the app/metal path if it is found within this extension root.# Call the class method ExtensionPath.metal_paths to get a list of the metal paths found in all enabled extensions.#defmetal_pathscheck_subdirectory("app/metal")end# Returns a list of all the rake task files found within this extension root.#defrake_task_pathsifcheck_subdirectory("lib/tasks")Dir[File.join("#{path}","lib/tasks/**","*.rake")]endend# Returns a list of extension subdirectories that should be marked for eager loading. At the moment that# includes all the controller, model and helper paths. The main purpose here is to ensure that extension# controllers are loaded before running cucumber features, and there may be a better way to achieve that.## Call the class method ExtensionPath.eager_load_paths to get a list for all enabled extensions.#defeager_load_paths[controller_paths,model_paths,helper_paths].flatten.compactendclass<<self# Returns the ExtensionPath object for the given extension name.#deffind(name)raiseLoadError,"Cannot return path for unknown extension: #{name}"unless@@known_paths[name.to_sym]@@known_paths[name.to_sym]end# Returns the root path recorded for the given extension name.#deffor(name)find(name).pathend# Returns a list of path objects for all the enabled extensions in the configured order.# If a configured extension has not been found during initialization, a LoadError will be thrown here.## Note that at this stage, in line with the usage of config.extensions = [], the extension names# are being passed around as symbols.#defenabledenabled_extensions=TrustyCms::Application.config.enabled_extensions@@known_paths.values_at(*enabled_extensions).compactend# Returns a list of the root paths to all the enabled extensions, in the configured order.#defenabled_pathsenabled.map(&:path)end[:load_paths,:plugin_paths,:helper_paths,:model_paths,:controller_paths,:eager_load_paths].eachdo|m|define_method(m)doenabled.map{|ep|ep.send(m)}.flatten.compactendend[:locale_paths,:view_paths,:metal_paths,:rake_task_paths].eachdo|m|define_method(m)doenabled.map{|ep|ep.send(m)}.flatten.compact.reverseendendendprivate# If the supplied path within the extension root exists and is a directory, its absolute path is returned. Otherwise, nil.#defcheck_subdirectory(subpath)subdirectory=File.join(path,subpath)subdirectoryifFile.directory?(subdirectory)endendend