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.
6 7 8 9 10 11 12 13 |
# File 'lib/pdk/module/update_manager.rb', line 6 def initialize require 'set' @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.
27 28 29 |
# File 'lib/pdk/module/update_manager.rb', line 27 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.
70 71 72 73 74 |
# File 'lib/pdk/module/update_manager.rb', line 70 def changed?(path) changes[:added].any? { |add| add[:path] == path } || changes[:removed].include?(path) || changes[:modified].key?(path) end |
#changes ⇒ Hash{Symbol => Set,Hash}
Generate a summary of the changes that will be applied to the module.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/pdk/module/update_manager.rb', line 42 def changes require 'pdk/util/filesystem' calculate_diffs { added: @added_files, removed: @removed_files.select { |f| PDK::Util::Filesystem.exist?(f) }, modified: @diff_cache.reject { |_, value| value.nil? }, } end |
#changes? ⇒ Boolean
Check if there are any pending changes to apply to the module.
58 59 60 61 62 |
# File 'lib/pdk/module/update_manager.rb', line 58 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.
19 20 21 |
# File 'lib/pdk/module/update_manager.rb', line 19 def modify_file(path, content) @modified_files << { path: path, content: content } end |
#remove_file(path) ⇒ Object
Store a pending file removal.
34 35 36 |
# File 'lib/pdk/module/update_manager.rb', line 34 def remove_file(path) @removed_files << path end |
#sync_changes! ⇒ Object
Apply any pending changes stored in the UpdateManager to the module.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/pdk/module/update_manager.rb', line 81 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.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/pdk/module/update_manager.rb', line 105 def unlink_file(path) require 'pdk/util/filesystem' if PDK::Util::Filesystem.file?(path) PDK.logger.debug(_("unlinking '%{path}'") % { path: path }) PDK::Util::Filesystem.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 |