Class: Bundler::GemBytes::Actions::Gemspec::DeleteDependency

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/gem_bytes/actions/gemspec/delete_dependency.rb

Overview

Remove a dependency in a gemspec

If a dependency on the given gem is not found, this action does nothing.

If one or more dependencies are found on the same gem as gem_name, the are removed from the gemspec.

The gemspec is updated via calls to the tree_rewriter object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree_rewriter, gemspec_block, receiver_name, dependencies) ⇒ DeleteDependency

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the delete dependency action

Parameters:

  • tree_rewriter (Parser::TreeRewriter)

    The object that updates the source

  • gemspec_block (Parser::AST::Node)

    The Gem::Specification block

  • receiver_name (Symbol)

    The name of the receiver for the Gem::Specification block

  • dependencies (Array<DependencyNode>)

    The dependency declarations found in the gemspec file



53
54
55
56
57
58
# File 'lib/bundler/gem_bytes/actions/gemspec/delete_dependency.rb', line 53

def initialize(tree_rewriter, gemspec_block, receiver_name, dependencies)
  @tree_rewriter = tree_rewriter
  @gemspec_block = gemspec_block
  @receiver_name = receiver_name
  @dependencies = dependencies
end

Instance Attribute Details

#dependenciesArray<DependencyNode> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The dependency declarations found in the gemspec file

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bundler/gem_bytes/actions/gemspec/delete_dependency.rb', line 46

class DeleteDependency
  # Initializes the delete dependency action
  # @param tree_rewriter [Parser::TreeRewriter] The object that updates the source
  # @param gemspec_block [Parser::AST::Node] The Gem::Specification block
  # @param receiver_name [Symbol] The name of the receiver for the Gem::Specification block
  # @param dependencies [Array<DependencyNode>] The dependency declarations found in the gemspec file
  # @api private
  def initialize(tree_rewriter, gemspec_block, receiver_name, dependencies)
    @tree_rewriter = tree_rewriter
    @gemspec_block = gemspec_block
    @receiver_name = receiver_name
    @dependencies = dependencies
  end

  attr_reader :tree_rewriter, :gemspec_block, :receiver_name, :dependencies, :gem_name

  # Adds or updates a dependency to the Gem::Specification block
  #
  # @example
  #   delete_dependency = DeleteDependency.new(tree_rewriter, gemspec_block, receiver_name, dependencies)
  #   gem_name = 'rubocop'
  #   depete_dependency.call(gem_name)
  # @param gem_name [String] The name of the gem to remove dependency on
  # @return [void]
  # @api public
  def call(gem_name)
    @gem_name = gem_name
    matching_dependencies = dependencies.select { |d| d.dependency.gem_name == gem_name }

    delete_dependencies(matching_dependencies) if matching_dependencies.any?
  end

  # Removes the matching dependencies from the gemspec
  # @param matching_dependencies [Array<DependencyNode>] The existing dependencies that match gem_name
  # @return [void]
  # @api private
  def delete_dependencies(matching_dependencies)
    matching_dependencies.each do |found_dependency|
      tree_rewriter.replace(full_line_range(found_dependency), '')
    end
  end

  private

  # Expand the range for a node to include any leading whitespace and newline
  # @param dependency_node [DependencyNode] The node to remove
  # @return [Parser::Source::Range] The range of the whole line including whitespace
  # @api private
  def full_line_range(dependency_node)
    range = dependency_node.node.loc.expression
    source_buffer = range.source_buffer
    # The whole line including leading and trailing whitespace
    line_range = source_buffer.line_range(range.line)
    # Expand the range to include the leading newline
    line_range.with(begin_pos: line_range.begin_pos - 1)
  end
end

#gem_nameString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The name of the gem to remove dependency on

Returns:

  • (String)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bundler/gem_bytes/actions/gemspec/delete_dependency.rb', line 46

