Class: Dependabot::Gradle::FileUpdater::LockfileUpdater
- Inherits:
-
Object
- Object
- Dependabot::Gradle::FileUpdater::LockfileUpdater
- Extended by:
- T::Sig
- Defined in:
- lib/dependabot/gradle/file_updater/lockfile_updater.rb
Constant Summary collapse
- INIT_SCRIPT_TASK_NAME =
T.let("dependabotResolveAll", String)
Instance Method Summary collapse
- #determine_root_dir(build_file:) ⇒ Object
-
#initialize(dependency_files:) ⇒ LockfileUpdater
constructor
A new instance of LockfileUpdater.
- #lockfiles_for_root(root_dir) ⇒ Object
- #normalized_directory_path(file) ⇒ Object
- #update_lockfiles(build_file) ⇒ Object
- #update_lockfiles_content(temp_dir, lockfiles, updated_lockfiles) ⇒ Object
Constructor Details
#initialize(dependency_files:) ⇒ LockfileUpdater
Returns a new instance of LockfileUpdater.
19 20 21 |
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 19 def initialize(dependency_files:) @dependency_files = dependency_files end |
Instance Method Details
#determine_root_dir(build_file:) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 64 def determine_root_dir(build_file:) settings_file = find_settings_file(build_file) return normalized_directory_path(settings_file) if settings_file file_path = normalized_file_path(build_file) return normalize_path(File.dirname(file_path, 2)) if file_path.end_with?("/gradle/libs.versions.toml") normalized_directory_path(build_file) end |
#lockfiles_for_root(root_dir) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 82 def lockfiles_for_root(root_dir) sub_build_roots = sub_build_roots_for(root_dir) dependency_files.select do |file| next false unless file.name.end_with?(".lockfile") file_path = normalized_file_path(file) next false unless path_under_root?(file_path, root_dir) sub_build_roots.none? { |sub_root| file_path.start_with?("#{sub_root}/") || file_path == sub_root } end end |
#normalized_directory_path(file) ⇒ Object
75 76 77 78 79 |
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 75 def normalized_directory_path(file) file_path = normalized_file_path(file) dir = File.dirname(file_path) dir == "/" ? "/" : normalize_path(dir) end |
#update_lockfiles(build_file) ⇒ Object
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 59 60 61 |
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 24 def update_lockfiles(build_file) root_dir = determine_root_dir(build_file: build_file) lockfiles = lockfiles_for_root(root_dir) return dependency_files unless 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, root_dir == "/" ? "" : root_dir.delete_prefix("/")) FileUtils.mkdir_p(cwd) write_properties_file(File.join(cwd, "gradle.properties")) init_script_path = File.join(cwd, "dependabot-locking.init.gradle") write_init_script(init_script_path) command_parts = [ "gradle", "--init-script", init_script_path, INIT_SCRIPT_TASK_NAME, "--write-locks", "--no-daemon" ] command = Shellwords.join(command_parts) SharedHelpers.run_shell_command(command, cwd: cwd) update_lockfiles_content(temp_dir, lockfiles, updated_files) rescue SharedHelpers::HelperSubprocessFailed => e Dependabot.logger.error("Failed to update lockfiles: #{e.}") return updated_files end updated_files end |
#update_lockfiles_content(temp_dir, lockfiles, updated_lockfiles) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/dependabot/gradle/file_updater/lockfile_updater.rb', line 102 def update_lockfiles_content(temp_dir, lockfiles, updated_lockfiles) lockfiles.each do |file| # Handle "/" directory as root - File.join treats "/" as absolute path and ignores prior components relative_dir = file.directory == "/" ? "" : file.directory lockfile_path = File.join(temp_dir, relative_dir, file.name) unless File.exist?(lockfile_path) Dependabot.logger.warn( "Lockfile #{file.name} was not regenerated by Gradle after a successful lockfile update run. " \ "Preserving existing lockfile." ) next end content = File.read(lockfile_path) next if content == file.content tmp_file = file.dup tmp_file.content = content index = updated_lockfiles.find_index { |f| f.name == file.name } if index updated_lockfiles[index] = tmp_file else updated_lockfiles << tmp_file end end end |