Class: Bunto::PluginManager

Inherits:
Object
  • Object
show all
Defined in:
lib/bunto/plugin_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ PluginManager

Create an instance of this class.

site - the instance of Bunto::Site we’re concerned with

Returns nothing



10
11
12
# File 'lib/bunto/plugin_manager.rb', line 10

def initialize(site)
  @site = site
end

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



3
4
5
# File 'lib/bunto/plugin_manager.rb', line 3

def site
  @site
end

Class Method Details

.require_from_bundlerObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bunto/plugin_manager.rb', line 32

def self.require_from_bundler
  if !ENV["BUNTO_NO_BUNDLER_REQUIRE"] && File.file?("Gemfile")
    require "bundler"

    Bundler.setup
    required_gems = Bundler.require(:bunto_plugins)
    message = "Required #{required_gems.map(&:name).join(", ")}"
    Bunto.logger.debug("PluginManager:", message)
    ENV["BUNTO_NO_BUNDLER_REQUIRE"] = "true"

    true
  else
    false
  end
end

Instance Method Details

#conscientious_requireObject

Require all the plugins which are allowed.

Returns nothing



17
18
19
20
21
# File 'lib/bunto/plugin_manager.rb', line 17

def conscientious_require
  require_plugin_files
  require_gems
  deprecation_checks
end

#deprecation_checksObject



89
90
91
92
93
94
95
96
97
# File 'lib/bunto/plugin_manager.rb', line 89

def deprecation_checks
  pagination_included = (site.config["gems"] || []).include?("bunto-paginate") ||
    defined?(Bunto::Paginate)
  if site.config["paginate"] && !pagination_included
    Bunto::Deprecator.deprecation_message "You appear to have pagination " \
      "turned on, but you haven't included the `bunto-paginate` gem. " \
      "Ensure you have `gems: [bunto-paginate]` in your configuration file."
  end
end

#plugin_allowed?(gem_name) ⇒ Boolean

Check whether a gem plugin is allowed to be used during this build.

gem_name - the name of the gem

Returns true if the gem name is in the whitelist or if the site is not

in safe mode.

Returns:

  • (Boolean)


54
55
56
# File 'lib/bunto/plugin_manager.rb', line 54

def plugin_allowed?(gem_name)
  !site.safe || whitelist.include?(gem_name)
end

#plugins_pathObject

Public: Setup the plugin search path

Returns an Array of plugin search paths



81
82
83
84
85
86
87
# File 'lib/bunto/plugin_manager.rb', line 81

def plugins_path
  if site.config["plugins_dir"].eql? Bunto::Configuration::DEFAULTS["plugins_dir"]
    [site.in_source_dir(site.config["plugins_dir"])]
  else
    Array(site.config["plugins_dir"]).map { |d| File.expand_path(d) }
  end
end

#require_gemsObject

Require each of the gem plugins specified.

Returns nothing.



26
27
28
29
30
# File 'lib/bunto/plugin_manager.rb', line 26

def require_gems
  Bunto::External.require_with_graceful_fail(
    site.gems.select { |gem| plugin_allowed?(gem) }
  )
end

#require_plugin_filesObject

Require all .rb files if safe mode is off

Returns nothing.



69
70
71
72
73
74
75
76
# File 'lib/bunto/plugin_manager.rb', line 69

def require_plugin_files
  unless site.safe
    plugins_path.each do |plugin_search_path|
      plugin_files = Utils.safe_glob(plugin_search_path, File.join("**", "*.rb"))
      Bunto::External.require_with_graceful_fail(plugin_files)
    end
  end
end

#whitelistObject

Build an array of allowed plugin gem names.

Returns an array of strings, each string being the name of a gem name

that is allowed to be used.


62
63
64
# File 'lib/bunto/plugin_manager.rb', line 62

def whitelist
  @whitelist ||= Array[site.config["whitelist"]].flatten
end