Class: Anima::ConfigMigrator

Inherits:
Object
  • Object
show all
Defined in:
lib/anima/config_migrator.rb

Overview

Merges new default settings into an existing config.toml without overwriting user-customized values.

Preserves the user’s formatting and comments by extracting text blocks from the template and appending them to the config file. Missing entire sections are appended with their separator comments; missing keys within existing sections are inserted at the end of the section.

Examples:

result = ConfigMigrator.new.run
result.status    #=> :updated
result.additions #=> [#<Addition section="paths" key="soul" value="/home/...">]

Defined Under Namespace

Classes: Addition, Result

Constant Summary collapse

ANIMA_HOME =
File.expand_path("~/.anima")
TEMPLATE_PATH =
File.expand_path("../../templates/config.toml", __dir__).freeze
SEPARATOR_PATTERN =

Section separator pattern used in the template (e.g. “# ─── LLM ───…”).

/^# ─── /

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path: File.join(ANIMA_HOME, "config.toml"), template_path: TEMPLATE_PATH, anima_home: ANIMA_HOME) ⇒ ConfigMigrator

Returns a new instance of ConfigMigrator.

Parameters:

  • config_path (String) (defaults to: File.join(ANIMA_HOME, "config.toml"))

    path to the user’s config.toml

  • template_path (String) (defaults to: TEMPLATE_PATH)

    path to the default config template

  • anima_home (String) (defaults to: ANIMA_HOME)

    expanded path to ~/.anima (for template interpolation)



40
41
42
43
44
45
46
# File 'lib/anima/config_migrator.rb', line 40

def initialize(config_path: File.join(ANIMA_HOME, "config.toml"),
  template_path: TEMPLATE_PATH,
  anima_home: ANIMA_HOME)
  @config_path = Pathname.new(config_path)
  @template_path = Pathname.new(template_path)
  @anima_home = anima_home
end

Instance Attribute Details

#additions [Array<Addition>] keys that were added([Array<Addition>]) ⇒ Object (readonly)

Outcome of a migration run.



32
# File 'lib/anima/config_migrator.rb', line 32

Result = Data.define(:status, :additions)

#key [String] key name within the section([String]) ⇒ Object (readonly)

A single config key that was added during migration.



27
# File 'lib/anima/config_migrator.rb', line 27

Addition = Data.define(:section, :key, :value)

#section [String] TOML section name([String]) ⇒ Object (readonly)

A single config key that was added during migration.



27
# File 'lib/anima/config_migrator.rb', line 27

Addition = Data.define(:section, :key, :value)

#status [Symbol] :not_found, :up_to_date, or :updated([Symbol], : up_to_date) ⇒ Object (readonly)

Outcome of a migration run.



32
# File 'lib/anima/config_migrator.rb', line 32

Result = Data.define(:status, :additions)

#value [Object] default value from the template([Object]) ⇒ Object (readonly)

A single config key that was added during migration.



27
# File 'lib/anima/config_migrator.rb', line 27

Addition = Data.define(:section, :key, :value)

Instance Method Details

#runResult

Merge missing settings from the template into the user’s config.

Returns:

  • (Result)

    status (:not_found, :up_to_date, :updated) and additions list



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/anima/config_migrator.rb', line 51

def run
  return Result.new(status: :not_found, additions: []) unless @config_path.exist?

  template_text = resolve_template
  template_config = TomlRB.parse(template_text)
  user_config = TomlRB.load_file(@config_path.to_s)

  additions = find_additions(user_config, template_config)
  return Result.new(status: :up_to_date, additions: []) if additions.empty?

  apply_additions(additions, template_text)
  Result.new(status: :updated, additions: additions)
end