Class: Mxrb::OfficialMarketplace::WidgetBundleInventory

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

Overview

Enumerates widget identities delivered either directly or as declared assets of a modelerProject package such as Data Widgets. rubocop:disable Metrics/AbcSize, Metrics/MethodLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, widget_ids:) ⇒ WidgetBundleInventory

Returns a new instance of WidgetBundleInventory.



166
167
168
169
# File 'lib/mxrb/official_marketplace/widget_package_installer.rb', line 166

def initialize(kind:, widget_ids:)
  @kind = kind
  @widget_ids = widget_ids.freeze
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



164
165
166
# File 'lib/mxrb/official_marketplace/widget_package_installer.rb', line 164

def kind
  @kind
end

#widget_idsObject (readonly)

Returns the value of attribute widget_ids.



164
165
166
# File 'lib/mxrb/official_marketplace/widget_package_installer.rb', line 164

def widget_ids
  @widget_ids
end

Class Method Details

.read(path) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/mxrb/official_marketplace/widget_package_installer.rb', line 171

def self.read(path)
  kind = PackageEnvelope.kind(path)
  return new(kind:, widget_ids: WidgetPackageInventory.read(path).widget_ids) if kind == :widget

  ModulePackageInventory.read(path)
  ids = Dir.mktmpdir('mxrb-widget-bundle-') do |temporary|
    Zip::File.open(path) do |archive|
      reader = ModulePackageReader.new(archive)
      descriptor = reader.descriptor
      descriptor.files.grep(%r{\Awidgets/[^/]+\.mpk\z}i).flat_map do |relative|
        entry = archive.find_entry(relative) || archive.find_entry(relative.tr('/', '\\'))
        destination = File.join(temporary, File.basename(relative))
        File.open(destination, 'wb') { ::IO.copy_stream(entry.get_input_stream, _1) }
        WidgetPackageInventory.read(destination).widget_ids
      end
    end
  end
  new(kind:, widget_ids: ids.uniq.sort)
rescue Zip::Error, REXML::ParseException => e
  raise MarketplaceError, "invalid Mendix widget bundle: #{e.message}"
end