Skip to content

Commit

Permalink
Refactoring updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dfop02 committed Jun 9, 2023
1 parent 8397c3f commit 4a11628
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
18 changes: 14 additions & 4 deletions bundler/lib/bundler/plugin.rb
Expand Up @@ -39,8 +39,9 @@ def install(names, options)
raise InvalidOption, "You cannot specify `--branch` and `--ref` at the same time." if options["branch"] && options["ref"]

specs = Installer.new.install(names, options)
plugins = plugins_to_save(names, specs)

save_plugins names, specs
save_plugins(plugins, specs)
rescue PluginError
specs_to_delete = specs.select {|k, _v| names.include?(k) && !index.commands.values.include?(k) }
specs_to_delete.each_value {|spec| Bundler.rm_rf(spec.full_gem_path) }
Expand Down Expand Up @@ -112,10 +113,10 @@ def gemfile_install(gemfile = nil, &inline)

return if definition.dependencies.empty?

plugins = definition.dependencies.map(&:name).reject {|p| index.installed? p }
installed_specs = Installer.new.install_definition(definition)
plugins = plugins_to_save(definition.dependencies.map(&:name), installed_specs)

save_plugins plugins, installed_specs, builder.inferred_plugins
save_plugins(plugins, installed_specs, builder.inferred_plugins)
end
rescue RuntimeError => e
unless e.is_a?(GemfileError)
Expand All @@ -141,6 +142,15 @@ def root
end
end

# Return array of plugins that we need save or update the path
# based if was not installed with that version yet
def plugins_to_save(plugins, installed_specs)
plugins.reject do |p|
installed_spec = installed_specs[p]
installed_spec.nil? || index.version_already_installed?(p, installed_spec.version)
end
end

def local_root
Bundler.app_config_path.join("plugin")
end
Expand Down Expand Up @@ -247,7 +257,7 @@ def installed?(plugin)
# @param [Array<String>] names of inferred source plugins that can be ignored
def save_plugins(plugins, specs, optional_plugins = [])
plugins.each do |name|
next if index.installed?(name)
next if index.version_already_installed?(name, specs[name].version)

spec = specs[name]

Expand Down
10 changes: 4 additions & 6 deletions bundler/lib/bundler/plugin/index.rb
Expand Up @@ -111,14 +111,12 @@ def command_plugin(command)
@commands[command]
end

# Plugin cannot be installed and updating to install
# Plugin version is already installed?
def version_already_installed?(name, version)
installed?(name) && !updating?(name, version)
end
plugin_path = @plugin_paths[name]
return false unless plugin_path

# An existing plugin must have a diff version to install again
def updating?(name, version)
version.to_s != @plugin_paths[name][/#{name}-(.*)$/, 1].to_s
File.basename(plugin_path) == "#{name}-#{version}"
end

# Plugin is already installed?
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/plugin/installer.rb
Expand Up @@ -114,7 +114,7 @@ def install_from_specs(specs)
# @param [String] name of the plugin gem to search in the source
# @param [String] version of the gem to install
#
# @return [Boolean] true if installed or not updating plugin
# @return [Boolean] true if plugin version already installed
def version_already_installed?(plugin, version)
Index.new.version_already_installed?(plugin, version)
end
Expand Down
10 changes: 5 additions & 5 deletions bundler/spec/plugins/install_spec.rb
Expand Up @@ -89,7 +89,7 @@
expect(out).to include("Installing foo 1.1")

bundle "plugin install foo --source #{file_uri_for(gem_repo2)} --verbose"
expect(out).to include("Using foo 1.1")
expect(out).to_not include("foo 1.1")
end

it "installs when --branch specified" do
Expand Down Expand Up @@ -251,7 +251,7 @@ def exec(command, args)
expect(out).to include("Bundle complete!")

expect(the_bundle).to include_gems("rack 1.0.0")
plugin_should_be_installed("foo")
plugin_should_be_installed("foo", version: "1.4.0")

gemfile <<-G
source '#{file_uri_for(gem_repo2)}'
Expand All @@ -265,7 +265,7 @@ def exec(command, args)
expect(out).to include("Bundle complete!")

expect(the_bundle).to include_gems("rack 1.0.0")
plugin_should_be_installed("foo")
plugin_should_be_installed("foo", version: "1.5.0")
end

it "downgrade plugins version listed in gemfile" do
Expand All @@ -287,7 +287,7 @@ def exec(command, args)
expect(out).to include("Bundle complete!")

expect(the_bundle).to include_gems("rack 1.0.0")
plugin_should_be_installed("foo")
plugin_should_be_installed("foo", version: "1.5.0")

gemfile <<-G
source '#{file_uri_for(gem_repo2)}'
Expand All @@ -301,7 +301,7 @@ def exec(command, args)
expect(out).to include("Bundle complete!")

expect(the_bundle).to include_gems("rack 1.0.0")
plugin_should_be_installed("foo")
plugin_should_be_installed("foo", version: "1.4.0")
end

it "install only plugins not installed yet listed in gemfile" do
Expand Down
4 changes: 3 additions & 1 deletion bundler/spec/support/matchers.rb
Expand Up @@ -220,10 +220,12 @@ def indent(string, padding = 4, indent_character = " ")
RSpec::Matchers.define_negated_matcher :not_include_gems, :include_gems
RSpec::Matchers.alias_matcher :include_gem, :include_gems

def plugin_should_be_installed(*names)
def plugin_should_be_installed(*names, version: nil)
names.each do |name|
expect(Bundler::Plugin).to be_installed(name)
path = Pathname.new(Bundler::Plugin.installed?(name))

expect(File.basename(path)).to eq("#{name}-#{version}") unless version.nil?
expect(path + "plugins.rb").to exist
end
end
Expand Down

0 comments on commit 4a11628

Please sign in to comment.