Class: Dependabot::Gradle::FileUpdater::LockfileUpdater

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

Instance Method Summary collapse

Constructor Details

#initialize(dependency_files:) ⇒ LockfileUpdater

Returns a new instance of LockfileUpdater.



17
18
19
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 17

def initialize(dependency_files:)
  @dependency_files = dependency_files
end

Instance Method Details

#populate_temp_directory(temp_dir) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 77

def populate_temp_directory(temp_dir)
  @dependency_files.each do |file|
    in_path_name = File.join(temp_dir, file.directory, file.name)
    FileUtils.mkdir_p(File.dirname(in_path_name))
    File.write(in_path_name, file.content)
  end
end

#update_lockfiles(build_file) ⇒ Object



22
23
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
50
51
52
53
54
55
56
57
58
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 22

def update_lockfiles(build_file)
  local_lockfiles = dependency_files.select do |file|
    file.directory == build_file.directory && file.name.end_with?(".lockfile")
  end

  # If we don't have any lockfiles in the build files don't generate one
  return dependency_files unless local_lockfiles.any?

  updated_files = dependency_files.dup
  SharedHelpers.in_a_temporary_directory do |temp_dir|
    populate_temp_directory(temp_dir)
    cwd = File.join(temp_dir, build_file.directory, build_file.name)
    cwd = File.dirname(cwd)

    # Create gradle.properties file with proxy settings
    # Would prefer to use command line arguments, but they don't work.
    properties_filename = File.join(temp_dir, build_file.directory, "gradle.properties")
    write_properties_file(properties_filename)

    command_parts = [
      "gradle",
      "dependencies",
      "--no-daemon",
      "--write-locks"
    ]
    command = Shellwords.join(command_parts)

    Dir.chdir(cwd) do
      SharedHelpers.run_shell_command(command, cwd: cwd)
      update_lockfiles_content(temp_dir, local_lockfiles, updated_files)
    rescue SharedHelpers::HelperSubprocessFailed => e
      puts "Failed to update lockfiles: #{e.message}"
      return updated_files
    end
  end
  updated_files
end

#update_lockfiles_content(temp_dir, local_lockfiles, updated_lockfiles) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 67

def update_lockfiles_content(temp_dir, local_lockfiles, updated_lockfiles)
  local_lockfiles.each do |file|
    f_content = File.read(File.join(temp_dir, file.directory, file.name))
    tmp_file = file.dup
    tmp_file.content = f_content
    updated_lockfiles[T.must(updated_lockfiles.index(file))] = tmp_file
  end
end

#write_properties_file(file_name) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 86

def write_properties_file(file_name) # rubocop:disable Metrics/PerceivedComplexity
  http_proxy = ENV.fetch("HTTP_PROXY", nil)
  https_proxy = ENV.fetch("HTTPS_PROXY", nil)
  http_split = http_proxy&.split(":")
  https_split = https_proxy&.split(":")
  http_proxy_host = http_split&.fetch(1, nil)&.gsub("//", "") || "host.docker.internal"
  https_proxy_host = https_split&.fetch(1, nil)&.gsub("//", "") || "host.docker.internal"
  http_proxy_port = http_split&.fetch(2) || "1080"
  https_proxy_port = https_split&.fetch(2) || "1080"
  properties_content = "
systemProp.http.proxyHost=#{http_proxy_host}
systemProp.http.proxyPort=#{http_proxy_port}
systemProp.https.proxyHost=#{https_proxy_host}
systemProp.https.proxyPort=#{https_proxy_port}"
  File.write(file_name, properties_content)
end