Add a source to the end of the list.
# File lib/rubigen/lookup.rb, line 84 def append_sources(*args) sources.concat(args.flatten) invalidate_cache! end
# File lib/rubigen/lookup.rb, line 107 def application_sources(filters = []) filters.unshift 'app' app_sources = [] app_sources << PathSource.new(:builtin, File.join(File.dirname(__FILE__), %w[.. .. app_generators])) app_sources << filtered_sources(filters) app_sources.flatten end
# File lib/rubigen/lookup.rb, line 140 def filtered_sources(filters) new_sources = [] new_sources << PathFilteredSource.new(:user, "#{Dir.user_home}/.rubigen/", *filters) if Object.const_defined?(:Gem) new_sources << GemPathSource.new(*filters) end new_sources end
Convenience method to lookup and instantiate a generator.
# File lib/rubigen/lookup.rb, line 166 def instance(generator_name, args = [], runtime_options = {}) active.lookup(generator_name).klass.new(args, full_options(runtime_options)) end
Lookup knows how to find generators' Specs from a list of Sources. Searches the sources, in order, for the first matching name.
# File lib/rubigen/lookup.rb, line 151 def lookup(generator_name) @found ||= {} generator_name = generator_name.to_s.downcase @found[generator_name] ||= cache.find { |spec| spec.name == generator_name } unless @found[generator_name] chars = generator_name.scan(%r./).map{|c|"#{c}.*?"} rx = %r^#{chars}$/ gns = cache.select {|spec| spec.name =~ rx } @found[generator_name] ||= gns.first if gns.length == 1 raise GeneratorError, "Pattern '#{generator_name}' matches more than one generator: #{gns.map{|sp|sp.name}.join(', ')}" if gns.length > 1 end @found[generator_name] or raise GeneratorError, "Couldn't find '#{generator_name}' generator" end
Add a source to the beginning of the list.
# File lib/rubigen/lookup.rb, line 90 def prepend_sources(*args) self._sources = args.flatten + self._sources invalidate_cache! end
Reset the source list.
# File lib/rubigen/lookup.rb, line 96 def reset_sources self._sources = [] invalidate_cache! end
The list of sources where we look, in order, for generators.
# File lib/rubigen/lookup.rb, line 66 def sources if self._sources.blank? if superclass == RubiGen::Base superclass_sources = superclass.sources diff = superclass_sources.inject([]) do |mem, source| found = false application_sources.each { |app_source| found ||= true if app_source == source} mem << source unless found mem end self._sources = diff end active.use_component_sources! if self._sources.blank? end self._sources end
Use application generators (app, ?).
# File lib/rubigen/lookup.rb, line 102 def use_application_sources!(*filters) self._sources = application_sources(filters) invalidate_cache! end
Use component generators (test_unit, etc).
Current application. If APP_ROOT is defined we know we’re generating in the context of this application, so search APP_ROOT/generators.
User home directory. Search ~/.rubigen/generators.
RubyGems. Search for gems containing /{scope}_generators folder.
Builtins. None currently.
Search can be filtered by passing one or more prefixes. e.g. use_component_sources!(:rubygems) means it will also search in the following folders:
User home directory. Search ~/.rubigen/rubygems_generators.
RubyGems. Search for gems containing /rubygems_generators folder.
# File lib/rubigen/lookup.rb, line 128 def use_component_sources!(*filters) new_sources = [] if defined? ::APP_ROOT new_sources << PathSource.new(:root, "#{::APP_ROOT}/generators") new_sources << PathSource.new(:vendor, "#{::APP_ROOT}/vendor/generators") new_sources << PathSource.new(:plugins, "#{::APP_ROOT}/vendor/plugins/*/**/generators") end new_sources << filtered_sources(filters) self._sources = new_sources.flatten invalidate_cache! end