class DeleteDependency
  # Initializes the delete dependency action
  # @param tree_rewriter [Parser::TreeRewriter] The object that updates the source
  # @param gemspec_block [Parser::AST::Node] The Gem::Specification block
  # @param receiver_name [Symbol] The name of the receiver for the Gem::Specification block
  # @param dependencies [Array<DependencyNode>] The dependency declarations found in the gemspec file
  # @api private
  def initialize(tree_rewriter, gemspec_block, receiver_name, dependencies)
    @tree_rewriter = tree_rewriter
    @gemspec_block = gemspec_block
    @receiver_name = receiver_name
    @dependencies = dependencies
  end

  attr_reader :tree_rewriter, :gemspec_block, :receiver_name, :dependencies, :gem_name

  # Adds or updates a dependency to the Gem::Specification block
  #
  # @example
  #   delete_dependency = DeleteDependency.new(tree_rewriter, gemspec_block, receiver_name, dependencies)
  #   gem_name = 'rubocop'
  #   depete_dependency.call(gem_name)
  # @param gem_name [String] The name of the gem to remove dependency on
  # @return [void]
  # @api public
  def call(gem_name)
    @gem_name = gem_name
    matching_dependencies = dependencies.select { |d| d.dependency.gem_name == gem_name }

    delete_dependencies(matching_dependencies) if matching_dependencies.any?
  end

  # Removes the matching dependencies from the gemspec
  # @param matching_dependencies [Array<DependencyNode>] The existing dependencies that match gem_name
  # @return [void]
  # @api private
  def delete_dependencies(matching_dependencies)
    matching_dependencies.each do |found_dependency|
      tree_rewriter.replace(full_line_range(found_dependency), '')
    end
  end

  private

  # Expand the range for a node to include any leading whitespace and newline
  # @param dependency_node [DependencyNode] The node to remove
  # @return [Parser::Source::Range] The range of the whole line including whitespace
  # @api private
  def full_line_range(dependency_node)
    range = dependency_node.node.loc.expression
    source_buffer = range.source_buffer
    # The whole line including leading and trailing whitespace
    line_range = source_buffer.line_range(range.line)
    # Expand the range to include the leading newline
    line_range.with(begin_pos: line_range.begin_pos - 1)
  end
end

#gemspec_blockParser::AST::Node (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The root AST node of the Gem::Specification block from the gemspec

Returns:

  • (Parser::AST::Node)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bundler/gem_bytes/actions/gemspec/delete_dependency.rb', line 46

class DeleteDependency
  # Initializes the delete dependency action
  # @param tree_rewriter [Parser::TreeRewriter] The object that updates the source
  # @param gemspec_block [Parser::AST::Node] The Gem::Specification block
  # @param receiver_name [Symbol] The name of the receiver for the Gem::Specification block
  # @param dependencies [Array<DependencyNode>] The dependency declarations found in the gemspec file
  # @api private
  def initialize(tree_rewriter, gemspec_block, receiver_name, dependencies)
    @tree_rewriter = tree_rewriter
    @gemspec_block = gemspec_block
    @receiver_name = receiver_name
    @dependencies = dependencies
  end

  attr_reader :tree_rewriter, :gemspec_block, :receiver_name, :dependencies, :gem_name

  # Adds or updates a dependency to the Gem::Specification block
  #
  # @example
  #   delete_dependency = DeleteDependency.new(tree_rewriter, gemspec_block, receiver_name, dependencies)
  #   gem_name = 'rubocop'
  #   depete_dependency.call(gem_name)
  # @param gem_name [String] The name of the gem to remove dependency on
  # @return [void]
  # @api public
  def call(gem_name)
    @gem_name = gem_name
    matching_dependencies = dependencies.select { |d| d.dependency.gem_name == gem_name }

    delete_dependencies(matching_dependencies) if matching_dependencies.any?
  end

  # Removes the matching dependencies from the gemspec
  # @param matching_dependencies [Array<DependencyNode>] The existing dependencies that match gem_name
  # @return [void]
  # @api private
  def delete_dependencies(matching_dependencies)
    matching_dependencies.each do |found_dependency|
      tree_rewriter.replace(full_line_range(found_dependency), '')
    end
  end

  private

  # Expand the range for a node to include any leading whitespace and newline
  # @param dependency_node [DependencyNode] The node to remove
  # @return [Parser::Source::Range] The range of the whole line including whitespace
  # @api private
  def full_line_range(dependency_node)
    range = dependency_node.node.loc.expression
    source_buffer = range.source_buffer
    # The whole line including leading and trailing whitespace
    line_range = source_buffer.line_range(range.line)
    # Expand the range to include the leading newline
    line_range.with(begin_pos: line_range.begin_pos - 1)
  end
end

#receiver_nameSymbol (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The name of the receiver for the Gem::Specification block

Returns:

  • (Symbol)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bundler/gem_bytes/actions/gemspec/delete_dependency.rb', line 46

