Class: PDK::Module::UpdateManager
- Inherits:
-
Object
- Object
- PDK::Module::UpdateManager
- Defined in:
- lib/pdk/module/update_manager.rb
Instance Method Summary collapse
-
#add_file(path, content) ⇒ Object
Store a pending file addition.
-
#changed?(path) ⇒ Boolean
Check if the update manager will change the specified file upon sync.
-
#changes ⇒ Hash{Symbol => Set,Hash}
Generate a summary of the changes that will be applied to the module.
-
#changes? ⇒ Boolean
Check if there are any pending changes to apply to the module.
- #clear! ⇒ Object
-
#initialize ⇒ UpdateManager
constructor
Initialises a blank UpdateManager object, which is used to store and process file additions/removals/modifications.
-
#make_file_executable(path) ⇒ Object
Store a pending file execute mode change.
-
#modify_file(path, content) ⇒ Object
Store a pending modification to an existing file.
-
#remove_file(path) ⇒ Object
Store a pending file removal.
-
#sync_changes! ⇒ Object
Apply any pending changes stored in the UpdateManager to the module.
-
#unlink_file(path) ⇒ Object
Remove a file from disk.
Constructor Details
#initialize ⇒ UpdateManager
Initialises a blank UpdateManager object, which is used to store and process file additions/removals/modifications.
8 9 10 11 12 13 14 15 16 |
# File 'lib/pdk/module/update_manager.rb', line 8 def initialize require 'set' @modified_files = Set.new @added_files = Set.new @removed_files = Set.new @executable_files = Set.new @diff_cache = {} end |
Instance Method Details
#add_file(path, content) ⇒ Object
Store a pending file addition.
30 31 32 |
# File 'lib/pdk/module/update_manager.rb', line 30 def add_file(path, content) @added_files << { path: path, content: content } end |
#changed?(path) ⇒ Boolean
Check if the update manager will change the specified file upon sync.
82 83 84 85 86 87 |
# File 'lib/pdk/module/update_manager.rb', line 82 def changed?(path) changes[:added].any? { |add| add[:path] == path } || changes[:removed].include?(path) || changes[:modified].key?(path) || changes[:'made executable'].include?(path) end |
#changes ⇒ Hash{Symbol => Set,Hash}
Generate a summary of the changes that will be applied to the module.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/pdk/module/update_manager.rb', line 52 def changes require 'pdk/util/filesystem' calculate_diffs { added: @added_files, removed: @removed_files.select { |f| PDK::Util::Filesystem.exist?(f) }, modified: @diff_cache.compact, 'made executable': @executable_files } end |
#changes? ⇒ Boolean
Check if there are any pending changes to apply to the module.
69 70 71 72 73 74 |
# File 'lib/pdk/module/update_manager.rb', line 69 def changes? !changes[:added].empty? || !changes[:removed].empty? || changes[:modified].any? { |_, value| !value.nil? } || !changes[:'made executable'].empty? end |
#clear! ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/pdk/module/update_manager.rb', line 89 def clear! @modified_files.clear @added_files.clear @removed_files.clear @executable_files.clear nil end |
#make_file_executable(path) ⇒ Object
Store a pending file execute mode change.
44 45 46 |
# File 'lib/pdk/module/update_manager.rb', line 44 def make_file_executable(path) @executable_files << path end |
#modify_file(path, content) ⇒ Object
Store a pending modification to an existing file.
22 23 24 |
# File 'lib/pdk/module/update_manager.rb', line 22 def modify_file(path, content) @modified_files << { path: path, content: content } end |
#remove_file(path) ⇒ Object
Store a pending file removal.
37 38 39 |
# File 'lib/pdk/module/update_manager.rb', line 37 def remove_file(path) @removed_files << path end |
#sync_changes! ⇒ Object
Apply any pending changes stored in the UpdateManager to the module.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/pdk/module/update_manager.rb', line 102 def sync_changes! calculate_diffs files_to_write = @added_files files_to_write += @modified_files.reject { |file| @diff_cache[file[:path]].nil? } @removed_files.each do |file| unlink_file(file) end files_to_write.each do |file| write_file(file[:path], file[:content]) end @executable_files.each do |file| update_execute_bits(file) end end |
#unlink_file(path) ⇒ Object
Remove a file from disk.
Like FileUtils.rm_f, this method will not fail if the file does not exist. Unlike FileUtils.rm_f, this method will not blindly swallow all exceptions.
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/pdk/module/update_manager.rb', line 130 def unlink_file(path) require 'pdk/util/filesystem' if PDK::Util::Filesystem.file?(path) PDK.logger.debug(format("unlinking '%{path}'", path: path)) PDK::Util::Filesystem.rm(path) else PDK.logger.debug(format("'%{path}': already gone", path: path)) end rescue StandardError => e raise PDK::CLI::ExitWithError, format("Unable to remove '%{path}': %{message}", path: path, message: e.) end |