Class: Dependabot::Gradle::FileParser::PropertyValueFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/gradle/file_parser/property_value_finder.rb

Constant Summary collapse

SINGLE_PROPERTY_DECLARATION_REGEX =
/(?:^|\s+|ext.)(?<name>[^\s=]+)\s*=\s*['"](?<value>[^\s]+)['"]/.
freeze
MULTI_PROPERTY_DECLARATION_REGEX =
/(?:^|\s+|ext.)(?<namespace>[^\s=]+)\s*=\s*\[(?<values>[^\]]+)\]/m.
freeze
NAMESPACED_DECLARATION_REGEX =
/(?:^|\s+)(?<name>[^\s:]+)\s*:\s*['"](?<value>[^\s]+)['"]\s*/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(dependency_files:) ⇒ PropertyValueFinder

Returns a new instance of PropertyValueFinder.



20
21
22
# File 'lib/dependabot/gradle/file_parser/property_value_finder.rb', line 20

def initialize(dependency_files:)
  @dependency_files = dependency_files
end

Instance Method Details

#property_details(property_name:, callsite_buildfile:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dependabot/gradle/file_parser/property_value_finder.rb', line 24

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
  if property_name.start_with?("project.")
    property_name = property_name.sub("project.", "")
  end

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

  # 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:) ⇒ Object



51
52
53
54
55
56
# File 'lib/dependabot/gradle/file_parser/property_value_finder.rb', line 51

def property_value(property_name:, callsite_buildfile:)
  property_details(
    property_name: property_name,
    callsite_buildfile: callsite_buildfile
  )&.fetch(:value)
end