Class: Igniter::Extensions::Contracts::Debug::PackAudit

Inherits:
Object
  • Object
show all
Defined in:
lib/igniter/extensions/contracts/debug/pack_audit.rb

Constant Summary collapse

REGISTRIES =
{
  node_kinds: ->(kernel) { kernel.nodes.to_h.keys.sort },
  dsl_keywords: ->(kernel) { kernel.dsl_keywords.to_h.keys.sort },
  validators: ->(kernel) { kernel.validators.entries.map(&:key).sort },
  normalizers: ->(kernel) { kernel.normalizers.entries.map(&:key).sort },
  runtime_handlers: ->(kernel) { kernel.runtime_handlers.to_h.keys.sort },
  diagnostics_contributors: ->(kernel) { kernel.diagnostics_contributors.entries.map(&:key).sort },
  effects: ->(kernel) { kernel.effects.to_h.keys.sort },
  executors: ->(kernel) { kernel.executors.to_h.keys.sort }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pack_snapshot:, installed_in_target_profile:, target_profile_fingerprint:, draft_registered_keys:, missing_node_definitions:, missing_dsl_keywords:, missing_runtime_handlers:, missing_registry_contracts:, install_error:, finalize_error:) ⇒ PackAudit

Returns a new instance of PackAudit.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 117

def initialize(pack_snapshot:, installed_in_target_profile:, target_profile_fingerprint:,
               draft_registered_keys:, missing_node_definitions:, missing_dsl_keywords:, missing_runtime_handlers:, missing_registry_contracts:, install_error:, finalize_error:)
  @pack_snapshot = pack_snapshot
  @installed_in_target_profile = installed_in_target_profile
  @target_profile_fingerprint = target_profile_fingerprint
  @draft_registered_keys = draft_registered_keys
  @missing_node_definitions = missing_node_definitions.freeze
  @missing_dsl_keywords = missing_dsl_keywords.freeze
  @missing_runtime_handlers = missing_runtime_handlers.freeze
  @missing_registry_contracts = missing_registry_contracts.transform_values(&:freeze).freeze
  @install_error = install_error
  @finalize_error = finalize_error
  freeze
end

Instance Attribute Details

#draft_registered_keysObject (readonly)

Returns the value of attribute draft_registered_keys.



21
22
23
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 21

def draft_registered_keys
  @draft_registered_keys
end

#finalize_errorObject (readonly)

Returns the value of attribute finalize_error.



21
22
23
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 21

def finalize_error
  @finalize_error
end

#install_errorObject (readonly)

Returns the value of attribute install_error.



21
22
23
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 21

def install_error
  @install_error
end

#installed_in_target_profileObject (readonly)

Returns the value of attribute installed_in_target_profile.



21
22
23
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 21

def installed_in_target_profile
  @installed_in_target_profile
end

#missing_dsl_keywordsObject (readonly)

Returns the value of attribute missing_dsl_keywords.



21
22
23
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 21

def missing_dsl_keywords
  @missing_dsl_keywords
end

#missing_node_definitionsObject (readonly)

Returns the value of attribute missing_node_definitions.



21
22
23
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 21

def missing_node_definitions
  @missing_node_definitions
end

#missing_registry_contractsObject (readonly)

Returns the value of attribute missing_registry_contracts.



21
22
23
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 21

def missing_registry_contracts
  @missing_registry_contracts
end

#missing_runtime_handlersObject (readonly)

Returns the value of attribute missing_runtime_handlers.



21
22
23
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 21

def missing_runtime_handlers
  @missing_runtime_handlers
end

#pack_snapshotObject (readonly)

Returns the value of attribute pack_snapshot.



21
22
23
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 21

def pack_snapshot
  @pack_snapshot
end

#target_profile_fingerprintObject (readonly)

Returns the value of attribute target_profile_fingerprint.



21
22
23
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 21

def target_profile_fingerprint
  @target_profile_fingerprint
end

Class Method Details

