Class: Dotenv::Merge::SmartMerger

Inherits:
Ast::Merge::SmartMergerBase
  • Object
show all
Includes:
Ast::Merge::CommentLayoutEmissionSupport, Ast::Merge::TrailingGroups::DestIterate
Defined in:
lib/dotenv/merge/smart_merger.rb

Overview

Smart merger for dotenv files. Intelligently combines template and destination dotenv files by matching environment variable names and preserving customizations.

Examples:

Basic merge

merger = SmartMerger.new(template_content, dest_content)
result = merger.merge
puts result.to_s

With options

merger = SmartMerger.new(
  template_content,
  dest_content,
  preference: :template,
  add_template_only_nodes: true,
)
result = merger.merge

With node_typing for per-node-type preferences

merger = SmartMerger.new(template, dest,
  node_typing: { "EnvLine" => ->(n) { NodeTyping.with_merge_type(n, :secret) } },
  preference: { default: :destination, secret: :template })

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_content, dest_content, signature_generator: nil, preference: :destination, add_template_only_nodes: false, remove_template_missing_nodes: false, corruption_handling: :heal, freeze_token: nil, match_refiner: nil, regions: nil, region_placeholder: nil, node_typing: nil, **options) ⇒ SmartMerger

Initialize a new SmartMerger

Parameters:

  • template_content (String)

    Content of the template dotenv file

  • dest_content (String)

    Content of the destination dotenv file

  • signature_generator (Proc, nil) (defaults to: nil)

    Custom signature generator

  • preference (Symbol, Hash) (defaults to: :destination)

    :destination, :template, or per-type Hash

  • add_template_only_nodes (Boolean) (defaults to: false)

    Whether to add template-only env vars (default: false)

  • remove_template_missing_nodes (Boolean) (defaults to: false)

    Whether to remove destination-only env vars that do not exist in the template (default: false)

  • corruption_handling (Symbol) (defaults to: :heal)

    How to handle detected historical duplicate-prefix corruption (:heal, :warn, :error, :skip)

  • freeze_token (String) (defaults to: nil)

    Token for freeze block markers (default: "dotenv-merge")

  • match_refiner (#call, nil) (defaults to: nil)

    Match refiner for fuzzy matching

  • regions (Array<Hash>, nil) (defaults to: nil)

    Region configurations for nested merging

  • region_placeholder (String, nil) (defaults to: nil)

    Custom placeholder for regions

  • node_typing (Hash{Symbol,String => #call}, nil) (defaults to: nil)

    Node typing configuration for per-node-type merge preferences

  • options (Hash)

    Additional options for forward compatibility



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dotenv/merge/smart_merger.rb', line 53

def initialize(
  template_content,
  dest_content,
  signature_generator: nil,
  preference: :destination,
  add_template_only_nodes: false,
  remove_template_missing_nodes: false,
  corruption_handling: :heal,
  freeze_token: nil,
  match_refiner: nil,
  regions: nil,
  region_placeholder: nil,
  node_typing: nil,
  **options
)
  @remove_template_missing_nodes = remove_template_missing_nodes
  @corruption_handling = ::Ast::Merge::Healer.normalize_mode(corruption_handling)

  super(
    template_content,
    dest_content,
    signature_generator: signature_generator,
    preference: preference,
    add_template_only_nodes: add_template_only_nodes,
    remove_template_missing_nodes: remove_template_missing_nodes,
    freeze_token: freeze_token,
    match_refiner: match_refiner,
    regions: regions,
    region_placeholder: region_placeholder,
    node_typing: node_typing,
    **options
  )
end

Instance Attribute Details

#corruption_handlingObject (readonly)

Returns the value of attribute corruption_handling.



31
32
33
# File 'lib/dotenv/merge/smart_merger.rb', line 31

def corruption_handling
  @corruption_handling
end