Class: Dependabot::Composer::FileUpdater::ManifestUpdater

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/composer/file_updater/manifest_updater.rb

Instance Method Summary collapse

Constructor Details

#initialize(dependencies:, manifest:) ⇒ ManifestUpdater

Returns a new instance of ManifestUpdater.



15
16
17
18
# File 'lib/dependabot/composer/file_updater/manifest_updater.rb', line 15

def initialize(dependencies:, manifest:)
  @dependencies = dependencies
  @manifest = manifest
end

Instance Method Details

#updated_manifest_contentObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dependabot/composer/file_updater/manifest_updater.rb', line 21

def updated_manifest_content
  T.must(
    dependencies.reduce(manifest.content.dup) do |content, dep|
      updated_content = content
      updated_requirements(dep).each do |new_req|
        old_req = old_requirement(dep, new_req)&.fetch(:requirement)
        updated_req = new_req.fetch(:requirement)

        regex =
          /
            "#{Regexp.escape(dep.name)}"\s*:\s*
            "#{Regexp.escape(old_req)}"
          /x

        updated_content = content&.gsub(regex) do |declaration|
          declaration.gsub(%("#{old_req}"), %("#{updated_req}"))
        end

        raise "Expected content to change!" if content == updated_content
      end

      updated_content
    end
  )
end