Class: Mmi::InstallRecord
- Inherits:
-
Object
- Object
- Mmi::InstallRecord
- Defined in:
- lib/mmi/install_record.rb
Defined Under Namespace
Classes: RecordEntry
Constant Summary collapse
- RECORD_FILE =
'.mmi_install_record'.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #add(url, relative_path, content_hash:) ⇒ Object
-
#initialize ⇒ InstallRecord
constructor
A new instance of InstallRecord.
- #install(dir) ⇒ Object
- #to_yaml ⇒ Object
Constructor Details
#initialize ⇒ InstallRecord
Returns a new instance of InstallRecord.
11 12 13 |
# File 'lib/mmi/install_record.rb', line 11 def initialize @record = {} end |
Class Method Details
.parse_record_file(dir) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mmi/install_record.rb', line 55 def self.parse_record_file(dir) record_file = File.(RECORD_FILE, dir) if File.exist?(record_file) parsed_record = Psych.unsafe_load_file(record_file) unless !parsed_record.is_a?(Hash) || parsed_record.keys.any?{|k| !k.is_a?(String)} && parsed_record.values.any?{|v| !v.is_a?(Record)} parsed_record else Mmi.warn("Found invalid install record at #{record_file}. The file will be ignored.") nil end else nil end end |
Instance Method Details
#add(url, relative_path, content_hash:) ⇒ Object
15 16 17 18 19 |
# File 'lib/mmi/install_record.rb', line 15 def add(url, relative_path, content_hash:) raise 'content_hash must be nil or an instance of Mmi::ContentHash::Base' unless content_hash.nil? || content_hash.is_a?(Mmi::ContentHash::Base) @record[relative_path] = RecordEntry.new(url, content_hash) end |
#install(dir) ⇒ Object
21 22 23 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 |
# File 'lib/mmi/install_record.rb', line 21 def install(dir) old_record = self.class.parse_record_file(dir) || {} @record.each do |relative_path, entry| target_file = File.(relative_path, dir) if old_record[relative_path] != entry || (File.exist?(target_file) && !entry.content_hash&.match_file?(target_file)) Mmi.info "Downloading #{entry.url.inspect} into #{target_file.inspect}." begin Mmi::InstallUtils.download_to_file(entry.url, target_file, entry.content_hash) rescue OpenURI::HTTPError => e Mmi.fail! "Error when requesting asset.\n#{e.inspect}" end else Mmi.info "Skipping #{entry.url.inspect} because it is already present at #{target_file.inspect}." end old_record.delete(relative_path) end old_record.each do |relative_path, _| target_file = File.(relative_path, dir) File.delete(target_file) if File.exist?(target_file) end File.write(File.(RECORD_FILE, dir), to_yaml) end |
#to_yaml ⇒ Object
51 52 53 |
# File 'lib/mmi/install_record.rb', line 51 def to_yaml Psych.dump(@record) end |