.build(pack, profile: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 32

def self.build(pack, profile: nil)
  manifest = pack.manifest
  baseline_snapshot = registry_snapshot(Igniter::Contracts.build_kernel)
  draft_snapshot, install_error = install_snapshot(pack)

  new(
    pack_snapshot: PackSnapshot.new(manifest),
    installed_in_target_profile: profile ? profile.pack_names.include?(manifest.name) : false,
    target_profile_fingerprint: profile&.fingerprint,
    draft_registered_keys: registry_delta(baseline_snapshot, draft_snapshot),
    missing_node_definitions: missing_node_definitions(manifest, draft_snapshot),
    missing_dsl_keywords: missing_dsl_keywords(manifest, draft_snapshot),
    missing_runtime_handlers: missing_runtime_handlers(manifest, draft_snapshot),
    missing_registry_contracts: missing_registry_contracts(manifest, draft_snapshot),
    install_error: install_error,
    finalize_error: finalize_error_for(pack)
  )
end

.finalize_error_for(pack) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 64

def self.finalize_error_for(pack)
  kernel = Igniter::Contracts.build_kernel
  kernel.install(pack)
  kernel.finalize
  nil
rescue StandardError => e
  "#{e.class}: #{e.message}"
end

.install_snapshot(pack) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 51

def self.install_snapshot(pack)
  kernel = Igniter::Contracts.build_kernel
  error = nil

  begin
    kernel.install(pack)
  rescue StandardError => e
    error = "#{e.class}: #{e.message}"
  end

  [registry_snapshot(kernel), error]
end

.missing_dsl_keywords(manifest, snapshot) ⇒ Object



87
88
89
90
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 87

def self.missing_dsl_keywords(manifest, snapshot)
  required = manifest.node_contracts.select(&:requires_dsl).map(&:kind)
  required.reject { |kind| snapshot.fetch(:dsl_keywords).include?(kind) }
end

.missing_node_definitions(manifest, snapshot) ⇒ Object



83
84
85
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 83

def self.missing_node_definitions(manifest, snapshot)
  manifest.node_contracts.map(&:kind).reject { |kind| snapshot.fetch(:node_kinds).include?(kind) }
end

.missing_registry_contracts(manifest, snapshot) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 97

def self.missing_registry_contracts(manifest, snapshot)
  manifest.registry_contracts.each_with_object({}) do |contract, memo|
    registry = normalize_registry(contract.registry)
    available = snapshot.fetch(registry)
    next if available.include?(contract.key)

    memo[registry] ||= []
    memo[registry] << contract.key
  end.transform_values(&:sort)
end

.missing_runtime_handlers(manifest, snapshot) ⇒ Object



92
93
94
95
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 92

def self.missing_runtime_handlers(manifest, snapshot)
  required = manifest.node_contracts.select(&:requires_runtime).map(&:kind)
  required.reject { |kind| snapshot.fetch(:runtime_handlers).include?(kind) }
end

.normalize_registry(name) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 108

def self.normalize_registry(name)
  case name.to_sym
  when :nodes
    :node_kinds
  else
    name.to_sym
  end
end

.registry_delta(before, after) ⇒ Object



77
78
79
80
81
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 77

def self.registry_delta(before, after)
  REGISTRIES.keys.to_h do |name|
    [name, after.fetch(name) - before.fetch(name)]
  end
end

.registry_snapshot(kernel) ⇒ Object



73
74
75
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 73

def self.registry_snapshot(kernel)
  REGISTRIES.to_h { |name, reader| [name, reader.call(kernel)] }
end

Instance Method Details

#explainObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 163

def explain
  return "#{name} looks complete" if ok?

  parts = []
  parts << "install_error=#{install_error}" if install_error
  parts << "missing node definitions: #{missing_node_definitions.join(", ")}" unless missing_node_definitions.empty?
  parts << "missing DSL keywords: #{missing_dsl_keywords.join(", ")}" unless missing_dsl_keywords.empty?
  parts << "missing runtime handlers: #{missing_runtime_handlers.join(", ")}" unless missing_runtime_handlers.empty?
  missing_registry_contracts.each do |registry, keys|
    parts << "missing #{registry}: #{keys.join(", ")}"
  end
  parts << "finalize_error=#{finalize_error}" if finalize_error
  parts.join("; ")
end

#nameObject



132
133
134
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 132

def name
  pack_snapshot.name
end

#ok?Boolean

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 136

def ok?
  install_error.nil? &&
    finalize_error.nil? &&
    missing_node_definitions.empty? &&
    missing_dsl_keywords.empty? &&
    missing_runtime_handlers.empty? &&
    missing_registry_contracts.empty?
end

#to_hObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/igniter/extensions/contracts/debug/pack_audit.rb', line 145

def to_h
  {
    pack: pack_snapshot.to_h,
    installed_in_target_profile: installed_in_target_profile,
    target_profile_fingerprint: target_profile_fingerprint,
    draft_registered_keys: draft_registered_keys,
    missing: {
      node_definitions: missing_node_definitions,
      dsl_keywords: missing_dsl_keywords,
      runtime_handlers: missing_runtime_handlers,
      registry_contracts: missing_registry_contracts
    },
    install_error: install_error,
    finalize_error: finalize_error,
    ok: ok?
  }
end