lib/ronin/installation.rb in ronin-1.0.0 vs lib/ronin/installation.rb in ronin-1.1.0.rc1

- old
+ new

@@ -15,40 +15,62 @@ # # You should have received a copy of the GNU General Public License # along with Ronin. If not, see <http://www.gnu.org/licenses/>. # +require 'set' + module Ronin # # The {Installation} module provides methods which help reflect on the # installation of Ronin on the system. # module Installation # The loaded gemspecs of all installed ronin libraries @gems = {} + @paths = Set[] # # Finds the installed Ronin libraries via RubyGems. # # @return [Hash{String => Gem::Specification}] # The names and gem-specs of the installed Ronin libraries. # # @since 1.0.0 # + # @api semipublic + # def Installation.gems - load_gemspecs! if @gems.empty? + load! if @gems.empty? return @gems end # + # The paths of the installed Ronin libraries. + # + # @return [Set<String>] + # The paths of the Ronin libraries. + # + # @since 1.0.1 + # + # @api semipublic + # + def Installation.paths + load! if @paths.empty? + return @paths + end + + # # The names of the additional Ronin libraries installed on the system. # # @return [Array<String>] # The library names. # # @since 1.0.0 # + # @api semipublic + # def Installation.libraries gems.keys end # @@ -67,19 +89,20 @@ # @return [Enumerator] # Returns an Enumerator if no block is given. # # @since 1.0.0 # + # @api semipublic + # def Installation.each_file(pattern) return enum_for(:each_file,pattern) unless block_given? # query the installed gems - gems.each_value do |gem| - gem_root = gem.full_gem_path - slice_index = gem_root.length + 1 + paths.each do |gem_path| + slice_index = gem_path.length + 1 - Dir.glob(File.join(gem_root,pattern)) do |path| + Dir.glob(File.join(gem_path,pattern)) do |path| yield path[slice_index..-1] end end return nil @@ -103,10 +126,12 @@ # @return [Enumerator] # If no block is given, an Enumerator will be returned. # # @since 1.0.0 # + # @api semipublic + # def Installation.each_file_in(directory,ext=nil) return enum_for(:each_file_in,directory,ext) unless block_given? pattern = File.join(directory,'**','*') pattern << ".#{ext}" if ext @@ -119,40 +144,84 @@ end protected # - # Loads the gemspecs for any installed Ronin libraries. + # Finds the installed Ronin gems. # # @return [true] # All Ronin libraries were successfully found. # - # @since 1.0.0 + # @since 1.0.1 # - def Installation.load_gemspecs! + # @api private + # + def Installation.load_gems! + register_gem = lambda { |gem| + @gems[gem.name] = gem + @paths << gem.full_gem_path + } + ronin_gem = Gem.loaded_specs['ronin'] - if ronin_gem - @gems['ronin'] = ronin_gem + # add the main ronin gem + register_gem[ronin_gem] - ronin_gem.dependent_gems.each do |gems| - gem = gems.first - @gems[gem.name] = gem - end - else - # if we cannot find an installed ronin gem, search the $LOAD_PATH - # for ronin gemspecs and load those - $LOAD_PATH.each do |lib_dir| - root_dir = File.expand_path(File.join(lib_dir,'..')) - gemspec_path = Dir[File.join(root_dir,'ronin*.gemspec')].first + # add any dependent gems + ronin_gem.dependent_gems.each do |gems| + register_gem[gems[0]] + end - if gemspec_path - gem = Gem::SourceIndex.load_specification(gemspec_path) + return true + end + + # + # Loads the gemspecs of Ronin libraries from the `$LOAD_PATH`. + # + # @return [true] + # All Ronin gemspecs were successfully found. + # + # @since 1.0.0 + # + # @api private + # + def Installation.load_gemspecs! + $LOAD_PATH.each do |lib_dir| + root_dir = File.expand_path(File.join(lib_dir,'..')) + gemspec_path = Dir[File.join(root_dir,'ronin*.gemspec')][0] + + if gemspec_path + # switch into the gem directory, before loading the gemspec + gem = Dir.chdir(root_dir) do + Gem::Specification.load(gemspec_path) + end + + # do not add duplicate ronin gems + unless @gems.has_key?(gem.name) @gems[gem.name] = gem + @paths << root_dir end end end return true + end + + # + # Finds the installed Ronin libraries. + # + # @return [true] + # All Ronin libraries were successfully found. + # + # @since 1.0.1 + # + # @api private + # + def Installation.load! + if Gem.loaded_specs.has_key?('ronin') + load_gems! + else + load_gemspecs! + end end end end