Class: Lutaml::Xsd::Commands::MetadataCommand::SetCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/lutaml/xsd/commands/metadata_command.rb

Overview

Set command implementation

Instance Attribute Summary

Attributes inherited from BaseCommand

#options

Instance Method Summary collapse

Constructor Details

#initialize(package_path, options) ⇒ SetCommand

Returns a new instance of SetCommand.



135
136
137
138
# File 'lib/lutaml/xsd/commands/metadata_command.rb', line 135

def initialize(package_path, options)
  super(options)
  @package_path = package_path
end

Instance Method Details

#runObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/lutaml/xsd/commands/metadata_command.rb', line 140

def run
  # Load existing package
  old_pkg = SchemaRepositoryPackage.new(@package_path)
  repo = old_pkg.load_repository

  # Get existing metadata
  validation = old_pkg.validate
  unless validation.valid?
    error "Cannot modify invalid package"
    validation.errors.each { |err| error "  - #{err}" }
    exit 1
  end

  # Get current metadata as a hash, excluding internal fields
   = validation..to_h.reject do |k, _|
    k.to_s.start_with?("_") || k == :using_default
  end

  # Parse and apply --set options
  (options[:set] || []).each do |setting|
    key, value = setting.split("=", 2)
    unless value
      error "Invalid setting format: #{setting}"
      error "Expected format: key=value"
      exit 1
    end
    [key] = value # Use string key, not symbol
  end

  # Preserve package configuration from original package
  xsd_mode = (validation.["xsd_mode"] || :include_all).to_sym
  resolution_mode = (validation.["resolution_mode"] || :resolved).to_sym
  serialization_format = (validation.["serialization_format"] || :marshal).to_sym

  # Create new package with updated metadata
  repo.to_package(
    options[:output],
    xsd_mode: xsd_mode,
    resolution_mode: resolution_mode,
    serialization_format: serialization_format,
    metadata: ,
  )

  output "✓ Package created with updated metadata: #{options[:output]}"
rescue StandardError => e
  error "Failed to set metadata: #{e.message}"
  verbose_output e.backtrace.join("\n") if verbose?
  exit 1
end