Class: Dependabot::Gradle::FileUpdater::Wrapper::PropertiesDocument

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/gradle/file_updater/wrapper/properties_document.rb

Overview

An order-preserving model of a ‘gradle-wrapper.properties` file.

Java’s ‘Properties.store` (used by Gradle’s wrapper task) discards comments, blank lines, custom keys and the original key ordering. To preserve everything the user configured, we parse the original file into this document, mutate only the keys we intend to change, and render it back verbatim.

Defined Under Namespace

Classes: Line

Constant Summary collapse

KEY_VALUE_REGEX =

Properties files use ‘=`, `:` or whitespace as the key/value separator. Gradle always writes `=`, but we parse all three so user-authored files are handled faithfully.

T.let(/\A(\s*)([^\s:=]+)(\s*[:=]\s*|\s+)(.*)\z/, Regexp)
COMMENT_REGEX =
T.let(/\A\s*[#!]/, Regexp)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ PropertiesDocument

Returns a new instance of PropertiesDocument.



58
59
60
# File 'lib/dependabot/gradle/file_updater/wrapper/properties_document.rb', line 58

def initialize(lines)
  @lines = lines
end

Class Method Details

.parse(content) ⇒ Object



36
37
38
39
# File 'lib/dependabot/gradle/file_updater/wrapper/properties_document.rb', line 36

def self.parse(content)
  lines = content.split("\n", -1).map { |line| parse_line(line) }
  new(lines)
end

.parse_line(line) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dependabot/gradle/file_updater/wrapper/properties_document.rb', line 42

def self.parse_line(line)
  return Line.new(raw: line) if line.strip.empty? || line.match?(COMMENT_REGEX)

  match = line.match(KEY_VALUE_REGEX)
  return Line.new(raw: line) unless match

  Line.new(
    raw: line,
    indent: match[1] || "",
    key: match[2],
    separator: match[3],
    value: match[4]
  )
end

Instance Method Details

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/dependabot/gradle/file_updater/wrapper/properties_document.rb', line 63

def key?(key)
  @lines.any? { |line| line.key == key }
end

#to_sObject



89
90
91
# File 'lib/dependabot/gradle/file_updater/wrapper/properties_document.rb', line 89

def to_s
  @lines.map(&:raw).join("\n")
end

#upsert(key, value) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dependabot/gradle/file_updater/wrapper/properties_document.rb', line 75

def upsert(key, value)
  existing = @lines.find { |line| line.key == key }
  if existing
    separator = existing.separator || "="
    existing.value = value
    existing.separator = separator
    existing.raw = "#{existing.indent}#{key}#{separator}#{value}"
    return
  end

  @lines << Line.new(raw: "#{key}=#{value}", key: key, separator: "=", value: value)
end

#value_for(key) ⇒ Object



68
69
70
# File 'lib/dependabot/gradle/file_updater/wrapper/properties_document.rb', line 68

def value_for(key)
  @lines.find { |line| line.key == key }&.value
end