lib/ronin/installation.rb in ronin-1.0.0.rc3 vs lib/ronin/installation.rb in ronin-1.0.0
- old
+ new
@@ -15,31 +15,29 @@
#
# You should have received a copy of the GNU General Public License
# along with Ronin. If not, see <http://www.gnu.org/licenses/>.
#
-require 'enumerator'
-
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 = {}
#
# 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
#
def Installation.gems
- Installation.load_gemspecs! if @gems.empty?
+ load_gemspecs! if @gems.empty?
return @gems
end
#
# The names of the additional Ronin libraries installed on the system.
@@ -48,27 +46,14 @@
# The library names.
#
# @since 1.0.0
#
def Installation.libraries
- Installation.gems.keys
+ gems.keys
end
#
- # The installation paths of installed Ronin libraries.
- #
- # @return [Hash{String => String}]
- # The paths to the installed Ronin libraries.
- #
- # @since 1.0.0
- #
- def Installation.paths
- Installation.load_gemspecs! if @paths.empty?
- return @paths
- end
-
- #
# Enumerates over all files within a given directory found in any
# of the installed Ronin libraries.
#
# @param [String] directory
# The directory path to search within.
@@ -82,47 +67,57 @@
# @return [Enumerator]
# Returns an Enumerator if no block is given.
#
# @since 1.0.0
#
- def Installation.each_file(directory)
- return enum_for(:each_file,directory) unless block_given?
+ def Installation.each_file(pattern)
+ return enum_for(:each_file,pattern) unless block_given?
- directory = File.join(directory,'')
-
# query the installed gems
- Installation.gems.each_value do |gem|
- gem.files.each do |file|
- if file[0...directory.length] == directory
- yield file[directory.length..-1]
- end
+ gems.each_value do |gem|
+ gem_root = gem.full_gem_path
+ slice_index = gem_root.length + 1
+
+ Dir.glob(File.join(gem_root,pattern)) do |path|
+ yield path[slice_index..-1]
end
end
return nil
end
#
- # Requires all files within the given directory.
+ # Enumerates over every file in a directory.
#
# @param [String] directory
- # A directory that resides within `lib/`.
+ # The directory to search within.
#
- # @return [Boolean]
- # Specifies whether any files were successfully required.
+ # @param [String, Symbol] ext
+ # The optional file extension to search for.
#
+ # @yield [name]
+ # The given block will be passed each matching file-name.
+ #
+ # @yieldparam [String] name
+ # The basename of the matching path within the directory.
+ #
+ # @return [Enumerator]
+ # If no block is given, an Enumerator will be returned.
+ #
# @since 1.0.0
#
- def Installation.require_all(directory)
- lib_dir = File.join('lib',directory)
- result = false
+ def Installation.each_file_in(directory,ext=nil)
+ return enum_for(:each_file_in,directory,ext) unless block_given?
- Installation.each_file(lib_dir) do |name|
- result |= (require File.join(directory,name))
- end
+ pattern = File.join(directory,'**','*')
+ pattern << ".#{ext}" if ext
- return result
+ slice_index = directory.length + 1
+
+ each_file(pattern) do |path|
+ yield path[slice_index..-1]
+ end
end
protected
#
@@ -136,29 +131,24 @@
def Installation.load_gemspecs!
ronin_gem = Gem.loaded_specs['ronin']
if ronin_gem
@gems['ronin'] = ronin_gem
- @paths['ronin'] = ronin_gem.full_gem_path
ronin_gem.dependent_gems.each do |gems|
gem = gems.first
-
@gems[gem.name] = gem
- @paths[gem.name] = gem.full_gem_path
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
if gemspec_path
gem = Gem::SourceIndex.load_specification(gemspec_path)
-
@gems[gem.name] = gem
- @paths[gem.name] = root_dir
end
end
end
return true