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

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

Constant Summary collapse

SUPPORTED_BUILD_FILE_NAMES =

rubocop:disable Layout/LineLength

%w(build.gradle build.gradle.kts).freeze
QUOTED_VALUE_REGEX =
/\s*['"][^\s]+['"]\s*/.freeze
FIND_PROPERTY_REGEX =

project.findProperty(‘property’) ?:

/\s*project\.findProperty\(#{QUOTED_VALUE_REGEX}\)\s*\?:/.freeze
GROOVY_HAS_PROPERTY_REGEX =

project.hasProperty(‘property’) ? project.getProperty(‘property’) :

/\s*project\.hasProperty\(#{QUOTED_VALUE_REGEX}\)\s*\?\s*project\.getProperty\(#{QUOTED_VALUE_REGEX}\)\s*:/.freeze
KOTLIN_HAS_PROPERTY_REGEX =

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

/\s*if\s*\(project\.hasProperty\(#{QUOTED_VALUE_REGEX}\)\)\s+project\.getProperty\(#{QUOTED_VALUE_REGEX}\)\s+else\s+/.freeze
GROOVY_PROPERTY_DECLARATION_AS_DEFAULTS_REGEX =
/(?:#{FIND_PROPERTY_REGEX}|#{GROOVY_HAS_PROPERTY_REGEX})?/.freeze
KOTLIN_PROPERTY_DECLARATION_AS_DEFAULTS_REGEX =
/(?:#{FIND_PROPERTY_REGEX}|#{KOTLIN_HAS_PROPERTY_REGEX})?/.freeze
PROPERTY_DECLARATION_AS_DEFAULTS_REGEX =
/(#{GROOVY_PROPERTY_DECLARATION_AS_DEFAULTS_REGEX}|#{KOTLIN_PROPERTY_DECLARATION_AS_DEFAULTS_REGEX})?/.freeze
VALUE_REGEX =
/#{PROPERTY_DECLARATION_AS_DEFAULTS_REGEX}\s*['"](?<value>[^\s]+)['"]/.freeze
GROOVY_SINGLE_PROPERTY_DECLARATION_REGEX =
/(?:^|\s+|ext.)(?<name>[^\s=]+)\s*=#{VALUE_REGEX}/.freeze
KOTLIN_SINGLE_PROPERTY_INDEX_DECLARATION_REGEX =
/\s*extra\[['"](?<name>[^\s=]+)['"]\]\s*=#{VALUE_REGEX}/.freeze
KOTLIN_SINGLE_PROPERTY_SET_REGEX =
/\s*set\(['"](?<name>[^\s=]+)['"]\s*,#{VALUE_REGEX}\)/.freeze
KOTLIN_SINGLE_PROPERTY_SET_DECLARATION_REGEX =
/\s*extra\.#{KOTLIN_SINGLE_PROPERTY_SET_REGEX}/.freeze
KOTLIN_SINGLE_PROPERTY_DECLARATION_REGEX =
/(#{KOTLIN_SINGLE_PROPERTY_INDEX_DECLARATION_REGEX}|#{KOTLIN_SINGLE_PROPERTY_SET_DECLARATION_REGEX})/.freeze
SINGLE_PROPERTY_DECLARATION_REGEX =
/(#{KOTLIN_SINGLE_PROPERTY_DECLARATION_REGEX}|#{GROOVY_SINGLE_PROPERTY_DECLARATION_REGEX})/.freeze
GROOVY_MULTI_PROPERTY_DECLARATION_REGEX =
/(?:^|\s+|ext.)(?<namespace>[^\s=]+)\s*=\s*\[(?<values>[^\]]+)\]/m.freeze
KOTLIN_BLOCK_PROPERTY_DECLARATION_REGEX =
/\s*(?<namespace>[^\s=]+)\.apply\s*{(?<values>[^\]]+)}/m.freeze
KOTLIN_MULTI_PROPERTY_DECLARATION_REGEX =
/\s*extra\[['"](?<namespace>[^\s=]+)['"]\]\s*=\s*mapOf\((?<values>[^\]]+)\)/m.freeze
MULTI_PROPERTY_DECLARATION_REGEX =
/(#{KOTLIN_MULTI_PROPERTY_DECLARATION_REGEX}|#{GROOVY_MULTI_PROPERTY_DECLARATION_REGEX})/.freeze
KOTLIN_MAP_NAMESPACED_DECLARATION_REGEX =
/(?:^|\s+)['"](?<name>[^\s:]+)['"]\s*to#{VALUE_REGEX}\s*/.freeze
REGULAR_NAMESPACED_DECLARATION_REGEX =
/(?:^|\s+)(?<name>[^\s:]+)\s*[:=]#{VALUE_REGEX}\s*/.freeze
NAMESPACED_DECLARATION_REGEX =
/(#{REGULAR_NAMESPACED_DECLARATION_REGEX}|#{KOTLIN_MAP_NAMESPACED_DECLARATION_REGEX})/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(dependency_files:) ⇒ PropertyValueFinder

rubocop:enable Layout/LineLength



79
80
81
# File 'lib/dependabot/gradle/file_parser/property_value_finder.rb', line 79

def initialize(dependency_files:)
  @dependency_files = dependency_files
end

Instance Method Details

#property_details(property_name:, callsite_buildfile:) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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:) ⇒ Object



106
107
108
109
110
111
# 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