| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
module Bundler |
| 4 |
module Plugin |
| 5 |
# Dsl to parse the Gemfile looking for plugins to install |
| 6 |
class DSL < Bundler::Dsl |
| 7 |
class PluginGemfileError < PluginError; end |
| 8 |
alias_method :_gem, :gem # To use for plugin installation as gem |
| 9 |
|
| 10 |
# So that we don't have to override all there methods to dummy ones |
| 11 |
# explicitly. |
| 12 |
# They will be handled by method_missing |
| 13 |
[:gemspec, :gem, :install_if, :platforms, :env].each {|m| undef_method m } |
| 14 |
|
| 15 |
# This lists the plugins that was added automatically and not specified by |
| 16 |
# the user. |
| 17 |
# |
| 18 |
# When we encounter :type attribute with a source block, we add a plugin |
| 19 |
# by name bundler-source-<type> to list of plugins to be installed. |
| 20 |
# |
| 21 |
# These plugins are optional and are not installed when there is conflict |
| 22 |
# with any other plugin. |
| 23 |
attr_reader :inferred_plugins |
| 24 |
|
| 25 |
def initialize |
| 26 |
super |
| 27 |
@sources = Plugin::SourceList.new |
| 28 |
@inferred_plugins = [] # The source plugins inferred from :type |
| 29 |
end |
| 30 |
|
| 31 |
def plugin(name, *args) |
| 32 |
_gem(name, *args) |
| 33 |
end |
| 34 |
|
| 35 |
def method_missing(name, *args) |
| 36 |
raise PluginGemfileError, "Undefined local variable or method `#{name}' for Gemfile" unless Bundler::Dsl.method_defined? name |
| 37 |
end |
| 38 |
|
| 39 |
def source(source, *args, &blk) |
| 40 |
options = args.last.is_a?(Hash) ? args.pop.dup : {} |
| 41 |
options = normalize_hash(options) |
| 42 |
return super unless options.key?("type") |
| 43 |
|
| 44 |
plugin_name = "bundler-source-#{options["type"]}" |
| 45 |
|
| 46 |
return if @dependencies.any? {|d| d.name == plugin_name } |
| 47 |
|
| 48 |
plugin(plugin_name) |
| 49 |
@inferred_plugins << plugin_name |
| 50 |
end |
| 51 |
end |
| 52 |
end |
| 53 |
end |