Class: Mxrb::OfficialMarketplace::ModulePackageInventory

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/official_marketplace/lifecycle.rb

Overview

Read-only view of a module package used to compare update boundaries.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, version:, module_id:, unit_ids:, files:) ⇒ ModulePackageInventory

Returns a new instance of ModulePackageInventory.



39
40
41
42
43
44
45
# File 'lib/mxrb/official_marketplace/lifecycle.rb', line 39

def initialize(name:, version:, module_id:, unit_ids:, files:)
  @name = name
  @version = version
  @module_id = module_id
  @unit_ids = unit_ids.freeze
  @files = files.freeze
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



37
38
39
# File 'lib/mxrb/official_marketplace/lifecycle.rb', line 37

def files
  @files
end

#module_idObject (readonly)

Returns the value of attribute module_id.



37
38
39
# File 'lib/mxrb/official_marketplace/lifecycle.rb', line 37

def module_id
  @module_id
end

#nameObject (readonly)

Returns the value of attribute name.



37
38
39
# File 'lib/mxrb/official_marketplace/lifecycle.rb', line 37

def name
  @name
end

#unit_idsObject (readonly)

Returns the value of attribute unit_ids.



37
38
39
# File 'lib/mxrb/official_marketplace/lifecycle.rb', line 37

def unit_ids
  @unit_ids
end

#versionObject (readonly)

Returns the value of attribute version.



37
38
39
# File 'lib/mxrb/official_marketplace/lifecycle.rb', line 37

def version
  @version
end

Class Method Details

.find_module(mpr, name) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



74
75
76
# File 'lib/mxrb/official_marketplace/lifecycle.rb', line 74

def self.find_module(mpr, name)
  mpr.units_by_containment('Modules').find { mpr.parse_contents(_1)['Name'] == name }
end

.read(path) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mxrb/official_marketplace/lifecycle.rb', line 48

def self.read(path)
  Dir.mktmpdir('mxrb-marketplace-inspect-') do |temporary|
    Zip::File.open(path) do |archive|
      reader = ModulePackageReader.new(archive)
      descriptor = reader.descriptor
      source_path = reader.extract_project(descriptor, temporary)
      staged = reader.stage_files(descriptor.files, temporary)
      source = IO::MprFile.open(source_path, readonly: true)
      module_unit = find_module(source, descriptor.name)
      raise MarketplaceError, "module #{descriptor.name.inspect} is absent from package MPR" unless module_unit

      ids = subtree_ids(source, module_unit.fetch('UnitID'))
      files = staged.transform_values { Digest::SHA256.file(_1).hexdigest }
      return new(
        name: descriptor.name, version: descriptor.version,
        module_id: module_unit.fetch('UnitID'), unit_ids: ids, files:
      )
    ensure
      source&.close
    end
  end
rescue Zip::Error, REXML::ParseException => e
  raise MarketplaceError, "invalid Mendix module package: #{e.message}"
end

.subtree_ids(mpr, root_id) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mxrb/official_marketplace/lifecycle.rb', line 78

def self.subtree_ids(mpr, root_id)
  ids = [root_id]
  loop do
    children = mpr.all_units.filter_map do |unit|
      unit.fetch('UnitID') if ids.include?(unit['ContainerID']) && !ids.include?(unit.fetch('UnitID'))
    end
    break if children.empty?

    ids.concat(children)
  end
  ids
end