Sha256: b8e2556f4df74e3c57a9bedc83e42bd629f827e92909a890543f615da2fb9ea9

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

# typed: true
module Parlour
  # The base class for user-defined RBI generation plugins.
  # @abstract
  class Plugin
    extend T::Sig
    extend T::Helpers
    abstract!

    @@registered_plugins = {}

    sig { returns(T::Hash[String, T.class_of(Plugin)]) }
    # Returns all registered plugins, as a hash of their paths to the {Plugin}
    # instances themselves.
    #
    # @return [{String, Plugin}]
    def self.registered_plugins
      @@registered_plugins
    end

    sig { params(new_plugin: T.class_of(Plugin)).void }
    # Called automatically by the Ruby interpreter when {Plugin} is subclassed.
    # This registers the new subclass into {registered_plugins}.
    #
    # @param new_plugin [Plugin] The new plugin.
    # @return [void]
    def self.inherited(new_plugin)
      registered_plugins[T.must(new_plugin.name)] = new_plugin
    end

    sig { params(plugins: T::Array[Plugin], generator: RbiGenerator).void }
    # Runs an array of plugins on a given generator instance.
    #
    # @param plugins [Array<Plugin>] An array of {Plugin} instances.
    # @param generator [RbiGenerator] The {RbiGenerator} to run the plugins on.
    # @return [void]
    def self.run_plugins(plugins, generator)
      plugins.each do |plugin|
        puts "=== #{plugin.class.name}"
        generator.current_plugin = plugin
        plugin.generate(generator.root)
      rescue Exception => e
        puts "!!! This plugin threw an exception: #{e}"
      end
    end

    sig { params(options: Hash).void }
    def initialize(options); end

    sig { abstract.params(root: RbiGenerator::Namespace).void }
    # Plugin subclasses should redefine this method and do their RBI generation
    # inside it.
    #
    # @abstract
    # @param root [RbiGenerator::Namespace] The root {RbiGenerator::Namespace}.
    # @return [void]
    def generate(root); end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
parlour-0.5.1 lib/parlour/plugin.rb
parlour-0.5.0 lib/parlour/plugin.rb
parlour-0.4.0 lib/parlour/plugin.rb