Class: Bridgetown::PluginManager

Inherits:
Object
  • Object
show all
Defined in:
lib/bridgetown-core/plugin_manager.rb

Constant Summary collapse

LEGACY_PLUGINS_GROUP =
:bridgetown_plugins
YARN_DEPENDENCY_REGEXP =
%r!(.+)@([^@]*)$!.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ PluginManager

Provides a plugin manager for the site

Parameters:



187
188
189
# File 'lib/bridgetown-core/plugin_manager.rb', line 187

def initialize(site)
  @site = site
end

Class Attribute Details

.registered_pluginsObject (readonly)

Returns the value of attribute registered_plugins.



48
49
50
# File 'lib/bridgetown-core/plugin_manager.rb', line 48

def registered_plugins
  @registered_plugins
end

Instance Attribute Details

#loaders_managerObject (readonly)

Returns the value of attribute loaders_manager.



8
9
10
# File 'lib/bridgetown-core/plugin_manager.rb', line 8

def loaders_manager
  @loaders_manager
end

#siteObject (readonly)

Returns the value of attribute site.



8
9
10
# File 'lib/bridgetown-core/plugin_manager.rb', line 8

def site
  @site
end

Class Method Details

.add_registered_plugin(gem_or_plugin_file) ⇒ Object



43
44
45
# File 'lib/bridgetown-core/plugin_manager.rb', line 43

def self.add_registered_plugin(gem_or_plugin_file)
  @registered_plugins << gem_or_plugin_file
end

.add_source_manifest(source_manifest) ⇒ Object

Parameters:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bridgetown-core/plugin_manager.rb', line 13

def self.add_source_manifest(source_manifest)
  unless source_manifest.is_a?(Bridgetown::Configuration::SourceManifest)
    raise "You must add a SourceManifest instance"
  end

  unless Bridgetown::Current.preloaded_configuration
    raise "A preloaded configuration must be present before adding source manifests"
  end

  Bridgetown::Deprecator.deprecation_message(
    "The #{source_manifest.origin} plugin should switch from using `add_source_manifest' to " \
    "the `source_manifest` initializer method"
  )

  Bridgetown::Current.preloaded_configuration.source_manifests << source_manifest
end

.add_yarn_dependency?(yarn_dependency, package_json) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
174
175
176
177
178
# File 'lib/bridgetown-core/plugin_manager.rb', line 168

def self.add_yarn_dependency?(yarn_dependency, package_json)
  return false if yarn_dependency.nil?

  # check matching version number is see if it's already installed
  if package_json["dependencies"]
    current_version = package_json["dependencies"][yarn_dependency.first]
    package_requires_updating?(current_version, yarn_dependency.last)
  else
    true
  end
end

.bundler_specsObject



50
51
52
# File 'lib/bridgetown-core/plugin_manager.rb', line 50

def bundler_specs
  @bundler_specs ||= Bundler.load.requested_specs
end

.find_yarn_dependency(loaded_gem) ⇒ Object



161
162
163
164
165
166
# File 'lib/bridgetown-core/plugin_manager.rb', line 161

def self.find_yarn_dependency(loaded_gem)
  yarn_dependency = loaded_gem.to_spec&.&.dig("yarn-add")&.match(YARN_DEPENDENCY_REGEXP)
  return nil if yarn_dependency&.length != 3 || yarn_dependency[2] == ""

  yarn_dependency[1..2]
end

.install_yarn_dependencies(required_gems = bundler_specs, name: nil) ⇒ Bundler::SpecSet

Iterates through loaded gems and finds yard-add gemspec metadata. If that exact package hasn't been installed, execute yarn add

Returns:

  • (Bundler::SpecSet)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/bridgetown-core/plugin_manager.rb', line 136

def self.install_yarn_dependencies(required_gems = bundler_specs, name: nil)
  return required_gems unless File.exist?("package.json")

  package_json = JSON.parse(File.read("package.json"))

  gems_to_search = if name
                     required_gems.select do |loaded_gem|
                       loaded_gem.to_spec&.name == name.to_s
                     end
                   else
                     required_gems
                   end

  gems_to_search.each do |loaded_gem|
    yarn_dependency = find_yarn_dependency(loaded_gem)
    next unless add_yarn_dependency?(yarn_dependency, package_json)

    # all right, time to install the package
    cmd = "yarn add #{yarn_dependency.join("@")}"
    system cmd
  end

  gems_to_search
end

.legacy_requireObject



115
116
117
118
119
# File 'lib/bridgetown-core/plugin_manager.rb', line 115

def self.legacy_require
  Bundler.require(LEGACY_PLUGINS_GROUP).select do |dep|
    (dep.groups & [LEGACY_PLUGINS_GROUP]).any? && dep.should_include?
  end
