| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
module Bundler |
| 4 |
# This is the interfacing class represents the API that we intend to provide |
| 5 |
# the plugins to use. |
| 6 |
# |
| 7 |
# For plugins to be independent of the Bundler internals they shall limit their |
| 8 |
# interactions to methods of this class only. This will save them from breaking |
| 9 |
# when some internal change. |
| 10 |
# |
| 11 |
# Currently we are delegating the methods defined in Bundler class to |
| 12 |
# itself. So, this class acts as a buffer. |
| 13 |
# |
| 14 |
# If there is some change in the Bundler class that is incompatible to its |
| 15 |
# previous behavior or if otherwise desired, we can reimplement(or implement) |
| 16 |
# the method to preserve compatibility. |
| 17 |
# |
| 18 |
# To use this, either the class can inherit this class or use it directly. |
| 19 |
# For example of both types of use, refer the file `spec/plugins/command.rb` |
| 20 |
# |
| 21 |
# To use it without inheriting, you will have to create an object of this |
| 22 |
# to use the functions (except for declaration functions like command, source, |
| 23 |
# and hooks). |
| 24 |
module Plugin |
| 25 |
class API |
| 26 |
autoload :Source, File.expand_path("api/source", __dir__) |
| 27 |
|
| 28 |
# The plugins should declare that they handle a command through this helper. |
| 29 |
# |
| 30 |
# @param [String] command being handled by them |
| 31 |
# @param [Class] (optional) class that handles the command. If not |
| 32 |
# provided, the `self` class will be used. |
| 33 |
def self.command(command, cls = self) |
| 34 |
Plugin.add_command command, cls |
| 35 |
end |
| 36 |
|
| 37 |
# The plugins should declare that they provide a installation source |
| 38 |
# through this helper. |
| 39 |
# |
| 40 |
# @param [String] the source type they provide |
| 41 |
# @param [Class] (optional) class that handles the source. If not |
| 42 |
# provided, the `self` class will be used. |
| 43 |
def self.source(source, cls = self) |
| 44 |
cls.send :include, Bundler::Plugin::API::Source |
| 45 |
Plugin.add_source source, cls |
| 46 |
end |
| 47 |
|
| 48 |
def self.hook(event, &block) |
| 49 |
Plugin.add_hook(event, &block) |
| 50 |
end |
| 51 |
|
| 52 |
# The cache dir to be used by the plugins for storage |
| 53 |
# |
| 54 |
# @return [Pathname] path of the cache dir |
| 55 |
def cache_dir |
| 56 |
Plugin.cache.join("plugins") |
| 57 |
end |
| 58 |
|
| 59 |
# A tmp dir to be used by plugins |
| 60 |
# Accepts names that get concatenated as suffix |
| 61 |
# |
| 62 |
# @return [Pathname] object for the new directory created |
| 63 |
def tmp(*names) |
| 64 |
Bundler.tmp(["plugin", *names].join("-")) |
| 65 |
end |
| 66 |
|
| 67 |
def method_missing(name, *args, &blk) |
| 68 |
return Bundler.send(name, *args, &blk) if Bundler.respond_to?(name) |
| 69 |
|
| 70 |
return SharedHelpers.send(name, *args, &blk) if SharedHelpers.respond_to?(name) |
| 71 |
|
| 72 |
super |
| 73 |
end |
| 74 |
|
| 75 |
def respond_to_missing?(name, include_private = false) |
| 76 |
SharedHelpers.respond_to?(name, include_private) || |
| 77 |
Bundler.respond_to?(name, include_private) || super |
| 78 |
end |
| 79 |
end |
| 80 |
end |
| 81 |
end |