|
| 1 | +# typed: strong |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "sorbet-runtime" |
| 5 | +require "shellwords" |
| 6 | + |
| 7 | +require "dependabot/gradle/file_parser" |
| 8 | +require "dependabot/gradle/file_updater" |
| 9 | + |
| 10 | +module Dependabot |
| 11 | + module Gradle |
| 12 | + class FileUpdater |
| 13 | + class GradleUpdaterBase |
| 14 | + extend T::Sig |
| 15 | + |
| 16 | + sig { params(dependency_files: T::Array[Dependabot::DependencyFile]).void } |
| 17 | + def initialize(dependency_files:) |
| 18 | + @dependency_files = dependency_files |
| 19 | + end |
| 20 | + |
| 21 | + sig { params(_file: Dependabot::DependencyFile).returns(T::Boolean) } |
| 22 | + def target_file?(_file) |
| 23 | + raise NotImplementedError |
| 24 | + end |
| 25 | + |
| 26 | + sig { returns(T::Array[String]) } |
| 27 | + def command_args |
| 28 | + raise NotImplementedError |
| 29 | + end |
| 30 | + |
| 31 | + sig { params(temp_dir: T.any(Pathname, String), build_file: Dependabot::DependencyFile).returns(String) } |
| 32 | + def working_dir(temp_dir, build_file) |
| 33 | + File.dirname(File.join(temp_dir, build_file.directory, build_file.name)) |
| 34 | + end |
| 35 | + |
| 36 | + sig { params(build_file: Dependabot::DependencyFile).returns(T::Array[Dependabot::DependencyFile]) } |
| 37 | + def update_files(build_file) |
| 38 | + local_files = dependency_files.select do |file| |
| 39 | + file.directory == build_file.directory && target_file?(file) |
| 40 | + end |
| 41 | + |
| 42 | + # If we don't have any files in the build files don't generate one |
| 43 | + return dependency_files unless local_files.any? |
| 44 | + |
| 45 | + updated_files = dependency_files.dup |
| 46 | + SharedHelpers.in_a_temporary_directory do |temp_dir| |
| 47 | + populate_temp_directory(temp_dir, @dependency_files) |
| 48 | + cwd = working_dir(temp_dir, build_file) |
| 49 | + |
| 50 | + # Create gradle.properties file with proxy settings |
| 51 | + # Would prefer to use command line arguments, but they don't work. |
| 52 | + properties_filename = File.join(cwd, "gradle.properties") |
| 53 | + write_properties_file(properties_filename) |
| 54 | + |
| 55 | + command = Shellwords.join(%w(gradle --no-daemon) + command_args) |
| 56 | + |
| 57 | + Dir.chdir(cwd) do |
| 58 | + SharedHelpers.run_shell_command(command, cwd: cwd) |
| 59 | + update_files_content(temp_dir, local_files, updated_files) |
| 60 | + rescue SharedHelpers::HelperSubprocessFailed => e |
| 61 | + puts "Failed to update files: #{e.message}" |
| 62 | + return updated_files |
| 63 | + end |
| 64 | + end |
| 65 | + updated_files |
| 66 | + end |
| 67 | + |
| 68 | + private |
| 69 | + |
| 70 | + sig do |
| 71 | + params( |
| 72 | + temp_dir: T.any(Pathname, String), |
| 73 | + local_files: T::Array[Dependabot::DependencyFile], |
| 74 | + updated_files: T::Array[Dependabot::DependencyFile] |
| 75 | + ).void |
| 76 | + end |
| 77 | + def update_files_content(temp_dir, local_files, updated_files) |
| 78 | + local_files.each do |file| |
| 79 | + f_content = File.read(File.join(temp_dir, file.directory, file.name)) |
| 80 | + tmp_file = file.dup |
| 81 | + tmp_file.content = f_content |
| 82 | + updated_files[T.must(updated_files.index(file))] = tmp_file |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + sig { params(temp_dir: T.any(Pathname, String), files: T::Array[Dependabot::DependencyFile]).void } |
| 87 | + def populate_temp_directory(temp_dir, files) |
| 88 | + files.each do |file| |
| 89 | + in_path_name = File.join(temp_dir, file.directory, file.name) |
| 90 | + FileUtils.mkdir_p(File.dirname(in_path_name)) |
| 91 | + File.write(in_path_name, file.content) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + sig { params(file_name: String).void } |
| 96 | + def write_properties_file(file_name) # rubocop:disable Metrics/PerceivedComplexity |
| 97 | + http_proxy = ENV.fetch("HTTP_PROXY", nil) |
| 98 | + https_proxy = ENV.fetch("HTTPS_PROXY", nil) |
| 99 | + http_split = http_proxy&.split(":") |
| 100 | + https_split = https_proxy&.split(":") |
| 101 | + http_proxy_host = http_split&.fetch(1, nil)&.gsub("//", "") || "host.docker.internal" |
| 102 | + https_proxy_host = https_split&.fetch(1, nil)&.gsub("//", "") || "host.docker.internal" |
| 103 | + http_proxy_port = http_split&.fetch(2) || "1080" |
| 104 | + https_proxy_port = https_split&.fetch(2) || "1080" |
| 105 | + properties_content = " |
| 106 | +systemProp.http.proxyHost=#{http_proxy_host} |
| 107 | +systemProp.http.proxyPort=#{http_proxy_port} |
| 108 | +systemProp.https.proxyHost=#{https_proxy_host} |
| 109 | +systemProp.https.proxyPort=#{https_proxy_port}" |
| 110 | + File.write(file_name, properties_content) |
| 111 | + end |
| 112 | + |
| 113 | + sig { returns(T::Array[Dependabot::DependencyFile]) } |
| 114 | + attr_reader :dependency_files |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments