summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsutosh Palai <asupalai@gmail.com>2016-06-06 22:27:02 +0530
committerAsutosh Palai <asupalai@gmail.com>2016-06-06 22:27:02 +0530
commit214d3d0c33751b206c37dd618db2f0deede1744c (patch)
treec0de8ab8ddc6d06f5dc4b6581e0023685b4f81e7
parentbfd4be3756fa6bb43c263e303df2995ab0e7ace9 (diff)
downloadbundler-214d3d0c33751b206c37dd618db2f0deede1744c.tar.gz
Nested the nested classes
-rw-r--r--lib/bundler/plugin/api.rb52
-rw-r--r--lib/bundler/plugin/dsl.rb32
-rw-r--r--lib/bundler/plugin/index.rb158
-rw-r--r--lib/bundler/plugin/installer.rb140
-rw-r--r--lib/bundler/plugin/installer/git.rb48
-rw-r--r--lib/bundler/plugin/installer/rubygems.rb30
-rw-r--r--lib/bundler/plugin/source_list.rb22
7 files changed, 250 insertions, 232 deletions
diff --git a/lib/bundler/plugin/api.rb b/lib/bundler/plugin/api.rb
index 4771c26121..12fa566d0b 100644
--- a/lib/bundler/plugin/api.rb
+++ b/lib/bundler/plugin/api.rb
@@ -21,34 +21,36 @@ module Bundler
# To use it without inheriting, you will have to create an object of this
# to use the functions (except for declaration functions like command, source,
# and hooks).
- class Plugin::API
- # The plugins should declare that they handle a command through this helper.
- #
- # @param [String] command being handled by them
- # @param [Class] (optional) class that shall handle the command. If not
- # provided, the `self` class will be used.
- def self.command(command, cls = self)
- Plugin.add_command command, cls
- end
+ module Plugin
+ class API
+ # The plugins should declare that they handle a command through this helper.
+ #
+ # @param [String] command being handled by them
+ # @param [Class] (optional) class that shall handle the command. If not
+ # provided, the `self` class will be used.
+ def self.command(command, cls = self)
+ Plugin.add_command command, cls
+ end
- # The cache dir to be used by the plugins for persistance storage
- #
- # @return [Pathname] path of the cache dir
- def cache
- Plugin.cache.join("plugins")
- end
+ # The cache dir to be used by the plugins for persistance storage
+ #
+ # @return [Pathname] path of the cache dir
+ def cache
+ Plugin.cache.join("plugins")
+ end
- # A tmp dir to be used by plugins
- #
- # @param [String] name unique for the plugin or the purpose
- # @return [Pathname] object for the new directory created
- def tmp(name)
- Bundler.tmp(File.join(["plugin", name]))
- end
+ # A tmp dir to be used by plugins
+ #
+ # @param [String] name unique for the plugin or the purpose
+ # @return [Pathname] object for the new directory created
+ def tmp(name)
+ Bundler.tmp(File.join(["plugin", name]))
+ end
- def method_missing(name, *args, &blk)
- super unless Bundler.respond_to?(name)
- Bundler.send(name, *args, &blk)
+ def method_missing(name, *args, &blk)
+ super unless Bundler.respond_to?(name)
+ Bundler.send(name, *args, &blk)
+ end
end
end
end
diff --git a/lib/bundler/plugin/dsl.rb b/lib/bundler/plugin/dsl.rb
index dc1eb538d5..4a3663fd1b 100644
--- a/lib/bundler/plugin/dsl.rb
+++ b/lib/bundler/plugin/dsl.rb
@@ -2,25 +2,27 @@
module Bundler
# Dsl to parse the Gemfile looking for plugins to install
- class Plugin::DSL < Bundler::Dsl
- alias_method :_gem, :gem # To use for plugin installation as gem
+ module Plugin
+ class DSL < Bundler::Dsl
+ alias_method :_gem, :gem # To use for plugin installation as gem
- # So that we don't have to override all there methods to dummy ones
- # explicitly.
- # They will be handled by method_missing
- [:gemspec, :gem, :path, :install_if, :platforms, :env].each {|m| undef_method m }
+ # So that we don't have to override all there methods to dummy ones
+ # explicitly.
+ # They will be handled by method_missing
+ [:gemspec, :gem, :path, :install_if, :platforms, :env].each {|m| undef_method m }
- def initialize
- super
- @sources = Plugin::SourceList.new
- end
+ def initialize
+ super
+ @sources = Plugin::SourceList.new
+ end
- def plugin(name, *args)
- _gem(name, *args)
- end
+ def plugin(name, *args)
+ _gem(name, *args)
+ end
- def method_missing(name, *args)
- super unless Bundler::Dsl.instance_methods.include? name
+ def method_missing(name, *args)
+ super unless Bundler::Dsl.instance_methods.include? name
+ end
end
end
end
diff --git a/lib/bundler/plugin/index.rb b/lib/bundler/plugin/index.rb
index c4a25be22e..b276bfdf4f 100644
--- a/lib/bundler/plugin/index.rb
+++ b/lib/bundler/plugin/index.rb
@@ -4,104 +4,106 @@ module Bundler
# Manages which plugins are installed and their sources. This also is supposed to map
# which plugin does what (currently the features are not implemented so this class is
# now a stub class).
- class Plugin::Index
- def initialize
- @plugin_paths = {}
- @commands = {}
+ module Plugin
+ class Index
+ def initialize
+ @plugin_paths = {}
+ @commands = {}
- load_index
- end
+ load_index
+ end
- # This function is to be called when a new plugin is installed. This function shall add
- # the functions of the plugin to existing maps and also the name to source location.
- #
- # @param [String] name of the plugin to be registered
- # @param [String] path where the plugin is installed
- # @param [Array<String>] commands that are handled by the plugin
- def register_plugin(name, path, commands)
- @plugin_paths[name] = path
+ # This function is to be called when a new plugin is installed. This function shall add
+ # the functions of the plugin to existing maps and also the name to source location.
+ #
+ # @param [String] name of the plugin to be registered
+ # @param [String] path where the plugin is installed
+ # @param [Array<String>] commands that are handled by the plugin
+ def register_plugin(name, path, commands)
+ @plugin_paths[name] = path
- common = commands & @commands.keys
- raise "Command(s) #{common.join(", ")} are already registered" if common.any?
- commands.each {|c| @commands[c] = name }
+ common = commands & @commands.keys
+ raise "Command(s) #{common.join(", ")} are already registered" if common.any?
+ commands.each {|c| @commands[c] = name }
- save_index
- end
+ save_index
+ end
- # Path where the index file is stored
- def index_file
- Plugin.root.join("index")
- end
+ # Path where the index file is stored
+ def index_file
+ Plugin.root.join("index")
+ end
- def plugin_path(name)
- Pathname.new @plugin_paths[name]
- end
+ def plugin_path(name)
+ Pathname.new @plugin_paths[name]
+ end
- # Fetch the name of plugin handling the command
- def command_plugin(command)
- @commands[command]
- end
+ # Fetch the name of plugin handling the command
+ def command_plugin(command)
+ @commands[command]
+ end
- private
+ private
- # Reads the index file from the directory and initializes the instance variables.
- def load_index
- SharedHelpers.filesystem_access(index_file, :read) do |index_f|
- valid_file = index_f && index_f.exist? && !index_f.size.zero?
- break unless valid_file
- data = index_f.read
- index = load_yaml(data)
- @plugin_paths = index["plugin_paths"] || {}
- @commands = index["commands"] || {}
+ # Reads the index file from the directory and initializes the instance variables.
+ def load_index
+ SharedHelpers.filesystem_access(index_file, :read) do |index_f|
+ valid_file = index_f && index_f.exist? && !index_f.size.zero?
+ break unless valid_file
+ data = index_f.read
+ index = load_yaml(data)
+ @plugin_paths = index["plugin_paths"] || {}
+ @commands = index["commands"] || {}
+ end
end
- end
- # Should be called when any of the instance variables change. Stores the instance
- # variables in YAML format. (The instance variables are supposed to be only String key value pairs)
- def save_index
- index = {
- "plugin_paths" => @plugin_paths,
- "commands" => @commands,
- }
+ # Should be called when any of the instance variables change. Stores the instance
+ # variables in YAML format. (The instance variables are supposed to be only String key value pairs)
+ def save_index
+ index = {
+ "plugin_paths" => @plugin_paths,
+ "commands" => @commands,
+ }
- SharedHelpers.filesystem_access(index_file) do |index_f|
- FileUtils.mkdir_p(index_f.dirname)
- File.open(index_f, "w") {|f| f.puts dump_hash(index) }
+ SharedHelpers.filesystem_access(index_file) do |index_f|
+ FileUtils.mkdir_p(index_f.dirname)
+ File.open(index_f, "w") {|f| f.puts dump_hash(index) }
+ end
end
- end
- def dump_yaml(hash)
- yaml = "---\n"
- yaml << dump_hash(hash)
- end
+ def dump_yaml(hash)
+ yaml = "---\n"
+ yaml << dump_hash(hash)
+ end
- def dump_hash(hash)
- yaml = String.new("")
- hash.each do |k, v|
- yaml << k << ": "
- if v.is_a?(Hash)
- yaml << "\n" << dump_hash(v).gsub(/^/, " ") << "\n"
- else
- yaml << v.to_s.gsub(/\s+/, " ").inspect << "\n"
+ def dump_hash(hash)
+ yaml = String.new("")
+ hash.each do |k, v|
+ yaml << k << ": "
+ if v.is_a?(Hash)
+ yaml << "\n" << dump_hash(v).gsub(/^/, " ") << "\n"
+ else
+ yaml << v.to_s.gsub(/\s+/, " ").inspect << "\n"
+ end
end
+ yaml
end
- yaml
- end
- def load_yaml(str)
- res = {}
- stack = [res]
- str.scan(/^( *)(.*):\s?(["']?)([^"'\n]*)\3\n/).each do |(indent, key, _, val)|
- depth = indent.scan(/ /).length
- if val.empty?
- new_hash = {}
- stack[depth][key] = new_hash
- stack[depth + 1] = new_hash
- else
- stack[depth][key] = val
+ def load_yaml(str)
+ res = {}
+ stack = [res]
+ str.scan(/^( *)(.*):\s?(["']?)([^"'\n]*)\3\n/).each do |(indent, key, _, val)|
+ depth = indent.scan(/ /).length
+ if val.empty?
+ new_hash = {}
+ stack[depth][key] = new_hash
+ stack[depth + 1] = new_hash
+ else
+ stack[depth][key] = val
+ end
end
+ res
end
- res
end
end
end
diff --git a/lib/bundler/plugin/installer.rb b/lib/bundler/plugin/installer.rb
index 38ca58ba09..d19634489b 100644
--- a/lib/bundler/plugin/installer.rb
+++ b/lib/bundler/plugin/installer.rb
@@ -6,95 +6,97 @@ module Bundler
# This class is supposed to be wrapper over the existing gem installation infra
# but currently it itself handles everything as the Source's subclasses (e.g. Source::RubyGems)
# are heavily dependent on the Gemfile.
- class Plugin::Installer
- autoload :Rubygems, "bundler/plugin/installer/rubygems"
- autoload :Git, "bundler/plugin/installer/git"
-
- def install(name, options)
- if options[:git]
- install_git(name, options)
- elsif options[:source]
- source = options[:source]
- version = options[:version] || [">= 0"]
-
- install_rubygems(name, source, version)
- else
- raise(ArgumentError, "You need to provide the source")
+ module Plugin
+ class Installer
+ autoload :Rubygems, "bundler/plugin/installer/rubygems"
+ autoload :Git, "bundler/plugin/installer/git"
+
+ def install(name, options)
+ if options[:git]
+ install_git(name, options)
+ elsif options[:source]
+ source = options[:source]
+ version = options[:version] || [">= 0"]
+
+ install_rubygems(name, source, version)
+ else
+ raise(ArgumentError, "You need to provide the source")
+ end
end
- end
- # Installs the plugin from Definition object created by limited parsing of
- # Gemfile searching for plugins to be installed
- #
- # @param [Definition] definiton object
- # @return [Hash] map of plugin names to thier paths
- def install_definition(definition)
- plugins = definition.dependencies.map(&:name)
+ # Installs the plugin from Definition object created by limited parsing of
+ # Gemfile searching for plugins to be installed
+ #
+ # @param [Definition] definiton object
+ # @return [Hash] map of plugin names to thier paths
+ def install_definition(definition)
+ plugins = definition.dependencies.map(&:name)
- definition.resolve_remotely!
- specs = definition.specs
+ definition.resolve_remotely!
+ specs = definition.specs
- paths = install_from_specs specs
+ paths = install_from_specs specs
- paths.select {|name, _| plugins.include? name }
- end
+ paths.select {|name, _| plugins.include? name }
+ end
- private
+ private
- def install_git(name, options)
- uri = options.delete(:git)
+ def install_git(name, options)
+ uri = options.delete(:git)
- options[:name] = name
- options[:uri] = uri
+ options[:name] = name
+ options[:uri] = uri
- git_source = Git.new options
- git_source.remote!
+ git_source = Git.new options
+ git_source.remote!
- git_source.install(git_source.specs.first)
+ git_source.install(git_source.specs.first)
- git_source.path
- end
+ git_source.path
+ end
- # Installs the plugin from rubygems source and returns the path where the
- # plugin was installed
- #
- # @param [String] name of the plugin gem to search in the source
- # @param [String] source the rubygems URL to resolve the gem
- # @param [Array, String] version (optional) of the gem to install
- #
- # @return [String] the path where the plugin was installed
- def install_rubygems(name, source, version = [">= 0"])
- rg_source = Rubygems.new "remotes" => source
- rg_source.remote!
- rg_source.dependency_names << name
+ # Installs the plugin from rubygems source and returns the path where the
+ # plugin was installed
+ #
+ # @param [String] name of the plugin gem to search in the source
+ # @param [String] source the rubygems URL to resolve the gem
+ # @param [Array, String] version (optional) of the gem to install
+ #
+ # @return [String] the path where the plugin was installed
+ def install_rubygems(name, source, version = [">= 0"])
+ rg_source = Rubygems.new "remotes" => source
+ rg_source.remote!
+ rg_source.dependency_names << name
- dep = Dependency.new name, version
+ dep = Dependency.new name, version
- deps_proxies = [DepProxy.new(dep, GemHelpers.generic_local_platform)]
- idx = rg_source.specs
+ deps_proxies = [DepProxy.new(dep, GemHelpers.generic_local_platform)]
+ idx = rg_source.specs
- specs = Resolver.resolve(deps_proxies, idx).materialize([dep])
- paths = install_from_specs specs
+ specs = Resolver.resolve(deps_proxies, idx).materialize([dep])
+ paths = install_from_specs specs
- paths[name]
- end
+ paths[name]
+ end
- # Installs the plugins and deps from the provided specs and returns map of
- # gems to their paths
- #
- # @param specs to install
- #
- # @return [Hash] map of names to path where the plugin was installed
- def install_from_specs(specs)
- paths = {}
+ # Installs the plugins and deps from the provided specs and returns map of
+ # gems to their paths
+ #
+ # @param specs to install
+ #
+ # @return [Hash] map of names to path where the plugin was installed
+ def install_from_specs(specs)
+ paths = {}
- specs.each do |spec|
- spec.source.install spec
+ specs.each do |spec|
+ spec.source.install spec
- paths[spec.name] = spec.full_gem_path
- end
+ paths[spec.name] = spec.full_gem_path
+ end
- paths
+ paths
+ end
end
end
end
diff --git a/lib/bundler/plugin/installer/git.rb b/lib/bundler/plugin/installer/git.rb
index 3bff511159..fbb6c5e40e 100644
--- a/lib/bundler/plugin/installer/git.rb
+++ b/lib/bundler/plugin/installer/git.rb
@@ -1,34 +1,38 @@
# frozen_string_literal: true
module Bundler
- class Plugin::Installer::Git < Bundler::Source::Git
- def cache_path
- @cache_path ||= begin
- git_scope = "#{base_name}-#{uri_hash}"
+ module Plugin
+ class Installer
+ class Git < Bundler::Source::Git
+ def cache_path
+ @cache_path ||= begin
+ git_scope = "#{base_name}-#{uri_hash}"
- Plugin.cache.join("bundler", "git", git_scope)
- end
- end
+ Plugin.cache.join("bundler", "git", git_scope)
+ end
+ end
- def install_path
- @install_path ||= begin
- git_scope = "#{base_name}-#{shortref_for_path(revision)}"
+ def install_path
+ @install_path ||= begin
+ git_scope = "#{base_name}-#{shortref_for_path(revision)}"
- Plugin.root.join("bundler", "gems", git_scope)
- end
- end
+ Plugin.root.join("bundler", "gems", git_scope)
+ end
+ end
- def version_message(spec)
- "#{spec.name} #{spec.version}"
- end
+ def version_message(spec)
+ "#{spec.name} #{spec.version}"
+ end
- def root
- Plugin.root
- end
+ def root
+ Plugin.root
+ end
- def generate_bin(spec, disable_extensions = false)
- # Need to find a way without code duplication
- # For now, we can ignore this
+ def generate_bin(spec, disable_extensions = false)
+ # Need to find a way without code duplication
+ # For now, we can ignore this
+ end
+ end
end
end
end
diff --git a/lib/bundler/plugin/installer/rubygems.rb b/lib/bundler/plugin/installer/rubygems.rb
index 3b44210bc7..7ae74fa93b 100644
--- a/lib/bundler/plugin/installer/rubygems.rb
+++ b/lib/bundler/plugin/installer/rubygems.rb
@@ -1,23 +1,27 @@
# frozen_string_literal: true
module Bundler
- class Plugin::Installer::Rubygems < Bundler::Source::Rubygems
- def version_message(spec)
- "#{spec.name} #{spec.version}"
- end
+ module Plugin
+ class Installer
+ class Rubygems < Bundler::Source::Rubygems
+ def version_message(spec)
+ "#{spec.name} #{spec.version}"
+ end
- private
+ private
- def requires_sudo?
- false # Will change on implementation of project level plugins
- end
+ def requires_sudo?
+ false # Will change on implementation of project level plugins
+ end
- def rubygems_dir
- Plugin.root
- end
+ def rubygems_dir
+ Plugin.root
+ end
- def cache_path
- Plugin.cache
+ def cache_path
+ Plugin.cache
+ end
+ end
end
end
end
diff --git a/lib/bundler/plugin/source_list.rb b/lib/bundler/plugin/source_list.rb
index b29cf57b51..29acc5beb0 100644
--- a/lib/bundler/plugin/source_list.rb
+++ b/lib/bundler/plugin/source_list.rb
@@ -3,18 +3,20 @@
module Bundler
# SourceList object to be used while parsing the Gemfile, setting the
# approptiate options to be used with Source classes for plugin installation
- class Plugin::SourceList < Bundler::SourceList
- def initialize
- super
- @rubygems_aggregate = Plugin::Installer::Rubygems.new
- end
+ module Plugin
+ class SourceList < Bundler::SourceList
+ def initialize
+ super
+ @rubygems_aggregate = Plugin::Installer::Rubygems.new
+ end
- def add_git_source(options = {})
- add_source_to_list Plugin::Installer::Git.new(options), git_sources
- end
+ def add_git_source(options = {})
+ add_source_to_list Plugin::Installer::Git.new(options), git_sources
+ end
- def add_rubygems_source(options = {})
- add_source_to_list Plugin::Installer::Rubygems.new(options), @rubygems_sources
+ def add_rubygems_source(options = {})
+ add_source_to_list Plugin::Installer::Rubygems.new(options), @rubygems_sources
+ end
end
end
end