Debian Codegraph @

lib/bundler/plugin/installer.rb

raw
1 # frozen_string_literal: true
2
3 module Bundler
4 # Handles the installation of plugin in appropriate directories.
5 #
6 # This class is supposed to be wrapper over the existing gem installation infra
7 # but currently it itself handles everything as the Source's subclasses (e.g. Source::RubyGems)
8 # are heavily dependent on the Gemfile.
9 module Plugin
10 class Installer
11 autoload :Rubygems, File.expand_path("installer/rubygems", __dir__)
12 autoload :Git, File.expand_path("installer/git", __dir__)
13 autoload :Path, File.expand_path("installer/path", __dir__)
14
15 def install(names, options)
16 check_sources_consistency!(options)
17
18 version = options[:version] || [">= 0"]
19
20 if options[:git]
21 install_git(names, version, options)
22 elsif options[:path]
23 install_path(names, version, options[:path])
24 else
25 sources = options[:source] || Gem.sources
26 install_rubygems(names, version, sources)
27 end
28 end
29
30 # Installs the plugin from Definition object created by limited parsing of
31 # Gemfile searching for plugins to be installed
32 #
33 # @param [Definition] definition object
34 # @return [Hash] map of names to their specs they are installed with
35 def install_definition(definition)
36 def definition.lock(*); end
37 definition.resolve_remotely!
38 specs = definition.specs
39
40 install_from_specs specs
41 end
42
43 private
44
45 def check_sources_consistency!(options)
46 if options.key?(:git) && options.key?(:local_git)
47 raise InvalidOption, "Remote and local plugin git sources can't be both specified"
48 end
49
50 # back-compat; local_git is an alias for git
51 if options.key?(:local_git)
52 Bundler::SharedHelpers.major_deprecation(2, "--local_git is deprecated, use --git")
53 options[:git] = options.delete(:local_git)
54 end
55
56 if (options.keys & [:source, :git, :path]).length > 1
57 raise InvalidOption, "Only one of --source, --git, or --path may be specified"
58 end
59
60 if (options.key?(:branch) || options.key?(:ref)) && !options.key?(:git)
61 raise InvalidOption, "--#{options.key?(:branch) ? "branch" : "ref"} can only be used with git sources"
62 end
63
64 if options.key?(:branch) && options.key?(:ref)
65 raise InvalidOption, "--branch and --ref can't be both specified"
66 end
67 end
68
69 def install_git(names, version, options)
70 source_list = SourceList.new
71 source = source_list.add_git_source({ "uri" => options[:git],
72 "branch" => options[:branch],
73 "ref" => options[:ref] })
74
75 install_all_sources(names, version, source_list, source)
76 end
77
78 def install_path(names, version, path)
79 source_list = SourceList.new
80 source = source_list.add_path_source({ "path" => path, "root_path" => SharedHelpers.pwd })
81
82 install_all_sources(names, version, source_list, source)
83 end
84
85 # Installs the plugin from rubygems source and returns the path where the
86 # plugin was installed
87 #
88 # @param [String] name of the plugin gem to search in the source
89 # @param [Array] version of the gem to install
90 # @param [String, Array<String>] source(s) to resolve the gem
91 #
92 # @return [Hash] map of names to the specs of plugins installed
93 def install_rubygems(names, version, sources)
94 source_list = SourceList.new
95
96 Array(sources).each {|remote| source_list.add_global_rubygems_remote(remote) }
97
98 install_all_sources(names, version, source_list)
99 end
100
101 def install_all_sources(names, version, source_list, source = nil)
102 deps = names.map {|name| Dependency.new(name, version, { "source" => source }) }
103
104 Bundler.configure_gem_home_and_path(Plugin.root)
105
106 Bundler.settings.temporary(deployment: false, frozen: false) do
107 definition = Definition.new(nil, deps, source_list, true)
108
109 install_definition(definition)
110 end
111 end
112
113 # Installs the plugins and deps from the provided specs and returns map of
114 # gems to their paths
115 #
116 # @param specs to install
117 #
118 # @return [Hash] map of names to the specs
119 def install_from_specs(specs)
120 paths = {}
121
122 specs.each do |spec|
123 spec.source.install spec
124
125 paths[spec.name] = spec
126 end
127
128 paths
129 end
130 end
131 end
132 end