class DeleteDependency
  # Initializes the delete dependency action
  # @param tree_rewriter [Parser::TreeRewriter] The object that updates the source
  # @param gemspec_block [Parser::AST::Node] The Gem::Specification block
  # @param receiver_name [Symbol] The name of the receiver for the Gem::Specification block
  # @param dependencies [Array<DependencyNode>] The dependency declarations found in the gemspec file
  # @api private
  def initialize(tree_rewriter, gemspec_block, receiver_name, dependencies)
    @tree_rewriter = tree_rewriter
    @gemspec_block = gemspec_block
    @receiver_name = receiver_name
    @dependencies = dependencies
  end

  attr_reader :tree_rewriter, :gemspec_block, :receiver_name, :dependencies, :gem_name

  # Adds or updates a dependency to the Gem::Specification block
  #
  # @example
  #   delete_dependency = DeleteDependency.new(tree_rewriter, gemspec_block, receiver_name, dependencies)
  #   gem_name = 'rubocop'
  #   depete_dependency.call(gem_name)
  # @param gem_name [String] The name of the gem to remove dependency on
  # @return [void]
  # @api public
  def call(gem_name)
    @gem_name = gem_name
    matching_dependencies = dependencies.select { |d| d.dependency.gem_name == gem_name }

    delete_dependencies(matching_dependencies) if matching_dependencies.any?
  end

  # Removes the matching dependencies from the gemspec
  # @param matching_dependencies [Array<DependencyNode>] The existing dependencies that match gem_name
  # @return [void]
  # @api private
  def delete_dependencies(matching_dependencies)
    matching_dependencies.each do |found_dependency|
      tree_rewriter.replace(full_line_range(found_dependency), '')
    end
  end

  private

  # Expand the range for a node to include any leading whitespace and newline
  # @param dependency_node [DependencyNode] The node to remove
  # @return [Parser::Source::Range] The range of the whole line including whitespace
  # @api private
  def full_line_range(dependency_node)
    range = dependency_node.node.loc.expression
    source_buffer = range.source_buffer
    # The whole line including leading and trailing whitespace
    line_range = source_buffer.line_range(range.line)
    # Expand the range to include the leading newline
    line_range.with(begin_pos: line_range.begin_pos - 1)
  end
end

#tree_rewriterParser::TreeRewriter (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The object that updates the source

Returns:

  • (Parser::TreeRewriter)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bundler/gem_bytes/actions/gemspec/delete_dependency.rb', line 46

class DeleteDependency
  # Initializes the delete dependency action
  # @param tree_rewriter [Parser::TreeRewriter] The object that updates the source
  # @param gemspec_block [Parser::AST::Node] The Gem::Specification block
  # @param receiver_name [Symbol] The name of the receiver for the Gem::Specification block
  # @param dependencies [Array<DependencyNode>] The dependency declarations found in the gemspec file
  # @api private
  def initialize(tree_rewriter, gemspec_block, receiver_name, dependencies)
    @tree_rewriter = tree_rewriter
    @gemspec_block = gemspec_block
    @receiver_name = receiver_name
    @dependencies = dependencies
  end

  attr_reader :tree_rewriter, :gemspec_block, :receiver_name, :dependencies, :gem_name

  # Adds or updates a dependency to the Gem::Specification block
  #
  # @example
  #   delete_dependency = DeleteDependency.new(tree_rewriter, gemspec_block, receiver_name, dependencies)
  #   gem_name = 'rubocop'
  #   depete_dependency.call(gem_name)
  # @param gem_name [String] The name of the gem to remove dependency on
  # @return [void]
  # @api public
  def call(gem_name)
    @gem_name = gem_name
    matching_dependencies = dependencies.select { |d| d.dependency.gem_name == gem_name }

    delete_dependencies(matching_dependencies) if matching_dependencies.any?
  end

  # Removes the matching dependencies from the gemspec
  # @param matching_dependencies [Array<DependencyNode>] The existing dependencies that match gem_name
  # @return [void]
  # @api private
  def delete_dependencies(matching_dependencies)
    matching_dependencies.each do |found_dependency|
      tree_rewriter.replace(full_line_range(found_dependency), '')
    end
  end

  private

  # Expand the range for a node to include any leading whitespace and newline
  # @param dependency_node [DependencyNode] The node to remove
  # @return [Parser::Source::Range] The range of the whole line including whitespace
  # @api private
  def full_line_range(dependency_node)
    range = dependency_node.node.loc.expression
    source_buffer = range.source_buffer
    # The whole line including leading and trailing whitespace
    line_range = source_buffer.line_range(range.line)
    # Expand the range to include the leading newline
    line_range.with(begin_pos: line_range.begin_pos - 1)
  end
end

Instance Method Details

#call(gem_name) ⇒ void

This method returns an undefined value.

Adds or updates a dependency to the Gem::Specification block

Examples:

delete_dependency = DeleteDependency.new(tree_rewriter, gemspec_block, receiver_name, dependencies)
gem_name = 'rubocop'
depete_dependency.call(gem_name)

Parameters:

  • gem_name (String)

    The name of the gem to remove dependency on



71
72
73
74
75
76
# File 'lib/bundler/gem_bytes/actions/gemspec/delete_dependency.rb', line 71

def call(gem_name)
  @gem_name = gem_name
  matching_dependencies = dependencies.select { |d| d.dependency.gem_name == gem_name }

  delete_dependencies(matching_dependencies) if matching_dependencies.any?
end

#delete_dependencies(matching_dependencies) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Removes the matching dependencies from the gemspec

Parameters:

  • matching_dependencies (Array<DependencyNode>)

    The existing dependencies that match gem_name



82
83
84
85
86
# File 'lib/bundler/gem_bytes/actions/gemspec/delete_dependency.rb', line 82

def delete_dependencies(matching_dependencies)
  matching_dependencies.each do |found_dependency|
    tree_rewriter.replace(full_line_range(found_dependency), '')
  end
end