Class: PDK::Module::UpdateManager

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/module/update_manager.rb

Instance Method Summary collapse

Constructor Details

#initializeUpdateManager

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.

Parameters:

  • path (String)

    The path where the file will be created.

  • content (String)

    The content of the new file.



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.

Parameters:

  • path (String)

    The path to the file.

Returns:

  • (Boolean)

    true if the file will be changed.

Raises:



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

#changesHash{Symbol => Set,Hash}

Generate a summary of the changes that will be applied to the module.

Returns:

  • (Hash{Symbol => Set,Hash})

    the summary of the pending changes.

Raises:



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.

Returns:

  • (Boolean)

    true if there are changes to apply to the module.

Raises:



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.

Parameters:

  • path (String)

    The path to the file to be modified.

  • content (String)

    The new content of the 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.

Parameters:

  • path (String)

    The path to the file to be removed.



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.

Raises:



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

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.

Parameters:

  • path (String)

    The path to the file to be removed.

Raises:



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.message,
  }
end