end

.legacy_yarn_and_register(required_gems, skip_yarn: false) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/bridgetown-core/plugin_manager.rb', line 121

def self.legacy_yarn_and_register(required_gems, skip_yarn: false)
  install_yarn_dependencies(required_gems) unless skip_yarn

  required_gems.each do |installed_gem|
    add_registered_plugin installed_gem
  end

  Bridgetown.logger.debug("PluginManager:",
                          "Required #{required_gems.map(&:name).join(", ")}")
end

.load_determined_bundler_environment(skip_yarn: false) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bridgetown-core/plugin_manager.rb', line 81

def self.load_determined_bundler_environment(skip_yarn: false)
  boot_file = File.join("config", "boot.rb")

  if File.file?(boot_file)
    # We'll let config/boot.rb take care of Bundler setup
    require File.expand_path(boot_file)
  elsif File.file?(File.join("config", "initializers.rb"))
    # We'll just make sure the default and environmental gems are available.
    # Note: the default Bundler config will set up all gem groups,
    #   see: https://bundler.io/guides/groups.html
    Bundler.setup(:default, Bridgetown.env)
  else
    # Only setup and require :bridgetown_plugins
    legacy_yarn_and_register(legacy_require, skip_yarn: skip_yarn)
  end
end

.new_source_manifest(*_args, **kwargs) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bridgetown-core/plugin_manager.rb', line 30

def self.new_source_manifest(*_args, **kwargs)
  unless Bridgetown::Current.preloaded_configuration
    raise "A preloaded configuration must be present before adding source manifests"
  end

  Bridgetown::Deprecator.deprecation_message(
    "The #{kwargs[:origin]} plugin should switch from using `new_source_manifest' to the " \
    "`source_manifest` initializer method"
  )

  add_source_manifest(Bridgetown::Configuration::SourceManifest.new(**kwargs))
end

.package_requires_updating?(current_version, dep_version) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/bridgetown-core/plugin_manager.rb', line 180

def self.package_requires_updating?(current_version, dep_version)
  current_version.nil? || (current_version != dep_version && !current_version.include?("/"))
end

.require_gem(name) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bridgetown-core/plugin_manager.rb', line 98

def self.require_gem(name)
  Bridgetown::Utils::RequireGems.require_with_graceful_fail(name)
  plugins = Bridgetown::PluginManager.install_yarn_dependencies(name: name)

  plugin_to_register = if plugins.length == 1
                         plugins.first
                       else
                         bundler_specs.find do |loaded_gem|
                           loaded_gem.to_spec&.name == name.to_s
                         end
                       end
  add_registered_plugin plugin_to_register

  Bridgetown.logger.debug("PluginManager:",
                          "Registered #{plugin_to_register.name}")
end

.setup_bundler(skip_yarn: false) ⇒ Object Also known as: require_from_bundler



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bridgetown-core/plugin_manager.rb', line 63

def self.setup_bundler(skip_yarn: false)
  if !ENV["BRIDGETOWN_NO_BUNDLER_REQUIRE"] &&
      (Bundler::SharedHelpers.in_bundle? || Bridgetown.env.test?)
    require "bundler"

    require_relative "utils/initializers"
    load_determined_bundler_environment(skip_yarn: skip_yarn)

    ENV["BRIDGETOWN_NO_BUNDLER_REQUIRE"] = "true"
    true
  else
    false
  end
end

.source_manifestsObject



54
55
56
57
58
59
60
# File 'lib/bridgetown-core/plugin_manager.rb', line 54

def source_manifests
  Bridgetown::Deprecator.deprecation_message(
    "Use the configuration's `source_manifests` method instead of the plugin manager"
  )

  Bridgetown::Current.preloaded_configuration.source_manifests
end

Instance Method Details

#plugins_pathArray<String>

Expands the path(s) of the plugins_dir config value

Returns:

  • (Array<String>)

    one or more plugin search paths



215
216
217
218
219
220
221
# File 'lib/bridgetown-core/plugin_manager.rb', line 215

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

#require_plugin_filesvoid

This method returns an undefined value.

Finds and registers plugins in the local folder(s)



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/bridgetown-core/plugin_manager.rb', line 194

def require_plugin_files
  plugins_path.each do |plugin_search_path|
    plugin_files = Utils.safe_glob(plugin_search_path, File.join("**", "*.rb"))

    # Require "site_builder.rb" first if present so subclasses can all
    # inherit from SiteBuilder without needing explicit require statements
    sorted_plugin_files = plugin_files.select do |path|
      path.include?("site_builder.rb")
    end + plugin_files.reject do |path|
      path.include?("site_builder.rb")
    end

    sorted_plugin_files.each do |plugin_file|
      self.class.add_registered_plugin plugin_file
    end
  end
end