class Dependabot::Gradle::FileParser::PropertyValueFinder

Constants

FIND_PROPERTY_REGEX

project.findProperty('property') ?:

GROOVY_HAS_PROPERTY_REGEX

project.hasProperty('property') ? project.getProperty('property') :

GROOVY_MULTI_PROPERTY_DECLARATION_REGEX
GROOVY_PROPERTY_DECLARATION_AS_DEFAULTS_REGEX
GROOVY_SINGLE_PROPERTY_DECLARATION_REGEX
KOTLIN_BLOCK_PROPERTY_DECLARATION_REGEX
KOTLIN_HAS_PROPERTY_REGEX

if(project.hasProperty(“property”)) project.getProperty(“property”) else

KOTLIN_MAP_NAMESPACED_DECLARATION_REGEX
KOTLIN_MULTI_PROPERTY_DECLARATION_REGEX
KOTLIN_PROPERTY_DECLARATION_AS_DEFAULTS_REGEX
KOTLIN_SINGLE_PROPERTY_DECLARATION_REGEX
KOTLIN_SINGLE_PROPERTY_INDEX_DECLARATION_REGEX
KOTLIN_SINGLE_PROPERTY_SET_DECLARATION_REGEX
KOTLIN_SINGLE_PROPERTY_SET_REGEX
MULTI_PROPERTY_DECLARATION_REGEX
NAMESPACED_DECLARATION_REGEX
PROPERTY_DECLARATION_AS_DEFAULTS_REGEX
QUOTED_VALUE_REGEX
REGULAR_NAMESPACED_DECLARATION_REGEX
SINGLE_PROPERTY_DECLARATION_REGEX
SUPPORTED_BUILD_FILE_NAMES

rubocop:disable Layout/LineLength

VALUE_REGEX

Attributes

dependency_files[R]

Public Class Methods

new(dependency_files:) click to toggle source

rubocop:enable Layout/LineLength

# File lib/dependabot/gradle/file_parser/property_value_finder.rb, line 79
def initialize(dependency_files:)
  @dependency_files = dependency_files
end

Public Instance Methods

property_details(property_name:, callsite_buildfile:) click to toggle source
# File lib/dependabot/gradle/file_parser/property_value_finder.rb, line 83
def property_details(property_name:, callsite_buildfile:)
  # If the root project was specified, just look in the top-level
  # buildfile
  if property_name.start_with?("rootProject.")
    property_name = property_name.sub("rootProject.", "")
    return properties(top_level_buildfile).fetch(property_name, nil)
  end

  # If this project was specified strip the specifier
  property_name = property_name.sub("project.", "") if property_name.start_with?("project.")

  # If a `properties` prefix was specified strip that out, too
  property_name = property_name.sub("properties.", "") if property_name.start_with?("properties.")

  # Look for a property in the callsite buildfile. If that fails, look
  # for the property in the top-level buildfile
  if properties(callsite_buildfile).fetch(property_name, nil)
    return properties(callsite_buildfile).fetch(property_name)
  end

  properties(top_level_buildfile).fetch(property_name, nil)
end
property_value(property_name:, callsite_buildfile:) click to toggle source
# File lib/dependabot/gradle/file_parser/property_value_finder.rb, line 106
def property_value(property_name:, callsite_buildfile:)
  property_details(
    property_name: property_name,
    callsite_buildfile: callsite_buildfile
  )&.fetch(:value)
end

Private Instance Methods

fetch_kotlin_block_property_declarations(buildfile) click to toggle source
# File lib/dependabot/gradle/file_parser/property_value_finder.rb, line 155
def fetch_kotlin_block_property_declarations(buildfile)
  properties = {}

  prepared_content(buildfile).
    scan(KOTLIN_BLOCK_PROPERTY_DECLARATION_REGEX) do
      captures = Regexp.last_match.named_captures
      namespace = captures.fetch("namespace")

      captures.fetch("values").
        scan(KOTLIN_SINGLE_PROPERTY_SET_REGEX) do
          declaration_string = Regexp.last_match.to_s.strip
          sub_captures = Regexp.last_match.named_captures
          name = sub_captures.fetch("name")
          full_name = if namespace == "extra"
                        name
                      else
                        [namespace, name].join(".")
                      end

          properties[full_name] = {
            value: sub_captures.fetch("value"),
            declaration_string: declaration_string,
            file: buildfile.name
          }
        end
    end

  properties
end
fetch_multi_property_declarations(buildfile) click to toggle source
# File lib/dependabot/gradle/file_parser/property_value_finder.rb, line 185
def fetch_multi_property_declarations(buildfile)
  properties = {}

  prepared_content(buildfile).scan(MULTI_PROPERTY_DECLARATION_REGEX) do
    captures = Regexp.last_match.named_captures
    namespace = captures.fetch("namespace").sub(/^ext\./, "")

    captures.fetch("values").scan(NAMESPACED_DECLARATION_REGEX) do
      declaration_string = Regexp.last_match.to_s.strip
      sub_captures = Regexp.last_match.named_captures
      name = sub_captures.fetch("name")
      full_name = [namespace, name].join(".")

      properties[full_name] = {
        value: sub_captures.fetch("value"),
        declaration_string: declaration_string,
        file: buildfile.name
      }
    end
  end

  properties
end
fetch_single_property_declarations(buildfile) click to toggle source
# File lib/dependabot/gradle/file_parser/property_value_finder.rb, line 135
def fetch_single_property_declarations(buildfile)
  properties = {}

  prepared_content(buildfile).scan(SINGLE_PROPERTY_DECLARATION_REGEX) do
    declaration_string = Regexp.last_match.to_s.strip
    captures = Regexp.last_match.named_captures
    name = captures.fetch("name").sub(/^ext\./, "")

    unless properties.key?(name)
      properties[name] = {
        value: captures.fetch("value"),
        declaration_string: declaration_string,
        file: buildfile.name
      }
    end
  end

  properties
end
prepared_content(buildfile) click to toggle source
# File lib/dependabot/gradle/file_parser/property_value_finder.rb, line 209
def prepared_content(buildfile)
  # Remove any comments
  buildfile.content.
    gsub(%r{(?<=^|\s)//.*$}, "\n").
    gsub(%r{(?<=^|\s)/\*.*?\*/}m, "")
end
properties(buildfile) click to toggle source
# File lib/dependabot/gradle/file_parser/property_value_finder.rb, line 117
def properties(buildfile)
  @properties ||= {}
  return @properties[buildfile.name] if @properties[buildfile.name]

  @properties[buildfile.name] = {}

  @properties[buildfile.name].
    merge!(fetch_single_property_declarations(buildfile))

  @properties[buildfile.name].
    merge!(fetch_kotlin_block_property_declarations(buildfile))

  @properties[buildfile.name].
    merge!(fetch_multi_property_declarations(buildfile))

  @properties[buildfile.name]
end
top_level_buildfile() click to toggle source
# File lib/dependabot/gradle/file_parser/property_value_finder.rb, line 216
def top_level_buildfile
  @top_level_buildfile ||= dependency_files.find do |f|
    SUPPORTED_BUILD_FILE_NAMES.include?(f.name)
  end
end