Class: Anima::ConfigMigrator
- Inherits:
-
Object
- Object
- Anima::ConfigMigrator
- 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.
Defined Under Namespace
Constant Summary collapse
- ANIMA_HOME =
File.("~/.anima")
- TEMPLATE_PATH =
File.("../../templates/config.toml", __dir__).freeze
- SEPARATOR_PATTERN =
Section separator pattern used in the template (e.g. “# ─── LLM ───…”).
/^# ─── /
Instance Attribute Summary collapse
-
#additions [Array<Addition>] keys that were added([Array<Addition>]) ⇒ Object
readonly
Outcome of a migration run.
-
#key [String] key name within the section([String]) ⇒ Object
readonly
A single config key that was added during migration.
-
#section [String] TOML section name([String]) ⇒ Object
readonly
A single config key that was added during migration.
-
#status [Symbol] :not_found, :up_to_date, or :updated([Symbol], : up_to_date) ⇒ Object
readonly
Outcome of a migration run.
-
#value [Object] default value from the template([Object]) ⇒ Object
readonly
A single config key that was added during migration.
Instance Method Summary collapse
-
#initialize(config_path: File.join(ANIMA_HOME, "config.toml"), template_path: TEMPLATE_PATH, anima_home: ANIMA_HOME) ⇒ ConfigMigrator
constructor
A new instance of ConfigMigrator.
-
#run ⇒ Result
Merge missing settings from the template into the user’s config.
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.
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
#run ⇒ Result
Merge missing settings from the template into the user’s config.
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 |