|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'parser/current' |
| 4 | +require 'rubocop-ast' |
| 5 | +require 'active_support/core_ext/object' |
| 6 | + |
| 7 | +module Bundler |
| 8 | + module GemBytes |
| 9 | + module Gemspec |
| 10 | + # Delete a dependency in a gemspec file |
| 11 | + # |
| 12 | + # This class works by parsing the gemspec file into an AST and then walking the |
| 13 | + # AST to find the Gem::Specification block (via #on_block). Once the block is |
| 14 | + # found, AST within that block is walked to locate the dependency declarations |
| 15 | + # (via #on_send). Any dependency declaration that matches the given gem name is |
| 16 | + # collected into the found_dependencies array. |
| 17 | + # |
| 18 | + # Once the Gem::Specification block is fully processed, any dependencies on the |
| 19 | + # given gem are deleted from the gemspec source. |
| 20 | + # |
| 21 | + # If the dependency is not found, the gemspec source is returned unmodified. |
| 22 | + # |
| 23 | + # @example |
| 24 | + # require 'bundler/gem_bytes' |
| 25 | + # |
| 26 | + # delete_dependency = Bundler::GemBytes::Gemspec::DeleteDependency.new('test_tool') |
| 27 | + # |
| 28 | + # gemspec_file = 'foo.gemspec' |
| 29 | + # gemspec = File.read(gemspec_file) |
| 30 | + # updated_gemspec = delete_dependency.call(gemspec, path: gemspec_file) |
| 31 | + # File.write(gemspec_file, updated_gemspec) |
| 32 | + # |
| 33 | + # @!attribute [r] gem_name |
| 34 | + # The name of the gem to add a dependency on (i.e. 'rubocop') |
| 35 | + # @return [String] |
| 36 | + # @api private |
| 37 | + # |
| 38 | + # @!attribute [r] receiver_name |
| 39 | + # The name of the receiver for the Gem::Specification block |
| 40 | + # |
| 41 | + # i.e. 'spec' in `spec.add_dependency 'rubocop', '~> 1.0'` |
| 42 | + # |
| 43 | + # @return [Symbol] |
| 44 | + # @api private |
| 45 | + # |
| 46 | + # @!attribute [r] found_gemspec_block |
| 47 | + # Whether the Gem::Specification block was found in the gemspec file |
| 48 | + # |
| 49 | + # Only valid after calling `#call`. |
| 50 | + # @return [Boolean] |
| 51 | + # @api private |
| 52 | + # |
| 53 | + # @!attribute [r] found_dependencies |
| 54 | + # The dependencies found in the gemspec file |
| 55 | + # |
| 56 | + # Only valid after calling `#call`. |
| 57 | + # @return [Array<Hash>] |
| 58 | + # @api private |
| 59 | + # |
| 60 | + # @api public |
| 61 | + class DeleteDependency < Parser::TreeRewriter |
| 62 | + # Create a new instance of a dependency upserter |
| 63 | + # @example |
| 64 | + # command = Bundler::GemBytes::Gemspec::DeleteDependency.new('my_gem') |
| 65 | + # @param gem_name [String] The name of the gem to add a dependency on |
| 66 | + def initialize(gem_name) |
| 67 | + super() |
| 68 | + |
| 69 | + @gem_name = gem_name |
| 70 | + |
| 71 | + @found_dependencies = [] |
| 72 | + end |
| 73 | + |
| 74 | + # Returns the content of the gemspec file with the dependency deleted |
| 75 | + # |
| 76 | + # @param code [String] The content of the gemspec file |
| 77 | + # @param path [String] This should be the path to the gemspspec file |
| 78 | + # |
| 79 | + # path is used to generate error messages only |
| 80 | + # |
| 81 | + # @return [String] The updated gemspec content with the dependency deleted |
| 82 | + # |
| 83 | + # @raise [ArgumentError] if the Gem Specification block is not found in the given gemspec |
| 84 | + # |
| 85 | + # @example |
| 86 | + # code = File.read('project.gemspec') |
| 87 | + # command = Bundler::GemBytes::DeleteDependency.new('my_gem') |
| 88 | + # updated_code = command.call(code) |
| 89 | + # puts updated_code |
| 90 | + # |
| 91 | + def call(code, path: '(string)') |
| 92 | + @found_gemspec_block = false |
| 93 | + rewrite(*parse(code, path)).tap do |_result| |
| 94 | + raise ArgumentError, 'Gem::Specification block not found' unless found_gemspec_block |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + attr_reader :gem_name, :receiver_name, :found_gemspec_block, :found_dependencies |
| 99 | + |
| 100 | + # Handles block nodes within the AST to locate the Gem Specification block |
| 101 | + # |
| 102 | + # @param node [Parser::AST::Node] The block node within the AST |
| 103 | + # @return [void] |
| 104 | + # @api private |
| 105 | + def on_block(node) |
| 106 | + return if receiver_name # already processing the Gem Specification block |
| 107 | + |
| 108 | + @found_gemspec_block = true |
| 109 | + @receiver_name = gem_specification_pattern.match(node) |
| 110 | + |
| 111 | + return unless receiver_name |
| 112 | + |
| 113 | + super # process the children of this node to find the existing dependencies |
| 114 | + |
| 115 | + delete_dependencies |
| 116 | + |
| 117 | + @receiver_name = nil |
| 118 | + end |
| 119 | + |
| 120 | + # Handles `send` nodes within the AST to locate dependency calls |
| 121 | + # |
| 122 | + # If receiver_name is not present then we are not in a Gem Specification block. |
| 123 | + # |
| 124 | + # @param node [Parser::AST::Node] The `send` node to check for dependency patterns |
| 125 | + # @return [void] |
| 126 | + # @api private |
| 127 | + def on_send(node) |
| 128 | + return unless receiver_name.present? |
| 129 | + return unless (match = dependency_pattern.match(node)) |
| 130 | + |
| 131 | + found_dependencies << { node:, match: } |
| 132 | + end |
| 133 | + |
| 134 | + private |
| 135 | + |
| 136 | + # Parses the given code into an AST |
| 137 | + # @param code [String] The code to parse |
| 138 | + # @param path [String] The path to the file being parsed (used for error messages only) |
| 139 | + # @return [Array<Parser::AST::Node, Parser::Source::Buffer>] The AST and buffer |
| 140 | + # @api private |
| 141 | + def parse(code, path) |
| 142 | + buffer = Parser::Source::Buffer.new(path, source: code) |
| 143 | + processed_source = RuboCop::AST::ProcessedSource.new(code, ruby_version, path) |
| 144 | + unless processed_source.valid_syntax? |
| 145 | + raise "Invalid syntax in #{path}\n#{processed_source.diagnostics.map(&:render).join("\n")}" |
| 146 | + end |
| 147 | + |
| 148 | + ast = processed_source.ast |
| 149 | + [buffer, ast] |
| 150 | + end |
| 151 | + |
| 152 | + # Deletes any dependency on the given gem within the Gem::Specification block |
| 153 | + # @param node [Parser::AST::Node] The block node within the AST |
| 154 | + # @return [void] |
| 155 | + # @api private |
| 156 | + def delete_dependencies |
| 157 | + found_dependencies.each do |found_dependency| |
| 158 | + dependency_node = found_dependency[:node] |
| 159 | + remove(range_including_leading_spaces(dependency_node)) |
| 160 | + end |
| 161 | + end |
| 162 | + |
| 163 | + # Returns the range of the dependency node including any leading spaces & newline |
| 164 | + # @param node [Parser::AST::Node] The node |
| 165 | + # @return [Parser::Source::Range] The range of the dependency node |
| 166 | + # @api private |
| 167 | + def range_including_leading_spaces(node) |
| 168 | + leading_spaces = leading_whitespace_count(node) |
| 169 | + range = node.loc.expression |
| 170 | + range.with(begin_pos: range.begin_pos - leading_spaces - 1, end_pos: range.end_pos) |
| 171 | + end |
| 172 | + |
| 173 | + # Returns the # of leading whitespace chars in the source line before the node |
| 174 | + # @param node [Parser::AST::Node] The node |
| 175 | + # @return [Integer] The number of leading whitespace characters |
| 176 | + # @api private |
| 177 | + def leading_whitespace_count(node) |
| 178 | + match_data = node.loc.expression.source_line.match(/^\s*/) |
| 179 | + match_data ? match_data[0].size : 0 |
| 180 | + end |
| 181 | + |
| 182 | + # Returns the Ruby version in use as a float (MAJOR.MINOR only) |
| 183 | + # @return [Float] The Ruby version number, e.g., 3.0 |
| 184 | + # @api private |
| 185 | + def ruby_version = RUBY_VERSION.match(/^(?<version>\d+\.\d+)/)['version'].to_f |
| 186 | + |
| 187 | + # The pattern to match a dependency declaration in the AST |
| 188 | + # @return [RuboCop::AST::NodePattern] The dependency pattern |
| 189 | + # @api private |
| 190 | + def dependency_pattern |
| 191 | + # :nocov: JRuby give false positive for this line being uncovered by tests |
| 192 | + @dependency_pattern ||= |
| 193 | + RuboCop::AST::NodePattern.new(<<~PATTERN) |
| 194 | + (send |
| 195 | + { (send _ :#{receiver_name}) | (lvar :#{receiver_name}) } |
| 196 | + ${ :add_dependency :add_runtime_dependency :add_development_dependency } |
| 197 | + (str #{gem_name ? "$\"#{gem_name}\"" : '$_gem_name'}) |
| 198 | + <(str $_version_constraint) ...> |
| 199 | + ) |
| 200 | + PATTERN |
| 201 | + # :nocov: |
| 202 | + end |
| 203 | + |
| 204 | + # The pattern to match the Gem::Specification block in the AST |
| 205 | + # @return [RuboCop::AST::NodePattern] The Gem::Specification pattern |
| 206 | + # @api private |
| 207 | + def gem_specification_pattern |
| 208 | + @gem_specification_pattern ||= |
| 209 | + RuboCop::AST::NodePattern.new(<<~PATTERN) |
| 210 | + (block (send (const (const nil? :Gem) :Specification) :new)(args (arg $_)) ...) |
| 211 | + PATTERN |
| 212 | + end |
| 213 | + end |
| 214 | + end |
| 215 | + end |
| 216 | +end |
0 commit comments