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.
-
#initialize ⇒ UpdateManager
constructor
Initialises a blank UpdateManager object, which is used to store and process file additions/removals/modifications.
-
#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.
13 14 15 16 17 18 |
# File 'lib/pdk/module/update_manager.rb', line 13 def initialize @modified_files = Set.new @added_files = Set.new @removed_files = Set.new @diff_cache = {} end |
Instance Method Details
#add_file(path, content) ⇒ Object
Store a pending file addition.
32 33 34 |
# File 'lib/pdk/module/update_manager.rb', line 32 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.
73 74 75 76 77 |
# File 'lib/pdk/module/update_manager.rb', line 73 def changed?(path) changes[:added].any? { |add| add[:path] == path } || changes[:removed].include?(path) || changes[:modified].keys.include?(path) end |
#changes ⇒ Hash{Symbol => Set,Hash}
Generate a summary of the changes that will be applied to the module.
47 48 49 50 51 52 53 54 55 |
# File 'lib/pdk/module/update_manager.rb', line 47 def changes calculate_diffs { added: @added_files, removed: @removed_files.select { |f| File.exist?(f) }, modified: @diff_cache.reject { |_, value| value.nil? }, } end |
#changes? ⇒ Boolean
Check if there are any pending changes to apply to the module.
61 62 63 64 65 |
# File 'lib/pdk/module/update_manager.rb', line 61 def changes? !changes[:added].empty? || !changes[:removed].empty? || changes[:modified].any? { |_, value| !value.nil? } end |
#modify_file(path, content) ⇒ Object
Store a pending modification to an existing file.
24 25 26 |
# File 'lib/pdk/module/update_manager.rb', line 24 def modify_file(path, content) @modified_files << { path: path, content: content } end |
#remove_file(path) ⇒ Object
Store a pending file removal.
39 40 41 |
# File 'lib/pdk/module/update_manager.rb', line 39 def remove_file(path) @removed_files << path end |
#sync_changes! ⇒ Object
Apply any pending changes stored in the UpdateManager to the module.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/pdk/module/update_manager.rb', line 84 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 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.
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/pdk/module/update_manager.rb', line 108 def unlink_file(path) if File.file?(path) PDK.logger.debug(_("unlinking '%{path}'") % { path: path }) FileUtils.rm(path) else PDK.logger.debug(_("'%{path}': already gone") % { path: path }) end rescue => e raise PDK::CLI::ExitWithError, _("Unable to remove '%{path}': %{message}") % { path: path, message: e., } end |