Skip to content

Commit

Permalink
(MAINT) Rubocop fixes and refactor load_from_gems
Browse files Browse the repository at this point in the history
Previously load_from_gems contained nested loops and block
continuation. It was hard to read and probably not that efficient when
processing many files.

This commit cleans the method up by splitting the operations out in to
three distinct operations, hopefully simplifying the code slightly.

In addition, the Rubocop fixes have also been applied in this commit.
  • Loading branch information
chelnak committed Oct 5, 2022
1 parent d429387 commit ed5ec22
Showing 1 changed file with 63 additions and 61 deletions.
124 changes: 63 additions & 61 deletions lib/puppet-lint/plugins.rb
Original file line number Diff line number Diff line change
@@ -1,77 +1,79 @@
require 'pathname'

class PuppetLint
# Public: Various methods that implement puppet-lint's plugin system
# Public: Various methods that implement puppet-lint's plugin system
#
# Examples
#
# PuppetLint::Plugins.load_spec_helper
class PuppetLint::Plugins
# Internal: Find any gems containing puppet-lint plugins and load them.
#
# Examples
#
# PuppetLint::Plugins.load_spec_helper
class Plugins
# Internal: Find any gems containing puppet-lint plugins and load them.
#
# Returns nothing.
def self.load_from_gems
gem_directories.select { |path|
(path + 'puppet-lint/plugins').directory?
}.each do |gem_path|
Dir["#{gem_path + 'puppet-lint/plugins'}/*.rb"].each do |file|
load(file)
end
end
# Returns nothing.
def self.load_from_gems
plugins_directories = gem_directories.select do |directory|
(directory + 'puppet-lint/plugins').to_s if (directory + 'puppet-lint/plugins').directory?
end

# Public: Load the puppet-lint spec_helper.rb
#
# Returns nothings.
def self.load_spec_helper
gemspec = gemspecs.select { |spec| spec.name == 'puppet-lint' }.first
load(Pathname.new(gemspec.full_gem_path) + 'spec/spec_helper.rb')
plugin_files = plugins_directories.each do |directory|
Dir["#{directory}/**/*.rb"]
end

class << self
private
plugin_files.each do |file|
load(file)
end
end

# Internal: Check if RubyGems is loaded and available.
#
# Returns true if RubyGems is available, false if not.
def rubygems?
defined?(::Gem)
end
# Public: Load the puppet-lint spec_helper.rb
#
# Returns nothings.
def self.load_spec_helper
gemspec = gemspecs.find { |spec| spec.name == 'puppet-lint' }
load(Pathname.new(gemspec.full_gem_path) + 'spec/spec_helper.rb')
end

# Internal: Retrieve a list of avaliable gemspecs.
#
# Returns an Array of Gem::Specification objects.
def gemspecs
@gemspecs ||= if Gem::Specification.respond_to?(:latest_specs)
Gem::Specification.latest_specs(load_prerelease_plugins?)
else
Gem.searcher.init_gemspecs
end
end
class << self
private

# Internal: Determine whether to load plugins that contain a letter in their version number.
#
# Returns true if the configuration is set to load "prerelease" gems, false otherwise.
def load_prerelease_plugins?
# Load prerelease plugins (which ruby defines as any gem which has a letter in its version number).
# Can't use puppet-lint configuration object here because this code executes before the command line is parsed.
if ENV['PUPPET_LINT_LOAD_PRERELEASE_PLUGINS']
return ['true', 'yes'].include?(ENV['PUPPET_LINT_LOAD_PRERELEASE_PLUGINS'].downcase)
end
false
# Internal: Check if RubyGems is loaded and available.
#
# Returns true if RubyGems is available, false if not.
def rubygems?
defined?(::Gem)
end

# Internal: Retrieve a list of avaliable gemspecs.
#
# Returns an Array of Gem::Specification objects.
def gemspecs
@gemspecs ||= if Gem::Specification.respond_to?(:latest_specs)
Gem::Specification.latest_specs(load_prerelease_plugins?)
else
Gem.searcher.init_gemspecs
end
end

# Internal: Determine whether to load plugins that contain a letter in their version number.
#
# Returns true if the configuration is set to load "prerelease" gems, false otherwise.
def load_prerelease_plugins?
# Load prerelease plugins (which ruby defines as any gem which has a letter in its version number).
# Can't use puppet-lint configuration object here because this code executes before the command line is parsed.
if ENV['PUPPET_LINT_LOAD_PRERELEASE_PLUGINS']
return ['true', 'yes'].include?(ENV['PUPPET_LINT_LOAD_PRERELEASE_PLUGINS'].downcase)
end
false
end

# Internal: Retrieve a list of available gem paths from RubyGems.
#
# Returns an Array of Pathname objects.
def gem_directories
if rubygems?
gemspecs.reject { |spec| spec.name == 'puppet-lint' }.map do |spec|
Pathname.new(spec.full_gem_path) + 'lib'
end
else
[]
# Internal: Retrieve a list of available gem paths from RubyGems.
#
# Returns an Array of Pathname objects.
def gem_directories
if rubygems?
gemspecs.reject { |spec| spec.name == 'puppet-lint' }.map do |spec|
Pathname.new(spec.full_gem_path) + 'lib'
end
else
[]
end
end
end
Expand Down

0 comments on commit ed5ec22

Please sign in to comment.